//Called when a message is received that a user has connected private void UserConnected(string name, bool isHost, bool isPlayer, bool isRedTeam, string clientID) { var c = new TeamPlayer(); c.name = name; c.isHost = isHost; c.isPlayer = isPlayer; c.id = clientID; var tempTeamName = isRedTeam ? CS.RED_TEAM : CS.BLUE_TEAM; var tempTeamID = tempTeamName; var fTeam = gClientGame.gameTeams.Where(team => team.id == tempTeamID); if (fTeam.FirstOrDefault() == null) { var t = new GameTeam(); t.name = tempTeamName; t.id = tempTeamID; t.teamPlayers.Add(c); gClientGame.gameTeams.Add(t); } else { //gClientGame.gameTeam.Where(team => team.name == tempTeamID).FirstOrDefault().teamPlayers.Add(c); fTeam.FirstOrDefault().teamPlayers.Add(c); } //players.Add(c); //TODO - Update the panel message to say "waiting for host to choose teams" }
public void FindPlayer(String fplayerID, ref TeamPlayer fPlayer) { foreach (var cntT in this.gameTeams) { var cntP = cntT.teamPlayers.FirstOrDefault(player => player.id == fplayerID); if (cntP != null) { fPlayer = cntP; } } if ((fPlayer.id == null) || (fPlayer.id == "")) { fPlayer = null; } }
//public void SetLobbyLineItem(GameClient gc) public void SetLobbyLineItem(GameTeam gt, TeamPlayer tp) { var client = FindObjectOfType <Client>(); //Set Default UI display for the LobbyLineItem clientID = tp.id; teamID = gt.id; lineItemText.text = tp.name; isCaller.isOn = !tp.isPlayer; //ToggleGroup toggleTeamGroup= GetComponent<ToggleGroup>(); //Note we are using the "name" property of the team as it's ID foreach (var tempTeam in client.gClientGame.gameTeams.ToList().Select((value, i) => new { i, value })) { var value = tempTeam.value; var index = tempTeam.i; var toggleTeam = Instantiate(ToggleTeamPrefab.GetComponent <Toggle>()); ToggleTeamItems.Add(toggleTeam); toggleTeam.transform.SetParent(transform, false); toggleTeam.transform.Translate(toggleOffset * (tempTeam.i)); toggleTeam.name = tempTeam.value.id; toggleTeam.group = toggleGroup; // isRedTeam.isOn = gt.name == CS.RED_TEAM; // isRedTeam.name = "fixthis"; // isBlueTeam.isOn = !isRedTeam.isOn; // isBlueTeam.name = gt.id; toggleTeam.isOn = toggleTeam.name == gt.id; //Having set defaults for what to display in the UI, reflect these back in the data structures //Obtain the Team ID, reading it back from the UI //TODO - Remove hardcoding of TEAMS //var tmpTeamId = (isRedTeam.isOn ? CS.RED_TEAM : (isBlueTeam.isOn ? CS.BLUE_TEAM:CS.NO_TEAM)); } //Move the player to the correct team client.gClientGame.MovePlayerTeam(clientID, teamID); //Have to also set the GameClient //SetGameClient(); }
public void MovePlayerTeam(String mplayerID, string mTeamID) //upserts a player and a team as specified by the ID { GameTeam fTeam = new GameTeam(); TeamPlayer fPlayer = new TeamPlayer(); //find the player in its existing team foreach (var cntT in this.gameTeams) { var cntP = cntT.teamPlayers.FirstOrDefault(player => player.id == mplayerID); if (cntP != null) { fPlayer = cntP; fTeam = cntT; } } //if no player is found then create a new player if (fPlayer.id == null) { fPlayer = new TeamPlayer(); } fPlayer.id = mplayerID; //remove the player from its old team if it existed in a different team to the one wanted if (fTeam.id != mTeamID) { //make sure the team you are moving to exists, if not create it var checkTeam = gameTeams.FirstOrDefault(team => team.id == mTeamID); if (checkTeam == null) { GameTeam newTeam = new GameTeam(); newTeam.id = mTeamID; gameTeams.Add(newTeam); } //remove from old team, will remove it if found fTeam.teamPlayers.Remove(fPlayer); //add to new team gameTeams.FirstOrDefault(team => team.id == mTeamID).teamPlayers.Add(fPlayer); } }
private void SetGameClient() { var client = FindObjectOfType <Client>(); var MyLobbyGroup = FindObjectOfType <Lobby>(); TeamPlayer gIncomingPlayer = new TeamPlayer(); //Find the player associated with this lineitem client.gClientGame.FindPlayer(clientID, ref gIncomingPlayer); //Check that the player is found if (gIncomingPlayer != null) { //Set the player's caller status //TODO - Is this line needed? gIncomingPlayer.isPlayer = !isCaller.isOn; //Obtain the Team ID //START HERE - This returns a NULL var tmpTeamId = toggleGroup.ActiveToggles().First().name; //Move the player to the correct team client.gClientGame.MovePlayerTeam(clientID, tmpTeamId); } }
//Server read public void OnIncomingData(string data) { Debug.Log("Server Receiving: " + data); //var gbs = FindObjectOfType<GameState>(); //parse the incoming data stream var aData = data.Split('|'); int x = 0; int z = 0; var distPosX = ""; var distPosZ = ""; var distWords = ""; var distPopulate = ""; var distReveal = ""; var distCardID = ""; //used for messaging GameMessage gIncomingMessage = new GameMessage(); gIncomingMessage = GameMessage.LoadFromText(data); GameMessage gOutgoingMessage = new GameMessage(); //used as temporary stores for found players and teams in the server TeamPlayer fPlayer = new TeamPlayer(); GameTeam fTeam = new GameTeam(); //used to refer to players and teams in incoming messages TeamPlayer gIncomingPlayer = new TeamPlayer(); GameTeam gIncomingTeam = new GameTeam(); //First Add Message to the list of events in the game gbs.g.GameMessages.Add(gIncomingMessage); // find the player who sent the message in the server's list of players gbs.g.FindPlayer(gIncomingMessage.sender.id, ref fPlayer); // find the player's details in the message received gIncomingMessage.FindPlayer(gIncomingMessage.sender.id, ref gIncomingPlayer); //Now decide what to do with the stored message switch (gIncomingMessage.name) { case CS.MESSAGE_CWHO: //If a host request is made reset the game and the list of connections if (fPlayer != null && gIncomingPlayer.isHost) { //update player parameters fPlayer.isHost = gIncomingPlayer.isHost; //update game parameters gbs.g.gameParameters.howManyPlaying = gIncomingMessage.gameParameters.howManyPlaying; gbs.g.gameParameters.howManyTeams = gIncomingMessage.gameParameters.howManyTeams; gbs.g.gameParameters.howManyCallers = gIncomingMessage.gameParameters.howManyCallers; gbs.g.gameParameters.sizeOfXDim = gIncomingMessage.gameParameters.sizeOfXDim; gbs.g.gameParameters.sizeOfYDim = gIncomingMessage.gameParameters.sizeOfYDim; populate = new string[gbs.g.gameParameters.sizeOfXDim * gbs.g.gameParameters.sizeOfYDim]; words = new string[2, gbs.g.gameParameters.sizeOfXDim *gbs.g.gameParameters.sizeOfYDim]; //clear all other client's connections var i = 0; if (clients.Count > 1) { do { var sc = clients[i]; //is the client still connected? if (IsConnected(sc.tcp)) //TODO - upgrade the server to be able to manage multiple games and present the list of games that are currently running to the players { sc.tcp.Close(); clients.Remove(sc); } i++; } while (i < clients.Count - 2); } //clear teams gbs.g.gameTeams.Clear(); //add in team for players not yet assigned var noTeam = new GameTeam { id = CS.NO_TEAM, name = CS.NO_TEAM }; gbs.g.gameTeams.Add(noTeam); } // add the new client details to the list of clients, remember that it will always be the last client in the list of clients that we do not have the details for clients[clients.Count - 1].clientID = gIncomingPlayer.id; //Add the new client details to the team of players that are unassigned team foreach (var client in clients) { var tmpTeamPlayer = new TeamPlayer { id = gIncomingPlayer.id, name = gIncomingPlayer.name, isHost = gIncomingPlayer.isHost, isPlayer = gIncomingPlayer.isPlayer }; gbs.g.gameTeams.FirstOrDefault(gt => gt.id == CS.NO_TEAM).teamPlayers.Add(tmpTeamPlayer); } //add in remaining teams, note they will have no players for (var cntTeams = 0; cntTeams < gbs.g.gameParameters.howManyTeams; cntTeams++) { var tmpTeam = new GameTeam { id = UnityEngine.Random.Range(0, 99999).ToString() }; //todo - remove hardcoding of only 2 teams if (cntTeams == 0) { tmpTeam.name = CS.RED_TEAM; } else { tmpTeam.name = CS.BLUE_TEAM; } gbs.g.gameTeams.Add(tmpTeam); } //Respond by telling all clients there is a new list of clients //Build the message //Message Header gOutgoingMessage.id = CS.MESSAGE_SCNN; gOutgoingMessage.name = gOutgoingMessage.id; gOutgoingMessage.type = CS.MESSAGE_EVENT; //Message Sender gOutgoingMessage.sender.id = CS.SERVER_ID; gOutgoingMessage.sender.name = CS.SERVER_NAME; //Message Details gOutgoingMessage.gameParameters = gbs.g.gameParameters; gOutgoingMessage.gameTeams = gbs.g.gameTeams; Broadcast(gOutgoingMessage, clients); break; case "CMOV": //Currently all validation for the gameboard move is done client side gbs.Incoming(gIncomingMessage.name); string strValidGameboardMove = gbs.UpdateGameboardDeckCardStatus(aData[2], aData[3]) ? "1" : "0"; Broadcast( "SMOV" + '|' + aData[1] + '|' // + aData[2] + '|' // + aData[3] + '|' + aData[2] + '|' + aData[3] + "|" + strValidGameboardMove, clients ); break; case "CHAN": //Update the server copy of the deck marking the card as having been played //Send message to GameBoardState string strValidHandMove = gbs.UpdateHandDeckCardStatus(aData[2], aData[3]) ? "1" : "0"; Broadcast( "SHAN" + '|' + aData[1] + '|' + aData[2] + '|' + aData[3] + '|' + strValidHandMove, clients ); break; case CS.MESSAGE_CDIC: //Send message to GameBoardState, this will populate the WordList /* * gbs.Incoming(aData); * * distWords = ""; * distPopulate = ""; * distCardID = ""; * * //Cannot use FindObjectOfType in the constructor, so have to assign in here * var worddictionary = FindObjectOfType<WordDictionary>(); * * //worddictionary.buildGameboard(); * * * foreach (var pair in worddictionary.gameBoardCardData) * { * distWords += pair.Value.wordList + ","; * distPopulate += pair.Value.populate + ","; * distCardID += pair.Value.cardid + ","; * } * * Broadcast( * "SDIC" + '|' + worddictionary.isRedStart + '|' + distWords + '|' + distPopulate + '|' + distCardID, + clients + ); */ //Send message to GameBoardState, this will populate the WordList gbs.Incoming(gIncomingMessage.name); //Respond by telling all clients the dictionary to use //build the dictionary /* * //TODO - Improve the efficiency of how you popolate the card details dictionary should really be doing this not the server * * //Cannot use FindObjectOfType in the constructor, so have to assign in here * var worddictionary = FindObjectOfType<WordDictionary>(); * * //TODO - Rewmove hardcodng for only one gameboard * var cnt = 0; * var gbd = new GameBoardDeck(); * gbs.g.GameBoardDecks.Add(gbd); * GameBoardDeck gbdFirstOrDefault = gbs.g.GameBoardDecks.FirstOrDefault(); * * //Populate the deck * foreach (var word in worddictionary.gameBoardCardData) * { * var card = new GameCard(); * card.id = word.Value.cardid; * card.cardSuit = word.Value.populate; * card.cardWord = word.Value.wordList; * gbdFirstOrDefault.gameCards.Add(card); * } * */ //Build the message //Message Header gOutgoingMessage.id = CS.MESSAGE_SDIC; gOutgoingMessage.name = gOutgoingMessage.id; gOutgoingMessage.type = CS.MESSAGE_REPLY; //Message Sender gOutgoingMessage.sender.id = CS.SERVER_ID; gOutgoingMessage.sender.name = CS.SERVER_NAME; //Message Details gOutgoingMessage.gameParameters = gbs.g.gameParameters; gOutgoingMessage.gameTeams = gbs.g.gameTeams; gOutgoingMessage.GameBoardDecks = gbs.g.GameBoardDecks; Broadcast(gOutgoingMessage.SaveToText().Replace(Environment.NewLine, ""), clients); break; case "CKEY": gbs.Incoming(gIncomingMessage.name); var tmpVal = aData[2].ToUpper(); Broadcast( "SKEY" + '|' + tmpVal, clients ); break; case "CBEG": //replace all teams gbs.g.gameTeams = gIncomingMessage.gameTeams; //Respond by telling all clients the updated details for all players of all teams //Build the message //Message Header gOutgoingMessage.id = CS.MESSAGE_SBEG; gOutgoingMessage.name = gOutgoingMessage.id; gOutgoingMessage.type = CS.MESSAGE_EVENT; //Message Sender gOutgoingMessage.sender.id = CS.SERVER_ID; gOutgoingMessage.sender.name = CS.SERVER_NAME; //Message Details gOutgoingMessage.gameParameters = gbs.g.gameParameters; gOutgoingMessage.gameTeams = gbs.g.gameTeams; Broadcast(gOutgoingMessage.SaveToText().Replace(Environment.NewLine, ""), clients); break; case "CPCC": //Send message to GameBoardState gbs.Incoming(gIncomingMessage.name); Broadcast(gbs.ghd.SaveToText().Replace(Environment.NewLine, ""), clients); break; case "CPCU": //Send message to GameBoardState gbs.Incoming(gIncomingMessage.name); Broadcast(gbs.ghd.SaveToText().Replace(Environment.NewLine, ""), clients); break; //Hand Card Affects on the board case "CGFU": distPosX = ""; distPosZ = ""; distWords = ""; distPopulate = ""; distReveal = ""; for (z = 0; z < gbs.g.gameParameters.sizeOfYDim; z++) { for (x = 0; x < gbs.g.gameParameters.sizeOfXDim; x++) { //implement additional attributes distPosX += gbs.gbd.gameCards[x + z * gbs.g.gameParameters.sizeOfXDim].cardXPos + ","; distPosZ += gbs.gbd.gameCards[x + z * gbs.g.gameParameters.sizeOfXDim].cardZPos + ","; distWords += gbs.gbd.gameCards[x + z * gbs.g.gameParameters.sizeOfXDim].cardWord + ","; distPopulate += gbs.gbd.gameCards[x + z * gbs.g.gameParameters.sizeOfXDim].cardSuit + ","; distReveal += gbs.gbd.gameCards[x + z * gbs.g.gameParameters.sizeOfXDim].cardRevealed + ","; distCardID += gbs.gbd.gameCards[x + z * gbs.g.gameParameters.sizeOfXDim].cardID + ","; } } Broadcast( "SGFU" + '|' + GameState.isRedTurn + '|' + distWords.Remove(distWords.LastIndexOf(',')) + '|' + distPopulate.Remove(distPopulate.LastIndexOf(',')) + '|' + distPosX.Remove(distPosX.LastIndexOf(',')) + '|' + distPosZ.Remove(distPosZ.LastIndexOf(',')) + '|' + distReveal.Remove(distReveal.LastIndexOf(',')) + '|' + distCardID.Remove(distCardID.LastIndexOf(',')), clients ); break; } }