rank endpoint done

This commit is contained in:
2021-11-07 14:00:18 -05:00
parent e232accdb1
commit e14f7fae74
18 changed files with 2536 additions and 25 deletions

View File

@@ -15,12 +15,42 @@ namespace TOOHUCardAPI.Data.Services
private readonly ILogger<RankService> _logger;
private readonly RankRepository _rankRepository;
private readonly UserRepository _userRepository;
private readonly UserService _userService;
private readonly StoreService _storeService;
public RankService(ILogger<RankService> logger, RankRepository rankRepository, UserRepository userRepository)
public RankService(ILogger<RankService> logger, RankRepository rankRepository, UserRepository userRepository, UserService userService, StoreService storeService)
{
_logger = logger;
_rankRepository = rankRepository;
_userRepository = userRepository;
_userService = userService;
_storeService = storeService;
}
private async Task AwardPointsForEntry(long steamid, RankEntry entry, List<RankEntry> allEntries)
{
int bonus = AppSettings.DefaultRankBonus;
int index = allEntries.FindIndex(i => i.Id == entry.Id)+1;
switch (index)
{
case <= 4:
bonus = 500 - (index - 1) * 100;
break;
case <= 9:
bonus = 170 - (index - 5) * 10;
break;
case <=15:
bonus = 125 - (index - 10) * 5;
break;
case <= 25:
bonus = 96 - (index - 16) * 4;
break;
case <= 50:
bonus = 58 - (index - 26) * 2;
break;
}
await _storeService.AwardPoints(steamid, bonus, $"reached rank index {index}");
}
public async Task<List<RankEntryDTO>> GetRankEntries(RankType rankType)
@@ -40,17 +70,26 @@ namespace TOOHUCardAPI.Data.Services
RankEntry toUpload = new RankEntry
{
RankType = RankTypeExtensions.ParseRankType(entry.RankType),
Username = entry.Username,
User = await _userRepository.GetUser(entry.SteamId),
AccountId = entry.UserId,
Version = entry.Version,
Wave = entry.Wave,
Damage = entry.Damage,
TowersUsed = towersUsed,
UpdateTime = DateTime.Now
};
await _rankRepository.InsertRankEntry(toUpload);
_logger.LogInformation("Uploaded a rank entry for {SteamId}", entry.SteamId);
toUpload = await _rankRepository.InsertRankEntry(toUpload);
List<RankEntry> allEntries = (await _rankRepository.GetRankEntries(toUpload.RankType)).ToList();
await _userService.UpdateUserAccountDetails(entry.SteamId, entry.UserId, entry.Username);
await _userService.UpdateUserMaxWave(entry.SteamId, entry.Wave, toUpload.RankType);
await AwardPointsForEntry(entry.SteamId, toUpload, allEntries);
_logger.LogInformation("Uploaded a rank entry for {SteamId} on ranking type {RankType}", entry.SteamId, toUpload.RankType.ToString());
}
public async Task ResetRank(long steamid, RankType rankType)
{
RankEntry entry = await _rankRepository.GetRankEntry(steamid, rankType);
await _rankRepository.DeleteRankEntry(entry);
_logger.LogInformation("Reset rank entry for {SteamId} on ranking type {RankType}", steamid, rankType.ToString());
}
}
}

View File

@@ -29,6 +29,13 @@ namespace TOOHUCardAPI.Data.Services
_logger.LogInformation("User with steamid {SteamId} was given {PointsGiven} points. New total: {PointsTotal}", user.SteamId, points, user.Point);
}
public async Task AwardPoints(long steamid, int points, string reason)
{
User user = await _userRepository.GetUser(steamid);
await AddPoints(points, user);
_logger.LogInformation("awarded {Points} points to {SteamId} for reason [\"{reason}\"]", points, steamid, reason);
}
public async Task<User> SaveKeyTotal(long steamId, int keyTotal, int keyUseCount)
{
User user = await _userRepository.GetUser(steamId);

View File

@@ -2,6 +2,7 @@ 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;
@@ -61,5 +62,31 @@ namespace TOOHUCardAPI.Data.Services
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);
}
}
}