Lots of additions and changes

This commit is contained in:
2021-10-30 11:20:36 -04:00
parent 3051b63e44
commit 9cf11e982f
29 changed files with 557 additions and 46 deletions

View File

@@ -6,11 +6,18 @@ namespace TOOHUCardAPI.Data
public class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Card> Cards { get; set; }
public AppDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);

View File

@@ -0,0 +1,33 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace TOOHUCardAPI.Data
{
public static class AppSettings
{
public static int PointsPerLevel { get; set; }
public static int PointsPerLevelNormal { get; set; }
public static int FirstWinBonusPoints { get; set; }
public static int PointsPerKey { get; set; }
public static int MaxKeyPurchaseAmount { get; set; }
public static void Init(IConfiguration configuration)
{
var fields = typeof(AppSettings).GetProperties();
foreach (var propertyInfo in fields)
{
if (propertyInfo.PropertyType == typeof(string))
{
propertyInfo.SetValue(null, configuration.GetValue<string>(propertyInfo.Name));
}
else if (propertyInfo.PropertyType == typeof(int))
{
propertyInfo.SetValue(null, configuration.GetValue<int>(propertyInfo.Name));
}
else if (propertyInfo.PropertyType == typeof(double))
{
propertyInfo.SetValue(null, configuration.GetValue<double>(propertyInfo.Name));
}
}
}
}
}

View File

@@ -1,6 +1,7 @@
using AutoMapper;
using TOOHUCardAPI.Data.Models;
using TOOHUCardAPI.DTO;
using TOOHUCardAPI.DTO.PlayerData;
namespace TOOHUCardAPI.Data
{
@@ -9,6 +10,8 @@ namespace TOOHUCardAPI.Data
public AutomapProfile()
{
CreateMap<User, PlayerDataGetResponseObjectPlayer>();
CreateMap<User, PlayerBaseDataResponse>();
}
}
}

View File

@@ -0,0 +1,10 @@
namespace TOOHUCardAPI.Data.Enums
{
public enum Rarity
{
NORMAL = 1,
RARE = 2,
SUPER_RARE = 3,
SUPER_SECRET_RARE = 4
}
}

View File

@@ -0,0 +1,25 @@
using System;
using TOOHUCardAPI.Data.Services;
using TOOHUCardAPI.DTO;
namespace TOOHUCardAPI.Data
{
public abstract class TooHooException : Exception
{
public abstract AbstractResponse response { get; }
}
public class InvalidUserException : TooHooException
{
public override AbstractResponse response => new InvalidUserResponse();
}
public class InsufficientPointsException : TooHooException
{
public override AbstractResponse response => new InsufficientPointsResponse();
}
public class NotFirstWinException : TooHooException
{
public override AbstractResponse response => new NotFirstWinResponse();
}
}

View File

@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using TOOHUCardAPI.Data.Enums;
namespace TOOHUCardAPI.Data.Models
{
public class Card
{
[Key]
public string ItemCode { get; set; }
public string CardName { get; set; }
public Rarity Rarity { get; set; }
public bool HasVoice { get; set; }
public bool HasPortrait { get; set; }
}
}

View File

@@ -13,10 +13,12 @@ namespace TOOHUCardAPI.Data.Models
public int PetLevel { get; set; }
public DateTime EndTime { get; set; }
public int KeyTotal { get; set; }
public int KeyUseCount { get; set; }
public DateTime KeySaveDate { get; set; }
public bool Vip { get; set; }
public bool Ban { get; set; }
public int Point { get; set; }
public DateTime LastFirstWin { get; set; }
public string LevelList { get; set; }
public List<EncodedCardGroup> EncodedCardGroups { get; set; }
}

View File

@@ -0,0 +1,87 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using TOOHUCardAPI.Data.Models;
using TOOHUCardAPI.Data.Repositories;
using TOOHUCardAPI.DTO;
namespace TOOHUCardAPI.Data.Services
{
public class StoreService
{
private readonly ILogger<StoreService> _logger;
private readonly UserRepository _userRepository;
public StoreService(ILogger<StoreService> logger, UserRepository userRepository)
{
_logger = logger;
_userRepository = userRepository;
}
private void AddPoints(int points, User user)
{
user.Point = Math.Max(0, user.Point + points);
_userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} was given {points} points. New total: {user.Point}");
}
public async Task<int> PurchaseMagicKey(int amt, string steamId)
{
int totalCost = AppSettings.PointsPerKey * amt;
amt = Math.Clamp(amt, 0, AppSettings.MaxKeyPurchaseAmount);
User user = _userRepository.GetUser(steamId);
if (user == null)
{
throw new InvalidUserException();
}
if (user.Point < totalCost)
{
throw new InsufficientPointsException();
}
AddPoints(-totalCost, user);
user.KeyTotal += amt;
_userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} purchased {amt} keys for {totalCost} points. New point value is {user.Point}");
return totalCost;
}
public async Task<int> GiveFirstWinBonus(string steamId)
{
User user = _userRepository.GetUser(steamId);
if (user == null)
{
throw new InvalidUserException();
}
if (user.LastFirstWin.AddDays(1) > DateTime.Now)
{
throw new NotFirstWinException();
}
AddPoints(AppSettings.FirstWinBonusPoints, user);
user.LastFirstWin = DateTime.Now;
_userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} received first win of the day bonus, earning {AppSettings.FirstWinBonusPoints} points. New value: {user.Point}");
return AppSettings.FirstWinBonusPoints;
}
}
public class InsufficientPointsResponse : AbstractResponse
{
public InsufficientPointsResponse()
{
Code = "0002";
Message = "Insufficient points";
}
}
public class NotFirstWinResponse : AbstractResponse
{
public NotFirstWinResponse()
{
Code = "0003";
Message = "Not first win of the day";
}
}
}