Files
squad-rain-ops-mini/Services/PointsService.cs

93 lines
2.9 KiB
C#

using RainOpsMini.Models;
using RainOpsMini.Helpers;
using SqlSugar;
using System;
using System.Threading.Tasks;
namespace RainOpsMini.Services
{
public static class PointsService
{
public static async Task<int> GetPointsAsync(string steamId)
{
try
{
using var db = DbHelper.GetInstance();
var user = await db.Queryable<UserIntegral>()
.FirstAsync(u => u.SteamId == steamId);
return user?.Points ?? 0;
}
catch (Exception ex)
{
RainOpsLog.Log($"[PointsService] GetPoints Error: {ex.Message}");
return 0;
}
}
public static async Task<int> AdjustPointsAsync(string steamId, int delta)
{
try
{
using var db = DbHelper.GetInstance();
var user = await db.Queryable<UserIntegral>()
.FirstAsync(u => u.SteamId == steamId);
if (user == null)
{
user = new UserIntegral
{
SteamId = steamId,
Points = delta > 0 ? delta : 0, // Initial points shouldn't be negative if creating
LastUpdated = DateTime.UtcNow
};
await db.Insertable(user).ExecuteCommandAsync();
}
else
{
user.Points += delta;
user.LastUpdated = DateTime.UtcNow;
await db.Updateable(user).ExecuteCommandAsync();
}
return user.Points;
}
catch (Exception ex)
{
RainOpsLog.Log($"[PointsService] AdjustPoints Error: {ex.Message}");
return 0;
}
}
public static async Task SetPointsAsync(string steamId, int value)
{
try
{
using var db = DbHelper.GetInstance();
var user = await db.Queryable<UserIntegral>()
.FirstAsync(u => u.SteamId == steamId);
if (user == null)
{
user = new UserIntegral
{
SteamId = steamId,
Points = value,
LastUpdated = DateTime.UtcNow
};
await db.Insertable(user).ExecuteCommandAsync();
}
else
{
user.Points = value;
user.LastUpdated = DateTime.UtcNow;
await db.Updateable(user).ExecuteCommandAsync();
}
}
catch (Exception ex)
{
RainOpsLog.Log($"[PointsService] SetPoints Error: {ex.Message}");
}
}
}
}