Lots of additions and restructuring

This commit is contained in:
2021-10-30 21:58:43 -04:00
parent 9cf11e982f
commit 20cac8c378
37 changed files with 4465 additions and 117 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using TOOHUCardAPI.Data.Enums;
using TOOHUCardAPI.Data.Models;
using TOOHUCardAPI.Data.Repositories;
using TOOHUCardAPI.DTO;
@@ -11,77 +13,80 @@ namespace TOOHUCardAPI.Data.Services
{
private readonly ILogger<StoreService> _logger;
private readonly UserRepository _userRepository;
private readonly CardRepository _cardRepository;
public StoreService(ILogger<StoreService> logger, UserRepository userRepository)
public StoreService(ILogger<StoreService> logger, UserRepository userRepository, CardRepository cardRepository)
{
_logger = logger;
_userRepository = userRepository;
_cardRepository = cardRepository;
}
private void AddPoints(int points, User user)
private async Task AddPoints(int points, User user)
{
user.Point = Math.Max(0, user.Point + points);
_userRepository.UpdateUser(user);
await _userRepository.UpdateUser(user);
_logger.LogInformation($"User with steamid {user.SteamId} was given {points} points. New total: {user.Point}");
}
public async Task<int> LevelUpCard(string steamId, string itemName, int levelIncrease)
{
User user = await _userRepository.GetUser(steamId);
Card card = await _cardRepository.GetCardByItemCode(itemName);
CardLevel userCardLevel = user.CardLevels.Find(cl => cl.Card.ItemCode == itemName) ?? new CardLevel()
{
Card = card,
Level = 0
};
int totalCost =
(card.Quality == Rarity.NORMAL ? AppSettings.PointsPerLevelNormal : AppSettings.PointsPerLevel) *
levelIncrease;
if (user.Point < totalCost)
{
throw new InsufficientPointsException();
}
if (!user.CardLevels.Contains(userCardLevel))
{
user.CardLevels.Add(userCardLevel);
}
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}");
return totalCost;
}
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();
}
User user = await _userRepository.GetUser(steamId);
if (user.Point < totalCost)
{
throw new InsufficientPointsException();
}
AddPoints(-totalCost, user);
await AddPoints(-totalCost, user);
user.KeyTotal += amt;
_userRepository.UpdateUser(user);
await _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();
}
User user = await _userRepository.GetUser(steamId);
if (user.LastFirstWin.AddDays(1) > DateTime.Now)
{
throw new NotFirstWinException();
}
AddPoints(AppSettings.FirstWinBonusPoints, user);
await AddPoints(AppSettings.FirstWinBonusPoints, user);
user.LastFirstWin = DateTime.Now;
_userRepository.UpdateUser(user);
await _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";
}
}
}