fea : Map단위로 캐릭터 이동 메시지 전달기능 추가

This commit is contained in:
qornwh1
2026-03-16 15:16:42 +09:00
parent e4429177db
commit 523247c9b1
2 changed files with 206 additions and 92 deletions

View File

@@ -2,6 +2,7 @@ using LiteNetLib;
using LiteNetLib.Utils; using LiteNetLib.Utils;
using MMOserver.Api; using MMOserver.Api;
using MMOserver.Game.Channel; using MMOserver.Game.Channel;
using MMOserver.Game.Channel.Maps;
using MMOserver.Game.Party; using MMOserver.Game.Party;
using MMOserver.Packet; using MMOserver.Packet;
using MMOserver.Utils; using MMOserver.Utils;
@@ -15,7 +16,7 @@ namespace MMOserver.Game;
public class GameServer : ServerBase public class GameServer : ServerBase
{ {
private readonly Dictionary<ushort, Action<NetPeer, int, byte[]>> packetHandlers; private readonly Dictionary<ushort, Action<NetPeer, int, byte[]>> packetHandlers;
private UuidGenerator userUuidGenerator; private readonly UuidGenerator userUuidGenerator;
public GameServer(int port, string connectionString) : base(port, connectionString) public GameServer(int port, string connectionString) : base(port, connectionString)
{ {
@@ -30,7 +31,7 @@ public class GameServer : ServerBase
[(ushort)PacketCode.CHANGE_MAP] = OnChangeMap, [(ushort)PacketCode.CHANGE_MAP] = OnChangeMap,
[(ushort)PacketCode.PARTY_CHANGE_MAP] = OnPartyChangeMap, [(ushort)PacketCode.PARTY_CHANGE_MAP] = OnPartyChangeMap,
[(ushort)PacketCode.REQUEST_PARTY] = OnRequestParty, [(ushort)PacketCode.REQUEST_PARTY] = OnRequestParty,
[(ushort)PacketCode.CHAT] = OnChat, [(ushort)PacketCode.CHAT] = OnChat
}; };
userUuidGenerator = new UuidGenerator(); userUuidGenerator = new UuidGenerator();
} }
@@ -44,7 +45,7 @@ public class GameServer : ServerBase
} }
// 세션에 넣지는 않는다. // 세션에 넣지는 않는다.
NetDataReader reader = new NetDataReader(payload); NetDataReader reader = new(payload);
short code = reader.GetShort(); short code = reader.GetShort();
short bodyLength = reader.GetShort(); short bodyLength = reader.GetShort();
@@ -76,7 +77,7 @@ public class GameServer : ServerBase
{ {
// 더미 클라다. // 더미 클라다.
ChannelManager cm = ChannelManager.Instance; ChannelManager cm = ChannelManager.Instance;
Player newPlayer = new Player Player newPlayer = new()
{ {
HashKey = hashKey, HashKey = hashKey,
PlayerId = hashKey, PlayerId = hashKey,
@@ -211,7 +212,7 @@ public class GameServer : ServerBase
private void SendLoadChannelPacket(NetPeer peer, int hashKey) private void SendLoadChannelPacket(NetPeer peer, int hashKey)
{ {
LoadChannelPacket loadChannelPacket = new LoadChannelPacket(); LoadChannelPacket loadChannelPacket = new();
foreach (Channel.Channel channel in ChannelManager.Instance.GetChannels().Values) foreach (Channel.Channel channel in ChannelManager.Instance.GetChannels().Values)
{ {
if (channel.ChannelId <= 0) if (channel.ChannelId <= 0)
@@ -224,26 +225,26 @@ public class GameServer : ServerBase
continue; continue;
} }
ChannelInfo info = new ChannelInfo(); ChannelInfo info = new();
info.ChannelId = channel.ChannelId; info.ChannelId = channel.ChannelId;
info.ChannelUserCount = channel.UserCount; info.ChannelUserCount = channel.UserCount;
info.ChannelUserMax = channel.UserCountMax; info.ChannelUserMax = channel.UserCountMax;
loadChannelPacket.Channels.Add(info); loadChannelPacket.Channels.Add(info);
} }
byte[] data = PacketSerializer.Serialize<LoadChannelPacket>((ushort)PacketCode.LOAD_CHANNEL, loadChannelPacket); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.LOAD_CHANNEL, loadChannelPacket);
SendTo(peer, data); SendTo(peer, data);
} }
// 나간 유저를 같은 채널 유저들에게 알림 (UPDATE_CHANNEL_USER IsAdd=false) // 나간 유저를 같은 채널 유저들에게 알림 (UPDATE_CHANNEL_USER IsAdd=false)
private void SendExitChannelPacket(NetPeer peer, int hashKey, int channelId, Player player) private void SendExitChannelPacket(NetPeer peer, int hashKey, int channelId, Player player)
{ {
UpdateChannelUserPacket packet = new UpdateChannelUserPacket UpdateChannelUserPacket packet = new()
{ {
Players = ToPlayerInfo(player), Players = ToPlayerInfo(player),
IsAdd = false IsAdd = false
}; };
byte[] data = PacketSerializer.Serialize<UpdateChannelUserPacket>((ushort)PacketCode.UPDATE_CHANNEL_USER, packet); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_CHANNEL_USER, packet);
// 이미 채널에서 제거된 후라 나간 본인에게는 전송되지 않음 // 이미 채널에서 제거된 후라 나간 본인에게는 전송되지 않음
BroadcastToChannel(channelId, data); BroadcastToChannel(channelId, data);
} }
@@ -264,7 +265,7 @@ public class GameServer : ServerBase
Player? myPlayer = channel.GetPlayer(hashKey); Player? myPlayer = channel.GetPlayer(hashKey);
// 1. 새 유저에게: 자신을 제외한 기존 채널 유저 목록 + 파티 목록 전송 // 1. 새 유저에게: 자신을 제외한 기존 채널 유저 목록 + 파티 목록 전송
IntoChannelPacket response = new IntoChannelPacket { ChannelId = channelId }; IntoChannelPacket response = new() { ChannelId = channelId };
foreach (int userId in channel.GetConnectUsers()) foreach (int userId in channel.GetConnectUsers())
{ {
if (userId == hashKey) if (userId == hashKey)
@@ -290,18 +291,18 @@ public class GameServer : ServerBase
}); });
} }
byte[] toNewUser = PacketSerializer.Serialize<IntoChannelPacket>((ushort)PacketCode.INTO_CHANNEL, response); byte[] toNewUser = PacketSerializer.Serialize((ushort)PacketCode.INTO_CHANNEL, response);
SendTo(peer, toNewUser); SendTo(peer, toNewUser);
// 2. 기존 유저들에게: 새 유저 입장 알림 // 2. 기존 유저들에게: 새 유저 입장 알림
if (myPlayer != null) if (myPlayer != null)
{ {
UpdateChannelUserPacket notify = new UpdateChannelUserPacket UpdateChannelUserPacket notify = new()
{ {
Players = ToPlayerInfo(myPlayer), Players = ToPlayerInfo(myPlayer),
IsAdd = true IsAdd = true
}; };
byte[] toOthers = PacketSerializer.Serialize<UpdateChannelUserPacket>((ushort)PacketCode.UPDATE_CHANNEL_USER, notify); byte[] toOthers = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_CHANNEL_USER, notify);
BroadcastToChannel(channelId, toOthers, peer); BroadcastToChannel(channelId, toOthers, peer);
} }
} }
@@ -318,19 +319,19 @@ public class GameServer : ServerBase
{ {
Log.Warning("[GameServer] LOAD_GAME 플레이어 없음 HashKey={Key}", hashKey); Log.Warning("[GameServer] LOAD_GAME 플레이어 없음 HashKey={Key}", hashKey);
byte[] denied = byte[] denied =
PacketSerializer.Serialize<LoadGamePacket>((ushort)PacketCode.LOAD_GAME, new LoadGamePacket { IsAccepted = false }); PacketSerializer.Serialize((ushort)PacketCode.LOAD_GAME, new LoadGamePacket { IsAccepted = false });
SendTo(peer, denied); SendTo(peer, denied);
return; return;
} }
LoadGamePacket packet = new LoadGamePacket LoadGamePacket packet = new()
{ {
IsAccepted = true, IsAccepted = true,
Player = ToPlayerInfo(player), Player = ToPlayerInfo(player),
MaplId = channelId, MaplId = channelId
}; };
byte[] data = PacketSerializer.Serialize<LoadGamePacket>((ushort)PacketCode.LOAD_GAME, packet); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.LOAD_GAME, packet);
SendTo(peer, data); SendTo(peer, data);
Log.Debug("[GameServer] LOAD_GAME HashKey={Key} PlayerId={PlayerId} ChannelId={ChannelId}", hashKey, player.PlayerId, channelId); Log.Debug("[GameServer] LOAD_GAME HashKey={Key} PlayerId={PlayerId} ChannelId={ChannelId}", hashKey, player.PlayerId, channelId);
} }
@@ -356,6 +357,32 @@ public class GameServer : ServerBase
} }
} }
// 특정 맵의 유저들에게 전송 (exclude 지정 시 해당 피어 제외)
private void BroadcastToMap(int channelId, int mapId, byte[] data, NetPeer? exclude = null,
DeliveryMethod method = DeliveryMethod.ReliableOrdered)
{
AMap? map = ChannelManager.Instance.GetChannel(channelId).GetMap(mapId);
if (map == null)
{
return;
}
foreach (int userId in map.GetUsers().Keys)
{
if (!sessions.TryGetValue(userId, out NetPeer? targetPeer))
{
continue;
}
if (exclude != null && targetPeer.Id == exclude.Id)
{
continue;
}
SendTo(targetPeer, data, method);
}
}
// ============================================================ // ============================================================
// Player ↔ PlayerInfo 변환 (패킷 전송 시에만 사용) // Player ↔ PlayerInfo 변환 (패킷 전송 시에만 사용)
// ============================================================ // ============================================================
@@ -372,7 +399,7 @@ public class GameServer : ServerBase
Mp = player.Mp, Mp = player.Mp,
MaxMp = player.MaxMp, MaxMp = player.MaxMp,
Position = new Position { X = player.PosX, Y = player.PosY, Z = player.PosZ }, Position = new Position { X = player.PosX, Y = player.PosY, Z = player.PosZ },
RotY = player.RotY, RotY = player.RotY
}; };
} }
@@ -413,14 +440,14 @@ public class GameServer : ServerBase
{ {
Log.Warning("[GameServer] INTO_CHANNEL 채널 인원 초과 HashKey={Key} ChannelId={ChannelId} UserCount={Count}/{Max}", Log.Warning("[GameServer] INTO_CHANNEL 채널 인원 초과 HashKey={Key} ChannelId={ChannelId} UserCount={Count}/{Max}",
hashKey, packet.ChannelId, newChannel.UserCount, newChannel.UserCountMax); hashKey, packet.ChannelId, newChannel.UserCount, newChannel.UserCountMax);
byte[] full = PacketSerializer.Serialize<IntoChannelPacket>((ushort)PacketCode.INTO_CHANNEL, byte[] full = PacketSerializer.Serialize((ushort)PacketCode.INTO_CHANNEL,
new IntoChannelPacket { ChannelId = -1 }); new IntoChannelPacket { ChannelId = -1 });
SendTo(peer, full); SendTo(peer, full);
return; return;
} }
// TODO: 실제 서비스에서는 DB/세션에서 플레이어 정보 로드 필요 // TODO: 실제 서비스에서는 DB/세션에서 플레이어 정보 로드 필요
Player newPlayer = new Player Player newPlayer = new()
{ {
HashKey = hashKey, HashKey = hashKey,
PlayerId = hashKey, PlayerId = hashKey,
@@ -458,7 +485,7 @@ public class GameServer : ServerBase
} }
// 새로운 파티를 복사한다 // 새로운 파티를 복사한다
PartyInfo tempParty = new PartyInfo(); PartyInfo tempParty = new();
tempParty.DeepCopySemi(preParty); tempParty.DeepCopySemi(preParty);
// 최대 인원 체크 // 최대 인원 체크
@@ -466,7 +493,7 @@ public class GameServer : ServerBase
{ {
Log.Warning("[GameServer] INTO_CHANNEL_PARTY 채널 인원 초과 HashKey={Key} ChannelId={ChannelId} UserCount={Count}/{Max}", Log.Warning("[GameServer] INTO_CHANNEL_PARTY 채널 인원 초과 HashKey={Key} ChannelId={ChannelId} UserCount={Count}/{Max}",
hashKey, packet.ChannelId, newChannel.UserCount, newChannel.UserCountMax); hashKey, packet.ChannelId, newChannel.UserCount, newChannel.UserCountMax);
byte[] full = PacketSerializer.Serialize<IntoChannelPacket>((ushort)PacketCode.INTO_CHANNEL, byte[] full = PacketSerializer.Serialize((ushort)PacketCode.INTO_CHANNEL,
new IntoChannelPacket { ChannelId = -1 }); new IntoChannelPacket { ChannelId = -1 });
SendTo(peer, full); SendTo(peer, full);
return; return;
@@ -478,12 +505,12 @@ public class GameServer : ServerBase
Player? player = preChannel.GetPlayer(memberId); Player? player = preChannel.GetPlayer(memberId);
if (player != null) if (player != null)
{ {
UpdateChannelUserPacket exitNotify = new UpdateChannelUserPacket UpdateChannelUserPacket exitNotify = new()
{ {
Players = ToPlayerInfo(player), Players = ToPlayerInfo(player),
IsAdd = false IsAdd = false
}; };
byte[] exitData = PacketSerializer.Serialize<UpdateChannelUserPacket>((ushort)PacketCode.UPDATE_CHANNEL_USER, exitNotify); byte[] exitData = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_CHANNEL_USER, exitNotify);
BroadcastToChannel(preChannelId, exitData); BroadcastToChannel(preChannelId, exitData);
// 이전 채널에서 제거 // 이전 채널에서 제거
@@ -496,15 +523,15 @@ public class GameServer : ServerBase
// 이전채널에서 파티를 지운다. // 이전채널에서 파티를 지운다.
preChannel.GetPartyManager().DeleteParty(hashKey, packet.PartyId, out preParty); preChannel.GetPartyManager().DeleteParty(hashKey, packet.PartyId, out preParty);
UpdatePartyPacket notify = new UpdatePartyPacket UpdatePartyPacket notify = new()
{ {
PartyId = preParty!.PartyId, PartyId = preParty!.PartyId,
Type = PartyUpdateType.DELETE, Type = PartyUpdateType.DELETE,
LeaderId = preParty.LeaderId, LeaderId = preParty.LeaderId
}; };
// 채널 전체 파티 목록 갱신 // 채널 전체 파티 목록 갱신
BroadcastToChannel(preChannelId, PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify)); BroadcastToChannel(preChannelId, PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify));
// 새로운 채널에 파티원 넣기 // 새로운 채널에 파티원 넣기
foreach (int memberId in tempParty.PartyMemberIds) foreach (int memberId in tempParty.PartyMemberIds)
@@ -514,7 +541,7 @@ public class GameServer : ServerBase
if (memberPeer != null) if (memberPeer != null)
{ {
// 새 채널에 유저 추가 // 새 채널에 유저 추가
Player newPlayer = new Player Player newPlayer = new()
{ {
HashKey = memberId, HashKey = memberId,
PlayerId = memberId, PlayerId = memberId,
@@ -536,15 +563,15 @@ public class GameServer : ServerBase
createdParty != null) createdParty != null)
{ {
// 새 채널 기존 유저들에게 파티 생성 알림 // 새 채널 기존 유저들에게 파티 생성 알림
UpdatePartyPacket createNotify = new UpdatePartyPacket UpdatePartyPacket createNotify = new()
{ {
PartyId = createdParty.PartyId, PartyId = createdParty.PartyId,
Type = PartyUpdateType.CREATE, Type = PartyUpdateType.CREATE,
LeaderId = createdParty.LeaderId, LeaderId = createdParty.LeaderId,
PartyName = createdParty.PartyName, PartyName = createdParty.PartyName
}; };
BroadcastToChannel(packet.ChannelId, BroadcastToChannel(packet.ChannelId,
PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, createNotify)); PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, createNotify));
} }
else else
{ {
@@ -591,17 +618,19 @@ public class GameServer : ServerBase
// 채널 내 플레이어 위치/방향 상태 갱신 // 채널 내 플레이어 위치/방향 상태 갱신
Player? player = cm.GetChannel(channelId).GetPlayer(hashKey); Player? player = cm.GetChannel(channelId).GetPlayer(hashKey);
if (player != null) if (player == null)
{ {
return;
}
player.PosX = packet.Position.X; player.PosX = packet.Position.X;
player.PosY = packet.Position.Y; player.PosY = packet.Position.Y;
player.PosZ = packet.Position.Z; player.PosZ = packet.Position.Z;
player.RotY = packet.RotY; player.RotY = packet.RotY;
}
// 같은 채널 유저들에게 위치/방향 브로드캐스트 (나 제외) // 같은 유저들에게 위치/방향 브로드캐스트 (나 제외)
byte[] data = PacketSerializer.Serialize<TransformPlayerPacket>((ushort)PacketCode.TRANSFORM_PLAYER, packet); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.TRANSFORM_PLAYER, packet);
BroadcastToChannel(channelId, data, peer, DeliveryMethod.Unreliable); BroadcastToMap(channelId, player.CurrentMapId, data, peer, DeliveryMethod.Unreliable);
} }
private void OnActionPlayer(NetPeer peer, int hashKey, byte[] payload) private void OnActionPlayer(NetPeer peer, int hashKey, byte[] payload)
@@ -615,9 +644,15 @@ public class GameServer : ServerBase
return; return;
} }
// 같은 채널 유저들에게 행동 브로드캐스트 (나 제외) Player? player = cm.GetChannel(channelId).GetPlayer(hashKey);
byte[] data = PacketSerializer.Serialize<ActionPlayerPacket>((ushort)PacketCode.ACTION_PLAYER, packet); if (player == null)
BroadcastToChannel(channelId, data, peer); {
return;
}
// 같은 맵 유저들에게 행동 브로드캐스트 (나 제외)
byte[] data = PacketSerializer.Serialize((ushort)PacketCode.ACTION_PLAYER, packet);
BroadcastToMap(channelId, player.CurrentMapId, data, peer);
} }
private void OnStatePlayer(NetPeer peer, int hashKey, byte[] payload) private void OnStatePlayer(NetPeer peer, int hashKey, byte[] payload)
@@ -633,17 +668,19 @@ public class GameServer : ServerBase
// 채널 내 플레이어 HP/MP 상태 갱신 // 채널 내 플레이어 HP/MP 상태 갱신
Player? player = cm.GetChannel(channelId).GetPlayer(hashKey); Player? player = cm.GetChannel(channelId).GetPlayer(hashKey);
if (player != null) if (player == null)
{ {
return;
}
player.Hp = packet.Hp; player.Hp = packet.Hp;
player.MaxHp = packet.MaxHp; player.MaxHp = packet.MaxHp;
player.Mp = packet.Mp; player.Mp = packet.Mp;
player.MaxMp = packet.MaxMp; player.MaxMp = packet.MaxMp;
}
// 같은 채널 유저들에게 스테이트 브로드캐스트 (나 제외) // 같은 유저들에게 스테이트 브로드캐스트 (나 제외)
byte[] data = PacketSerializer.Serialize<StatePlayerPacket>((ushort)PacketCode.STATE_PLAYER, packet); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.STATE_PLAYER, packet);
BroadcastToChannel(channelId, data, peer); BroadcastToMap(channelId, player.CurrentMapId, data, peer);
} }
private void OnRequestParty(NetPeer peer, int hashKey, byte[] payload) private void OnRequestParty(NetPeer peer, int hashKey, byte[] payload)
@@ -669,15 +706,15 @@ public class GameServer : ServerBase
return; return;
} }
UpdatePartyPacket notify = new UpdatePartyPacket UpdatePartyPacket notify = new()
{ {
PartyId = party!.PartyId, PartyId = party!.PartyId,
Type = PartyUpdateType.CREATE, Type = PartyUpdateType.CREATE,
LeaderId = party.LeaderId, LeaderId = party.LeaderId,
PlayerId = hashKey, PlayerId = hashKey,
PartyName = party.PartyName, PartyName = party.PartyName
}; };
byte[] data = PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify);
BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신 BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신
break; break;
} }
@@ -689,14 +726,14 @@ public class GameServer : ServerBase
return; return;
} }
UpdatePartyPacket notify = new UpdatePartyPacket UpdatePartyPacket notify = new()
{ {
PartyId = party!.PartyId, PartyId = party!.PartyId,
Type = PartyUpdateType.JOIN, Type = PartyUpdateType.JOIN,
LeaderId = party.LeaderId, LeaderId = party.LeaderId,
PlayerId = hashKey, PlayerId = hashKey
}; };
byte[] data = PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify);
BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신 BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신
break; break;
} }
@@ -708,9 +745,9 @@ public class GameServer : ServerBase
return; return;
} }
UpdatePartyPacket notify = new UpdatePartyPacket UpdatePartyPacket notify = new()
{ {
PartyId = party.PartyId, Type = PartyUpdateType.DELETE, LeaderId = party.LeaderId, PlayerId = hashKey, PartyId = party.PartyId, Type = PartyUpdateType.DELETE, LeaderId = party.LeaderId, PlayerId = hashKey
}; };
// 파티가 남아있으면 살린다. // 파티가 남아있으면 살린다.
@@ -719,7 +756,7 @@ public class GameServer : ServerBase
notify.Type = PartyUpdateType.LEAVE; notify.Type = PartyUpdateType.LEAVE;
} }
byte[] data = PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify);
BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신 (탈퇴자 포함) BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신 (탈퇴자 포함)
break; break;
} }
@@ -731,13 +768,13 @@ public class GameServer : ServerBase
return; return;
} }
UpdatePartyPacket notify = new UpdatePartyPacket UpdatePartyPacket notify = new()
{ {
PartyId = party!.PartyId, PartyId = party!.PartyId,
Type = PartyUpdateType.DELETE, Type = PartyUpdateType.DELETE,
LeaderId = party.LeaderId, LeaderId = party.LeaderId
}; };
byte[] data = PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify);
BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신 BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신
break; break;
} }
@@ -749,15 +786,15 @@ public class GameServer : ServerBase
return; return;
} }
UpdatePartyPacket notify = new UpdatePartyPacket UpdatePartyPacket notify = new()
{ {
PartyId = req.PartyId, PartyId = req.PartyId,
Type = PartyUpdateType.UPDATE, Type = PartyUpdateType.UPDATE,
LeaderId = party?.LeaderId ?? 0, LeaderId = party?.LeaderId ?? 0,
PlayerId = hashKey, PlayerId = hashKey,
PartyName = req.PartyName, PartyName = req.PartyName
}; };
byte[] data = PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify);
BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신 BroadcastToChannel(channelId, data); // 채널 전체 파티 목록 갱신
break; break;
} }
@@ -782,16 +819,16 @@ public class GameServer : ServerBase
} }
// 서버에서 발신자 정보 채워줌 (클라 위조 방지) // 서버에서 발신자 정보 채워줌 (클라 위조 방지)
ChatPacket res = new ChatPacket ChatPacket res = new()
{ {
Type = req.Type, Type = req.Type,
SenderId = sender.PlayerId, SenderId = sender.PlayerId,
SenderNickname = sender.Nickname, SenderNickname = sender.Nickname,
TargetId = req.TargetId, TargetId = req.TargetId,
Message = req.Message, Message = req.Message
}; };
byte[] data = PacketSerializer.Serialize<ChatPacket>((ushort)PacketCode.CHAT, res); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.CHAT, res);
switch (req.Type) switch (req.Type)
{ {
@@ -846,20 +883,47 @@ public class GameServer : ServerBase
Channel.Channel channel = cm.GetChannel(channelId); Channel.Channel channel = cm.GetChannel(channelId);
Player? player = channel.GetPlayer(hashKey); Player? player = channel.GetPlayer(hashKey);
if (player != null) if (player == null)
{ {
return;
}
int oldMapId = player.CurrentMapId;
if (!channel.ChangeMap(hashKey, player, packet.MapId)) if (!channel.ChangeMap(hashKey, player, packet.MapId))
{ {
// 맵이 없으면 무효
Log.Warning("[GameServer] CHANGE_MAP 유효하지 않은 맵 HashKey={Key} MapId={MapId}", hashKey, packet.MapId); Log.Warning("[GameServer] CHANGE_MAP 유효하지 않은 맵 HashKey={Key} MapId={MapId}", hashKey, packet.MapId);
return; return;
} }
// 이동 완료 알림 PlayerInfo playerInfo = ToPlayerInfo(player);
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); ChangeMapPacket exitNotify = new() { MapId = oldMapId, IsAdd = false, Player = playerInfo };
BroadcastToMap(channelId, oldMapId, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, exitNotify));
// 새 맵 유저들에게 입장 알림 (본인 제외)
ChangeMapPacket enterNotify = new() { MapId = packet.MapId, IsAdd = true, Player = playerInfo };
BroadcastToMap(channelId, packet.MapId, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, enterNotify), peer);
// 본인에게 새 맵 플레이어 목록 전달
ChangeMapPacket response = new() { MapId = packet.MapId };
AMap? newMap = channel.GetMap(packet.MapId);
if (newMap != null)
{
foreach ((int uid, Player p) in newMap.GetUsers())
{
if (uid == hashKey)
{
continue;
} }
response.Players.Add(ToPlayerInfo(p));
}
}
SendTo(peer, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, response));
Log.Debug("[GameServer] CHANGE_MAP HashKey={Key} OldMap={OldMapId} NewMap={MapId}", hashKey, oldMapId, packet.MapId);
} }
private void OnPartyChangeMap(NetPeer peer, int hashKey, byte[] payload) private void OnPartyChangeMap(NetPeer peer, int hashKey, byte[] payload)
@@ -891,7 +955,6 @@ public class GameServer : ServerBase
} }
// 파티원 전체 맵 이동 + 각자에게 알림 // 파티원 전체 맵 이동 + 각자에게 알림
byte[] data = PacketSerializer.Serialize<PartyChangeMapPacket>((ushort)PacketCode.PARTY_CHANGE_MAP, packet);
foreach (int memberId in party.PartyMemberIds) foreach (int memberId in party.PartyMemberIds)
{ {
Player? memberPlayer = channel.GetPlayer(memberId); Player? memberPlayer = channel.GetPlayer(memberId);
@@ -900,12 +963,41 @@ public class GameServer : ServerBase
continue; continue;
} }
int oldMapId = memberPlayer.CurrentMapId;
channel.ChangeMap(memberId, memberPlayer, packet.MapId); channel.ChangeMap(memberId, memberPlayer, packet.MapId);
if (sessions.TryGetValue(memberId, out NetPeer? memberPeer)) if (!sessions.TryGetValue(memberId, out NetPeer? memberPeer))
{ {
SendTo(memberPeer, data); continue;
} }
PlayerInfo memberInfo = ToPlayerInfo(memberPlayer);
// 기존 맵 유저들에게 퇴장 알림
ChangeMapPacket exitNotify = new() { MapId = oldMapId, IsAdd = false, Player = memberInfo };
BroadcastToMap(channelId, oldMapId, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, exitNotify));
// 새 맵 유저들에게 입장 알림 (본인 제외)
ChangeMapPacket enterNotify = new() { MapId = packet.MapId, IsAdd = true, Player = memberInfo };
BroadcastToMap(channelId, packet.MapId, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, enterNotify), memberPeer);
// 본인에게 새 맵 플레이어 목록 전달
ChangeMapPacket response = new() { MapId = packet.MapId };
AMap? newMap = channel.GetMap(packet.MapId);
if (newMap != null)
{
foreach ((int uid, Player p) in newMap.GetUsers())
{
if (uid == memberId)
{
continue;
}
response.Players.Add(ToPlayerInfo(p));
}
}
SendTo(memberPeer, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, response));
} }
Log.Debug("[GameServer] PARTY_CHANGE_MAP HashKey={Key} PartyId={PartyId} MapId={MapId}", hashKey, packet.PartyId, packet.MapId); Log.Debug("[GameServer] PARTY_CHANGE_MAP HashKey={Key} PartyId={PartyId} MapId={MapId}", hashKey, packet.PartyId, packet.MapId);
@@ -931,24 +1023,24 @@ public class GameServer : ServerBase
PartyId = partyId.Value, PartyId = partyId.Value,
Type = PartyUpdateType.LEAVE, Type = PartyUpdateType.LEAVE,
LeaderId = remaining.LeaderId, LeaderId = remaining.LeaderId,
PlayerId = hashKey, PlayerId = hashKey
} }
: new UpdatePartyPacket : new UpdatePartyPacket
{ {
PartyId = partyId.Value, PartyId = partyId.Value,
Type = PartyUpdateType.DELETE, Type = PartyUpdateType.DELETE,
LeaderId = -1, LeaderId = -1,
PlayerId = hashKey, PlayerId = hashKey
}; };
byte[] data = PacketSerializer.Serialize<UpdatePartyPacket>((ushort)PacketCode.UPDATE_PARTY, notify); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.UPDATE_PARTY, notify);
BroadcastToChannel(channelId, data); BroadcastToChannel(channelId, data);
} }
private void SendError(NetPeer peer, ErrorCode code) private void SendError(NetPeer peer, ErrorCode code)
{ {
ErrorPacket err = new ErrorPacket { Code = code }; ErrorPacket err = new() { Code = code };
byte[] data = PacketSerializer.Serialize<ErrorPacket>((ushort)PacketCode.ERROR, err); byte[] data = PacketSerializer.Serialize((ushort)PacketCode.ERROR, err);
SendTo(peer, data); SendTo(peer, data);
} }

