Example #1
0
    public void Setup(Action <Exception> cb)
    {
        // Catch archivist set events for new game creation. We use this to know
        // when a new game has been created for this player.
        mage.archivist.on("game:set", (object sender, VaultValue vaultValue) => {
            if ((string)vaultValue.data["type"] == "cpu")
            {
                currentGameId = (string)vaultValue.index["gameId"];
                logger.debug("Got new game event " + currentGameId);
                TaskManagerMainThread.Queue(onNewGame(currentGameId));
            }
        });

        cb(null);
    }
Example #2
0
    // Called when a game match has been found and the handshake sequence should be
    // initiated.
    private IEnumerator onNewGame(string gameId)
    {
        // Make sure we're on the queue screen
        if (Application.loadedLevelName != "MultiplayView")
        {
            logger.error("Got a new game object when we're not on the queue screen");
            yield break;
        }

        // Make sure we are in the queue
        if (handshakeCompletedCb == null)
        {
            logger.error("Got a new game object but have not entered the queue");
            yield break;
        }

        // Make sure we don't have a game started already
        if (game.tCurrentGame != null)
        {
            logger.error("Got a new game object when a game has already been initialized");
            yield break;
        }

        // Setup the current game object and accept the invitation
        logger.debug("Setting up new game");
        new Task(game.SetupCurrentGame(gameId, (Exception setupError) => {
            if (setupError != null)
            {
                finalCallback(setupError);
                return;
            }

            // Setup timeout for final handshake completion. The handshake should
            // take a matter of milliseconds to complete. If it doesn't then the
            // player has gone so we can re-join the queue.
            handshakeTimer = new Timer((object state) => {
                handshakeTimer = null;
                logger.warning("Handshake time out");
                TaskManagerMainThread.Queue(playerReadyFailed());
            }, null, handshakeCompleteTimeout, Timeout.Infinite);

            // Accept the handshake on our end
            logger.debug("Successfully setup new game");
            new Task(accept(gameId, (Exception acceptError) => {
                if (acceptError != null)
                {
                    handshakeTimer.Dispose();
                    handshakeTimer = null;
                    finalCallback(acceptError);
                    return;
                }

                // Determine opponent userId
                string enemyUserId = (string)game.tCurrentGame["players"][0];
                if (enemyUserId == GameSettings.UserId)
                {
                    enemyUserId = (string)game.tCurrentGame["players"][1];
                }

                // Check if opponent has accepted, otherwise wait for it
                Tome.OnChanged checkOpponentReady = (JToken oldValue) => {
                    if ((bool)game.tCurrentGame["playerReady"][enemyUserId] && handshakeTimer != null)
                    {
                        logger.debug("Both players accepted game");
                        handshakeTimer.Dispose();
                        handshakeTimer = null;

                        TaskManagerMainThread.Queue(onPlayerReady());
                    }
                };

                logger.debug("Successfully accepted game match");
                (game.tCurrentGame["playerReady"] as TomeObject).onChanged += checkOpponentReady;
                checkOpponentReady(null);
            }));
        }));
    }