public void serializer_defined_but_not_for_test_message_Serialize_throws_MissingMessageSerializerException()
        {
            var messageSerialization = new MessageSerialization();
            messageSerialization.RegisterSerializer(0, new TestMessageSerializer());

            Assert.Throws<MissingMessageSerializerException>(() => messageSerialization.Serialize(new TestMessage()));
        }
        public void serializer_defined_for_message_with_MessageTypeId_65535_Serialize_returns_serialized_object()
        {
            var messageSerialization = new MessageSerialization();
            messageSerialization.RegisterSerializer(CustomMessage.TypeId, new TestMessageSerializer());

            Assert.AreEqual(Encoding.UTF8.GetBytes("abc"), messageSerialization.Serialize(new CustomMessage { Text = "abc" }));
        }
        public void serializer_defined_but_not_for_test_message_Serialize_throws_MissingMessageSerializerException()
        {
            var messageSerialization = new MessageSerialization();

            messageSerialization.RegisterSerializer(0, new TestMessageSerializer());

            Assert.Throws <MissingMessageSerializerException>(() => messageSerialization.Serialize(new TestMessage()));
        }
        public void default_serializer_defined_but_no_specific_message_serializer_for_test_message_use_default_serializer()
        {
            var messageSerialization = new MessageSerialization
            {
                DefaultSerializer = new TestMessageSerializer()
            };

            Assert.AreEqual(Encoding.UTF8.GetBytes("abc"), messageSerialization.Serialize(new TestMessage { Text = "abc" }));
        }
        public void serializer_defined_for_message_with_MessageTypeId_65535_Serialize_returns_serialized_object()
        {
            var messageSerialization = new MessageSerialization();

            messageSerialization.RegisterSerializer(CustomMessage.TypeId, new TestMessageSerializer());

            Assert.AreEqual(Encoding.UTF8.GetBytes("abc"), messageSerialization.Serialize(new CustomMessage {
                Text = "abc"
            }));
        }
        public void default_serializer_defined_but_no_specific_message_serializer_for_test_message_use_default_serializer()
        {
            var messageSerialization = new MessageSerialization
            {
                DefaultSerializer = new TestMessageSerializer()
            };

            Assert.AreEqual(Encoding.UTF8.GetBytes("abc"), messageSerialization.Serialize(new TestMessage {
                Text = "abc"
            }));
        }
        public void MessageSerializationException_should_be_thrown_on_exception_while_serializing_message()
        {
            var messageSerialization = new MessageSerialization();

            messageSerialization.RegisterSerializer(ExceptionTestMessage.TypeId, new ExceptionTestMessageSerializer());

            var messageThatCausesExceptionOnSerialization = new ExceptionTestMessage(true, false);
            var exception = Assert.Throws <MessageSerializationException>(() => messageSerialization.Serialize(messageThatCausesExceptionOnSerialization));

            Assert.IsInstanceOf <TestException>(exception.InnerException);
        }
 public void no_serializer_defined_Serialize_throws_MissingMessageSerializerException()
 {
     var messageSerialization = new MessageSerialization();
     Assert.Throws<MissingMessageSerializerException>(() => messageSerialization.Serialize(new TestMessage()));
 }
        public void MessageDeserializationException_should_be_thrown_on_exception_while_deserializing_message()
        {
            var messageSerialization = new MessageSerialization();
            messageSerialization.RegisterSerializer(ExceptionTestMessage.TypeId, new ExceptionTestMessageSerializer());
            messageSerialization.RegisterDeserializer(ExceptionTestMessage.TypeId, new ExceptionTestMessageDeserializer());

            var messageThatCausesExceptionOnDeserialization = new ExceptionTestMessage(false, true);
            var message = messageSerialization.Serialize(messageThatCausesExceptionOnDeserialization);
            var exception = Assert.Throws<MessageDeserializationException>(() => messageSerialization.Deserialize(ExceptionTestMessage.TypeId, message));
            Assert.IsInstanceOf<TestException>(exception.InnerException);
        }
        public void no_serializer_defined_Serialize_throws_MissingMessageSerializerException()
        {
            var messageSerialization = new MessageSerialization();

            Assert.Throws <MissingMessageSerializerException>(() => messageSerialization.Serialize(new TestMessage()));
        }
