92 lines
3.6 KiB
C#
92 lines
3.6 KiB
C#
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;
|
|
|
|
namespace TOOHUCardAPI.Data.Services
|
|
{
|
|
public class StoreService
|
|
{
|
|
private readonly ILogger<StoreService> _logger;
|
|
private readonly UserRepository _userRepository;
|
|
private readonly CardRepository _cardRepository;
|
|
|
|
public StoreService(ILogger<StoreService> logger, UserRepository userRepository, CardRepository cardRepository)
|
|
{
|
|
_logger = logger;
|
|
_userRepository = userRepository;
|
|
_cardRepository = cardRepository;
|
|
}
|
|
|
|
private async Task AddPoints(int points, User user)
|
|
{
|
|
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}");
|
|
}
|
|
|
|
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 = await _userRepository.GetUser(steamId);
|
|
if (user.Point < totalCost)
|
|
{
|
|
throw new InsufficientPointsException();
|
|
}
|
|
|
|
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}");
|
|
return totalCost;
|
|
}
|
|
|
|
public async Task<int> GiveFirstWinBonus(string steamId)
|
|
{
|
|
User user = await _userRepository.GetUser(steamId);
|
|
|
|
if (user.LastFirstWin.AddDays(1) > DateTime.Now)
|
|
{
|
|
throw new NotFirstWinException();
|
|
}
|
|
await AddPoints(AppSettings.FirstWinBonusPoints, user);
|
|
user.LastFirstWin = DateTime.Now;
|
|
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;
|
|
}
|
|
}
|
|
|
|
} |