diff --git a/Common/Database/ClientData.cs b/Common/Database/ClientData.cs new file mode 100644 index 0000000..67bc5f3 --- /dev/null +++ b/Common/Database/ClientData.cs @@ -0,0 +1,31 @@ +using Common.Resources.Proto; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace Common.Database +{ + public class ClientData + { + public static readonly IMongoCollection collection = Global.db.GetCollection("ClientDatas"); + + public static ClientDataScheme? GetClientData(uint uid, ClientDataType type, uint id) + { + return collection.AsQueryable().Where(c => c.OwnerUid == uid && c.Type == type && c.ClientDataId == id).FirstOrDefault(); + } + + public static void SetClientData(uint uid, ClientDataType type, uint id, byte[] data) + { + collection.UpdateOne(Builders.Filter.Where(c => c.OwnerUid == uid && c.Type == type && c.ClientDataId == id), Builders.Update.Set("Type", type).Set("ClientDataId", id).Set("Data", data).SetOnInsert("OwnerUid", uid), new UpdateOptions() { IsUpsert = true }); + } + } + +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + public class ClientDataScheme + { + public ObjectId Id { get; set; } + public uint OwnerUid { get; set; } + public ClientDataType Type { get; set; } + public uint ClientDataId { get; set; } + public byte[] Data { get; set; } + } +} diff --git a/GameServer/Handlers/GetClientDataReqHandler.cs b/GameServer/Handlers/GetClientDataReqHandler.cs index e96483f..9b86360 100644 --- a/GameServer/Handlers/GetClientDataReqHandler.cs +++ b/GameServer/Handlers/GetClientDataReqHandler.cs @@ -1,4 +1,5 @@ -using Common.Resources.Proto; +using Common.Database; +using Common.Resources.Proto; namespace PemukulPaku.GameServer.Handlers { @@ -8,8 +9,24 @@ namespace PemukulPaku.GameServer.Handlers public void Handle(Session session, Packet packet) { GetClientDataReq Data = packet.GetDecodedBody(); + ClientDataScheme? clientData = Common.Database.ClientData.GetClientData(session.Player.User.Uid, Data.Type, Data.Id); + GetClientDataRsp Rsp = new() { retcode = GetClientDataRsp.Retcode.Succ, Id = Data.Id, Type = Data.Type }; - session.Send(Packet.FromProto(new GetClientDataRsp() { retcode = GetClientDataRsp.Retcode.NotFound, Type = Data.Type, Id = Data.Id }, CmdId.GetClientDataRsp)); + if (clientData is not null) + { + Rsp.ClientDataLists.Add(new() + { + Id = clientData.ClientDataId, + Type = clientData.Type, + Data = clientData.Data + }); + } + else + { + Rsp.retcode = GetClientDataRsp.Retcode.NotFound; + } + + session.Send(Packet.FromProto(Rsp, CmdId.GetClientDataRsp)); } } } diff --git a/GameServer/Handlers/SetClientDataReqHandler.cs b/GameServer/Handlers/SetClientDataReqHandler.cs new file mode 100644 index 0000000..f18c469 --- /dev/null +++ b/GameServer/Handlers/SetClientDataReqHandler.cs @@ -0,0 +1,17 @@ +using Common.Database; +using Common.Resources.Proto; + +namespace PemukulPaku.GameServer.Handlers +{ + [PacketCmdId(CmdId.SetClientDataReq)] + internal class SetClientDataReqHandler : IPacketHandler + { + public void Handle(Session session, Packet packet) + { + SetClientDataReq Data = packet.GetDecodedBody(); + Common.Database.ClientData.SetClientData(session.Player.User.Uid, Data.ClientData.Type, Data.ClientData.Id, Data.ClientData.Data); + + session.Send(Packet.FromProto(new SetClientDataRsp() { retcode = SetClientDataRsp.Retcode.Succ, Id = Data.ClientData.Id, Type = Data.ClientData.Type }, CmdId.SetClientDataRsp)); + } + } +}