/// <summary>
        /// Handler for the RequestPlayNow message.
        /// </summary>
        /// <param name="netMsg">Network message that was received.</param>
        protected virtual void OnPlayNowRequested(NetworkMessage netMsg)
        {
            var msg = netMsg.ReadMessage <RequestPlayNowMessage>();

            if (msg != null)
            {
                var game = registeredGameServers.Find(x => string.IsNullOrEmpty(x.password) && x.numPlayers < x.maxPlayers);
                if (game != null)
                {
                    var responseMsg = new ResponseJoinGameRoomMessage();
                    responseMsg.success = true;
                    responseMsg.ip      = game.ip;
                    responseMsg.port    = game.port;
                    netMsg.conn.Send(GameRoomsNetworkProtocol.ResponseJoinGameRoom, responseMsg);
                }
                else
                {
                    var requestMsg = new RequestCreateGameRoomMessage();
                    requestMsg.playerConnectionId = netMsg.conn.connectionId;
                    requestMsg.name       = "Default game";
                    requestMsg.maxPlayers = 4;
                    requestMsg.password   = null;
                    CreateGameRoom(netMsg.conn, requestMsg);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Requests the creation of a new game room with the specified properties.
        /// </summary>
        /// <param name="name">The name of this room.</param>
        /// <param name="maxPlayers">The maximum number of players allowed on this room.</param>
        /// <param name="password">Password (null for public rooms).</param>
        /// <param name="properties">The list of properties of this room.</param>
        /// <param name="successCallback">The callback to execute when the room could be created.</param>
        /// <param name="errorCallback">The callback to execute when the room could not be created.</param>
        public virtual void CreateGameRoom(string name, int maxPlayers, string password,
                                           List <Property> properties,
                                           Action <string, int> successCallback, Action <CreateGameRoomError> errorCallback)
        {
            createGameRoomSuccessCallback = successCallback;
            createGameRoomErrorCallback   = errorCallback;

            var msg = new RequestCreateGameRoomMessage();

            msg.name       = name;
            msg.maxPlayers = maxPlayers;
            msg.password   = password;
            msg.properties = properties.ToArray();
            networkClient.client.Send(GameRoomsNetworkProtocol.RequestCreateGameRoom, msg);
        }
        /// <summary>
        /// Creates a new game room.
        /// </summary>
        /// <param name="playerConnection">The network connection of the player requesting the creation of a game room.</param>
        /// <param name="msg">The network message containing the information of the game room to create.</param>
        protected virtual void CreateGameRoom(NetworkConnection playerConnection, RequestCreateGameRoomMessage msg)
        {
            var zoneServer = SelectZoneServer();

            if (zoneServer != null)
            {
                ConnectToZoneServer(zoneServer.ip, zoneServer.port)
                .Then(client =>
                {
                    awaitingPlayerConnections.Add(playerConnection);
                    msg.playerConnectionId = playerConnection.connectionId;
                    return(RequestCreateGameRoom(client, msg));
                })
                .Then(responseSpawnGameServerMessage =>
                {
                    if (!responseSpawnGameServerMessage.success)
                    {
                        var connection = awaitingPlayerConnections.Find(x => x.connectionId == responseSpawnGameServerMessage.playerConnectionId);
                        if (connection != null)
                        {
                            var responseMsg     = new ResponseCreateGameRoomMessage();
                            responseMsg.success = false;
                            connection.Send(GameRoomsNetworkProtocol.ResponseCreateGameRoom, responseMsg);
                            awaitingPlayerConnections.Remove(connection);
                        }
                    }
                })
                .Catch(e =>
                {
                    Debug.Log(e.Message);
                });
            }
            else
            {
                var responseMsg = new ResponseCreateGameRoomMessage();
                responseMsg.success = false;
                responseMsg.error   = CreateGameRoomError.ZoneServerUnavailable;
                playerConnection.Send(GameRoomsNetworkProtocol.ResponseCreateGameRoom, responseMsg);
            }
        }
        /// <summary>
        /// Requests the creation of a game room to the zone server.
        /// </summary>
        /// <param name="client">The network client connected to the zone server.</param>
        /// <param name="msg">The network message containing the information of the game room to create.</param>
        /// <returns>A promise with the network response to the game server spawn.</returns>
        protected IPromise <ResponseSpawnGameServerMessage> RequestCreateGameRoom(NetworkClient client, RequestCreateGameRoomMessage msg)
        {
            var promise = new Promise <ResponseSpawnGameServerMessage>();

            client.RegisterHandler(ZoneServerNetworkProtocol.ResponseSpawnGameServer, netMsg =>
            {
                var responseMsg = netMsg.ReadMessage <ResponseSpawnGameServerMessage>();
                client.Disconnect();
                promise.Resolve(responseMsg);
            });
            client.Send(GameRoomsNetworkProtocol.RequestCreateGameRoom, msg);
            return(promise);
        }