Added renaming users and main gw2 accounts
This commit is contained in:
parent
aad07809d2
commit
c4ecb512d7
13 changed files with 172 additions and 22 deletions
|
@ -18,12 +18,14 @@ namespace Lieb.Controllers
|
|||
RaidService _raidService;
|
||||
UserService _userService;
|
||||
GuildWars2AccountService _gw2AccountService;
|
||||
DiscordService _discordService;
|
||||
|
||||
public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService)
|
||||
public DiscordBotController(RaidService raidService, UserService userService, GuildWars2AccountService gw2AccountService, DiscordService discordService)
|
||||
{
|
||||
_raidService = raidService;
|
||||
_userService = userService;
|
||||
_gw2AccountService = gw2AccountService;
|
||||
_discordService = discordService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
@ -81,7 +83,7 @@ namespace Lieb.Controllers
|
|||
{
|
||||
if(signUp.userId != 0)
|
||||
{
|
||||
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
|
||||
int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId;
|
||||
await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.SignedUp, signUp.signedUpByUserId);
|
||||
}
|
||||
else
|
||||
|
@ -96,7 +98,7 @@ namespace Lieb.Controllers
|
|||
{
|
||||
if(signUp.userId != 0)
|
||||
{
|
||||
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
|
||||
int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId;
|
||||
await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.Maybe, signUp.signedUpByUserId);
|
||||
}
|
||||
else
|
||||
|
@ -111,7 +113,7 @@ namespace Lieb.Controllers
|
|||
{
|
||||
if(signUp.userId != 0)
|
||||
{
|
||||
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
|
||||
int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId;
|
||||
await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.Backup, signUp.signedUpByUserId);
|
||||
}
|
||||
else
|
||||
|
@ -126,7 +128,7 @@ namespace Lieb.Controllers
|
|||
{
|
||||
if(signUp.userId != 0)
|
||||
{
|
||||
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
|
||||
int accountId = _userService.GetMainAccount(signUp.userId).GuildWars2AccountId;
|
||||
await _raidService.SignUp(signUp.raidId, signUp.userId, accountId, signUp.roleId, SignUpType.Flex, signUp.signedUpByUserId);
|
||||
}
|
||||
else
|
||||
|
@ -141,7 +143,6 @@ namespace Lieb.Controllers
|
|||
{
|
||||
if(signUp.userId != 0)
|
||||
{
|
||||
int accountId = _userService.GetLiebUserGW2AccountOnly(signUp.userId).GuildWars2Accounts.FirstOrDefault(new GuildWars2Account()).GuildWars2AccountId;
|
||||
await _raidService.SignOff(signUp.raidId, signUp.userId, signUp.signedUpByUserId);
|
||||
}
|
||||
else
|
||||
|
@ -176,5 +177,12 @@ namespace Lieb.Controllers
|
|||
|
||||
return DiscordService.ConvertRaid(raid);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("[action]")]
|
||||
public List<ulong> GetUserRenameServers()
|
||||
{
|
||||
return _discordService.GetUserRenameServers();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,8 +40,8 @@ namespace Lieb.Data
|
|||
GuildWars2Account bloodseeker = new GuildWars2Account() { AccountName = "Bloodseeker.2043" };
|
||||
var users = new LiebUser[]
|
||||
{
|
||||
//new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
|
||||
new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
|
||||
new LiebUser{Id=0, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
|
||||
//new LiebUser{Id=194863625477816321, Name="Sarah", Birthday=DateTime.Parse("1992-01-15"), GuildWars2Accounts = new List<GuildWars2Account>(){ linaith, sarah} },
|
||||
#if DEBUG
|
||||
//new LiebUser{Id=194455125769715713, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
|
||||
new LiebUser{Id=1, Name="Lisa", GuildWars2Accounts = new List<GuildWars2Account>(){ hierpiepts}},
|
||||
|
|
|
@ -51,6 +51,15 @@ namespace Lieb.Data
|
|||
}
|
||||
}
|
||||
|
||||
public List<ulong> GetUserRenameServers()
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
return context.DiscordSettings
|
||||
.Where(s => s.ChangeUserNames)
|
||||
.Select(s => s.DiscordSettingsId)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task PostRaidMessage(int raidId)
|
||||
{
|
||||
try
|
||||
|
@ -320,5 +329,31 @@ namespace Lieb.Data
|
|||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public async Task RenameUser(ulong userId, string name, string account)
|
||||
{
|
||||
try
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(Constants.HttpClientName);
|
||||
|
||||
ApiRenameUser renameUser = new ApiRenameUser()
|
||||
{
|
||||
userId = userId,
|
||||
Name = name,
|
||||
Account = account,
|
||||
ServerIds = GetUserRenameServers()
|
||||
};
|
||||
|
||||
var messageItemJson = new StringContent(
|
||||
JsonSerializer.Serialize(renameUser),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
|
||||
var httpResponseMessage = await httpClient.PostAsync("raid/RenameUser", messageItemJson);
|
||||
|
||||
httpResponseMessage.EnsureSuccessStatusCode();
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,10 +7,12 @@ namespace Lieb.Data
|
|||
public class GuildWars2AccountService
|
||||
{
|
||||
private readonly IDbContextFactory<LiebContext> _contextFactory;
|
||||
private readonly DiscordService _discordService;
|
||||
|
||||
public GuildWars2AccountService(IDbContextFactory<LiebContext> contextFactory)
|
||||
public GuildWars2AccountService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
_discordService = discordService;
|
||||
}
|
||||
|
||||
public GuildWars2Account GetAccount(int gw2AccountId)
|
||||
|
@ -29,17 +31,29 @@ namespace Lieb.Data
|
|||
using var context = _contextFactory.CreateDbContext();
|
||||
if (account.GuildWars2AccountId == 0)
|
||||
{
|
||||
LiebUser? user = context.LiebUsers.FirstOrDefault(u => u.Id == userId);
|
||||
LiebUser? user = context.LiebUsers.Include(u => u.GuildWars2Accounts).FirstOrDefault(u => u.Id == userId);
|
||||
if(user != null)
|
||||
{
|
||||
user.GuildWars2Accounts.Add(account);
|
||||
await context.SaveChangesAsync();
|
||||
if(user.GuildWars2Accounts.Count == 1)
|
||||
{
|
||||
user.MainGW2Account = account.GuildWars2AccountId;
|
||||
await _discordService.RenameUser(userId, user.Name, account.AccountName);
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Update(account);
|
||||
await context.SaveChangesAsync();
|
||||
LiebUser? user = context.LiebUsers.Include(u => u.GuildWars2Accounts).FirstOrDefault(u => u.Id == userId);
|
||||
if(user != null && user.MainGW2Account == account.GuildWars2AccountId)
|
||||
{
|
||||
await _discordService.RenameUser(userId, user.Name, account.AccountName);
|
||||
}
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,9 +64,18 @@ namespace Lieb.Data
|
|||
if (account != null)
|
||||
{
|
||||
context.Equipped.RemoveRange(account.EquippedBuilds);
|
||||
context.RaidSignUps.RemoveRange(context.RaidSignUps.Where(s => s.GuildWars2AccountId == accountId));
|
||||
await context.SaveChangesAsync();
|
||||
context.GuildWars2Accounts.Remove(account);
|
||||
LiebUser? user = context.LiebUsers.Include(u => u.GuildWars2Accounts).FirstOrDefault(u => u.GuildWars2Accounts.Contains(account));
|
||||
await context.SaveChangesAsync();
|
||||
if(user != null && user.MainGW2Account == account.GuildWars2AccountId)
|
||||
{
|
||||
GuildWars2Account newMain = user.GuildWars2Accounts.FirstOrDefault(new GuildWars2Account());
|
||||
user.MainGW2Account = newMain.GuildWars2AccountId;
|
||||
await context.SaveChangesAsync();
|
||||
await _discordService.RenameUser(user.Id, user.Name, newMain.AccountName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,12 @@ namespace Lieb.Data
|
|||
public class UserService
|
||||
{
|
||||
private readonly IDbContextFactory<LiebContext> _contextFactory;
|
||||
private readonly DiscordService _discordService;
|
||||
|
||||
public UserService(IDbContextFactory<LiebContext> contextFactory)
|
||||
public UserService(IDbContextFactory<LiebContext> contextFactory, DiscordService discordService)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
_discordService = discordService;
|
||||
}
|
||||
|
||||
public List<LiebUser> GetLiebUsers()
|
||||
|
@ -56,6 +58,16 @@ namespace Lieb.Data
|
|||
return new LiebUser();
|
||||
}
|
||||
|
||||
public GuildWars2Account GetMainAccount(ulong userId)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
LiebUser user = context.LiebUsers
|
||||
.Include(u => u.GuildWars2Accounts)
|
||||
.ToList()
|
||||
.FirstOrDefault(u => u.Id == userId, new LiebUser());
|
||||
return user.GuildWars2Accounts.FirstOrDefault(g => g.GuildWars2AccountId == user.MainGW2Account, new GuildWars2Account());
|
||||
}
|
||||
|
||||
public async Task CreateUser(ulong discordId, string userName)
|
||||
{
|
||||
using var context = _contextFactory.CreateDbContext();
|
||||
|
@ -94,6 +106,7 @@ namespace Lieb.Data
|
|||
userToChange.Birthday = user.Birthday;
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
await _discordService.RenameUser(user.Id, user.Name, GetMainAccount(user.Id).AccountName);
|
||||
}
|
||||
|
||||
public async Task UpdateBannedUntil(ulong userId, DateTime? date)
|
||||
|
|
|
@ -18,6 +18,7 @@ namespace Lieb.Models
|
|||
|
||||
public DateTime? Birthday { get; set; }
|
||||
public DateTime? BannedUntil { get; set; }
|
||||
public int MainGW2Account { get; set; }
|
||||
public ICollection<GuildWars2Account> GuildWars2Accounts { get; set; } = new List<GuildWars2Account>();
|
||||
public ICollection<RoleAssignment> RoleAssignments { get; set; } = new List<RoleAssignment>();
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
@using Lieb.Models.GuildWars2
|
||||
@using Lieb.Models.GuildWars2.Raid
|
||||
@inject RaidService RaidService
|
||||
@inject UserService UserService
|
||||
|
||||
<table class="table">
|
||||
@{
|
||||
|
@ -145,7 +146,8 @@
|
|||
}
|
||||
else
|
||||
{
|
||||
await RaidService.SignUp(_raid.RaidId, _liebUserId, _usableAccounts.FirstOrDefault().GuildWars2AccountId, role.RaidRoleId, signUpType);
|
||||
int gw2AccountId = UserService.GetMainAccount(_liebUserId).GuildWars2AccountId;
|
||||
await RaidService.SignUp(_raid.RaidId, _liebUserId, gw2AccountId, role.RaidRoleId, signUpType);
|
||||
}
|
||||
_Parent.HasChanged();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue