Esempio n. 1
0
 public void OnStateFluxGameInstanceCreated(GameInstanceCreatedMessage message)
 {
     DebugLog($"Server says {message.GameInstance.HostPlayer.Name} began hosting a new game instance: {message.GameInstance.Name}.  (I am {this.LastUsername})");
     if (message.GameInstance.HostPlayer.Name == LastUsername)
     {
         DebugLog($"Current user is hosting a game");
         _hostingGame = true;
     }
 }
        public void CreateGameInstance(CreateGameInstanceMessage message)
        {
            var currentPlayer = _websocket.GetCurrentSessionPlayer();

            Assert.ThrowIfNull(currentPlayer, "requires a user session", _websocket);

            var game = _server.Games.FirstOrDefault(g => g.Name == message.GameName);

            if (game == null)
            {
                // add the game
                game = new Game
                {
                    Name        = message.GameName,
                    MaxPlayers  = message.MaxPlayers,
                    MinPlayers  = message.MinPlayers,
                    Description = message.GameName,
                };
                Server.Instance.Games.Add(game);

                string msg = $"Adding new game '{message.GameName}'.";
                _websocket.LogMessage(msg);
            }

            if (game.Instances.Any(g => g.HostPlayer.Id == currentPlayer.Id))
            {
                string msg = $"Player can only create one game instance at a time.";
                _websocket.LogMessage(msg);
                throw new Exception(msg);
            }

            if (game.Instances.Any(g => g.Name == message.InstanceName))
            {
                string msg = $"Player tried to create game instance called '{message.InstanceName}' but that is already taken for game {message.GameName}!";
                _websocket.LogMessage(msg);
                throw new Exception(msg);
            }

            var gameInstance = Server.Instance.HostGameInstance(currentPlayer, game, message.InstanceName);

            _websocket.LogMessage($"Player creates game instance '{message.GameName}:{message.InstanceName}'");

            var broadcastMessage = new GameInstanceCreatedMessage()
            {
                GameInstance = gameInstance
            };

            _websocket.Broadcast(broadcastMessage, new GameInstanceRef(gameInstance), true);
        }
Esempio n. 3
0
    IEnumerator ReceiveAndDispatchMessages()
    {
        while (!connection.SocketOpenWithIdentity)
        {
            foreach (var listener in listeners)
            {
                listener.OnStateFluxWaitingToConnect();
            }
            yield return(new WaitForSeconds(1));
        }

        while (true)
        {
            openWithIdentity = connection.SocketOpenWithIdentity;
            if (!connected && openWithIdentity)
            {
                connected = true;
                userName  = connection.UserName;
                foreach (var listener in listeners)
                {
                    listener.OnStateFluxConnect();
                }
            }
            else if (connected && !openWithIdentity)
            {
                connected = false;
                userName  = "";
                foreach (var listener in listeners)
                {
                    listener.OnStateFluxDisconnect();
                }
            }

            bool draining      = true;
            int  drainingCount = 50;
            while (draining && (drainingCount--) > 0)
            {
                StateFlux.Model.Message message = connection.ReceiveResponse();
                draining = (message != null);
                if (draining)
                {
                    if (message.MessageType == MessageTypeNames.HostStateChanged)
                    {
                        // FIXME: convert all the others to linq method ForEach
                        listeners.ForEach(l => l.OnStateFluxHostStateChanged((HostStateChangedMessage)message));
                    }
                    else if (message.MessageType == MessageTypeNames.MiceChanged)
                    {
                        MiceChangedMessage msg = (MiceChangedMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxMiceChanged(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.HostCommandChanged)
                    {
                        HostCommandChangedMessage msg = (HostCommandChangedMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxHostCommandChanged(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GuestCommandChanged)
                    {
                        GuestCommandChangedMessage msg = (GuestCommandChangedMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGuestCommandChanged(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GuestInputChanged)
                    {
                        GuestInputChangedMessage msg = (GuestInputChangedMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGuestInputChanged(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.PlayerListing)
                    {
                        PlayerListingMessage msg = (PlayerListingMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxPlayerListing(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GameInstanceCreated)
                    {
                        GameInstanceCreatedMessage msg = (GameInstanceCreatedMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGameInstanceCreated(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GameInstanceListing)
                    {
                        GameInstanceListingMessage msg = (GameInstanceListingMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGameInstanceListing(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GameInstanceJoined)
                    {
                        GameInstanceJoinedMessage msg = (GameInstanceJoinedMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGameInstanceJoined(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GameInstanceStart)
                    {
                        GameInstanceStartMessage msg = (GameInstanceStartMessage)message;
                        Debug.Log($"Game start message, host is {msg.Host.Name}");
                        Debug.Log($"Current player name is {connection.CurrentPlayer.Name}");
                        isHosting = (msg.Host.Name == connection.CurrentPlayer.Name);
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGameInstanceStart(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GameInstanceStopped)
                    {
                        GameInstanceStoppedMessage msg = (GameInstanceStoppedMessage)message;
                        Debug.Log($"Game stopped message, host is {msg.Host.Name}");
                        Debug.Log($"Current player name is {connection.CurrentPlayer.Name}");
                        isHosting = (msg.Host.Name == connection.CurrentPlayer.Name);
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGameInstanceStopped(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.GameInstanceLeft)
                    {
                        GameInstanceLeftMessage msg = (GameInstanceLeftMessage)message;
                        Debug.Log($"Player {msg.Player.Name} left {msg.GameName}:{msg.InstanceName}");
                        Debug.Log($"Current player name is {connection.CurrentPlayer.Name}");
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxGameInstanceLeft(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.ChatSaid)
                    {
                        ChatSaidMessage msg = (ChatSaidMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxChatSaid(msg);
                        }
                    }
                    else if (message.MessageType == MessageTypeNames.ServerError)
                    {
                        ServerErrorMessage msg = (ServerErrorMessage)message;
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxServerError(msg);
                        }
                    }
                    else
                    {
                        foreach (var listener in listeners)
                        {
                            listener.OnStateFluxOtherMessage(message);
                        }
                    }
                }
            }

            yield return(null);
        }
    }
Esempio n. 4
0
 void OnGameInstanceCreated(GameInstanceCreatedMessage message)
 {
     Console.WriteLine("Game instance created:");
     Console.WriteLine("---------------");
     Console.WriteLine($"{JsonConvert.SerializeObject(message)}");
 }
Esempio n. 5
0
 public void OnStateFluxGameInstanceCreated(GameInstanceCreatedMessage message)
 {
 }