168 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			168 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|   | //using System; | ||
|  | //using System.Collections.Generic; | ||
|  | //using System.Threading.Tasks; | ||
|  | //using Supabase; | ||
|  | //using Postgrest; | ||
|  | //using Postgrest.Attributes; | ||
|  | //using Postgrest.Models; | ||
|  | 
 | ||
|  | //#region Model | ||
|  | 
 | ||
|  | //[Table("players")] | ||
|  | //public class PlayerRow : BaseModel | ||
|  | //{ | ||
|  | //    //[PrimaryKey("wallet_address", false)] | ||
|  | //    //[Column("wallet_address")] | ||
|  | //    //public string WalletAddress { get; set; } | ||
|  | 
 | ||
|  | //    //[Column("total_kills")] | ||
|  | //    //public int TotalKills { get; set; } | ||
|  | 
 | ||
|  | //    //[Column("average_placement")] | ||
|  | //    //public decimal? AveragePlacement { get; set; } | ||
|  | 
 | ||
|  | //    //[Column("win_percentage")] | ||
|  | //    //public decimal? WinPercentage { get; set; } | ||
|  | 
 | ||
|  | //    //[Column("in_game_currency")] | ||
|  | //    //public long InGameCurrency { get; set; } | ||
|  | 
 | ||
|  | //    //[Column("purchased_items")] | ||
|  | //    //public Dictionary<string, object> PurchasedItems { get; set; } = new(); | ||
|  | 
 | ||
|  | //    //[Column("updated_at")] | ||
|  | //    //public DateTimeOffset? UpdatedAt { get; set; } | ||
|  | //} | ||
|  | 
 | ||
|  | //#endregion | ||
|  | 
 | ||
|  | ////public static class SupabasePlayers | ||
|  | ////{ | ||
|  | ////    // TODO: set these | ||
|  | ////    public static string SupabaseUrl = "https://YOUR-PROJECT.supabase.co"; | ||
|  | ////    public static string SupabaseAnonKey = "YOUR-ANON-KEY"; | ||
|  | 
 | ||
|  | ////    private static Client _client; | ||
|  | 
 | ||
|  | ////    public static async Task InitAsync() | ||
|  | ////    { | ||
|  | ////        if (_client != null) return; | ||
|  | 
 | ||
|  | ////        var options = new SupabaseOptions | ||
|  | ////        { | ||
|  | ////            AutoConnectRealtime = false, | ||
|  | ////            Schema = "public" | ||
|  | ////        }; | ||
|  | 
 | ||
|  | ////        _client = new Client(SupabaseUrl, SupabaseAnonKey, options); | ||
|  | ////        await _client.InitializeAsync(); | ||
|  | ////    } | ||
|  | 
 | ||
|  | ////    // ---------- CREATE (or get existing via UPSERT on PK) ---------- | ||
|  | ////    public static async Task<PlayerRow> CreateIfMissingAsync( | ||
|  | ////        string walletAddress, | ||
|  | ////        int totalKills = 0, | ||
|  | ////        decimal? averagePlacement = null, | ||
|  | ////        decimal? winPercentage = null, | ||
|  | ////        long inGameCurrency = 0, | ||
|  | ////        Dictionary<string, object> purchasedItems = null) | ||
|  | ////    { | ||
|  | ////        await InitAsync(); | ||
|  | 
 | ||
|  | ////        var row = new PlayerRow | ||
|  | ////        { | ||
|  | ////            WalletAddress = walletAddress, | ||
|  | ////            TotalKills = totalKills, | ||
|  | ////            AveragePlacement = averagePlacement, | ||
|  | ////            WinPercentage = winPercentage, | ||
|  | ////            InGameCurrency = inGameCurrency, | ||
|  | ////            PurchasedItems = purchasedItems ?? new Dictionary<string, object>(), | ||
|  | ////            UpdatedAt = DateTimeOffset.UtcNow | ||
|  | ////        }; | ||
|  | 
 | ||
|  | ////        var resp = await _client | ||
|  | ////            .From<PlayerRow>() | ||
|  | ////            .Upsert(row); // upsert by PK | ||
|  | 
 | ||
|  | ////        return resp.Models.Count > 0 ? resp.Models[0] : row; | ||
|  | ////    } | ||
|  | 
 | ||
