Files
a301_mmo_game_server/MMOTestServer/MMOserver/Game/Service/ChangeMapHandler.cs

87 lines
2.8 KiB
C#

using LiteNetLib;
using MMOserver.Game.Channel;
using MMOserver.Game.Channel.Maps;
using MMOserver.Packet;
using ProtoBuf;
using Serilog;
using ServerLib.Packet;
using ServerLib.Service;
namespace MMOserver.Game.Service;
/*
* 맵 이동 메시지 핸들러
*/
public partial 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)
{
return;
}
int oldMapId = player.CurrentMapId;
int newMapId = packet.MapId;
// 일단 보스맵에서 로비로 원복할떄 캐싱해둔 걸로 교체
if (newMapId == -1)
{
if (player.PreviousMapId > 0)
{
// 레이드 맵 사용 종료 처리
channel.RemoveInstanceMap(oldMapId);
newMapId = player.PreviousMapId;
player.PreviousMapId = 0;
}
}
if (!channel.ChangeMap(hashKey, player, newMapId))
{
Log.Warning("[GameServer] CHANGE_MAP 유효하지 않은 맵 HashKey={Key} MapId={MapId}", hashKey, newMapId);
return;
}
PlayerInfo playerInfo = player.ToPlayerInfo();
// 기존 맵 유저들에게 퇴장 알림
ChangeMapPacket exitNotify = new() { MapId = oldMapId, IsAdd = false, Player = playerInfo };
BroadcastToMap(channelId, oldMapId, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, exitNotify));
// 새 맵 유저들에게 입장 알림 (본인 제외)
ChangeMapPacket enterNotify = new() { MapId = newMapId, IsAdd = true, Player = playerInfo };
BroadcastToMap(channelId, newMapId, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, enterNotify), peer);
// 본인에게 새 맵 플레이어 목록 전달
ChangeMapPacket response = new() { MapId = newMapId };
AMap? newMap = channel.GetMap(newMapId);
if (newMap != null)
{
foreach ((int uid, Player memberPlayer) in newMap.GetUsers())
{
if (uid == hashKey)
{
continue;
}
response.Players.Add(memberPlayer.ToPlayerInfo());
}
}
SendTo(peer, PacketSerializer.Serialize((ushort)PacketCode.CHANGE_MAP, response));
Log.Debug("[GameServer] CHANGE_MAP HashKey={Key} OldMap={OldMapId} NewMap={MapId}", hashKey, oldMapId, newMapId);
}
}