コード例 #1
0
        //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;
            }
        }
コード例 #2
0
 public void Send(GameMessage data)
 {
     Send(data.SaveToText().Replace(Environment.NewLine, ""));
 }
コード例 #3
0
        private void AcceptTcpClient(IAsyncResult ar)
        {
            // var gbs = FindObjectOfType<GameState>();

            //Listen for a client connection
            var listener = (TcpListener)ar.AsyncState;

            //Get a list of all the users that are connected, do this before the client connects

            /*
             * var Allusers = "";
             * foreach (var i in clients)
             *  Allusers +=
             *      "|"
             + i.clientName + ","
             + (i.isHost ? 1 : 0) + ","
             + (i.isPlayer ? 1 : 0) + ","
             + i.teamID + ","
             + i.clientID;
             */

            //If a client connection occurs add it to the list of clients
            var sc = new ServerClient(listener.EndAcceptTcpClient(ar));

            clients.Add(sc);

            //And now add it to the game
            //First create a new player

            /*
             * var c = new TeamPlayer();
             * c.id = sc.clientID;
             */

            //Now add the player to team, at the moment it has no team so place it in a dummy team used for players yet to be assigned a team
            gbs.g.MovePlayerTeam(sc.clientID, CS.NO_TEAM);

            /*
             * var fTeam =  gbs.g.gameTeams.Where(team => team.id == CS.NO_TEAM);
             * if (fTeam.FirstOrDefault() == null)
             * {
             *  var t = new GameTeam();
             *  t.name = CS.NO_TEAM;
             *  t.id = CS.NO_TEAM;
             *  t.teamPlayers.Add(c);
             *  gbs.g.gameTeams.Add(t);
             * }
             * else
             * {
             *  //gClientGame.gameTeam.Where(team => team.name == tempTeamID).FirstOrDefault().teamPlayers.Add(c);
             *  fTeam.FirstOrDefault().teamPlayers.Add(c);
             * }
             */

            //Once a connection occurs the listener will stop, so if you want to listen for more clients you need to restart listening again.
            startListening();

            Debug.Log("Somebody has connected. Starting to listen for any other clients");

            //Ask last person that connected to state who they are
            //Build the message
            GameMessage gOutgoingMessage = new GameMessage();

            //Message Header
            gOutgoingMessage.id   = CS.MESSAGE_SWHO;
            gOutgoingMessage.name = gOutgoingMessage.id;
            gOutgoingMessage.type = CS.MESSAGE_COMMAND;

            //Message Sender
            gOutgoingMessage.sender.id   = CS.SERVER_ID;
            gOutgoingMessage.sender.name = CS.SERVER_ID;

            //Message Receiver
            gOutgoingMessage.receiver.id = sc.clientID;

            //Broadcast the message
            //Broadcast("SWHO" + gbs.g.SaveToText(), clients[clients.Count - 1]);
            Broadcast(gOutgoingMessage.SaveToText().Replace(Environment.NewLine, ""), clients[clients.Count - 1]);

            //Finally Add Message to the list of events in the game
            gbs.g.GameMessages.Add(gOutgoingMessage);
        }
