rank endpoint done
This commit is contained in:
@@ -14,6 +14,7 @@ namespace TOOHUCardAPI.Data
|
||||
public static int MaxKeyPurchaseAmount { get; set; }
|
||||
public static string StarterDeckGroupKey { get; set; }
|
||||
public static string StarterDeckGroupData { get; set; }
|
||||
public static int DefaultRankBonus { get; set; }
|
||||
public static void Init(IConfiguration configuration)
|
||||
{
|
||||
var fields = typeof(AppSettings).GetProperties();
|
||||
|
||||
@@ -10,5 +10,6 @@ namespace TOOHUCardAPI.Data.Models
|
||||
public int Id { get; set; }
|
||||
public string GroupKey { get; set; }
|
||||
public string EncodedString { get; set; }
|
||||
public long UserSteamId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,7 @@ namespace TOOHUCardAPI.Data.Models
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
public RankType RankType { get; set; }
|
||||
public string Username { get; set; }
|
||||
public User User { get; set; }
|
||||
public long AccountId { get; set; }
|
||||
public string Version { get; set; }
|
||||
public int Wave { get; set; }
|
||||
public float Damage { get; set; }
|
||||
|
||||
@@ -11,5 +11,6 @@ namespace TOOHUCardAPI.Data.Models
|
||||
public int Id { get; set; }
|
||||
public string TowerKey { get; set; }
|
||||
public string EncodedData { get; set; }
|
||||
public int RankEntryId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,8 @@ namespace TOOHUCardAPI.Data.Models
|
||||
public int PowerMaxTotal { get; set; }
|
||||
public string PetModel { get; set; }
|
||||
public string PetEffect { get; set; }
|
||||
public string Username { get; set; }
|
||||
public long AccountId { get; set; }
|
||||
public List<EncodedCardGroup> EncodedCardGroups { get; set; }
|
||||
public List<CardLevel> CardLevels { get; set; }
|
||||
}
|
||||
|
||||
@@ -27,12 +27,25 @@ namespace TOOHUCardAPI.Data.Repositories
|
||||
|
||||
public async Task<IEnumerable<RankEntry>> GetRankEntries(RankType rankType)
|
||||
{
|
||||
return await AllIncluded().Where(i => i.RankType == rankType).ToListAsync();
|
||||
return await AllIncluded().Where(i => i.RankType == rankType).OrderByDescending(i => i.Wave).ThenByDescending(i => i.Damage).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task InsertRankEntry(RankEntry entry)
|
||||
public async Task<RankEntry> GetRankEntry(long steamid, RankType rankType)
|
||||
{
|
||||
await _appDbContext.RankEntries.AddAsync(entry);
|
||||
return await AllIncluded().Where(i => i.User.SteamId == steamid && i.RankType == rankType).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<RankEntry> InsertRankEntry(RankEntry entry)
|
||||
{
|
||||
var newEntry = await _appDbContext.RankEntries.AddAsync(entry);
|
||||
await _appDbContext.SaveChangesAsync();
|
||||
return newEntry.Entity;
|
||||
}
|
||||
|
||||
public async Task DeleteRankEntry(RankEntry entry)
|
||||
{
|
||||
entry.TowersUsed.Clear();
|
||||
_appDbContext.RankEntries.Remove(entry);
|
||||
await _appDbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user