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
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue