Ejemplo n.º 1
0
        public void TestMessageHandler()
        {
            MockSender sender = new() { Id = 42 };

            TestMessageHandler handler = new();

            MockReceiver receiver = new();

            MessageProcessor processor = new();

            processor.SetMessageReceiver(receiver);

            MessageDictionary dic = MessageDictionary.GetInstance();

            processor.RegisterMessageHandler(handler);

            dic.AddOutgoingMessage(Module, Type, p =>
            {
                return(Tuple.Create(TestValue, 42));
            });

            var msg = dic.CreateMessage(Type);

            receiver.ReceiveMessage(new RawMessage(sender, msg.Serialize()));

            Assert.IsTrue(handler.Tcs.Task.Wait(100));
        }
    }
Ejemplo n.º 2
0
        public GameStorageLogic()
        {
            Messages.AddOutgoingMessage("Game Storage", "Games", (p) =>
            {
                var wrapper = (IEnumerable <GameWrapper>)p[0];
                return(wrapper.Select(wr => Tuple.Create(wr.Id, wr.Factory.Name)).ToArray());
            },
                                        typeof(Tuple <int, string>[]), "Game Id", "Game Name");
            Messages.AddOutgoingMessage("Game Storage", "New Game", (p) =>
            {
                var wrapper = (GameWrapper)p[0];
                return(Tuple.Create(wrapper.Id, wrapper.Factory.Name));
            },
                                        typeof(Tuple <int, string>), "Game Id", "Game Name");

            Messages.AddIngoingMessage("Game Storage", "Games", typeof(int[]), "IDs of supported games");
            Messages.AddIngoingMessage("Game Storage", "New Game", typeof(int), "ID of supported game");
        }
Ejemplo n.º 3
0
 private GameMessageFactory()
 {
     Messages.AddOutgoingMessage("Game", "Initialize");
     Messages.AddOutgoingMessage("Game", "Start");
     Messages.AddOutgoingMessage("Game", "GameOver");
 }
Ejemplo n.º 4
0
        private LobbyMessageFactory()
        {
            Messages.AddOutgoingMessage("Lobby", "Lobbies", (obj) =>
            {
                var lobbies = obj[0] as IReadOnlyDictionary <int, Lobby>;

                return(lobbies.Values.Select(l =>
                {
                    var playerIds = l.Players.Select(p => p.Id).ToArray();
                    var hostId = l.Host.Connection.Id;
                    return Tuple.Create(l.Id, l.Name, l.Backend.Game.Id, hostId, playerIds);
                }
                                             ).ToArray());
            }, typeof(Tuple <int, string, int, int, int[]>[]), "Lobby Id", "Lobby Name", "Game Id", "Host Id", "Player Ids");

            Messages.AddOutgoingMessage("Lobby", "PlayerJoinedLobby", (obj) =>
            {
                var player = (Player)obj[0];
                var lobby  = (Lobby)obj[1];
                return(Tuple.Create(player.Id, lobby.Id));
            }, typeof(Tuple <int, int>), "Player Id", "Lobby Id");
            Messages.AddOutgoingMessage("Lobby", "YouJoinedLobby", (obj) =>
            {
                var lobby = (Lobby)obj[0];
                return(lobby.Id);
            }, typeof(int), "Lobby Id");
            Messages.AddOutgoingMessage("Lobby", "YouLeftLobby", null, null);

            Messages.AddOutgoingMessage("Lobby", "LobbyCreated", (obj) =>
            {
                var lobby     = (Lobby)obj[0];
                var playerIds = lobby.Players.Select(p => p.Id).ToArray();
                var hostId    = lobby.Host.Connection.Id;

                return(Tuple.Create(lobby.Id, lobby.Name, lobby.Backend.Game.Id, hostId, playerIds));
            }, typeof(Tuple <int, int, int, int[]>), "Lobby Id", "Lobby Name", "Game Id", "Host Id", "Player Ids");

            Messages.AddOutgoingMessage("Lobby", "PlayerLeftLobby", (obj) =>
            {
                var player = (Player)obj[0];
                var lobby  = (Lobby)obj[1];
                return(Tuple.Create(player.Id, lobby.Id));
            }, typeof(Tuple <int, int>), "Player Id", "Lobby Id");

            Messages.AddOutgoingMessage("Lobby", "LobbyClosed", (obj) =>
            {
                var lobby = (Lobby)obj[0];
                return(lobby.Id);
            }, typeof(int), "Lobby Id");

            Messages.AddOutgoingMessage("Lobby", "GameSettings", (obj) =>
            {
                var lobby = (Lobby)obj[0];
                return(lobby.GameSettings.Select(p => Tuple.Create(p.Key, p.Value.Value)).ToArray());
            }, typeof(Tuple <string, object>[]), "Name", "Value");
            Messages.AddOutgoingMessage("Lobby", "GameModeChanged", (obj) =>
            {
                var lobby = (Lobby)obj[0];
                return(lobby.SelectedGameMode.Name);
            }, typeof(string), "Gamemode Name");
            Messages.AddOutgoingMessage("Lobby", "SetReady", (obj) =>
            {
                var lc = (LobbyConnection)obj[0];
                return(Tuple.Create(lc.Connection.Id, lc.Ready));
            }, typeof(Tuple <int, bool>), "Ready");
            Messages.AddOutgoingMessage("Lobby", "ReadyStates", (obj) =>
            {
                var lobby = (Lobby)obj[0];
                return(lobby.Connections.Select(c => Tuple.Create(c.Key, c.Value.Ready)).ToArray());
            }, typeof(Tuple <int, bool>), "Connection ID", "Ready");

            /*Messages.AddOutgoingMessage("Lobby", "Lobby Settings", (obj) =>
             * {
             *  var lobby = (Lobby)obj[0];
             * //   var options = lobby.Logic.GameSettings.Select(p => Tuple.Create(p.Key, p.Value)).ToArray();
             * //   var gameMode = lobby.Logic.GameMode.Name;
             *  return ;
             * }, typeof(Tuple<Tuple<string, object>[], ), "Lobby Settings", "-Name", "-value", );
             */
        }