View File

@@ -243,9 +243,7 @@ public class PartyInfoData
} }
} }
// INTO_CHANNEL // INTO_CHANNEL 클라->서버: 입장할 채널 ID / 서버->클라: 채널 내 나 이외 플레이어 목록
// 클라->서버: 입장할 채널 ID
// 서버->클라: 채널 내 나 이외 플레이어 목록
[ProtoContract] [ProtoContract]
public class IntoChannelPacket public class IntoChannelPacket
{ {
@@ -723,9 +721,33 @@ public class ChangeMapPacket
get; get;
set; set;
} }
// 새 맵의 기존 플레이어 목록 (이동한 본인에게 전달)
[ProtoMember(2)]
public List<PlayerInfo> Players
{
get;
set;
} = new List<PlayerInfo>();
// 입장(true) / 퇴장(false) - 기존 맵 플레이어들에게 전달
[ProtoMember(3)]
public bool IsAdd
{
get;
set;
} }
// PARTY_CHANGE_MAP (클라 -> 서버 & 서버 -> 클라) // 이동한 플레이어 정보 - 기존 맵 플레이어들에게 전달
[ProtoMember(4)]
public PlayerInfo Player
{
get;
set;
}
}
// PARTY_CHANGE_MAP (클라 -> 서버 전용)
[ProtoContract] [ProtoContract]
public class PartyChangeMapPacket public class PartyChangeMapPacket
{ {