Example #1
0
 public void OnlyShare(SerializableCommand sCommand)
 {
     if (Game1.networkType == Game1.NetworkType.Online && sCommand.shouldShare)
     {
         client.Share(sCommand);
     }
 }
Example #2
0
 public void OnReceive(byte[] data)
 {
     if (Encoding.ASCII.GetString(data).StartsWith(lobbyHostString))
     {
         lobbyHostReceived = true;
         Console.WriteLine("received lobby host package");
         NotifyObservers();
     }
     else if (Encoding.ASCII.GetString(data).StartsWith(upToDateString))
     {
         upToDateReceived = true;
         Console.WriteLine("im up to date");
         StartSendQueue();
         NotifyObservers();
     }
     else
     {
         SerializableCommand input = Serializer.Deserialize(data);
         if (input != null)
         {
             Console.WriteLine("received " + input.typeName);
             NotifyObservers(input);
         }
     }
 }
Example #3
0
 public void HandleInput(SerializableCommand sCommand)
 {
     // sharing before handling
     // so that if handle generates more commands, correct order is kept
     OnlyShare(sCommand);
     CommandHandler.Handle(sCommand);
 }
        public void TestExecute()
        {
            var testClass = new TestClass();
            var command   = new SerializableCommand <TestClass>(testClass, t => t.IncreaseNumber());

            command.Execute();
            Assert.AreEqual(1, testClass.number);
        }
Example #5
0
 public static byte[] SerializeInput(SerializableCommand input)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         new BinaryFormatter().Serialize(stream, input);
         return(stream.ToArray());
     }
 }
Example #6
0
        public void TestOperateOnState()
        {
            var gameStateChanger = new GameStateChanger();
            var increaseNumberOfEnemiesCommand = new SerializableCommand <GameStateChanger>
                                                     (gameStateChanger, changer => changer.IncreaseNumberOfEnemies());

            increaseNumberOfEnemiesCommand.Execute();

            Assert.AreEqual(1, TestGameState.Instance.numberOfEnemies);
            Assert.AreEqual(0, TestGameState.Instance.numberOfPlayers);
        }
Example #7
0
    public static void Handle(SerializableCommand seriCommand)
    {
        GenerateFromInput inputFunc = null;

        switch (seriCommand.typeName)
        {
        // have to use explicit names and functions
        // because static members cannot be inherited or marked as abstract
        case MoveMBECommand.name:
            inputFunc = MoveMBECommand.FromInput;
            break;

        case MoveGMCommand.name:
            inputFunc = MoveGMCommand.FromInput;
            break;

        case MoveQBECommand.name:
            inputFunc = MoveQBECommand.FromInput;
            break;

        case CreateGhostPlayerCommand.name:
            inputFunc = CreateGhostPlayerCommand.FromSerializable;
            break;

        case ColorRequestedCommand.name:
            inputFunc = ColorRequestedCommand.FromSerializable;
            break;

        case ColorClaimedCommand.name:
            inputFunc = ColorClaimedCommand.FromSerializable;
            break;

        case GameOverCommand.name:
            inputFunc = GameOverCommand.FromSerializable;
            break;

        case RemoveGhostPlayerCommand.name:
            inputFunc = RemoveGhostPlayerCommand.FromSerializable;
            break;

        case UnClaimColorCommand.name:
            inputFunc = UnClaimColorCommand.FromSerializable;
            break;
        }

        if (inputFunc != null)
        {
            CommandQueue.Queue(inputFunc(seriCommand));
        }
    }
Example #8
0
    public static MoveGMCommand FromInput(SerializableCommand sCommand)
    {
        try
        {
            GhostMeeple movingEl             = (GhostMeeple)Board.Instance().FindByUID(sCommand.UID);
            PyramidFloorBoardElement floorEl = (PyramidFloorBoardElement)Board.Instance().FindByUID(sCommand.body);

            return(new MoveGMCommand(movingEl, floorEl));
        }
        catch (InvalidCastException ice)
        {
            Console.WriteLine(ice.Message);
            return(null);
        }
    }
Example #9
0
 public void NotifyObservers(SerializableCommand input)
 {
     // not using foreach, since observers can remove themselves on execution
     // and modify observers list
     for (int i = observers.Count - 1; i >= 0; i--)
     {
         IObserver o = observers[i];
         if (o is IInputObserver)
         {
             ((IInputObserver)o).Update(input);
         }
         else
         {
             o.Update();
         }
     }
 }
