Beispiel #1
0
        private void handleGetHostList()
        {
            var newMessage = new ResistileMessage(0, ResistileMessageTypes.hostList, "");

            newMessage.messageArray = new ArrayList(availableHosts.ToArray());
            writeClient(newMessage);
        }
Beispiel #2
0
    public void doAction(ResistileMessage message)
    {
        lock (thisLock)
        {
            messageFromThread = message;
            switch (message.messageCode)
            {
            case ResistileMessageTypes.opponentFound:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.opponentCanceled:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.startGame:
                msgFromThread = message.messageCode;
                break;

            default:
                Debug.Log("Unrecognized Message Type: " + message.messageCode + " --- " + message.message);
                break;
            }
        }
    }
Beispiel #3
0
        private void writeClient(string msg)
        {
            string serverResponse = "Server to clinet(" + clNo + ") " + msg;
            var    message        = new ResistileMessage(0, 0, serverResponse);

            writeClient(message);
        }
Beispiel #4
0
 private void gameOver(ResistileMessage messageFromThread)
 {
     GameHandler.gameHandler.guessScorePanel.SetActive(true);
     GameHandler.gameHandler.endTurn.interactable = false;
     GameHandler.gameHandler.setAllTileDrag(false);
     GameHandler.gameHandler.waitingForOpponent.SetActive(false);
 }
 public bool sendMessage(ResistileMessage message)
 {
     using (StringWriter textWriter = new StringWriter())
     {
         serializer.Serialize(textWriter, message);
         NetworkStream serverStream = dest.GetStream();
         byte[]        outStream    = Encoding.ASCII.GetBytes(textWriter.ToString() + "$");
         try {
             serverStream.Write(outStream, 0, outStream.Length);
             serverStream.Flush();
             return(true);
         }
         catch (Exception e)
         {
             if (!quitGame)
             {
                 SceneManager.LoadScene("LoginScreen");
                 networkManager.searchingForServer = true;
                 DestroyImmediate(gameObject);
                 //DestroyImmediate(GameObject.Find("AudioManager"));
             }
             else
             {
                 if (!Application.isEditor)
                 {
                     System.Diagnostics.Process.GetCurrentProcess().Kill();
                 }
             }
             runNetworkThread = false;
             return(false);
         }
     }
 }
Beispiel #6
0
        private void handleGameLoaded()
        {
            var player                = gameManager.getPlayer(clName);
            var playersOpponent       = gameManager.getOpponent(clName);
            var initializeGameMessage = new ResistileMessage(gameID, ResistileMessageTypes.initializeGame,
                                                             playersOpponent.userName);

            initializeGameMessage.turn = gameManager.currentTurnPlayer == player;
            ArrayList playerHand = new ArrayList();

            foreach (GameTile tile in player.hand)
            {
                playerHand.Add(tile.id);
            }
            initializeGameMessage.PlayerHand = playerHand;


            ArrayList wirehand = new ArrayList();

            foreach (GameTile tile in gameManager.wireHand)
            {
                wirehand.Add(tile.id);
            }
            initializeGameMessage.WireHand            = wirehand;
            initializeGameMessage.PrimaryObjective    = player.primaryObjective;
            initializeGameMessage.secondaryObjectives = new ArrayList(player.secondaryObjective);
            writeClient(initializeGameMessage);
        }
Beispiel #7
0
        private void handleReplay(ResistileMessage message)
        {
            var player         = gameManager.getPlayer(clName);
            var opponent       = gameManager.getOpponent(clName);
            var opponentHandle = handleClients.Find(handle => handle.clName == opponent.userName);

            if (message.replay == false)
            {
                var messageToSend = new ResistileMessage(gameID, ResistileMessageTypes.replay)
                {
                    replay = false
                };
                opponentHandle.writeClient(messageToSend);
            }
            else
            {
                player.replay = message.replay;
                if (player.replay && opponent.replay)
                {
                    //send both replay
                    var messageToSend = new ResistileMessage(gameID, ResistileMessageTypes.replay)
                    {
                        replay = true
                    };
                    writeClient(messageToSend);
                    opponentHandle.writeClient(messageToSend);
                    handleAcceptOpponent(new ResistileMessage(0, ResistileMessageTypes.acceptOpponent, opponent.userName));
                }
            }
        }
