Server persistence: EF Core profiles + coin ledger + authoritative rewards

- EF Core (SQLite dev / Postgres prod via config); ProfileRow JSON blob +
  LedgerRow audit; EnsureCreated at startup
- C# Gamification port (ranks/elo/coins/xp/achievements/titles) → server
  computes match rewards; ProfileService (get/update/plan/buyCoins/applyMatch)
- JWT endpoints: profile GET/PUT, plan, coins packs/buy, match/result;
  auth upserts the profile
- Tested end-to-end (buy + ranked win+kot persisted & server-computed)
- Client still mock-backed for now (wiring is the next step)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-06-04 16:52:25 +03:30
parent cdb8d522dd
commit d0b8976713
9 changed files with 507 additions and 9 deletions
@@ -0,0 +1,110 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Hokm.Server.Profiles;
public class StatsDto
{
public int Games { get; set; }
public int Wins { get; set; }
public int Losses { get; set; }
public int KotsFor { get; set; }
public int KotsAgainst { get; set; }
public int Tricks { get; set; }
public int BestWinStreak { get; set; }
public int CurrentWinStreak { get; set; }
}
/// <summary>Mirrors the client UserProfile (camelCase JSON).</summary>
public class ProfileDto
{
public string Id { get; set; } = "";
public string Username { get; set; } = "";
public string DisplayName { get; set; } = "بازیکن";
public string Avatar { get; set; } = "a-fox";
public string? AvatarImage { get; set; }
public string? Phone { get; set; }
public string? Email { get; set; }
public string Plan { get; set; } = "free";
public long? PlanUntil { get; set; }
public int Level { get; set; } = 1;
public int Xp { get; set; }
public int Coins { get; set; } = 1000;
public int Rating { get; set; } = 1000;
public StatsDto Stats { get; set; } = new();
public List<string> OwnedAvatars { get; set; } = new() { "a-fox", "a-lion" };
public List<string> OwnedCardFronts { get; set; } = new() { "classic" };
public List<string> OwnedCardBacks { get; set; } = new() { "classic" };
public List<string> OwnedTitles { get; set; } = new() { "novice" };
public List<string> OwnedReactionPacks { get; set; } = new();
public List<string> OwnedStickerPacks { get; set; } = new();
public string? Title { get; set; } = "novice";
public string CardFront { get; set; } = "classic";
public string CardBack { get; set; } = "classic";
public Dictionary<string, int> Achievements { get; set; } = new();
public List<string> Unlocked { get; set; } = new();
public long CreatedAt { get; set; }
}
public class MatchSummaryDto
{
public bool Ranked { get; set; }
public int Stake { get; set; }
public bool Won { get; set; }
public bool KotFor { get; set; }
public bool KotAgainst { get; set; }
public int TricksWon { get; set; }
public int Rounds { get; set; }
}
public class AchievementUnlockDto
{
public string Id { get; set; } = "";
public string NameFa { get; set; } = "";
public string NameEn { get; set; } = "";
public string Icon { get; set; } = "";
public int CoinReward { get; set; }
}
public class TitleUnlockDto
{
public string Id { get; set; } = "";
public string NameFa { get; set; } = "";
public string NameEn { get; set; } = "";
}
public class RewardResultDto
{
public int RatingBefore { get; set; }
public int RatingAfter { get; set; }
public int RatingDelta { get; set; }
public int CoinsBefore { get; set; }
public int CoinsAfter { get; set; }
public int CoinsDelta { get; set; }
public int XpGained { get; set; }
public int LevelBefore { get; set; }
public int LevelAfter { get; set; }
public bool LeveledUp { get; set; }
public List<AchievementUnlockDto> NewAchievements { get; set; } = new();
public List<TitleUnlockDto> NewTitles { get; set; } = new();
public bool Promoted { get; set; }
public bool Demoted { get; set; }
}
public class CoinPackDto
{
public string Id { get; set; } = "";
public int Coins { get; set; }
public int Bonus { get; set; }
public int PriceToman { get; set; }
public string? Tag { get; set; }
}
public static class JsonOpts
{
public static readonly JsonSerializerOptions Default = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
}