private static void OnPlayerDead(object sender, PlayerBallManager.PlayerDeadArgs msg) { var conn = AgarConnMgr.Get(msg.UserId); MarkMgr.Remove(msg.UserId); // 为自己发送死亡信息 var selfMsg = new Msg_AgarSelf(); selfMsg.Operat = Msg_AgarSelf.Dead; SendMessage(AgarServer, selfMsg, conn); // 为其他玩家推送玩家死亡信息 var pubMsg = new Msg_AgarPlayInfo(); pubMsg.Operat = Msg_AgarPlayInfo.Remove; pubMsg.UserId = msg.UserId; SendMessageExceptOne(AgarServer, pubMsg, conn); }
private static bool OnProcessPlayerInfo(NetServer server, int id, NetIncomingMessage msg) { Msg_AgarPlayInfo r = new Msg_AgarPlayInfo(); r.R(msg); uint uid = r.UserId; if (r.Operat == Msg_AgarPlayInfo.Changed) { if (!PlayerBallMgr.IsContain(uid)) { return(true); } PlayerBall newBall = PlayerBallMgr.Get(uid); uint tag = r.Tag; if (GameMessageHelper.Is_Changed(tag, GameMessageHelper.POSITION_TAG)) { newBall.X = r.X; newBall.Y = r.Y; } if (GameMessageHelper.Is_Changed(tag, GameMessageHelper.RADIUS_TAG)) { newBall.Radius = r.Radius; } if (GameMessageHelper.Is_Changed(tag, GameMessageHelper.COLOR_TAG)) { newBall.Color = r.Color; } if (GameMessageHelper.Is_Changed(tag, GameMessageHelper.NAME_TAG)) { newBall.Name = r.Name; } bool RaduisChanged = false; bool FoodChanged = false; bool PlayerChanged = false; // 检查食物有没有被吃 if (UpdateFood(uid, ref newBall)) { FoodChanged = true; } // 检查能不能吃其他玩家 if (UpdatePlayer(uid, ref newBall)) { PlayerChanged = true; } if (FoodChanged || PlayerChanged) { r.Tag = r.Tag | GameMessageHelper.RADIUS_TAG; r.Radius = newBall.Radius; var self = new Msg_AgarSelf(); self.Operat = Msg_AgarSelf.GroupUp; self.Radius = newBall.Radius; RaduisChanged = true; SendMessage(server, self, msg.SenderConnection); } PlayerBallMgr.Change(uid, newBall); // 检查会不会被其他玩家吃 uint EatId = 0; if (UpdateOtherPlayer(uid, newBall, out EatId)) { if (PlayerBallMgr.IsContain(EatId)) { var EatBall = PlayerBallMgr.Get(EatId); var conn = AgarConnMgr.Get(EatId); EatBall.Radius += newBall.Radius; PlayerBallMgr.Change(EatId, EatBall); // 向其他玩家发送 Msg_AgarPlayInfo eatMsg = new Msg_AgarPlayInfo(); eatMsg.Operat = Msg_AgarPlayInfo.Changed; eatMsg.UserId = EatId; eatMsg.Tag = GameMessageHelper.RADIUS_TAG; eatMsg.Radius = EatBall.Radius; RaduisChanged = true; SendMessageExceptOne(server, eatMsg, conn); // 向自身发送 var selfEatMsg = new Msg_AgarSelf(); selfEatMsg.Operat = Msg_AgarSelf.GroupUp; selfEatMsg.Radius = EatBall.Radius; SendMessage(server, selfEatMsg, conn); } } if (RaduisChanged) { MarkMgr.Update(uid, newBall.Radius); } } SendMessageExceptOne(server, r, msg.SenderConnection); return(true); }
static void Main(string[] args) { OnAgarServerProgerss(); OnHappyServerProgerss(); while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape) { NetIncomingMessage msg; while ((msg = AgarServer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryRequest: AgarServer.SendDiscoveryResponse(null, msg.SenderEndPoint); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); if (status == NetConnectionStatus.Connected) { Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!"); Console.WriteLine(AgarServer.Connections.Count); AgarConnMgr.Add(msg.SenderConnection); } else if (status == NetConnectionStatus.Disconnected) { Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnect!"); uint removeId = AgarConnMgr.Get(msg.SenderConnection); PlayerBallMgr.Remove(removeId); AgarConnMgr.Remove(msg.SenderConnection); Console.WriteLine(AgarServer.Connections.Count); } break; case NetIncomingMessageType.Data: int id = msg.ReadInt32(); if (!ProcessPacket(AgarServer, id, msg)) { DispatchPacket(AgarServer, id, msg); } break; } } while ((msg = HappyServer.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryRequest: HappyServer.SendDiscoveryResponse(null, msg.SenderEndPoint); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); if (status == NetConnectionStatus.Connected) { Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!"); Console.WriteLine(HappyServer.Connections.Count); HappyConnMgr.Add(msg.SenderConnection); } else if (status == NetConnectionStatus.Disconnected) { Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnect!"); uint removeId = HappyConnMgr.Get(msg.SenderConnection); HappyPlayerMgr.Remove(removeId); HappyConnMgr.Remove(msg.SenderConnection); Console.WriteLine(HappyServer.Connections.Count); } break; case NetIncomingMessageType.Data: int id = msg.ReadInt32(); if (!ProcessPacket(HappyServer, id, msg)) { DispatchPacket(HappyServer, id, msg); } break; } } Thread.Sleep(1); } AgarServer.Shutdown("app exiting"); }