155 lines
4.5 KiB
C#
155 lines
4.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using SQLiteEF;
|
|
using SteamApiWrapper.ReturnTypes;
|
|
using SteamGame = SteamApiWrapper.ReturnTypes.Game;
|
|
using Player = SQLiteEF.Player;
|
|
using SteamPlayer = SteamApiWrapper.ReturnTypes.Player;
|
|
using Steam = SteamApiWrapper.SteamApiWrapper;
|
|
|
|
namespace Tracker;
|
|
|
|
public class Tracker : IDisposable
|
|
{
|
|
private IConfiguration Configuration { get; init; }
|
|
private readonly Thread _trackerThread;
|
|
private bool _running = true;
|
|
|
|
public Tracker()
|
|
{
|
|
Configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
_trackerThread = new (TrackerLoop);
|
|
_trackerThread.Start();
|
|
}
|
|
|
|
private void UpdatePlayers()
|
|
{
|
|
Context context = new (Configuration);
|
|
IQueryable<ulong> players = context.Players.Select(p => p.SteamId);
|
|
GetPlayerSummaries summaries = Steam.GetPlayerSummaries(players.ToArray());
|
|
foreach (SteamPlayer summariesPlayer in summaries.players)
|
|
{
|
|
if (context.Players.Find(summariesPlayer.steamid) is not { } player)
|
|
{
|
|
context.Players.Add(new (summariesPlayer.steamid, summariesPlayer.personaname,
|
|
summariesPlayer.profileurl, summariesPlayer.avatar));
|
|
continue;
|
|
}
|
|
player.Name = summariesPlayer.personaname;
|
|
player.ProfileUrl = summariesPlayer.profileurl;
|
|
player.AvatarUrl = summariesPlayer.avatar;
|
|
try
|
|
{
|
|
context.SaveChanges();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateOwnedGames()
|
|
{
|
|
Context context = new (Configuration);
|
|
IQueryable<Player> players = context.Players;
|
|
foreach (Player player in players)
|
|
{
|
|
GetOwnedGames ownedGames = Steam.GetOwnedGames(player.SteamId);
|
|
foreach (SteamGame ownedGame in ownedGames.games)
|
|
{
|
|
if (context.Games.Find(ownedGame.appid) is not { } game)
|
|
{
|
|
game = new(ownedGame.appid, ownedGame.name);
|
|
}
|
|
|
|
if (!player.Games.Contains(game))
|
|
{
|
|
player.Games.Add(game);
|
|
game.PlayedBy.Add(player);
|
|
player.TrackedTimes.Add(new (game, player, ownedGame.playtime_forever));
|
|
}
|
|
|
|
try
|
|
{
|
|
context.SaveChanges();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateGametimes()
|
|
{
|
|
Context context = new (Configuration);
|
|
IQueryable<Player> players = context.Players;
|
|
foreach (Player player in players)
|
|
{
|
|
GetRecentlyPlayedGames recentlyPlayed = Steam.GetRecentlyPlayedGames(player.SteamId);
|
|
foreach (SteamGame recentlyPlayedGame in recentlyPlayed.games)
|
|
{
|
|
if (context.Games.Find(recentlyPlayedGame.appid) is not { } game)
|
|
{
|
|
game = new(recentlyPlayedGame.appid, recentlyPlayedGame.name);
|
|
}
|
|
|
|
if (!player.Games.Contains(game))
|
|
{
|
|
player.Games.Add(game);
|
|
game.PlayedBy.Add(player);
|
|
}
|
|
|
|
player.TrackedTimes.Add(new (game, player, recentlyPlayedGame.playtime_forever));
|
|
|
|
try
|
|
{
|
|
context.SaveChanges();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void TrackerLoop()
|
|
{
|
|
while (_running)
|
|
{
|
|
try
|
|
{
|
|
Thread.Sleep(TimeSpan.FromHours(1));
|
|
}
|
|
catch (ThreadInterruptedException)
|
|
{
|
|
Console.WriteLine("Thread interrupted");
|
|
}
|
|
catch (ThreadAbortException)
|
|
{
|
|
_running = false;
|
|
}
|
|
}
|
|
Console.WriteLine("Thread exited");
|
|
}
|
|
|
|
public void ForceLoop()
|
|
{
|
|
_trackerThread.Interrupt();
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
Tracker _ = new ();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_running = false;
|
|
_trackerThread.Interrupt();
|
|
}
|
|
} |