mirror of
https://gitee.com/xiarenalofs/squad-rain-ops-mini.git
synced 2026-08-08 22:35:32 +08:00
Initial commit: add RainOpsMini project and documentation
This commit is contained in:
112
SteamTimeQuery/Program.cs
Normal file
112
SteamTimeQuery/Program.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System.Text.Json;
|
||||
using SteamTimeQuery;
|
||||
|
||||
string configFile = "config.json";
|
||||
AppConfig config;
|
||||
|
||||
if (!File.Exists(configFile))
|
||||
{
|
||||
Console.WriteLine("初次运行,请输入运行端口 (默认 5000):");
|
||||
string? portInput = Console.ReadLine();
|
||||
int port = 5000;
|
||||
if (!string.IsNullOrWhiteSpace(portInput) && int.TryParse(portInput, out int p))
|
||||
{
|
||||
port = p;
|
||||
}
|
||||
|
||||
config = new AppConfig
|
||||
{
|
||||
Port = port,
|
||||
ApiKey = "" // 请在 config.json 中配置 Steam Web API Key
|
||||
};
|
||||
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
File.WriteAllText(configFile, JsonSerializer.Serialize(config, options));
|
||||
Console.WriteLine($"配置已保存到 {configFile}");
|
||||
}
|
||||
else
|
||||
{
|
||||
string json = File.ReadAllText(configFile);
|
||||
config = JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.WebHost.UseUrls($"http://*:{config.Port}");
|
||||
builder.Services.AddHttpClient();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/api/playtime", async (string steamid, IHttpClientFactory clientFactory) =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(steamid)) return Results.BadRequest("steamid is required");
|
||||
|
||||
string apiKey = config.ApiKey;
|
||||
string appId = "393380"; // Squad
|
||||
|
||||
var client = clientFactory.CreateClient();
|
||||
client.Timeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
// 1. Get Nickname (GetPlayerSummaries)
|
||||
string summaryUrl = $"http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={apiKey}&steamids={steamid}";
|
||||
string nickname = "Unknown";
|
||||
string avatar = "";
|
||||
|
||||
try
|
||||
{
|
||||
var summaryResp = await client.GetAsync(summaryUrl);
|
||||
if (summaryResp.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await summaryResp.Content.ReadAsStringAsync();
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (doc.RootElement.TryGetProperty("response", out var resp) &&
|
||||
resp.TryGetProperty("players", out var players) &&
|
||||
players.GetArrayLength() > 0)
|
||||
{
|
||||
var player = players[0];
|
||||
if (player.TryGetProperty("personaname", out var nameProp)) nickname = nameProp.GetString() ?? "Unknown";
|
||||
if (player.TryGetProperty("avatarfull", out var avatarProp)) avatar = avatarProp.GetString() ?? "";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error fetching summary: {ex.Message}");
|
||||
}
|
||||
|
||||
// 2. Get Playtime (GetOwnedGames)
|
||||
string gamesUrl = $"http://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key={apiKey}&steamid={steamid}&format=json&appids_filter[0]={appId}";
|
||||
int minutes = 0;
|
||||
|
||||
try
|
||||
{
|
||||
var gamesResp = await client.GetAsync(gamesUrl);
|
||||
if (gamesResp.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await gamesResp.Content.ReadAsStringAsync();
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (doc.RootElement.TryGetProperty("response", out var resp) &&
|
||||
resp.TryGetProperty("games", out var games) &&
|
||||
games.GetArrayLength() > 0)
|
||||
{
|
||||
minutes = games[0].GetProperty("playtime_forever").GetInt32();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error fetching games: {ex.Message}");
|
||||
}
|
||||
|
||||
return Results.Json(new {
|
||||
steamId = steamid,
|
||||
nickname = nickname,
|
||||
total_minutes = minutes,
|
||||
minutes = minutes,
|
||||
playtime = minutes,
|
||||
hours = minutes / 60,
|
||||
avatar = avatar
|
||||
});
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user