예제 #1
0
        //
        public TomeValue(JValue value, JToken root) : base(value)
        {
            //
            this.root = root;
            if (this.root == null)
            {
                this.root = this;
            }

            //
            OnChanged += EmitToParents;
        }
예제 #2
0
        //
        public void Destroy()
        {
            lock ((object)this)
            {
                if (OnDestroy != null)
                {
                    OnDestroy.Invoke();
                }

                OnChanged = null;
                OnDestroy = null;
            }
        }
예제 #3
0
        //
        public TomeArray(JArray data, JToken root)
        {
            //
            this.root = root;
            if (this.root == null)
            {
                this.root = this;
            }

            //
            for (var i = 0; i < data.Count; i += 1)
            {
                Add(Tome.Conjure(data[i], this.root));
            }

            //
            OnChanged += EmitToParents;
            OnAdd     += EmitChanged;
            OnDel     += EmitChanged;
        }
예제 #4
0
        //
        public void Destroy()
        {
            lock ((object)this)
            {
                foreach (JToken value in this)
                {
                    Tome.Destroy(value);
                }

                if (OnDestroy != null)
                {
                    OnDestroy.Invoke();
                }

                OnChanged = null;
                OnDestroy = null;
                OnAdd     = null;
                OnDel     = null;
            }
        }
예제 #5
0
        //
        public TomeObject(JObject data, JToken root)
        {
            //
            this.root = root;
            if (this.root == null)
            {
                this.root = this;
            }

            //
            foreach (JProperty property in data.Properties())
            {
                Add(property.Name, Tome.Conjure(property.Value, this.root));
            }

            //
            OnChanged += EmitToParents;
            OnAdd     += EmitChanged;
            OnDel     += EmitChanged;
        }
예제 #6
0
        //
        public void Destroy()
        {
            lock ((object)this)
            {
                foreach (KeyValuePair <string, JToken> property in this)
                {
                    Tome.Destroy(property.Value);
                }

                if (OnDestroy != null)
                {
                    OnDestroy.Invoke();
                }

                OnChanged = null;
                OnDestroy = null;
                OnAdd     = null;
                OnDel     = null;
            }
        }
예제 #7
0
        //
        public void Assign(JToken newValue)
        {
            lock ((object)this)
            {
                switch (newValue.Type)
                {
                case JTokenType.Array:
                    var newTomeArray = new TomeArray((JArray)newValue, root);
                    Replace(newTomeArray);

                    if (Parent == null)
                    {
                        // If replace was successfuly move over event handlers and call new OnChanged handler
                        // The instance in which replace would not be successful, is when the old and new values are the same
                        OnChanged -= EmitToParents;
                        OnChanged += newTomeArray.OnChanged;
                        newTomeArray.OnChanged = OnChanged;
                        newTomeArray.OnDestroy = OnDestroy;
                        OnAdd -= EmitChanged;
                        OnAdd += newTomeArray.OnAdd;
                        newTomeArray.OnAdd = OnAdd;
                        OnDel -= EmitChanged;
                        OnDel += newTomeArray.OnDel;
                        newTomeArray.OnDel = OnDel;

                        if (newTomeArray.OnChanged != null)
                        {
                            newTomeArray.OnChanged.Invoke(null);
                        }
                    }
                    else
                    {
                        // Otherwise call original OnChanged handler
                        if (OnChanged != null)
                        {
                            OnChanged.Invoke(null);
                        }
                    }
                    break;

                case JTokenType.Object:
                    var newTomeObject = new TomeObject((JObject)newValue, root);
                    Replace(newTomeObject);

                    if (Parent == null)
                    {
                        // If replace was successfuly move over event handlers and call new OnChanged handler
                        // The instance in which replace would not be successful, is when the old and new values are the same
                        OnChanged -= EmitToParents;
                        OnChanged += newTomeObject.OnChanged;
                        newTomeObject.OnChanged = OnChanged;
                        newTomeObject.OnDestroy = OnDestroy;
                        OnAdd -= EmitChanged;
                        OnAdd += newTomeObject.OnAdd;
                        newTomeObject.OnAdd = OnAdd;
                        OnDel -= EmitChanged;
                        OnDel += newTomeObject.OnDel;
                        newTomeObject.OnDel = OnDel;

                        if (newTomeObject.OnChanged != null)
                        {
                            newTomeObject.OnChanged.Invoke(null);
                        }
                    }
                    else
                    {
                        // Otherwise call original OnChanged handler
                        if (OnChanged != null)
                        {
                            OnChanged.Invoke(null);
                        }
                    }
                    break;

                default:
                    var newTomeValue = new TomeValue((JValue)newValue, root);
                    Replace(newTomeValue);

                    if (Parent == null)
                    {
                        // If replace was successfuly move over event handlers and call new OnChanged handler
                        // The instance in which replace would not be successful, is when the old and new values are the same
                        OnChanged -= EmitToParents;
                        OnChanged += newTomeValue.OnChanged;
                        newTomeValue.OnChanged = OnChanged;
                        newTomeValue.OnDestroy = OnDestroy;

                        if (newTomeValue.OnChanged != null)
                        {
                            newTomeValue.OnChanged.Invoke(null);
                        }
                    }
                    else
                    {
                        // Otherwise call original OnChanged handler
                        if (OnChanged != null)
                        {
                            OnChanged.Invoke(null);
                        }
                    }
                    break;
                }
            }
        }
예제 #8
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);
            }));
        }));
    }