|  | ////    // ---------- READ ---------- | ||
|  | ////    public static async Task<PlayerRow> GetAsync(string walletAddress) | ||
|  | ////    { | ||
|  | ////        await InitAsync(); | ||
|  | 
 | ||
|  | ////        var resp = await _client | ||
|  | ////            .From<PlayerRow>() | ||
|  | ////            .Select("*") // 3.5.x requires a string | ||
|  | ////            .Filter("wallet_address", Postgrest.Constants.Operator.Equals, walletAddress) | ||
|  | ////            .Get(); | ||
|  | 
 | ||
|  | ////        return resp.Models.Count > 0 ? resp.Models[0] : null; | ||
|  | ////    } | ||
|  | 
 | ||
|  | ////    // ---------- PATCH ---------- | ||
|  | ////    public static async Task<PlayerRow> PatchAsync( | ||
|  | ////        string walletAddress, | ||
|  | ////        int? totalKills = null, | ||
|  | ////        decimal? averagePlacement = null, | ||
|  | ////        decimal? winPercentage = null, | ||
|  | ////        long? inGameCurrency = null, | ||
|  | ////        Dictionary<string, object> purchasedItems = null) | ||
|  | ////    { | ||
|  | ////        await InitAsync(); | ||
|  | 
 | ||
|  | ////        var updateResp = await _client | ||
|  | ////            .From<PlayerRow>() | ||
|  | ////            .Filter("wallet_address", Postgrest.Constants.Operator.Equals, walletAddress) | ||
|  | ////            .Set(p => | ||
|  | ////            { | ||
|  | ////                var dict = new Dictionary<object, object>(); | ||
|  | 
 | ||
|  | ////                if (totalKills.HasValue) dict["total_kills"] = totalKills.Value; | ||
|  | ////                if (averagePlacement.HasValue) dict["average_placement"] = averagePlacement.Value; | ||
|  | ////                if (winPercentage.HasValue) dict["win_percentage"] = winPercentage.Value; | ||
|  | ////                if (inGameCurrency.HasValue) dict["in_game_currency"] = inGameCurrency.Value; | ||
|  | ////                if (purchasedItems != null) dict["purchased_items"] = purchasedItems; | ||
|  | 
 | ||
|  | ////                dict["updated_at"] = DateTimeOffset.UtcNow; | ||
|  | ////                return dict; | ||
|  | ////            }) | ||
|  | ////            .Update(); | ||
|  | 
 | ||
|  | ////        return updateResp.Models.Count > 0 | ||
|  | ////            ? updateResp.Models[0] | ||
|  | ////            : await GetAsync(walletAddress); | ||
|  | ////    } | ||
|  | 
 | ||
|  | ////    // ---------- REPLACE (full row) ---------- | ||
|  | ////    public static async Task<PlayerRow> ReplaceAsync(PlayerRow fullRow) | ||
|  | ////    { | ||
|  | ////        await InitAsync(); | ||
|  | 
 | ||
|  | ////        if (string.IsNullOrWhiteSpace(fullRow.WalletAddress)) | ||
|  | ////            throw new ArgumentException("WalletAddress (PK) is required."); | ||
|  | 
 | ||
|  | ////        fullRow.UpdatedAt = DateTimeOffset.UtcNow; | ||
|  | 
 | ||
|  | ////        var resp = await _client | ||
|  | ////            .From<PlayerRow>() | ||
|  | ////            .Upsert(fullRow); // upsert by PK | ||
|  | 
 | ||
|  | ////        return resp.Models.Count > 0 ? resp.Models[0] : fullRow; | ||
|  | ////    } | ||
|  | 
 | ||
|  | ////    // ---------- DELETE ---------- | ||
|  | ////    public static async Task<bool> DeleteAsync(string walletAddress) | ||
|  | ////    { | ||
|  | ////        await InitAsync(); | ||
|  | 
 | ||
|  | ////        await _client | ||
|  | ////            .From<PlayerRow>() | ||
|  | ////            .Filter("wallet_address", Postgrest.Constants.Operator.Equals, walletAddress) | ||
|  | ////            .Delete(); | ||
|  | 
 | ||
|  | ////        return true; // PostgREST usually returns empty body on delete | ||
|  | ////    } | ||
|  | ////} |