using Microsoft.EntityFrameworkCore; using RadicadeServer.DB; using RadicadeServer.RecNet.Progression; namespace RadicadeServer.Utils { public static class ProgressionAwardService { public const int XpPerGiftBoxOpened = 1; public const int XpPerGameCompleted = 2; public static async Task AwardXpAsync(RecDBContext db, long playerId, int xp, string reason) { if (playerId <= 0 || xp <= 0) return 0; var progression = await db.Progression.FirstOrDefaultAsync(x => x.PlayerId == playerId); if (progression == null) { progression = new ProgressionData { PlayerId = playerId }; await db.Progression.AddAsync(progression); } int granted = (int)Math.Min(xp, int.MaxValue); if (granted <= 0) { await db.SaveChangesAsync(); return 0; } int oldLevel = progression.Level; progression.XP = (int)Math.Min(progression.XP + granted, int.MaxValue); await db.SaveChangesAsync(); int computedLevel = LevelUpGiftService.ComputeLevel(LevelUpGiftService.ParseMap(), progression.XP); if (computedLevel > progression.Level) progression.Level = computedLevel; await LevelUpGiftService.DeliverLevelUpGiftsAsync(db, playerId, recomputeFromXp: true, fromLevel: oldLevel, notify: true); await db.SaveChangesAsync(); await RoomProgressionService.SendProgressionLevelUpdateAsync(db, playerId, progression); return granted; } } }