Beispiel #8
0
    public void noReplay()
    {
        ResistileMessage message = new ResistileMessage(0, ResistileMessageTypes.replay, "");

        message.replay = false;
        NetworkManager.networkManager.sendMessage(message);
        SceneManager.LoadScene("MainMenu");
    }
Beispiel #9
0
        private void handleDeclineOpponent(ResistileMessage message)
        {
            //in message client name
            var opponentToBeDeclined = handleClients.Find(client => client.clName == message.message);

            opponentToBeDeclined.writeClient(0, ResistileMessageTypes.hostDeclined, clName);
            availableHosts.Add(clName);
        }
Beispiel #10
0
 private void tilePlaced(ResistileMessage message) //Opponent Ends Turn
 {
     GameHandler.gameHandler.placeTile(message.tileID, (int)message.coordinates[0], (int)message.coordinates[1], message.rotation);
     GameHandler.gameHandler.changeTurn();
     GameHandler.gameHandler.setTurn();
     GameHandler.gameHandler.setAllTileDrag(true);
     GameHandler.gameHandler.cleanSolderedBoard();
 }
Beispiel #11
0
    //RECEIVE MESSAGES FROM SERVER
    private void hostList(ResistileMessage message)
    {
        var hosts = message.messageArray;

        foreach (string host in hosts)
        {
            addHost(host);
        }
    }
Beispiel #12
0
    public void replay()
    {
        ResistileMessage message = new ResistileMessage(0, ResistileMessageTypes.replay, "");

        message.replay = true;
        NetworkManager.networkManager.sendMessage(message);
        GameHandler.gameHandler.playAgain.GetComponent <Button>().interactable = false;
        GameHandler.gameHandler.alert.alert("Waiting for Opponent to Accept", 3.0f, false);
    }
Beispiel #13
0
    public void guessResistance()
    {
        GameHandler.gameHandler.submitResButton.GetComponent <Button>().interactable    = false;
        GameHandler.gameHandler.playerResGuess.GetComponent <InputField>().interactable = false;
        ResistileMessage message = new ResistileMessage(0, ResistileMessageTypes.guessResistance, "");

        message.guess = float.Parse(GameHandler.gameHandler.playerResGuess.text);
        NetworkManager.networkManager.sendMessage(message);
    }
Beispiel #14
0
        private void handleAcceptOpponent(ResistileMessage message)
        {
            var opponentToBeAccepted = handleClients.Find(client => client.clName == message.message);

            gameID = generateGameId();
            opponentToBeAccepted.gameID = gameID;
            opponentToBeAccepted.writeClient(gameID, ResistileMessageTypes.startGame, "");
            gameManager = new GameManager(clName, opponentToBeAccepted.clName);
            opponentToBeAccepted.gameManager = gameManager;
        }
Beispiel #15
0
 private void validMove(ResistileMessage message) //You End Turn
 {
     GameHandler.gameHandler.currentTile.GetComponent <Draggable>().enabled = false;
     GameHandler.gameHandler.currentTile.transform.FindChild("RotateButton").gameObject.SetActive(false);
     GameHandler.gameHandler.changeTurn();
     GameHandler.gameHandler.setTurn();
     GameHandler.gameHandler.setAllTileDrag(false);
     GameHandler.gameHandler.removeRotate(GameHandler.gameHandler.currentTile);
     GameHandler.gameHandler.cleanSolderedBoard();
     GameHandler.gameHandler.currentTile = null;
 }
Beispiel #16
0
 private void replay(ResistileMessage message)
 {
     if (message.replay == true)
     {
         SceneManager.LoadScene("Board");
     }
     else
     {
         GameHandler.gameHandler.noPlayAgain();
     }
 }
Beispiel #17
0
 //RECEIVE MESSAGES FROM SERVER
 public void login(ResistileMessage message)
 {
     Debug.Log("Received Login Message, isValid = " + message.turn);
     if (message.turn)
     {
         SceneManager.LoadScene("MainMenu");
     }
     else
     {
         alert.alert("Username Unavailable.", 3.0f, true);
     }
 }
Beispiel #18
0
 //RECEIVE MESSAGES FROM SERVER
 public void login(ResistileMessage message)
 {
     if (message.turn)
     {
         NetworkManager.networkManager.username = gameObject.GetComponent <Settings>().username;
         SceneManager.LoadScene("MainMenu");
     }
     else
     {
         alert.alert("Username Unavailable.", 3.0f, true);
     }
 }
Beispiel #19
0
        private void writeClientOnValidMove(bool isGameOver, bool isSolder, GameTile tile, int rotation, int[] coords, ResistilePlayer player, ResistilePlayer opponent,
                                            GameTile solder)
        {
            //Sent opponent placed tile data
            var opponentHandle = handleClients.Find(handle => handle.clName == opponent.userName);
            var oppMsg1        = new ResistileMessage(gameID, ResistileMessageTypes.tilePlaced);

            oppMsg1.tileID      = tile.id;
            oppMsg1.turn        = true;
            oppMsg1.rotation    = rotation;
            oppMsg1.coordinates = new ArrayList(coords);
            opponentHandle.writeClient(oppMsg1);

            writeClient(gameID, ResistileMessageTypes.validMove, "");
            if (isGameOver)
            {
                writeClient(gameID, ResistileMessageTypes.gameOver);
                opponentHandle.writeClient(gameID, ResistileMessageTypes.gameOver);

                gameManager.checkEndGameSecondaryObjectives(player);
                gameManager.checkEndGameSecondaryObjectives(opponent);
                gameManager.calculateResistance();
                gameManager.checkEndGamePrimaryObjective(player, opponent);
            }
            else
            {
                Thread.Sleep(100);
                //Sent player new tile data
                var messageToBeSent = new ResistileMessage(gameID, ResistileMessageTypes.drawTile);
                var card            = gameManager.draw(tile, player);
                messageToBeSent.turn   = false;
                messageToBeSent.tileID = card.id;
                writeClient(messageToBeSent);

                Thread.Sleep(100);
                if (card.type.Contains("Wire"))
                {
                    var oppMsg2 = new ResistileMessage(gameID, ResistileMessageTypes.drawTile);
                    oppMsg2.turn   = true;
                    oppMsg2.tileID = card.id;
                    opponentHandle.writeClient(oppMsg2);
                }
                if (isSolder)
                {
                    var messageToBeSent2 = new ResistileMessage(gameID, ResistileMessageTypes.drawTile);
                    var cardID2          = gameManager.draw(solder, player);
                    messageToBeSent2.tileID = cardID2.id;
                    messageToBeSent2.turn   = false;
                    writeClient(messageToBeSent2);
                }
            }
        }
Beispiel #20
0
        private void handleEndTurn(ResistileMessage message)
        {
            //if invalid move MessageType is ResistileMessageTypes.invalidMove
            //if valid move MessageType is drawTile to this client, tileplaced to other client. to be written....
            var tile = gameManager.deck.allTiles[message.tileID];
            var tileToBeCheckedClone = tile.Clone();

            for (int i = 0; i < message.rotation; i++)
            {
                tileToBeCheckedClone.Rotate();
            }
            //check if solder placed
            bool validMove = false;
            bool isSolder  = message.solderId > 0;

            int[] coords = (int[])message.coordinates.ToArray(typeof(int));

            if (isSolder) //soldermove
            {
                validMove = gameManager.board.IsValidSolder(tileToBeCheckedClone, coords);
            }
            else
            {
                validMove = gameManager.board.IsValidMove(tileToBeCheckedClone, coords);
            }

            if (!validMove)
            {
                writeClient(gameID, ResistileMessageTypes.invalidMove, "");
            }
            else
            {
                var player   = gameManager.getPlayer(clName);
                var opponent = gameManager.getOpponent(clName);
                tile = tileToBeCheckedClone;
                GameTile solder;
                bool     isGameOver;

                if (isSolder)
                {
                    //AddTileWithSolder
                    solder     = gameManager.deck.allTiles[message.solderId];
                    isGameOver = gameManager.AddTileWithSolder(player, tile, solder, coords);
                    writeClientOnValidMove(isGameOver, isSolder, tile, message.rotation, coords, player, opponent, solder);
                }
                else
                {
                    isGameOver = gameManager.AddTile(player, tile, coords);
                    writeClientOnValidMove(isGameOver, isSolder, tile, message.rotation, coords, player, opponent, null);
                }
            }
        }
    public void doAction(ResistileMessage message)
    {
        switch (message.messageCode)
        {
        case ResistileMessageTypes.ping:
            Debug.Log("Received Ping From Server");
            break;

        default:
            Debug.Log("Unrecognized Message Type: " + message.messageCode + " --- " + message.message);
            break;
        }
    }
Beispiel #22
0
        private void handleRequestJoinGame(ResistileMessage message)
        {
            var theHost = handleClients.Find(client => client.clName == message.message);

            if (theHost != null)
            {
                theHost.writeClient(0, ResistileMessageTypes.opponentFound, this.clName);
            }
            else
            {
                writeClient(0, ResistileMessageTypes.hostNotFound, message.message);
            }
            availableHosts.Remove(theHost.clName);
        }
Beispiel #23
0
    public void doAction(ResistileMessage message)
    {
        messageFromThread = message;
        lock (thisLock)
        {
            switch (message.messageCode)
            {
            case ResistileMessageTypes.initializeGame:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.tilePlaced:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.drawTile:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.validMove:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.invalidMove:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.gameOver:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.gameResults:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.replay:
                msgFromThread = message.messageCode;
                break;

            case ResistileMessageTypes.opponentQuit:
                msgFromThread = message.messageCode;
                break;

            default:
                Debug.Log("Unrecognized Message Type: " + message.messageCode + " --- " + message.message);
                break;
            }
        }
    }
Beispiel #24
0
 private void writeClient(ResistileMessage msg)
 {
     lock (thisLock2)
     {
         using (StringWriter textWriter = new StringWriter())
         {
             NetworkStream serverStream = clientSocket.GetStream();
             serializer.Serialize(textWriter, msg);
             var    line      = textWriter.ToString();
             byte[] outStream = Encoding.ASCII.GetBytes(line + "$");
             serverStream.Write(outStream, 0, outStream.Length);
             serverStream.Flush();
         }
     }
 }
Beispiel #25
0
        private void handleLogin(ResistileMessage message)
        {
            ResistileMessage messageToSend = new ResistileMessage(gameID, ResistileMessageTypes.login);

            // create a player object with username
            if (handleClients.Exists(handle => handle.clName == message.message))
            {
                messageToSend.turn = false;
            }
            else
            {
                messageToSend.turn = true;
                clName             = message.message;
            }
            writeClient(messageToSend);
        }
Beispiel #26
0
    public void doAction(ResistileMessage message)
    {
        messageFromThread = message;
        lock (thisLock)
        {
            switch (message.messageCode)
            {
            case (ResistileMessageTypes.login):
                msgFromThread = message.messageCode;
                break;

            default:
                Debug.Log("Unrecognized Message Type: " + message.messageCode + " --- " + message.message);
                break;
            }
        }
    }
