From 3051b63e44cb41b7e9d4d8349c60a6412cf665e7 Mon Sep 17 00:00:00 2001 From: littlefoot Date: Fri, 29 Oct 2021 00:46:00 -0400 Subject: [PATCH] Now supports saving card groups, this is probably the ugliest code ive ever written but anything to avoid 20 cardgroup properties. we should see if there's a better way, it could probably be a lot cleaner --- .../Controllers/PlayerDataController.cs | 62 ++++++++++-- TOOHUCardAPI/DTO/AbstractRequest.cs | 10 ++ TOOHUCardAPI/DTO/AbstractResponse.cs | 14 +++ TOOHUCardAPI/DTO/PlayerDataRequestObjects.cs | 14 ++- TOOHUCardAPI/DTO/PlayerDataResponseObjects.cs | 36 +++---- TOOHUCardAPI/Data/AutomapProfile.cs | 1 - TOOHUCardAPI/Data/Models/EncodedCardGroup.cs | 4 +- TOOHUCardAPI/Data/Models/User.cs | 1 + .../Data/Repositories/UserRepository.cs | 14 ++- ...groupid for encoded card group.Designer.cs | 97 +++++++++++++++++++ ...29035809_groupid for encoded card group.cs | 35 +++++++ ...roupkey, they send full string.Designer.cs | 97 +++++++++++++++++++ ...842_fix groupkey, they send full string.cs | 34 +++++++ .../Migrations/AppDbContextModelSnapshot.cs | 6 ++ 14 files changed, 393 insertions(+), 32 deletions(-) create mode 100644 TOOHUCardAPI/DTO/AbstractRequest.cs create mode 100644 TOOHUCardAPI/DTO/AbstractResponse.cs create mode 100644 TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.Designer.cs create mode 100644 TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.cs create mode 100644 TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.Designer.cs create mode 100644 TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.cs diff --git a/TOOHUCardAPI/Controllers/PlayerDataController.cs b/TOOHUCardAPI/Controllers/PlayerDataController.cs index 006b9dc..673697a 100644 --- a/TOOHUCardAPI/Controllers/PlayerDataController.cs +++ b/TOOHUCardAPI/Controllers/PlayerDataController.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; @@ -6,6 +7,7 @@ using System.Net.Http; using System.Reflection; using System.Threading.Tasks; using AutoMapper; +using AutoMapper.QueryableExtensions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -47,18 +49,64 @@ namespace TOOHUCardAPI.Controllers return await InvokeEndpointHandlerForMethod(this, method, body); } + [EndpointHandler("save_card_group")] + private async Task SaveCardGroup(string body) + { + PlayerDataSaveCardGroupRequestObject request = + JsonConvert.DeserializeObject(body); + User user = _userRepository.GetUser(request.SteamId); + if (user == null) + { + return new PlayerDataSaveCardGroupResponseObject() + { + Code = "0001", + Message = "Invalid player" + }; + } + + EncodedCardGroup group = user.EncodedCardGroups.FirstOrDefault(group => group.GroupKey == request.GroupKey) ?? new EncodedCardGroup() + { + GroupKey = request.GroupKey + }; + group.EncodedString = request.GroupData; + user.EncodedCardGroups = user.EncodedCardGroups.Where(group2 => group.Id != group2.Id).Append(group).ToList(); + _userRepository.UpdateUser(user); + return new PlayerDataSaveCardGroupResponseObject(); + } + [EndpointHandler("get")] private async Task Get(string body) { PlayerDataGetRequestObject requestObject = JsonConvert.DeserializeObject(body); - Dictionary users = requestObject.Ids.Aggregate(new Dictionary(), - (dictionary, pair) => + IEnumerable users = requestObject.Ids.Values.Select(val => _userRepository.GetOrCreateUser(val)); + IEnumerable queriedUserSteamIds = requestObject.Ids.Select(i => i.Value); + IEnumerable responsePlayers = users + .Where(user => queriedUserSteamIds.Contains(user.SteamId)) + .Select(user => _mapper.Map(user)); + Dictionary playersStatic = requestObject.Ids + .Select(pair => + KeyValuePair.Create(pair.Key, responsePlayers.FirstOrDefault(resp => resp.SteamId == pair.Value))) + .ToDictionary(kv => kv.Key, kv => kv.Value); + Dictionary> dynamicResponses = playersStatic + .Select(kv => + { + var obj = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(kv.Value)); + return KeyValuePair.Create(kv.Key, obj); + }) + .ToDictionary(kv => kv.Key, kv => kv.Value); + foreach (var kv in dynamicResponses) + { + var props = kv.Value; + var user = users.FirstOrDefault(user1 => user1.SteamId == requestObject.Ids[kv.Key]); + foreach (var cardgroup in user.EncodedCardGroups ?? new List()) { - User user = _userRepository.GetOrCreateUser(pair.Value); - dictionary[pair.Key] = user; - return dictionary; - }); - PlayerDataGetResponseObject response = new PlayerDataGetResponseObject(users, _mapper); + props[$"{cardgroup.GroupKey}"] = cardgroup.EncodedString; + } + } + PlayerDataGetResponseObject response = new PlayerDataGetResponseObject() + { + Players = dynamicResponses + }; return response; } diff --git a/TOOHUCardAPI/DTO/AbstractRequest.cs b/TOOHUCardAPI/DTO/AbstractRequest.cs new file mode 100644 index 0000000..7c2b0fc --- /dev/null +++ b/TOOHUCardAPI/DTO/AbstractRequest.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace TOOHUCardAPI.DTO +{ + public abstract class AbstractRequest + { + [JsonProperty("method")] + public string Method { get; set; } + } +} \ No newline at end of file diff --git a/TOOHUCardAPI/DTO/AbstractResponse.cs b/TOOHUCardAPI/DTO/AbstractResponse.cs new file mode 100644 index 0000000..c1ec837 --- /dev/null +++ b/TOOHUCardAPI/DTO/AbstractResponse.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; +using Newtonsoft.Json; + +namespace TOOHUCardAPI.DTO +{ + public abstract class AbstractResponse + { + public static string SUCCESS_CODE = "0000"; + [JsonProperty("code")] + public string Code { get; set; } = SUCCESS_CODE; + [JsonProperty("msg")] + public string Message { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/TOOHUCardAPI/DTO/PlayerDataRequestObjects.cs b/TOOHUCardAPI/DTO/PlayerDataRequestObjects.cs index a4897ff..a0036ac 100644 --- a/TOOHUCardAPI/DTO/PlayerDataRequestObjects.cs +++ b/TOOHUCardAPI/DTO/PlayerDataRequestObjects.cs @@ -1,10 +1,20 @@ using System.Collections.Generic; +using Newtonsoft.Json; namespace TOOHUCardAPI.DTO { - public class PlayerDataGetRequestObject + public class PlayerDataGetRequestObject : AbstractRequest { - public string Method { get; set; } public Dictionary Ids; } + + public class PlayerDataSaveCardGroupRequestObject: AbstractRequest + { + public string SteamId { get; set; } + public string UserId { get; set; } + [JsonProperty("group_key")] + public string GroupKey { get; set; } + [JsonProperty("group_data")] + public string GroupData { get; set; } + } } \ No newline at end of file diff --git a/TOOHUCardAPI/DTO/PlayerDataResponseObjects.cs b/TOOHUCardAPI/DTO/PlayerDataResponseObjects.cs index 5eb5ccc..95e2298 100644 --- a/TOOHUCardAPI/DTO/PlayerDataResponseObjects.cs +++ b/TOOHUCardAPI/DTO/PlayerDataResponseObjects.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Dynamic; using System.Linq; +using System.Reflection; using AutoMapper; using Newtonsoft.Json; using TOOHUCardAPI.Data.Models; @@ -9,30 +11,17 @@ namespace TOOHUCardAPI.DTO public class PlayerDataGetResponseObject { [JsonProperty("bo")] - public Dictionary Players { get; set; } - - public PlayerDataGetResponseObject(Dictionary users, IMapper mapper) - { - this.Players = users.Aggregate(new Dictionary(), - (players, pair) => - { - User user = pair.Value; - players[pair.Key] = mapper.Map(user); - return players; - }); - } + public Dictionary> Players { get; set; } + } /** * Fields pulled from the game code * Looking up Gamerules.Playerdata */ - public class PlayerDataGetResponseObjectPlayer + public class PlayerDataGetResponseObjectPlayer: AbstractResponse { - [JsonProperty("code")] public string Code { get; set; } = "0000"; - - [JsonProperty("msg")] public string Message { get; set; } = string.Empty; - //[JsonProperty("steamid")] - //public string SteamId { get; set; } + [JsonProperty("steamid")] + public string SteamId { get; set; } [JsonProperty("max_wave")] public int MaxWave { get; set; } = 0; [JsonProperty("max_team_wave")] public int MaxTeamWave { get; set; } = 0; [JsonProperty("pet_level")] public int PetLevel { get; set; } = 1; @@ -42,5 +31,16 @@ namespace TOOHUCardAPI.DTO [JsonProperty("vip")] public int Vip { get; set; } = 1; [JsonProperty("point")] public int Point { get; set; } = 10000; [JsonProperty("level_list")] public string LevelList { get; set; } = string.Empty; + + public Dictionary ToDynamicProperties() + { + return GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public) + .ToDictionary(prop => prop.Name, prop => prop.GetValue(this)); + } + } + + public class PlayerDataSaveCardGroupResponseObject : AbstractResponse + { + } } \ No newline at end of file diff --git a/TOOHUCardAPI/Data/AutomapProfile.cs b/TOOHUCardAPI/Data/AutomapProfile.cs index 33df9e7..08fb721 100644 --- a/TOOHUCardAPI/Data/AutomapProfile.cs +++ b/TOOHUCardAPI/Data/AutomapProfile.cs @@ -9,7 +9,6 @@ namespace TOOHUCardAPI.Data public AutomapProfile() { CreateMap(); - CreateMap(); } } } \ No newline at end of file diff --git a/TOOHUCardAPI/Data/Models/EncodedCardGroup.cs b/TOOHUCardAPI/Data/Models/EncodedCardGroup.cs index ebe2a43..125a21b 100644 --- a/TOOHUCardAPI/Data/Models/EncodedCardGroup.cs +++ b/TOOHUCardAPI/Data/Models/EncodedCardGroup.cs @@ -1,12 +1,14 @@ using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace TOOHUCardAPI.Data.Models { public class EncodedCardGroup { [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } - public int CardGroupNumber { get; set; } + public string GroupKey { get; set; } public string EncodedString { get; set; } } } \ No newline at end of file diff --git a/TOOHUCardAPI/Data/Models/User.cs b/TOOHUCardAPI/Data/Models/User.cs index a850294..031eaf8 100644 --- a/TOOHUCardAPI/Data/Models/User.cs +++ b/TOOHUCardAPI/Data/Models/User.cs @@ -15,6 +15,7 @@ namespace TOOHUCardAPI.Data.Models public int KeyTotal { get; set; } public DateTime KeySaveDate { get; set; } public bool Vip { get; set; } + public bool Ban { get; set; } public int Point { get; set; } public string LevelList { get; set; } public List EncodedCardGroups { get; set; } diff --git a/TOOHUCardAPI/Data/Repositories/UserRepository.cs b/TOOHUCardAPI/Data/Repositories/UserRepository.cs index b8c77b2..53cb834 100644 --- a/TOOHUCardAPI/Data/Repositories/UserRepository.cs +++ b/TOOHUCardAPI/Data/Repositories/UserRepository.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using AutoMapper; +using Microsoft.EntityFrameworkCore; using TOOHUCardAPI.Data.Models; namespace TOOHUCardAPI.Data.Repositories @@ -16,9 +18,14 @@ namespace TOOHUCardAPI.Data.Repositories _mapper = mapper; } + private IQueryable GetAllUsersQuery() + { + return _appDbContext.Users.Include(u => u.EncodedCardGroups).AsQueryable(); + } + public User GetUser(string steamId) { - return _appDbContext.Users.Where((user => user.SteamId.Equals(steamId))).FirstOrDefault(); + return GetAllUsersQuery().FirstOrDefault(user => user.SteamId.Equals(steamId)); } public User CreateUser(string steamId) @@ -29,7 +36,8 @@ namespace TOOHUCardAPI.Data.Repositories Vip = true, EndTime = DateTime.MaxValue, LevelList = string.Empty, - PetLevel = 1 + PetLevel = 1, + EncodedCardGroups = new List() }; _appDbContext.Users.Add(user); _appDbContext.SaveChanges(); @@ -38,7 +46,7 @@ namespace TOOHUCardAPI.Data.Repositories public User UpdateUser(User user) { - var trackedUser = _appDbContext.Users.FirstOrDefault(u => u.SteamId == user.SteamId); + var trackedUser = GetAllUsersQuery().FirstOrDefault(u => u.SteamId == user.SteamId); if (trackedUser == default) { return trackedUser; diff --git a/TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.Designer.cs b/TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.Designer.cs new file mode 100644 index 0000000..71f0782 --- /dev/null +++ b/TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.Designer.cs @@ -0,0 +1,97 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TOOHUCardAPI.Data; + +namespace TOOHUCardAPI.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20211029035809_groupid for encoded card group")] + partial class groupidforencodedcardgroup + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.11"); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.EncodedCardGroup", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("CardGroupNumber") + .HasColumnType("INTEGER"); + + b.Property("EncodedString") + .HasColumnType("TEXT"); + + b.Property("UserSteamId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserSteamId"); + + b.ToTable("EncodedCardGroup"); + }); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.User", b => + { + b.Property("SteamId") + .HasColumnType("TEXT"); + + b.Property("Ban") + .HasColumnType("INTEGER"); + + b.Property("EndTime") + .HasColumnType("TEXT"); + + b.Property("KeySaveDate") + .HasColumnType("TEXT"); + + b.Property("KeyTotal") + .HasColumnType("INTEGER"); + + b.Property("LevelList") + .HasColumnType("TEXT"); + + b.Property("MaxTeamWave") + .HasColumnType("INTEGER"); + + b.Property("MaxWave") + .HasColumnType("INTEGER"); + + b.Property("PetLevel") + .HasColumnType("INTEGER"); + + b.Property("Point") + .HasColumnType("INTEGER"); + + b.Property("Vip") + .HasColumnType("INTEGER"); + + b.HasKey("SteamId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.EncodedCardGroup", b => + { + b.HasOne("TOOHUCardAPI.Data.Models.User", null) + .WithMany("EncodedCardGroups") + .HasForeignKey("UserSteamId"); + }); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.User", b => + { + b.Navigation("EncodedCardGroups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.cs b/TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.cs new file mode 100644 index 0000000..d787ee5 --- /dev/null +++ b/TOOHUCardAPI/Migrations/20211029035809_groupid for encoded card group.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace TOOHUCardAPI.Migrations +{ + public partial class groupidforencodedcardgroup : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Ban", + table: "Users", + type: "INTEGER", + nullable: false, + defaultValue: false); + + migrationBuilder.AddColumn( + name: "CardGroupNumber", + table: "EncodedCardGroup", + type: "INTEGER", + nullable: false, + defaultValue: 0); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Ban", + table: "Users"); + + migrationBuilder.DropColumn( + name: "CardGroupNumber", + table: "EncodedCardGroup"); + } + } +} diff --git a/TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.Designer.cs b/TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.Designer.cs new file mode 100644 index 0000000..478dc6e --- /dev/null +++ b/TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.Designer.cs @@ -0,0 +1,97 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TOOHUCardAPI.Data; + +namespace TOOHUCardAPI.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20211029043842_fix groupkey, they send full string")] + partial class fixgroupkeytheysendfullstring + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.11"); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.EncodedCardGroup", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EncodedString") + .HasColumnType("TEXT"); + + b.Property("GroupKey") + .HasColumnType("TEXT"); + + b.Property("UserSteamId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UserSteamId"); + + b.ToTable("EncodedCardGroup"); + }); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.User", b => + { + b.Property("SteamId") + .HasColumnType("TEXT"); + + b.Property("Ban") + .HasColumnType("INTEGER"); + + b.Property("EndTime") + .HasColumnType("TEXT"); + + b.Property("KeySaveDate") + .HasColumnType("TEXT"); + + b.Property("KeyTotal") + .HasColumnType("INTEGER"); + + b.Property("LevelList") + .HasColumnType("TEXT"); + + b.Property("MaxTeamWave") + .HasColumnType("INTEGER"); + + b.Property("MaxWave") + .HasColumnType("INTEGER"); + + b.Property("PetLevel") + .HasColumnType("INTEGER"); + + b.Property("Point") + .HasColumnType("INTEGER"); + + b.Property("Vip") + .HasColumnType("INTEGER"); + + b.HasKey("SteamId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.EncodedCardGroup", b => + { + b.HasOne("TOOHUCardAPI.Data.Models.User", null) + .WithMany("EncodedCardGroups") + .HasForeignKey("UserSteamId"); + }); + + modelBuilder.Entity("TOOHUCardAPI.Data.Models.User", b => + { + b.Navigation("EncodedCardGroups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.cs b/TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.cs new file mode 100644 index 0000000..3de1191 --- /dev/null +++ b/TOOHUCardAPI/Migrations/20211029043842_fix groupkey, they send full string.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace TOOHUCardAPI.Migrations +{ + public partial class fixgroupkeytheysendfullstring : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "CardGroupNumber", + table: "EncodedCardGroup"); + + migrationBuilder.AddColumn( + name: "GroupKey", + table: "EncodedCardGroup", + type: "TEXT", + nullable: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "GroupKey", + table: "EncodedCardGroup"); + + migrationBuilder.AddColumn( + name: "CardGroupNumber", + table: "EncodedCardGroup", + type: "INTEGER", + nullable: false, + defaultValue: 0); + } + } +} diff --git a/TOOHUCardAPI/Migrations/AppDbContextModelSnapshot.cs b/TOOHUCardAPI/Migrations/AppDbContextModelSnapshot.cs index e91366b..11cc204 100644 --- a/TOOHUCardAPI/Migrations/AppDbContextModelSnapshot.cs +++ b/TOOHUCardAPI/Migrations/AppDbContextModelSnapshot.cs @@ -25,6 +25,9 @@ namespace TOOHUCardAPI.Migrations b.Property("EncodedString") .HasColumnType("TEXT"); + b.Property("GroupKey") + .HasColumnType("TEXT"); + b.Property("UserSteamId") .HasColumnType("TEXT"); @@ -40,6 +43,9 @@ namespace TOOHUCardAPI.Migrations b.Property("SteamId") .HasColumnType("TEXT"); + b.Property("Ban") + .HasColumnType("INTEGER"); + b.Property("EndTime") .HasColumnType("TEXT");