feat : 맵관리 코드 추가 작업

This commit is contained in:
qornwh1
2026-03-16 14:50:29 +09:00
parent f564199cb5
commit e4429177db
9 changed files with 282 additions and 21 deletions

View File

@@ -27,6 +27,8 @@ public class GameServer : ServerBase
[(ushort)PacketCode.TRANSFORM_PLAYER] = OnTransformPlayer,
[(ushort)PacketCode.ACTION_PLAYER] = OnActionPlayer,
[(ushort)PacketCode.STATE_PLAYER] = OnStatePlayer,
[(ushort)PacketCode.CHANGE_MAP] = OnChangeMap,
[(ushort)PacketCode.PARTY_CHANGE_MAP] = OnPartyChangeMap,
[(ushort)PacketCode.REQUEST_PARTY] = OnRequestParty,
[(ushort)PacketCode.CHAT] = OnChat,
};
@@ -78,7 +80,8 @@ public class GameServer : ServerBase
{
HashKey = hashKey,
PlayerId = hashKey,
Nickname = hashKey.ToString()
Nickname = hashKey.ToString(),
CurrentMapId = 1
};
cm.AddUser(1, hashKey, newPlayer, peer);
@@ -421,9 +424,11 @@ public class GameServer : ServerBase
{
HashKey = hashKey,
PlayerId = hashKey,
Nickname = hashKey.ToString()
Nickname = hashKey.ToString(),
CurrentMapId = 1
};
// 채널에 추가
cm.AddUser(packet.ChannelId, hashKey, newPlayer, peer);
Log.Debug("[GameServer] INTO_CHANNEL HashKey={Key} ChannelId={ChannelId}", hashKey, packet.ChannelId);
@@ -828,6 +833,84 @@ public class GameServer : ServerBase
}
}
private void OnChangeMap(NetPeer peer, int hashKey, byte[] payload)
{
ChangeMapPacket packet = Serializer.Deserialize<ChangeMapPacket>(new ReadOnlyMemory<byte>(payload));
ChannelManager cm = ChannelManager.Instance;
int channelId = cm.HasUser(hashKey);
if (channelId < 0)
{
return;
}
Channel.Channel channel = cm.GetChannel(channelId);
Player? player = channel.GetPlayer(hashKey);
if (player != null)
{
if (!channel.ChangeMap(hashKey, player, packet.MapId))
{
// 맵이 없으면 무효
Log.Warning("[GameServer] CHANGE_MAP 유효하지 않은 맵 HashKey={Key} MapId={MapId}", hashKey, packet.MapId);
return;
}
// 이동 완료 알림
byte[] data = PacketSerializer.Serialize<ChangeMapPacket>((ushort)PacketCode.CHANGE_MAP, packet);
SendTo(peer, data);
Log.Debug("[GameServer] CHANGE_MAP HashKey={Key} MapId={MapId}", hashKey, packet.MapId);
}
}
private void OnPartyChangeMap(NetPeer peer, int hashKey, byte[] payload)
{
PartyChangeMapPacket packet = Serializer.Deserialize<PartyChangeMapPacket>(new ReadOnlyMemory<byte>(payload));
ChannelManager cm = ChannelManager.Instance;
int channelId = cm.HasUser(hashKey);
if (channelId < 0)
{
return;
}
Channel.Channel channel = cm.GetChannel(channelId);
// 맵 유효성 체크
if (channel.GetMap(packet.MapId) == null)
{
Log.Warning("[GameServer] PARTY_CHANGE_MAP 유효하지 않은 맵 HashKey={Key} MapId={MapId}", hashKey, packet.MapId);
return;
}
// 파티 확인
PartyInfo? party = channel.GetPartyManager().GetParty(packet.PartyId);
if (party == null)
{
Log.Warning("[GameServer] PARTY_CHANGE_MAP 파티 없음 HashKey={Key} PartyId={PartyId}", hashKey, packet.PartyId);
return;
}
// 파티원 전체 맵 이동 + 각자에게 알림
byte[] data = PacketSerializer.Serialize<PartyChangeMapPacket>((ushort)PacketCode.PARTY_CHANGE_MAP, packet);
foreach (int memberId in party.PartyMemberIds)
{
Player? memberPlayer = channel.GetPlayer(memberId);
if (memberPlayer == null)
{
continue;
}
channel.ChangeMap(memberId, memberPlayer, packet.MapId);
if (sessions.TryGetValue(memberId, out NetPeer? memberPeer))
{
SendTo(memberPeer, data);
}
}
Log.Debug("[GameServer] PARTY_CHANGE_MAP HashKey={Key} PartyId={PartyId} MapId={MapId}", hashKey, packet.PartyId, packet.MapId);
}
// 채널 퇴장/연결 해제 시 파티 자동 탈퇴 처리
private void HandlePartyLeaveOnExit(int channelId, int hashKey)
{