public bool JoinGame(GameJoin gj)
        {
            clog("Told to join gameId: " + gj.gameId + ", side: " + gj.side);
            if (gj.gameId == "")
            {
                clog("Bad gameId!");
                return(false);
            }

            networkManager.gameSession.gameId          = gj.gameId;
            networkManager.gameSession.side            = gj.side;
            networkManager.gameSession.uuidOtherPlayer = gj.uuidOtherPlayer;
            networkManager.gameSession.status          = STATUS.JOINED;

            networkManager.gameSession.currentGs = new GameState();
            var gs = networkManager.gameSession.currentGs;

            gs.currentPlayer = networkManager.gameSession.uuidPlayer;

            // Update Ball:
            if (gs.balls.Length == 0)
            {
                gs.balls = new Ball[1];
            }
            BallControl bc = theBall.GetComponent <BallControl>();

            bc.uuid     = gj.ballId; // Add uuid to ball.
            gs.balls[0] = Ball.CopyBall(bc);

            // Match Assignments:
            // Given a side, 0 one is player one (left). 1 other player (right)
            PlayerControls left  = null;
            PlayerControls right = null;

            if (players.Length != 2)
            {
                return(false); // Can't join this game.
            }

            foreach (GameObject g in players)
            {
                if (left == null)
                {
                    left = g.GetComponent <PlayerControls>();
                    continue;
                }

                right = g.GetComponent <PlayerControls>();
                if (right.transform.position.x < left.transform.position.x)
                {
                    var tmp = left;
                    left  = right;
                    right = tmp;
                }
            }

            clog("Left sel: " + left.transform.position.x + "Right other: " + right.transform.position.x);

            if (networkManager.gameSession.side == 0)
            {
                left.uuid      = networkManager.gameSession.uuidPlayer; // Player 1 assigned in match by server.
                left.ownPlayer = true;

                right.uuid      = networkManager.gameSession.uuidOtherPlayer;
                right.ownPlayer = false;
                gs.players[0]   = Player.CopyPlayer(left);
                gs.players[1]   = Player.CopyPlayer(right);
            }
            else if (networkManager.gameSession.side == 1)
            {
                right.uuid      = networkManager.gameSession.uuidPlayer; // Player 2 assigned in match by server.
                right.ownPlayer = true;

                left.uuid      = networkManager.gameSession.uuidOtherPlayer;
                left.ownPlayer = false;

                gs.players[0] = Player.CopyPlayer(right);
                gs.players[1] = Player.CopyPlayer(left);
            }

            // Assign player state:
            networkManager.gameSession.currentGs = gs;

            clog("Transition to inGame.");
            networkManager.gameSession.status = STATUS.INGAME;
            return(true);
        }
Ejemplo n.º 2
0
        // Match whatever WebSocket text is sending
        // Consistency: General rule here is that the game state if not timestamped, events may not represent the same time window.
        void HandleMessage(string message)
        {
            var msg = MessageWrapper.UnWrapMessage(message);

            // Not quite symetric, but the server is text only.
            switch (msg.type)
            {
            case "qotd":
                break;

            case "notification":
                Notification notification = Messaging <Notification> .Deserialize(message);

                gameManager.clog(notification.notificationText);
                break;

            case "register":
                GameRegister register = Messaging <GameRegister> .Deserialize(message);

                gameSession.sessionId  = register.sessionId;
                gameSession.uuidPlayer = register.uuidPlayer;
                break;

            case "gameJoin":
                GameJoin gj = Messaging <GameJoin> .Deserialize(message);

                gameManager.JoinGame(gj);
                break;

            case "scoreEvent":
                ScoreEvent se = Messaging <ScoreEvent> .Deserialize(message);

                gameManager.UpdateScore(se);
                break;

            case "moveEvent":
                MoveEvent me = Messaging <MoveEvent> .Deserialize(message);

                gameManager.UpdatePosition(me);
                break;

            case "gameState":
                GameState serverGs = Messaging <GameState> .Deserialize(message);

                gameSession.currentGs.sequence = serverGs.sequence;
                //UpdateLocalGame(serverGs);
                break;

            case "contactEvent":
                ContactEvent ce = Messaging <ContactEvent> .Deserialize(message);

                gameManager.HandleContactEvent(ce);
                break;

            case "resign":
                GameResign resign = Messaging <GameResign> .Deserialize(message);

                gameManager.theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
                gameManager.ghostBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
                break;

            case "nextRound":
                NextRound nr = Messaging <NextRound> .Deserialize(message);

                gameManager.StartNextRound(nr);
                break;

            case "gameRestart":
                GameRestart gr = Messaging <GameRestart> .Deserialize(message);

                gameManager.RestartGame(gr);
                break;

            default:
                gameManager.clog("Unknown message arrived: " + msg.type + ", message: " + message);
                break;
            }
        }