private static void SendPlayersInfo(NetHandle caller) { if (players.Count < 2) { return; } List <PlayerInfo> playerInfos = new List <PlayerInfo>(); foreach (var player in players.ToArray()) { playerInfos.Add(player.Value.playerInfo); } BBMessage message = new BBMessage(); message.MessageType = mt.PlayerData; message.PlayerInfos.AddRange(playerInfos); DatagramPacket packet = BBPacket.BuildOnRun(0, 0, message.ToByteArray()); foreach (var player in players.ToArray()) { packet.Send(caller, player.Key); } }
private static void MessageCallback(NetHandle caller, DatagramPacket datagram) { if (datagram == null) { return; } BBPacket packet = new BBPacket(datagram); BBMessage bbmessage = BBMessage.Parser.ParseFrom(packet.Bytes, 0, packet.Bytes.Length); switch (bbmessage.MessageType) { case mt.Connect: lock (lockObj) { foreach (var player in players) { if (player.Value.playerInfo.Name == bbmessage.PlayerInfo.Name) { goto default; } } players.Add(datagram.point, new Player { playerInfo = bbmessage.PlayerInfo, playerTimeout = new PlayerTimeout() }); } caller.Send(datagram); Console.WriteLine("Player {0} was connected", bbmessage.PlayerInfo.Name); SendPlayersInfo(caller); break; case mt.Text: Console.WriteLine(bbmessage.MessageText); break; case mt.PlayerData: lock (lockObj) { foreach (var player in players) { var info = player.Value.playerInfo; if (Equals(player.Key, datagram.point)) { info.MergeFrom(bbmessage.PlayerInfo); } } } SendPlayersInfo(caller); break; default: break; } lock (lockObj) { foreach (var player in players) { if (Equals(player.Key, datagram.point)) { player.Value.playerTimeout.ClearSeconds(); } } } }