135 lines
4.9 KiB
C#
135 lines
4.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using QuestShare.Server.Models;
|
|
|
|
namespace QuestShare.Server.Managers
|
|
{
|
|
public class ShareManager
|
|
{
|
|
|
|
// party members are only stored in memory, so they will be lost when the server restarts
|
|
private static readonly Dictionary<ulong, List<ulong>> PartyMembers = [];
|
|
public static Share? GetShare(string shareCode)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
return context.Shares.Where(s => s.ShareCode == shareCode).FirstOrDefault();
|
|
}
|
|
public static Share? GetShare(Client host)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
return context.Shares.Where(s => s.ShareHost == host).FirstOrDefault();
|
|
}
|
|
|
|
public static async Task<Share> AddShare(Share share)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
context.Entry(share.ShareHost).State = EntityState.Unchanged;
|
|
var s = context.Shares.Add(share);
|
|
await context.SaveChangesAsync();
|
|
return s.Entity;
|
|
}
|
|
|
|
public static void RemoveShare(string shareCode)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
var share = context.Shares.Where(s => s.ShareCode == shareCode).FirstOrDefault();
|
|
if (share != null)
|
|
{
|
|
context.Shares.Remove(share);
|
|
// remove all share group members
|
|
var shareMembers = context.Clients.Where(c => c.ConnectedShareCode == shareCode);
|
|
foreach (var member in shareMembers)
|
|
{
|
|
member.ConnectedShareCode = "";
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public static async Task UpdateActiveQuest(string shareCode, uint questId, byte questStep)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
var share = await context.Shares.Where(s => s.ShareCode == shareCode).FirstOrDefaultAsync();
|
|
if (share == null)
|
|
{
|
|
// log error
|
|
Console.Error.WriteLine($"Failed to update quests for share {shareCode}");
|
|
return;
|
|
}
|
|
share.SharedQuestStep = questStep;
|
|
share.SharedQuestId = questId;
|
|
var records = await context.SaveChangesAsync();
|
|
Log.Debug($"[UPDATE] Updated {records} quests for share {shareCode}");
|
|
}
|
|
|
|
public static async Task AddGroupMember(Share share, Client client)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
var clientDb = await context.Clients.Where(c => c.CharacterId == client.CharacterId).FirstAsync();
|
|
clientDb.ConnectedShareCode = share.ShareCode;
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
public static async Task RemoveGroupMember(Share share, Client client)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
var clientDb = await context.Clients.Where(c => c.CharacterId == client.CharacterId).FirstOrDefaultAsync();
|
|
if (clientDb != null)
|
|
{
|
|
clientDb.ConnectedShareCode = "";
|
|
await context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public static async Task<List<Client>> GetShareMembers(Share share)
|
|
{
|
|
using var context = new QuestShareContext();
|
|
var members = await context.Clients.Where(c => c.ConnectedShareCode == share.ShareCode).ToListAsync();
|
|
return members;
|
|
}
|
|
|
|
public static void AddBroadcastPartyMember(ulong hostCharacterId, ulong characterId)
|
|
{
|
|
if (!PartyMembers.TryGetValue(hostCharacterId, out var value))
|
|
{
|
|
value = ([]);
|
|
PartyMembers[hostCharacterId] = value;
|
|
}
|
|
|
|
value.Add(characterId);
|
|
}
|
|
|
|
public static void RemoveBroadcastPartyMember(ulong hostCharacterId, ulong characterId)
|
|
{
|
|
if (PartyMembers.TryGetValue(hostCharacterId, out var value))
|
|
{
|
|
value.Remove(characterId);
|
|
}
|
|
}
|
|
|
|
public static void SetBroadcastPartyMembers(ulong hostCharacterId, List<ulong> characterIds)
|
|
{
|
|
PartyMembers[hostCharacterId] = characterIds;
|
|
}
|
|
|
|
public static void DisbandBroadcastParty(ulong hostCharacterId)
|
|
{
|
|
PartyMembers.Remove(hostCharacterId);
|
|
}
|
|
|
|
public static bool HasBroadcastParty(ulong hostCharacterId)
|
|
{
|
|
return PartyMembers.ContainsKey(hostCharacterId);
|
|
}
|
|
|
|
public static List<ulong> GetBroadcastPartyMembers(ulong hostCharacterId)
|
|
{
|
|
return PartyMembers.GetValueOrDefault(hostCharacterId, []);
|
|
}
|
|
|
|
public static string GenerateShareCode()
|
|
{
|
|
return Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
|
|
}
|
|
}
|
|
}
|