コード例 #4
0
        //Reading messages from the Server
        private void OnIncomingData(string data)
        {
            var x = 0;
            var z = 0;

            Debug.Log("Client Receiving: " + data);
            int howManyPlaying = 0;
            int howManyTeams   = 0;
            int howManyCallers = 0;

            int sizeOfXDim = 0;
            int sizeOfYDim = 0;
            var cardID     = "";

            //Use these for temporary messages
            Game        gEventGame     = new Game();
            GameMessage gMessagePlayer = new GameMessage();

            //TODO - remove this when all messaging is done via XML
            if (!data[0].Equals('<'))
            {
                var aData = data.Split('|');
                switch (aData[0])
                {
                case "SMOV":
//                        x = int.Parse(aData[2]);
//                        z = int.Parse(aData[3]);
                    cardID = aData[2];
                    if (aData[4] == "1")
                    {
//                          GameBoard.Instance.TryGameboardMove(x, z, cardID);
                        GameBoard.Instance.TryGameboardMove(cardID);
                    }
                    else
                    {
                        Debug.Log("Invalid Move attempted by " + aData[1]);
                    }
                    break;

                case "SHAN":
                    cardID = aData[2];
                    if (aData[4] == "1")
                    {
                        GameBoard.Instance.TryHandMove(cardID);
                    }
                    else
                    {
                        Debug.Log("Invalid Move attempted by " + aData[1]);
                    }
                    break;

                case "SDIC":
                    GameBoard.Instance.isRedTurn  = aData[1] == "0" ? false: true;
                    GameBoard.Instance.isRedStart = aData[1] == "0" ? false: true;

                    var wData = aData[2].Split(',');
                    GameBoard.Instance.words = wData;

                    var pData = aData[3].Split(',');
                    GameBoard.Instance.populate = pData;

                    var iData = aData[4].Split(',');
                    GameBoard.Instance.cardids = iData;

                    GameBoard.Instance.GeneratePlayerGameboard();

                    GetGameCardDeck(CS.CREATE);
                    break;

                case "SKEY":
                {
                    switch (aData[1])
                    {
                    case "R":
                        //need to get rid of the local gameCard copy if we receive a restart. When the scene loads we need to make sure we load a new set of cards
                        gcd.gameCards.RemoveAll(gameCards => gameCards.cardID != "");
                        GameBoard.Instance.ResartGame();
                        break;

                    case "P":
                        GameBoard.Instance.SetCamera(aData[1]);
                        break;
                        ;

                    case "C":
                        GameBoard.Instance.SetCamera(aData[1]);
                        break;
                    }
                }
                break;

                case "SGFU":
                    //Forced client update of Gameboard from server
                    GameBoard.Instance.isRedTurn = aData[1] == "False" ? false: true;

                    wData = aData[2].Split(',');
                    GameBoard.Instance.words = wData;

                    pData = aData[3].Split(',');
                    GameBoard.Instance.populate = pData;

                    var xData = aData[4].Split(',').Select(s => Int32.Parse(s)).ToArray();
                    GameBoard.Instance.xPos = xData;

                    var zData = aData[5].Split(',').Select(s => Int32.Parse(s)).ToArray();;
                    GameBoard.Instance.zPos = zData;

                    var rData = aData[6].Split(',');
                    GameBoard.Instance.reveal = rData;

                    GameBoard.Instance.UpdatePlayerGameboard();
                    break;
                }
            }
            //Must be XML
            //This section is harccoded for only dealing with decks, it is the first use of XML messaging and eventually must be removed
            else if (1 == 0)
            {
                //TODO - Assumes that the xml message is one to populate a game card deck
                gcdTemp = GameHandDeck.LoadFromText(data);
                if (!gcdTemp.SaveToText().Equals(gcd.SaveToText()))
                {
                    gcd = GameHandDeck.LoadFromText(data);
                    //TODO - Assumes only a red and blue deck
                    gcdRed  = new GameHandDeck();
                    gcdBlue = new GameHandDeck();
                    for (var cnt = 0; cnt < gcd.gameCards.Count; cnt++)
                    {
                        var gc = gcd.gameCards[cnt];
                        if (gc.cardSuit == CS.RED_TEAM)
                        {
                            gcdRed.gameCards.Add(gc);
                        }
                        else if (gc.cardSuit == CS.BLUE_TEAM)
                        {
                            gcdBlue.gameCards.Add(gc);
                        }
                    }

                    // If there are differences render the deck
                    GameBoard.Instance.GeneratePlayerHand();
                }
            }
            else if (1 == 1)
            {
                GameMessage gIncomingMessage = new GameMessage();
                GameMessage gOutgoingMessage = new GameMessage();
                gIncomingMessage = GameMessage.LoadFromText(data);
                switch (gIncomingMessage.name)
                {
                case CS.MESSAGE_SWHO:
                    //This message sets the ClientID from the server
                    gClientPlayer.id = gIncomingMessage.receiver.id;

                    foreach (var cntT in gIncomingMessage.gameTeams)
                    {
                        var fPlayer = cntT.teamPlayers.Where(player => player.id == gClientPlayer.id);
                        if (fPlayer.FirstOrDefault() == null)
                        {
                            gClientTeam.id = cntT.id;
                        }
                        gClientTeam.name = cntT.name;
                    }

                    //Hosts will use their preferences in order to determine the game parameters e.g. number of players, board dimentions
                    //Note that cannot write the number of participants that is used to decide to start the game
                    //this must be initiated from the server sending a message to the client

                    if (gClientPlayer.isHost)
                    {
                        howManyPlaying = PREFS.getPrefInt("MinPlayers");
                        howManyTeams   = PREFS.getPrefInt("NumTeams");
                        howManyCallers = PREFS.getPrefInt("NumCallers");
                        sizeOfXDim     = PREFS.getPrefInt("GridXDim");
                        sizeOfYDim     = PREFS.getPrefInt("GridZDim");
                    }
                    else
                    {
                        howManyPlaying = 0;
                        howManyTeams   = 0;
                        howManyCallers = 0;
                        sizeOfXDim     = 0;
                        sizeOfYDim     = 0;
                    }
                    //Respond the ask for more details about the player
                    //Build the message
                    //Message Header
                    gOutgoingMessage.id   = CS.MESSAGE_CWHO;
                    gOutgoingMessage.name = gOutgoingMessage.id;
                    gOutgoingMessage.type = CS.MESSAGE_REPLY;

                    //Message Sender
                    gOutgoingMessage.sender.id   = gClientPlayer.id;
                    gOutgoingMessage.sender.name = gClientPlayer.name;

                    //Message Details
                    gOutgoingMessage.gameParameters.howManyPlaying = howManyPlaying;
                    gOutgoingMessage.gameParameters.howManyTeams   = howManyTeams;
                    gOutgoingMessage.gameParameters.howManyCallers = howManyCallers;

                    gOutgoingMessage.gameParameters.sizeOfXDim = sizeOfXDim;
                    gOutgoingMessage.gameParameters.sizeOfYDim = sizeOfYDim;

                    gOutgoingMessage.gameTeams.Clear();
                    gOutgoingMessage.gameTeams.Add(gClientTeam);

                    Send(gOutgoingMessage);
                    break;

                case CS.MESSAGE_SCNN:
                    gClientGame.gameParameters = gIncomingMessage.gameParameters;
                    gClientGame.gameTeams      = gIncomingMessage.gameTeams;

                    int cntIncomingPlayers = 0;
                    foreach (var t in gIncomingMessage.gameTeams)
                    {
                        foreach (var p in t.teamPlayers)
                        {
                            cntIncomingPlayers++;
                        }
                    }

                    if ((gClientGame.gameParameters.howManyPlaying == cntIncomingPlayers) && (cntIncomingPlayers != 0))
                    {
                        GameManager.Instance.gridXDim.text = gClientGame.gameParameters.sizeOfXDim.ToString();
                        GameManager.Instance.gridZDim.text = gClientGame.gameParameters.sizeOfYDim.ToString();
                        GameManager.Instance.OpenLobby();
                    }
                    break;

                case CS.MESSAGE_SBEG:
                    //Update Game Data
                    gClientGame.gameParameters = gIncomingMessage.gameParameters;
                    gClientGame.gameTeams      = gIncomingMessage.gameTeams;

                    //Update Client Player Data
                    gClientGame.FindPlayer(gClientPlayer.id, ref gClientPlayer);

                    //Update Client Team Data
                    gClientGame.FindPlayerTeam(gClientPlayer.id, ref gClientTeam);

                    //Start the Game
                    GameManager.Instance.StartGame();
                    break;
                }
            }
        }