92 lines
3.5 KiB
C#
92 lines
3.5 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;
|
|
|
|
namespace TOOHUCardAPI.Data.Services
|
|
{
|
|
public class UserService
|
|
{
|
|
private readonly ILogger<UserService> _logger;
|
|
private readonly UserRepository _userRepository;
|
|
|
|
public UserService(ILogger<UserService> logger, UserRepository userRepository)
|
|
{
|
|
_logger = logger;
|
|
_userRepository = userRepository;
|
|
}
|
|
|
|
public async Task<User> LoginUser(long steamId)
|
|
{
|
|
_logger.LogInformation("User {SteamId} just logged in", steamId);
|
|
User user = await _userRepository.GetOrCreateUser(steamId);
|
|
if (user.LastDailyLoginBonus.AddDays(1) <= DateTime.Now)
|
|
{
|
|
user = await HandleDailyLoginBonus(user);
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
private async Task<User> HandleDailyLoginBonus(User user)
|
|
{
|
|
user.KeyTotal += AppSettings.DailyKeyBonus;
|
|
user.LastDailyLoginBonus = DateTime.Now;;
|
|
await _userRepository.UpdateUser(user);
|
|
_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(long steamId, string groupKey, string groupData)
|
|
{
|
|
User user = await _userRepository.GetUser(steamId);
|
|
|
|
EncodedCardGroup group = user.EncodedCardGroups.FirstOrDefault(group => group.GroupKey == groupKey) ?? new EncodedCardGroup()
|
|
{
|
|
GroupKey = groupKey
|
|
};
|
|
group.EncodedString = groupData;
|
|
user.EncodedCardGroups = user.EncodedCardGroups.Where(group2 => group.Id != group2.Id).Append(group).ToList();
|
|
_logger.LogInformation("{SteamId} just stored a card group", user.SteamId);
|
|
await _userRepository.UpdateUser(user);
|
|
}
|
|
|
|
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 {SteamId} saved new pet data with model: {PetModel} and effect {PetEffect}", user.SteamId, petModel, petEffect);
|
|
}
|
|
|
|
public async Task UpdateUserAccountDetails(long steamid, long newAccountId, string newUsername)
|
|
{
|
|
User user = await _userRepository.GetUser(steamid);
|
|
user.AccountId = newAccountId;
|
|
user.Username = newUsername;
|
|
await _userRepository.UpdateUser(user);
|
|
}
|
|
|
|
public async Task UpdateUserMaxWave(long steamid, int wave, RankType rankType)
|
|
{
|
|
User user = await _userRepository.GetUser(steamid);
|
|
switch (rankType)
|
|
{
|
|
case RankType.Single:
|
|
user.MaxWave = Math.Max(wave, user.MaxWave);
|
|
break;
|
|
case RankType.Team:
|
|
user.MaxTeamWave = Math.Max(wave, user.MaxTeamWave);
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(rankType), rankType, null);
|
|
}
|
|
|
|
await _userRepository.UpdateUser(user);
|
|
}
|
|
}
|
|
} |