Beispiel #27
0
        private void handleGuessResistance(ResistileMessage message)
        {
            var player = gameManager.getPlayer(clName);

            player.setGuess(message.guess);
            gameManager.checkGuessAchieved(player);
            if (gameManager.getPlayer(clName).guessed&& gameManager.getOpponent(clName).guessed)
            {
                //TODO: program this
                //wait two players to come here,
                //then calculate primary based on who is closest,
                //secondaries
                //the winner based on most score gained
                //then send both the message
                var opponent = gameManager.getOpponent(clName);

                gameManager.calculateWhoWon(player, opponent);


                var sendThisMessage = new ResistileMessage(gameID, ResistileMessageTypes.gameResults);
                sendThisMessage.messageArray = new ArrayList();
                sendThisMessage.messageArray.Add(player.primaryAchieved);       //primaryScore bool
                var playerSecondaryObjectives = player.getSecondaryObjectiveResults();
                sendThisMessage.messageArray.Add(playerSecondaryObjectives[0]); //secondaryObj1 bool
                sendThisMessage.messageArray.Add(playerSecondaryObjectives[1]); // secondaryObj2 bool
                sendThisMessage.messageArray.Add(player.guessAchieved);         // guessScore bool
                sendThisMessage.win = player.won;
                writeClient(sendThisMessage);


                sendThisMessage = new ResistileMessage(gameID, ResistileMessageTypes.gameResults);
                var opponentHandle = handleClients.Find(handles => handles.clName == gameManager.getOpponent(clName).userName);
                sendThisMessage.messageArray = new ArrayList();
                sendThisMessage.messageArray.Add(opponent.primaryAchieved);       //primaryScore bool
                var opponentSecondaryObjectives = opponent.getSecondaryObjectiveResults();
                sendThisMessage.messageArray.Add(opponentSecondaryObjectives[0]); //secondaryObj1 bool
                sendThisMessage.messageArray.Add(opponentSecondaryObjectives[1]); // secondaryObj2 bool
                sendThisMessage.messageArray.Add(opponent.guessAchieved);         // guessScore bool
                sendThisMessage.win = opponent.won;
                opponentHandle.writeClient(sendThisMessage);
            }
            //var resistance = gameManager.calculateResistance();
            //var guess = message.guess;
        }
Beispiel #28
0
 private void invalidMove(ResistileMessage message)
 {
     GameHandler.gameHandler.alert.alert("Invalid Move, Please Select a Valid Placement", 2.0f, true);
     if (GameHandler.gameHandler.currentTile.GetComponent <TileData>().type.Contains("Resistor"))
     {
         GameHandler.gameHandler.currentTile.transform.SetParent(GameHandler.gameHandler.resHand.transform, false);
     }
     else if (GameHandler.gameHandler.currentTile.GetComponent <TileData>().type.Contains("Wire"))
     {
         GameHandler.gameHandler.currentTile.transform.SetParent(GameHandler.gameHandler.wireHand.transform, false);
     }
     if (GameHandler.gameHandler.solderTile != null)
     {
         GameHandler.gameHandler.solderTile.transform.SetParent(GameHandler.gameHandler.resHand.transform, false);
     }
     GameHandler.gameHandler.solder      = null;
     GameHandler.gameHandler.currentTile = null;
     GameHandler.gameHandler.setAllTileDrag(true);
 }
Beispiel #29
0
    public void endTurn()
    {
        ResistileMessage message = new ResistileMessage(GameHandler.gameHandler.gameID, ResistileMessageTypes.endTurn, "");

        if (GameHandler.gameHandler.solderTile != null)  //If solder was placed
        {
            TileData solderTile = GameHandler.gameHandler.solderTile.GetComponent <TileData>();
            message.solderId = solderTile.tileID;
            message.message  = ResistileServer.GameTileTypes.solder;
        }
        else
        {
            message.solderId = 0;
        }
        message.tileID   = GameHandler.gameHandler.currentTile.GetComponent <TileData>().tileID;
        message.rotation = GameHandler.gameHandler.currentTile.GetComponent <TileData>().rotation;
        GameObject parent = GameHandler.gameHandler.currentTile.transform.parent.gameObject;

        message.coordinates = new ArrayList(BoardHandler.CoordinatesOf(parent));
        NetworkManager.networkManager.sendMessage(message);
        GameHandler.gameHandler.currentTile.GetComponent <Draggable>().enabled = false;
    }
Beispiel #30
0
    private void gameResults(ResistileMessage message)
    {
        int pScore = 0, s1Score = 0, s2Score = 0, gScore = 0, tScore = 0;

        if ((bool)message.messageArray[0])
        {
            pScore = 2;
        }
        if ((bool)message.messageArray[1])
        {
            s1Score = 1;
        }
        if ((bool)message.messageArray[2])
        {
            s2Score = 1;
        }
        if ((bool)message.messageArray[3])
        {
            gScore = 1;
        }
        tScore = pScore + s1Score + s2Score + gScore;
        GameHandler.gameHandler.gameOver(message.win, pScore, s1Score, s2Score, gScore, tScore);
    }