More additions, lucky card based on day, redo of migrations because i messed up

This commit is contained in:
2021-11-01 14:59:36 -04:00
parent 685f09a889
commit 0b94455b4b
48 changed files with 284 additions and 7404 deletions

View File

@@ -12,6 +12,7 @@ namespace TOOHUCardAPI.Data
{
public DbSet<User> Users { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<RankEntry> RankEntries { get; set; }
private readonly IEnumerable<ISeeder> _seeders = new List<ISeeder>
{

View File

@@ -0,0 +1,9 @@
namespace TOOHUCardAPI.Data.Enums
{
public enum RankType
{
Single = 'S',
Team = 'D',
All = 'T'
}
}

View File

@@ -2,9 +2,9 @@ namespace TOOHUCardAPI.Data.Enums
{
public enum Rarity
{
NORMAL = 1,
RARE = 2,
SUPER_RARE = 3,
SUPER_SECRET_RARE = 4
Normal = 1,
Rare = 2,
SuperRare = 3,
SuperSecretRare = 4
}
}

View File

@@ -2,7 +2,7 @@ namespace TOOHUCardAPI.Data.Models
{
public class CardLevel
{
public string UserSteamId { get; set; }
public long UserSteamId { get; set; }
public string CardItemCode { get; set; }
public Card Card { get; set; }
public int Level { get; set; }

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using TOOHUCardAPI.Data.Enums;
namespace TOOHUCardAPI.Data.Models
{
public class RankEntry
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public RankType RankType { get; set; }
public User User { get; set; }
public string Version { get; set; }
public int Wave { get; set; }
public long Damage { get; set; }
public List<RankTowerEntry> TowersUsed { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace TOOHUCardAPI.Data.Models
{
public class RankTowerEntry
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Card BaseUnit { get; set; }
public int Star { get; set; }
public int Damage { get; set; }
public int Attack { get; set; }
public int Power { get; set; }
public List<Card> Equipment { get; set; }
}
}

View File

@@ -7,7 +7,7 @@ namespace TOOHUCardAPI.Data.Models
public class User
{
[Key]
public string SteamId { get; set; }
public long SteamId { get; set; }
public int MaxWave { get; set; }
public int MaxTeamWave { get; set; }
public int PetLevel { get; set; }

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TOOHUCardAPI.Data.Models;
@@ -23,5 +24,10 @@ namespace TOOHUCardAPI.Data.Repositories
return card;
}
public async Task<IEnumerable<Card>> GetAllCards()
{
return await _context.Cards.ToListAsync();
}
}
}

View File

@@ -29,7 +29,7 @@ namespace TOOHUCardAPI.Data.Repositories
.AsQueryable();
}
public async Task<User> GetUser(string steamId, bool allowNull = false)
public async Task<User> GetUser(long steamId, bool allowNull = false)
{
User user = await GetAllUsersQuery().FirstOrDefaultAsync(user => user.SteamId.Equals(steamId));
if (user == null && !allowNull)
@@ -40,7 +40,7 @@ namespace TOOHUCardAPI.Data.Repositories
return user;
}
public async Task<User> CreateUser(string steamId)
public async Task<User> CreateUser(long steamId)
{
User user = new User
{
@@ -77,7 +77,7 @@ namespace TOOHUCardAPI.Data.Repositories
return user;
}
public async Task<User> GetOrCreateUser(string steamId)
public async Task<User> GetOrCreateUser(long steamId)
{
User user = await GetUser(steamId, true) ?? await CreateUser(steamId);
return user;

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using TOOHUCardAPI.Data.Repositories;
using TOOHUCardAPI.DTO;
namespace TOOHUCardAPI.Data.Services
{
public class GameConfigurationService
{
private readonly ILogger<GameConfigurationService> _logger;
private readonly CardRepository _cardRepository;
private List<string> _cardBlacklist = new()
{
"_",
"BonusEgg"
};
public GameConfigurationService(ILogger<GameConfigurationService> logger, CardRepository cardRepository)
{
_logger = logger;
_cardRepository = cardRepository;
}
public async Task<GameConfigResponse> GetGameConfiguration()
{
var random = new Random(DateTime.Now.Date.GetHashCode());
GameConfigResponse response = new GameConfigResponse();
var cards = await _cardRepository.GetAllCards();
var validCards =
cards.Where(card => _cardBlacklist.Select(bl => !card.CardName.Contains(bl)).All(res => res))
.Select(card => card.CardName).Append("all");
var enumerable = validCards as string[] ?? validCards.ToArray();
var index = random.Next(0, enumerable.Length);
response.LuckCard = enumerable.ElementAt(index);
return response;
}
}
}

View File

@@ -0,0 +1,10 @@
using Microsoft.Extensions.Logging;
namespace TOOHUCardAPI.Data.Services
{
public class RankService
{
private readonly ILogger<RankService> _logger;
}
}

View File

@@ -26,28 +26,29 @@ namespace TOOHUCardAPI.Data.Services
{
user.Point = Math.Max(0, user.Point + points);
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} was given {points} points. New total: {user.Point}");
_logger.LogInformation("User with steamid {SteamId} was given {PointsGiven} points. New total: {PointsTotal}", user.SteamId, points, user.Point);
}
public async Task<User> SaveKeyTotal(string steamId, int keyTotal, int keyUseCount)
public async Task<User> SaveKeyTotal(long steamId, int keyTotal, int keyUseCount)
{
User user = await _userRepository.GetUser(steamId);
_logger.LogInformation($"User {user.SteamId} just stored some key total data, previous total and use: ({user.KeyTotal},{user.KeyUseCount}). new ({keyTotal},{keyUseCount})");
_logger.LogInformation("User {SteamId} just stored some key total data, previous total and use: ({OldKeyTotal},{OldKeyUseCount}). new ({NewKeyTotal},{NewKeyUseCount})", user.SteamId, user.KeyTotal, user.KeyUseCount, keyTotal, keyUseCount);
user.KeyTotal = keyTotal;
user.KeyUseCount = keyUseCount;
await _userRepository.UpdateUser(user);
return user;
}
public async Task<int> ChangePowerMaxTotal(string steamId, int amt)
public async Task<int> ChangePowerMaxTotal(long steamId, int amt)
{
User user = await _userRepository.GetUser(steamId);
user.PowerMaxTotal = Math.Clamp(user.PowerMaxTotal + amt, 0, int.MaxValue);
await _userRepository.UpdateUser(user);
_logger.LogInformation("User {SteamId} changed the number of MaxPower items they had by {Amount}", user.SteamId, amt);
return user.PowerMaxTotal;
}
public async Task<int> LevelUpCard(string steamId, string itemName, int levelIncrease)
public async Task<int> LevelUpCard(long steamId, string itemName, int levelIncrease)
{
User user = await _userRepository.GetUser(steamId);
Card card = await _cardRepository.GetCardByItemCode(itemName);
@@ -57,7 +58,7 @@ namespace TOOHUCardAPI.Data.Services
Level = 0
};
int totalCost =
(card.Quality == Rarity.NORMAL ? AppSettings.PointsPerLevelNormal : AppSettings.PointsPerLevel) *
(card.Quality == Rarity.Normal ? AppSettings.PointsPerLevelNormal : AppSettings.PointsPerLevel) *
levelIncrease;
if (user.Point < totalCost)
{
@@ -70,11 +71,11 @@ namespace TOOHUCardAPI.Data.Services
await AddPoints(-totalCost, user);
userCardLevel.Level += levelIncrease;
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} leveled up card {card.CardName} to level {userCardLevel.Level}");
_logger.LogInformation("User with steamid {SteamId} leveled up card {CardName} to level {Level}", user.SteamId, card.CardName, userCardLevel.Level);
return totalCost;
}
public async Task<int> PurchaseMagicKey(int amt, string steamId)
public async Task<int> PurchaseMagicKey(int amt, long steamId)
{
int totalCost = AppSettings.PointsPerKey * amt;
amt = Math.Clamp(amt, 0, AppSettings.MaxKeyPurchaseAmount);
@@ -87,11 +88,11 @@ namespace TOOHUCardAPI.Data.Services
await AddPoints(-totalCost, user);
user.KeyTotal += amt;
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} purchased {amt} keys for {totalCost} points. New point value is {user.Point}");
_logger.LogInformation("User with steamid {SteamId} purchased {NumberKeys} keys for {TotalCost} points. New point value is {NewPoints}", user.SteamId, amt, totalCost, user.Point);
return totalCost;
}
public async Task<int> GiveFirstWinBonus(string steamId)
public async Task<int> GiveFirstWinBonus(long steamId)
{
User user = await _userRepository.GetUser(steamId);
@@ -104,7 +105,7 @@ namespace TOOHUCardAPI.Data.Services
await AddPoints(points, user);
user.LastFirstWin = DateTime.Now;
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} received first win of the day bonus, earning {points} points. New value: {user.Point}");
_logger.LogInformation("User with steamid {SteamId} received first win of the day bonus, earning {BonusPoints} points. New value: {UserPoints}", user.SteamId, points, user.Point);
return points;
}
}