Example #10
0
        public void TestSerialization()
        {
            var listOfCommands = new List <ICommand>();

            var methodGameStateChanger = new MethodGameStateChanger();
            var setPlayersAndEnemies   = TestHelper.GeneratePlayerAndEnemiesCommand();

            listOfCommands.Add(setPlayersAndEnemies);

            var gameStateChanger        = new GameStateChanger();
            var increaseNumberOfEnemies = new SerializableCommand <GameStateChanger>
                                              (gameStateChanger, changer => changer.IncreaseNumberOfEnemies());

            listOfCommands.Add(increaseNumberOfEnemies);


            byte[] bytes = new byte[0];

            using (var memoryStream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(memoryStream, listOfCommands);
                bytes = memoryStream.ToArray();
            }

            var newListOfCommands = new List <ICommand>();

            using (var memoryStream = new MemoryStream(bytes, 0, bytes.Length))
            {
                memoryStream.Write(bytes, 0, bytes.Length);
                memoryStream.Position = 0;
                var data = new BinaryFormatter().Deserialize(memoryStream);
                newListOfCommands = data as List <ICommand>;
            }

            foreach (var command in newListOfCommands)
            {
                command.Execute();
            }

            Assert.AreEqual(1, TestGameState.Instance.numberOfPlayers);
            Assert.AreEqual(3, TestGameState.Instance.numberOfEnemies);
        }
    //#####
    // país mágico
    //#####

    //send commands to the other client
    void SendCommands()
    {
        Dictionary <int, List <Command> > commandDict = new Dictionary <int, List <Command> >();

        commandDict = GetCommandDict(playerTurnhandler.Robots);

        for (int i = 0; i < playerTurnhandler.Robots.Count; i++)
        {
            if (playerTurnhandler.Robots[i].GetComponent <RobotBehaviour>().Commands.Count > 0)
            {
                //Debug.Log(playerTurnhandler.Robots[i].GetComponent<RobotBehaviour>().Commands[0].targetPosition.x + " y: " +  //playerTurnhandler.Robots[i].GetComponent<RobotBehaviour>().Commands[0].targetPosition.y);
            }
        }
        ServerBehaviour.SerializableCommandList scList = new ServerBehaviour.SerializableCommandList();


        foreach (KeyValuePair <int, List <Command> > pair in commandDict)
        {
            for (int i = 0; i < pair.Value.Count; i++)
            {
                Command c = pair.Value[i];

                Type t = c.GetType();
                if (t == typeof(MoveCommand))
                {
                    SerializableCommand sc = new SerializableCommand(pair.Key, c.targetPosition, c.lifeDuration, SerializableCommand.CommandType.Move, 0);
                    scList.Add(sc);
                }
                else if (t == typeof(PushCommand))
                {
                    SerializableCommand sc = new SerializableCommand(pair.Key, c.targetPosition, c.lifeDuration, SerializableCommand.CommandType.Push, 0);
                    scList.Add(sc);
                }
            }
        }

        //Debug.Log(scList.Count + " commands added to the list, asking serverbheaviour to send them!");
        server.SendCommands(scList);
    }
Example #12
0
        public void TestList()
        {
            var listOfCommands = new List <ICommand>();

            var methodGameStateChanger = new MethodGameStateChanger();
            var setPlayersAndEnemies   = TestHelper.GeneratePlayerAndEnemiesCommand();

            listOfCommands.Add(setPlayersAndEnemies);

            var gameStateChanger        = new GameStateChanger();
            var increaseNumberOfEnemies = new SerializableCommand <GameStateChanger>
                                              (gameStateChanger, changer => changer.IncreaseNumberOfEnemies());

            listOfCommands.Add(increaseNumberOfEnemies);

            foreach (var command in listOfCommands)
            {
                command.Execute();
            }

            Assert.AreEqual(1, TestGameState.Instance.numberOfPlayers);
            Assert.AreEqual(3, TestGameState.Instance.numberOfEnemies);
        }
Example #13
0
 public static ColorClaimedCommand FromSerializable(SerializableCommand sCommand)
 {
     return(new ColorClaimedCommand(sCommand.UID, int.Parse(sCommand.body)));
 }
Example #14
0
 public void Update(SerializableCommand input)
 {
     input.shouldShare = false;
     HandleInput(input);
 }
Example #15
0
 public static ColorRequestedCommand FromSerializable(SerializableCommand sCommand)
 {
     return(new ColorRequestedCommand(sCommand.UID));
 }
Example #16
0
 public static CreateGhostPlayerCommand FromSerializable(SerializableCommand sCommand)
 {
     return(new CreateGhostPlayerCommand(sCommand.body, sCommand.UID));
 }
Example #17
0
 public SerializableNetworkCommand(SerializationInfo info, StreamingContext context)
 {
     sequenceIndex = info.GetUInt32("sequenceIndex");
     command       = (SerializableCommand)info.GetValue("command", typeof(SerializableCommand));
 }
Example #18
0
 public SerializableNetworkCommand(uint sequenceIndex, SerializableCommand command)
 {
     this.command       = command;
     this.sequenceIndex = sequenceIndex;
 }
Example #19
0
 public void Share(SerializableCommand input)
 {
     Share(Serializer.SerializeInput(input));
 }
Example #20
0
 public static UnClaimColorCommand FromSerializable(SerializableCommand sCommand)
 {
     return(new UnClaimColorCommand(int.Parse(sCommand.body)));
 }
Example #21
0
    public static GameOverCommand FromSerializable(SerializableCommand sCommand)
    {
        GhostPlayer winner = PlayerManager.Instance().GetByUID(sCommand.UID);

        return(new GameOverCommand(winner));
    }
Example #22
0
 public static RemoveGhostPlayerCommand FromSerializable(SerializableCommand sCommand)
 {
     return(new RemoveGhostPlayerCommand(sCommand.UID));
 }