public void OnPlayerFirstConnect(ConnectionService con) { if (CurrentGameState != GameState.Room) { Interface.Log("Closing conenction"); //con.SendReply(NetworkingTags.Controller, NetworkingTags.ControllerSubjects.DisconnectYou, ""); //IDsToIgnore.Add(con.id); con.Close(); } }
protected void Close() => connectionController.Close();
public void OnDataReceived(ConnectionService con, ref NetworkMessage msg) { if (IDsToIgnore.Contains(con.id)) { Interface.Log("Ignoring id " + con.id); return; } if (msg.tag == NetworkingTags.Server) { if (msg.subject == NetworkingTags.ServerSubjects.ClientReadyToPlay) { theClients[msg.senderID].ReadyToPlay = true; bool allReadyToPlay = true; foreach (KeyValuePair <int, PlayerInfo> theKey in theClients) { if (!theKey.Value.ReadyToPlay) { allReadyToPlay = false; break; } } if (allReadyToPlay) { foreach (KeyValuePair <int, PlayerInfo> theKey in theClients) { PacketUseTypeID PlayerData = new PacketUseTypeID(); PlayerData.client_id = (ushort)theKey.Key; PlayerData.thePlayerType = (int)theKey.Value.thePlayerType; theListOfPlayers.Add(PlayerData);; } foreach (KeyValuePair <int, PlayerInfo> theKey in theClients) { DarkRiftServer.GetConnectionServiceByID((ushort)theKey.Key).SendReply(NetworkingTags.Controller, NetworkingTags.ControllerSubjects.StartGame, theListOfPlayers); theKey.Value.ReadyToPlay = false; } theListOfPlayers.Clear(); CurrentGameState = GameState.Game; } } else if (msg.subject == NetworkingTags.ServerSubjects.ClientNotReady) { theClients[msg.senderID].ReadyToPlay = false; } else if (msg.subject == NetworkingTags.ServerSubjects.ChangeStateToRoom) { // CurrentGameState = GameState.Room; } else if (msg.subject == NetworkingTags.ServerSubjects.ChangeStateToGame) { //CurrentGameState = GameState.Game; } else if (msg.subject == NetworkingTags.ServerSubjects.ILose) { Interface.Log("Received message from" + con.id + " to lose"); con.SendReply(NetworkingTags.Controller, NetworkingTags.ControllerSubjects.YouLose, ""); theClients[msg.senderID].Lost = true; int AmountOfLost = 0; int IDofWinner = -1; foreach (KeyValuePair <int, PlayerInfo> theKey in theClients) { if (theKey.Value.Lost) { AmountOfLost++; } else { IDofWinner = theKey.Key; } } Interface.Log("ID of winner is " + IDofWinner); Interface.Log("Amount of lost is " + AmountOfLost); if (AmountOfLost == (theClients.Count - 1)) { DarkRiftServer.GetConnectionServiceByID((ushort)IDofWinner).SendReply(NetworkingTags.Controller, NetworkingTags.ControllerSubjects.YouWin, ""); //Reset lost counter. foreach (KeyValuePair <int, PlayerInfo> theKey in theClients) { theKey.Value.Lost = false; } } else if (AmountOfLost == theClients.Count) { ConnectionService[] allID = DarkRiftServer.GetAllConnections(); for (int i = 0; i < allID.Length; i++) { allID[i].SendReply(NetworkingTags.Controller, NetworkingTags.ControllerSubjects.Draw, ""); } } } else if (msg.subject == NetworkingTags.ServerSubjects.PlayerRestarting) { theClients[msg.senderID].ReadyToPlay = false; msg.DecodeData(); int money = (int)msg.data; theClients[msg.senderID].Money = (int)msg.data; //Interface.Log("setting money to " + (int)msg.data); bool allRestarted = true; foreach (KeyValuePair <int, PlayerInfo> theKey in theClients) { if (theKey.Value.ReadyToPlay) { allRestarted = false; break; } } if (allRestarted) { CurrentGameState = GameState.Room; } } else if (msg.subject == NetworkingTags.ServerSubjects.GetMoneyForPlayer) { Interface.Log("sending money : " + theClients[con.id].Money); con.SendReply(NetworkingTags.Controller, NetworkingTags.ControllerSubjects.GetMoneyForPlayer, theClients[con.id].Money); } else if (msg.subject == NetworkingTags.ServerSubjects.SetMoneyForPlayer) { Interface.Log("setting money to " + (int)msg.data); theClients[con.id].Money = (int)msg.data; } else if (msg.subject == NetworkingTags.ServerSubjects.GetNumOfPlayers) { if (DarkRiftServer.GetNumberOfConnections() <= 4) { ConnectionService[] allID = DarkRiftServer.GetAllConnections(); for (int i = 0; i < allID.Length; i++) { allID[i].SendReply(NetworkingTags.Misc, NetworkingTags.MiscSubjects.RetNumOfPlayers, DarkRiftServer.GetNumberOfConnections()); } } else { con.Close(); //con.SendReply(NetworkingTags.Misc, NetworkingTags.MiscSubjects.FullSlotted, ""); } } else if (msg.subject == NetworkingTags.ServerSubjects.StartGameFromHost) { //ConnectionService[] allID = DarkRiftServer.GetAllConnections(); //for (int i = 0; i < allID.Length; i++) //{ // allID[i].SendReply(NetworkingTags.Misc, NetworkingTags.ServerSubjects.StartGameFromHost, ""); //} } } else if (msg.tag == NetworkingTags.Controller) { if (msg.subject == NetworkingTags.ControllerSubjects.JoinMessage) { if (CurrentGameState == GameState.Room) { if (!theClients.ContainsKey(msg.senderID)) { PlayerNum++; int color = PlayerColorsAvailableClass.GetNextAvailableColor(); PlayerInfo newPlayer = new PlayerInfo(); newPlayer.Lost = false; newPlayer.ReadyToPlay = false; newPlayer.SpawnPoint = PlayerNum; newPlayer.thePlayerType = (PlayerType)color; //we haven't add a condition to make sure its less than 4. theClients.Add(msg.senderID, newPlayer); msg.data = color; } else { msg.data = theClients[msg.senderID].thePlayerType; } } else { //Interface.Log("Disconnecting player: game room is " + CurrentGameState); //con.Close(); } } } }