Files
caliverse_server/GameServer/Contents/Room/Action/RoomAction.cs
2025-05-01 07:20:41 +09:00

124 lines
3.7 KiB
C#

using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using ServerCore;
using ServerBase;
using ServerCommon;
namespace GameServer;
public class RoomAction : EntityActionBase
{
private readonly ConcurrentDictionary<int, Room> m_rooms = new();
public RoomAction(Player owner) : base(owner)
{}
public override async Task<Result> onInit()
{
await Task.CompletedTask;
var result = new Result();
return result;
}
public override void onClear()
{
return;
}
private Room? getRoom(int room_id) => m_rooms.GetValueOrDefault(room_id);
public bool IsExist(int roomId)
{
return m_rooms.ContainsKey(roomId);
}
public async Task<Result> setRoomFromDoc(RoomDoc docBase)
{
var result = new Result();
string err_msg;
var owner = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var doc_attr = docBase.getAttrib<RoomAttrib>();
if (null == doc_attr)
{
err_msg = $"Fail to get doc attrib : {nameof(RoomAttrib)} is null";
result.setFail(ServerErrorCode.EntityAttributeNotFound, err_msg);
Log.getLogger().error(err_msg);
return result;
}
// db 에서 설정하는 것으로, 없으면 생성해야 한다.
var room = getRoom(doc_attr.RoomId);
if (null == room)
{
room = new Room(owner);
m_rooms.TryAdd(doc_attr.RoomId, room);
}
var room_attribute = room.getEntityAttribute<RoomAttribute>();
if (null == room_attribute)
{
err_msg = $"Fail to get entity attribute : {nameof(RoomAttribute)} is null";
result.setFail(ServerErrorCode.EntityAttributeIsNull, err_msg);
Log.getLogger().error(err_msg);
return result;
}
// doc 에서 직접 copy 된 경우 pk, sk 를 셋팅해야 한다.
docBase.setCombinationKeyForPK(owner.getUserGuid());
docBase.setCombinationKeyForSK(doc_attr.RoomId.ToString());
var is_apply = docBase.onApplyPKSK();
if (ServerErrorCode.Success != is_apply)
{
err_msg = $"Fail to copy to attribute from doc : invalid argument {nameof(setRoomFromDoc)}";
result.setFail(is_apply, err_msg);
Log.getLogger().error(err_msg);
return result;
}
var is_success = room_attribute.copyEntityAttributeFromDoc(docBase);
if (false == is_success)
{
err_msg = $"Fail to copy room attribute : to {nameof(RoomAttribute)} from {nameof(RoomDoc)}";
result.setFail(ServerErrorCode.DynamoDbDocCopyToEntityAttributeFailed, err_msg);
Log.getLogger().error(err_msg);
}
return await Task.FromResult(result);
}
public async Task<Result> notifyRoomsToClient()
{
var owner = getOwner() as Player;
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var notify_packet = new ClientToGame
{
Message = new ClientToGameMessage
{
OwnedRoomNoti = new ClientToGameMessage.Types.OwnedRoomNoti()
}
};
foreach (var room in m_rooms)
{
var room_content_action = room.Value.getEntityAction<RoomContentAction>();
var data = room_content_action?.toRoomData4Client();
if (null == data) continue;
notify_packet.Message.OwnedRoomNoti.RoomList.Add(data);
}
GameServerApp.getServerLogic().onSendPacket(owner, notify_packet);
return await Task.FromResult(new Result());
}
}