// An evil player wants to plant a bomb private void GameServer_BombPlacing(Client sender) { if (GameServer.Instance.GameManager.GameHasBegun) { var player = sender.Player; if (player.CurrentBombAmount > 0) { var bo = GameServer.Instance.GameManager.BombList.Find(b => b.CellPosition == player.CellPosition); if (bo != null) return; var bomb = new Bomb(player.Id, player.CellPosition, player.BombPower, player.BombTimer, player.Speed); bomb.Initialize(GameServer.Instance.GameManager.CurrentMap.Size, GameServer.Instance.GameManager.CurrentMap.CollisionLayer, GameServer.Instance.GameManager.HazardMap); GameServer.Instance.GameManager.CurrentMap.Board[bomb.CellPosition.X, bomb.CellPosition.Y] = bomb; GameServer.Instance.GameManager.CurrentMap.CollisionLayer[bomb.CellPosition.X, bomb.CellPosition.Y] = true; GameServer.Instance.GameManager.AddBomb(bomb); player.CurrentBombAmount--; GameServer.Instance.SendPlayerPlacingBomb(sender, bomb.CellPosition); } } }
void ReceiveMapSelection(Client client, string md5) { if (client.IsHost) { Instance.SelectedMapName = MapLoader.GetMapNameFromMd5(md5); SendSelectedMap(client); } }
/// <summary> /// Sends the id generate by the server to the corresponding client /// </summary> /// <param name="client">The corresponding client</param> public void SendClientId(Client client) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.ClientId); message.Write(client.ClientId); _server.SendMessage(message, client.ClientConnection, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] Sent server generated id to its corresponding client (id: " + client.ClientId + ")"); }
// TODO: Merge opcodes for move direction, something like that: Move(LookDirection) void DataProcessing(NetIncomingMessage message, ref Client client) { switch (message.ReadByte()) { case (byte)MessageType.ClientMessage.NeedMap: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Need map ! Let's give it to him :)"); ReceiveNeedMap(client); break; case (byte)MessageType.ClientMessage.MapSelection: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Has selected a map to play ! :)"); ReceiveMapSelection(client, message.ReadString()); break; case (byte)MessageType.ClientMessage.Credentials: ReceiveCredentials(client, message.ReadString(), message.ReadString()); break; case (byte)MessageType.ClientMessage.Ready: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Ready message !"); ReceiveReady(client, message.ReadBoolean()); break; case (byte)MessageType.ClientMessage.WantToStartGame: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] WantToStartGame message !"); ReceiveWantToStartGame(); break; case (byte)MessageType.ClientMessage.HasMap: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] HasMap message !"); ReceiveHasMap(client); break; case (byte)MessageType.ClientMessage.MoveDown: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Want to move down !"); ReceiveMovePlayer(client, LookDirection.Down); break; case (byte)MessageType.ClientMessage.MoveLeft: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Want to move left !"); ReceiveMovePlayer(client, LookDirection.Left); break; case (byte)MessageType.ClientMessage.MoveRight: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Want to move right !"); ReceiveMovePlayer(client, LookDirection.Right); break; case (byte)MessageType.ClientMessage.MoveUp: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Want to move up !"); ReceiveMovePlayer(client, LookDirection.Up); break; case (byte)MessageType.ClientMessage.Standing: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Want to stay here !"); ReceiveMovePlayer(client, LookDirection.Idle, message.ReadTime(client.ClientConnection, true)); break; case (byte)MessageType.ClientMessage.PlaceBomb: Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Want to place bomb !"); ReceiveBombPlacing(client); break; } }
void ReceiveCredentials(Client client, string username, string password) { client.Username = username; client.Password = password; //MainServer.SendCheckIfOnline(username, password); Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Credentials. (username: "******"|password: "******")"); // Create a new player var player = new Player(client.ClientId); Instance.GameManager.AddPlayer(client, player); }
void ReceiveReady(Client client, bool ready) { client.IsReady = ready; if (client.IsReady) { Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Client is ready !"); } else { Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Client is not ready actually !"); } SendIsReady(client, ready); }
// Send all players to this player public void SendClientsToNew(Client client) { foreach (Client currentClient in Clients) { if (client != currentClient) { NetOutgoingMessage message = GetClientInfo(currentClient); _server.SendMessage(message, client.ClientConnection, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] Sent the player (client #" + currentClient.ClientId + ") to the new player (client #" + client.ClientId + ")"); } } Program.Log.Info("[SEND] Sent the players to the new player (client #" + client.ClientId + ")"); }
public void SendAvailableMaps(Client client) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.AvailableMaps); // Number of maps message.Write(MapLoader.MapFileDictionary.Count); foreach (var map in MapLoader.MapFileDictionary) { message.Write(map.Key); message.Write(map.Value); } _server.SendMessage(message, client.ClientConnection, NetDeliveryMethod.ReliableOrdered); }
private NetOutgoingMessage GetClientInfo(Client client) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.NewClientInfo); message.Write(client.ClientId); message.Write(client.Username); message.Write(client.IsReady); return message; }
// Send to all player that a player committed suicide public void SendSuicidePlayer(Client suicidalClient) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.PlayerSuicide); message.Write(suicidalClient.ClientId); _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] " + suicidalClient.Player.Name + " committed suicide !"); }
public void SendSelectedMap(Client client) { try { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.SelectedMap); string currentMapMd5 = MapLoader.MapFileDictionary[Instance.SelectedMapName]; message.Write(currentMapMd5); _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); } catch (Exception) { throw new Exception("Error sending the md5 hash of the selected map !"); } }
void ReceiveHasMap(Client client) { client.HasMap = true; }
public void SendPowerUpPickUp(Client client, PowerUp powerUp) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.PowerUpPickUp); message.Write(client.ClientId); message.Write(powerUp.CellPosition); message.Write((byte)powerUp.Type); _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SENT] Power up pick up by client #" + client.ClientId + " ! (type: " + powerUp.Type + "|position: " + powerUp.CellPosition + ")"); }
/// <summary> /// Check if the client's username is already taken and provide a new unique username /// </summary> /// <param name="client">Client we want to check the username</param> /// <returns>A non-used username</returns> private string CheckUsernameAlreadyTaken(Client client) { var playerNames = _playerList.Select(p => p.Name).ToList(); if (playerNames.Contains(client.Username)) { var concat = 1; while (playerNames.Contains(client.Username + concat)) { concat++; } client.Username = client.Username + concat; } return client.Username; }
/// <summary> /// Event raised when a client is disconnecting /// </summary> /// <param name="sender">Client that is disconnecting</param> /// <param name="e">Optionnal arguments</param> private void OnDisconnectedClient(Client sender, EventArgs e) { if (DisconnectedClient != null) DisconnectedClient(sender, e); }
private void GameServer_DisconnectedClient(Client sender, EventArgs e) { if (GameServer.Instance.GameManager.GameInitialized) { sender.Player.IsAlive = false; GameServer.Instance.SendRemovePlayer(sender.Player); } //MainServer.SendCurrentPlayerAmount(); }
public void SendEnd(Client client) { if (client.ClientConnection.Status == NetConnectionStatus.Connected) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.End); message.Write(client.Player.IsAlive); _server.SendMessage(message, client.ClientConnection, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] 'End' to player #" + client.Player.Id); } }
void ReceiveNeedMap(Client client) { SendCurrentMap(client); Program.Log.Info("[RECEIVE][Client #" + client.ClientId + "] Client needs the current map, sending it to him ! :)"); }
void ReceiveMovePlayer(Client client, LookDirection movement, double? ping = null) { // Receives the player's current direction if (client.Player.IsAlive) client.Player.SetLookDirection(movement, ping); }
private void GameServer_ConnectedClient(Client client, EventArgs e) { if (true) // TODO: Check that the server is not full { // Add client to the list GameServer.Instance.Clients.AddClient(client); //MainServer.SendCurrentPlayerAmount(); } else { client.ClientConnection.Disconnect("Full Server"); Program.Log.Info("[FULLGAME] Client tried to connect but server is full !"); } }
private void SendCurrentMap(Client client) { if (client.ClientConnection.Status == NetConnectionStatus.Connected) { try { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.Map); message.Write(Instance.SelectedMapName); message.Write(MapLoader.MapFileDictionary[Instance.SelectedMapName]); string path = "Content/Maps/" + Instance.SelectedMapName; byte[] mapData = File.ReadAllBytes(path); message.Write(mapData.Length); foreach (var bt in mapData) { message.Write(bt); } _server.SendMessage(message, client.ClientConnection, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] Sent the map to client #" + client.ClientId); } catch (FileNotFoundException ex) { throw new Exception("This map doesn't exist !"); } } }
/// <summary> /// Event raised when a client is connecting /// </summary> /// <param name="sender">Client that is disconnecting</param> /// <param name="e">Optionnal arguments</param> private void OnConnectedClient(Client sender, EventArgs e) { if (ConnectedClient != null) ConnectedClient(sender, e); }
private void SendIsReady(Client client, bool ready) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.IsReady); message.Write(client.ClientId); message.Write(ready); _server.SendToAll(message, client.ClientConnection, NetDeliveryMethod.ReliableUnordered, 0); //Program.Log.Info("[SEND] Sent that this client is ready or not (id: " + client.ClientId + "|ready: " + ready + ")"); }
// Send the player's movement to all other players public void SendPlayerPosition(Client client, bool notDir, bool exceptHim) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.PlayerPosition); message.Write(client.Player.Position.X); message.Write(client.Player.Position.Y); message.Write((byte)client.Player.CurrentDirection); message.Write(client.ClientId); string logMessage = "[SEND] Sent position of client #" + client.ClientId + " !"; if (exceptHim) { _server.SendToAll(message, client.ClientConnection, NetDeliveryMethod.ReliableOrdered, 0); logMessage += " (except him)"; } else { _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); } Program.Log.Info(logMessage); }
// Send to all player that a player has been killed by someone public void SendKillPlayer(Client victimClient, Client killerClient) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.PlayerKill); message.Write(victimClient.ClientId); message.Write(killerClient.ClientId); _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] " + victimClient.Player.Name + " has been killed by " + killerClient.Player.Name + " !"); }
void ReceiveBombPlacing(Client client) { if (client.Player.IsAlive) OnBombPlacing(client); }
public void AddPlayer(Client client, Player player) { client.Username = CheckUsernameAlreadyTaken(client); player.Name = client.Username; client.Player = player; _playerList.Add(player); base.AddPlayer(player); if (GameServer.Instance.GameManager.GameInitialized) { client.Spectating = true; client.NewClient = true; } // Send the server generated id to the corresponding client GameServer.Instance.SendClientId(client); // Send list of available map GameServer.Instance.SendAvailableMaps(client); // Send selected map GameServer.Instance.SendSelectedMap(client); GameServer.Instance.SendNewClientInfo(client); GameServer.Instance.SendClientsToNew(client); }
// Send this new player to already existing players public void SendNewClientInfo(Client client) { if (client.ClientConnection.Status == NetConnectionStatus.Connected) { NetOutgoingMessage message = GetClientInfo(client); _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND] Sent the new player to all other players (client #" + client.ClientId + ")"); } }
// Send to all players that this player has placed a bomb public void SendPlayerPlacingBomb(Client client, Point position) { NetOutgoingMessage message = _server.CreateMessage(); message.Write((byte)MessageType.ServerMessage.PlayerPlacingBomb); message.Write(client.ClientId); message.Write(position); _server.SendToAll(message, NetDeliveryMethod.ReliableOrdered); Program.Log.Info("[SEND][Client #" + client.ClientId + "] Planted a bomb !"); }
private void OnBombPlacing(Client sender) { if (BombPlacing != null) BombPlacing(sender); }