View File

@@ -18,9 +18,9 @@ namespace TOOHUCardAPI.Data.Services
_userRepository = userRepository;
}
public async Task<User> LoginUser(string steamId)
public async Task<User> LoginUser(long steamId)
{
_logger.LogInformation($"User {steamId} just logged in");
_logger.LogInformation("User {SteamId} just logged in", steamId);
User user = await _userRepository.GetOrCreateUser(steamId);
if (user.LastDailyLoginBonus.AddDays(1) <= DateTime.Now)
{
@@ -35,11 +35,11 @@ namespace TOOHUCardAPI.Data.Services
user.KeyTotal += AppSettings.DailyKeyBonus;
user.LastDailyLoginBonus = DateTime.Now;;
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User {user.SteamId} received a daily login bonus. Keys earned: {AppSettings.DailyKeyBonus}. New Key total: {user.KeyTotal}");
_logger.LogInformation("User {SteamId} received a daily login bonus. Keys earned: {DailyKeyBonus}. New Key total: {KeyTotal}", user.SteamId, AppSettings.DailyKeyBonus, user.KeyTotal);
return user;
}
public async Task SaveCardGroup(string steamId, string groupKey, string groupData)
public async Task SaveCardGroup(long steamId, string groupKey, string groupData)
{
User user = await _userRepository.GetUser(steamId);
@@ -49,17 +49,17 @@ namespace TOOHUCardAPI.Data.Services
};
group.EncodedString = groupData;
user.EncodedCardGroups = user.EncodedCardGroups.Where(group2 => group.Id != group2.Id).Append(group).ToList();
_logger.LogInformation($"{user.SteamId} just stored a card group");
_logger.LogInformation("{SteamId} just stored a card group", user.SteamId);
await _userRepository.UpdateUser(user);
}
public async Task SavePetData(string steamId, string petModel, string petEffect)
public async Task SavePetData(long steamId, string petModel, string petEffect)
{
User user = await _userRepository.GetUser(steamId);
user.PetModel = petModel;
user.PetEffect = petEffect;
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User {user.SteamId} saved new pet data with model: {petModel} and effect {petEffect}");
_logger.LogInformation("User {SteamId} saved new pet data with model: {PetModel} and effect {PetEffect}", user.SteamId, petModel, petEffect);
}
}
}