Ejemplo n.º 11
0
    public void GameChannelSetup(string gameID)
    {
        gameChannel = socket.MakeChannel(string.Format("game:{0}", gameID));

        gameChannel.On(Message.InBoundEvent.phx_reply, data =>
        {
            if (boardsDrawn)
            {
                return;
            }
            // Debug.Log("On phx_reply" + MessageSerialization.Serialize(data));
            var game = data.payload["response"]["game"];

            if (GameManager.GetInstance().isWatching)
            {
                int[] pieceVal = data.payload["response"]["game"]["piece_values"].ToObject <int[]>();
                Tile newTile   = GameManager.GetInstance().SpawnNewTile(pieceVal, tileSpawnTrans.position, tileSpawnTrans, true);
                newTile.transform.DOMove(tilePlaceTrans.position, 0.5f);
            }
            gameUI.timeLimit       = (int)game["time_limit"];
            gameUI.timingText.text = (string)game["time_limit"];
            DrawBoards(game);
            boardsDrawn = true;
        });

        gameChannel.On("game:new_piece", data =>
        {
            // Debug.Log(string.Format("------on new_piece------ {0}", MessageSerialization.Serialize(data)));
            var piece      = data.payload["piece"];
            int[] pieceVal = piece.ToObject <int[]>();
            // SoundManager.GetInstance().MakeClickSound();
            EventManager.GetInstance().PostNotification(EVENT_TYPE.NEW_PIECE, this, pieceVal);
            Tile newTile = GameManager.GetInstance().SpawnNewTile(pieceVal, tileSpawnTrans.position, tileSpawnTrans, true);
            newTile.transform.DOMove(tilePlaceTrans.position, 0.5f);
            newTile.startingPosition = tilePlaceTrans.position;
            if (!GameManager.GetInstance().isWatching)
            {
                newTile.transform.tag = Constants.MOVABLE_TAG;
            }
        });

        gameChannel.On("game:player_joined", data =>
        {
            Debug.Log(string.Format("------on player_joined------ {0}", MessageSerialization.Serialize(data)));
            var game         = data.payload["game"];
            gameUI.timeLimit = (int)game["time_limit"];
            DrawBoards(game);
        });
        gameChannel.On("game:stopped", data =>
        {
            // Debug.Log("------on game_stopped------" + MessageSerialization.Serialize(data));
            EventManager.GetInstance().PostNotification(EVENT_TYPE.GAMEOVER);
        });
        gameChannel.On("game:player_left", data =>
        {
            Debug.Log(MessageSerialization.Serialize(data));
            EventManager.GetInstance().PostNotification(EVENT_TYPE.PLAYER_LEFT);
        });
        gameChannel.On("game:over", data =>
        {
            Debug.Log(MessageSerialization.Serialize(data));
            EventManager.GetInstance().PostNotification(EVENT_TYPE.GAMEOVER);
        });
        gameChannel.On("game:place_piece", data =>
        {
            var game         = data.payload["game"];;
            var piece        = data.payload["piece"];
            string[] players = game["players"].ToObject <string[]>();
            string player_nick;

            // Debug.Log(string.Format("------on place_piece----- {0}", MessageSerialization.Serialize(data)));
            if (!initGrids && GameManager.GetInstance().isWatching)
            {
                Debug.Log("Connection on watching");
                gameUI.timeLimit      = (int)game["time_limit"];
                string[] player_nicks = game["player_nicks"].ToObject <string[]>();
                var playerScores      = new Dictionary <int, KeyValuePair <string, string> >();
                for (int i = 0; i < players.Length; i++)
                {
                    string player_id = players[i];
                    player_nick      = player_nicks[i];
                    int point        = game["points"][player_id].ToObject <int>();
                    playerScores[i]  = new KeyValuePair <string, string>(player_nick, (string)game["points"][player_id]);
                    string game_id   = game["id"].ToObject <string>();
                    Dictionary <string, int> grid = game["player_boards"][player_id]["grid"].ToObject <Dictionary <string, int> >();
                    GridState state = new GridState(
                        player_id, player_nick, point, game_id, grid
                        );
                    gameController.UpdateGrid(player_id, state);
                }
                EventManager.GetInstance().PostNotification(EVENT_TYPE.SCORE_CHANGE, null, playerScores);
                initGrids = true;
                return;
            }

            string sender = (string)data.payload["player_id"];
            int index     = Array.IndexOf(players, sender);
            player_nick   = (string)game["player_boards"][sender]["nick"];
            Dictionary <string, int> gridData = game["player_boards"][sender]["grid"].ToObject <Dictionary <string, int> >();
            string pointStr = (string)game["points"][sender];
            var playerScore = new KeyValuePair <string, string>(player_nick, pointStr);
            var param       = new Dictionary <int, KeyValuePair <string, string> > {
                { index, playerScore }
            };
            EventManager.GetInstance().PostNotification(EVENT_TYPE.SCORE_CHANGE, null, param);
            StartCoroutine(gameController.UpdateGridData(sender, gridData, piece));
        });
    }