private void _getConnections() { IPEndPoint ipep = null; byte[] data = _client.Receive(ref ipep); int pos = 0; int n = BinaryHelper.ReadInt32(data, ref pos); _connections = new List <IPEndPoint>(); long ip; int port; for (int i = 0; i < n; i++) { ip = BinaryHelper.ReadLong(data, ref pos); port = BinaryHelper.ReadInt32(data, ref pos); if (ip != -1 && port != -1) { _connections.Add(new IPEndPoint(new IPAddress(ip), port)); } else { _connections.Add(null); } } _connections.Add(new IPEndPoint(CIp, CPort)); }
private Dictionary <int, Bonus> _getBonuses(byte[] data) { int pos = 0; int num = BinaryHelper.ReadInt32(data, ref pos); Dictionary <int, Bonus> result = new Dictionary <int, Bonus>(); Bonus b = null; for (int i = 0; i < num; i++) { int bId = BinaryHelper.ReadInt32(data, ref pos); int bx = BinaryHelper.ReadInt32(data, ref pos); int by = BinaryHelper.ReadInt32(data, ref pos); BonusType type = (BonusType)BinaryHelper.ReadByte(data, ref pos); b = _createBonus(bId, type, bx, by); result.Add(b.Id, b); } return(result); }
private void _analyzeUDPCommand(byte[] data, IPEndPoint ipep) { for (int i = 0; i < _connections.Count; i++) { if (_connections[i].Address.Address == ipep.Address.Address && _connections[i].Port == ipep.Port) { _requestTime[i] = DateTime.Now; } } int pos = 0; int id = BinaryHelper.ReadInt32(data, ref pos); //check if id is id and not a server command if (id >= 0) { //if id is id //load current pos and direction of player float x = BinaryHelper.ReadFloat(data, ref pos); float y = BinaryHelper.ReadFloat(data, ref pos); Directions dir = (Directions)BinaryHelper.ReadInt32(data, ref pos); //if player shooted bool shooting = BinaryHelper.ReadBool(data, ref pos); int team = BinaryHelper.ReadInt32(data, ref pos); //selected inv item byte curInvItem = BinaryHelper.ReadByte(data, ref pos); InvType invType = (InvType)BinaryHelper.ReadInt32(data, ref pos); //check if bonus was removed int removedBonus = BinaryHelper.ReadInt32(data, ref pos); if (removedBonus != -1) { if (OnBonusRemoved != null) { OnBonusRemoved(removedBonus); } } //load statistics information int Deaths = BinaryHelper.ReadInt32(data, ref pos); int Kills = BinaryHelper.ReadInt32(data, ref pos); //check if invItem was used lock (Players[id]) { Players[id].CurrentInvItemAKey = curInvItem; Players[id].X = x; Players[id].Y = y; Players[id].Direction = dir; Players[id].SetTeam((Teams)team); Players[id].Kills = Kills; Players[id].Deaths = Deaths; if (shooting) { Players[id].Shoot(true); } if (invType != InvType.Unknown && invType != InvType.Cannon) { GameLevel.CreateEntity(_createInvItem(invType), id); } } //check if new bonus was created if (pos != data.Length) { int bId = BinaryHelper.ReadInt32(data, ref pos); int bx = (int)BinaryHelper.ReadFloat(data, ref pos); int by = (int)BinaryHelper.ReadFloat(data, ref pos); BonusType type = (BonusType)BinaryHelper.ReadByte(data, ref pos); Bonus b = _createBonus(bId, type, bx, by); GameLevel.AddBonus(b); } } else { //if id is a server command UDPCommands command = (UDPCommands)id; switch (command) { case UDPCommands.AddPlayer: { Player t = new Player(Players.Count, 0, 0); t.Id = BinaryHelper.ReadInt32(data, ref pos); t.Removed = true; if (t.Id == Players.Count) { Players.Add(t); _connections.Add(ipep); _requestTime.Add(DateTime.Now); } else { Players[t.Id] = t; _connections[t.Id] = ipep; _requestTime[t.Id] = DateTime.Now; } break; } case UDPCommands.AddPlayerComplete: { id = BinaryHelper.ReadInt32(data, ref pos); lock (Players[id]) { Players[id].Removed = false; } break; } case UDPCommands.ClientDisconnected: { id = BinaryHelper.ReadInt32(data, ref pos); lock (Players[id]) { Players[id] = null; _connections[id] = null; _requestTime[id] = new DateTime(); } //MessageBox.Show("Client " + id + " disconnected"); break; } case UDPCommands.ConnectToGame: { _connectToGame(ipep); break; } case UDPCommands.ServerChanged: { long ip = BinaryHelper.ReadLong(data, ref pos); if (ip == CIp.Address) { _isMain = true; } GameLevel.BonusId = BinaryHelper.ReadInt32(data, ref pos); break; } } } }
public AddPlayerResult AddPlayer(List <Player> Players) { this.Players = Players; _client.Send(BitConverter.GetBytes((int)UDPCommands.ConnectToGame), sizeof(int), ServerIpEp); _getConnections(); for (int i = 0; i < _connections.Count; i++) { if (_connections[i] != null) { currentTeamCaret += 1; Player player = new Player(i, 0, 0); if (gameMode == Modes.Deathmatch) { player.SetTeam((Teams)currentTeamCaret); } Players.Add(player); _requestTime.Add(DateTime.Now); } else { Players.Add(null); _requestTime.Add(new DateTime()); } } AddPlayerResult result = new AddPlayerResult(); IPEndPoint ipep = null; int pos = 0; byte[] data = _client.Receive(ref ipep); result.Mode = BinaryHelper.ReadInt32(data, ref pos); gameMode = (Modes)result.Mode; // Receiving var currentGameState = _receiveGameState(_client); result.LevelWidth = (int)currentGameState[0]; result.LevelHeight = (int)currentGameState[1]; result.Tiles = (List <GEntity>)currentGameState[2]; result.Entities = (List <GEntity>)currentGameState[3]; result.Spawners = (List <GEntity>)currentGameState[4]; data = _client.Receive(ref ipep); // Bonuses data result.Bonuses = _getBonuses(data); pos = 0; data = _client.Receive(ref ipep); Spawner spawner = (Spawner)result.Spawners[Program.Rand.Next(0, result.Spawners.Count - 1)]; CurrentPlayer = new Player(BinaryHelper.ReadInt32(data, ref pos), spawner.X, spawner.Y); result.Player = CurrentPlayer; if (gameMode == Modes.Deathmatch) { result.Player.SetTeam((Teams)currentTeamCaret); } Players[CurrentPlayer.Id] = CurrentPlayer; _requestTime[CurrentPlayer.Id] = DateTime.Now; _thread = new Thread(_waitForClientData); _thread.Start(); System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = 100; timer.Start(); CurrentPlayer.Removed = true; IsConnected = false; timer.Tick += _timer_Tick; data = new byte[sizeof(int) * 2]; pos = 0; BinaryHelper.Write((int)UDPCommands.AddPlayer, ref pos, ref data); BinaryHelper.Write(CurrentPlayer.Id, ref pos, ref data); _broadcast(data); return(result); }