Esempio n. 1
0
        public void OnRoomDestroyed(RegisteredRoom room)
        {
            room.OnDestroyedEvent -= OnRoomDestroyed;

            GameIp    = "";
            GamePort  = -1;
            lobbyRoom = null;

            gameSpawnTask = null;

            State = Config.PlayAgainEnabled ? LobbyState.Preparations : LobbyState.GameOver;
        }
Esempio n. 2
0
        /// <summary>
        /// Start process on spawner side with given spawn <paramref name="options"/>, <paramref name="customOptions"/> and <paramref name="spawner"/>
        /// </summary>
        /// <param name="options"></param>
        /// <param name="customOptions"></param>
        /// <param name="spawner"></param>
        /// <returns></returns>
        public virtual SpawnTask Spawn(MstProperties options, MstProperties customOptions, RegisteredSpawner spawner)
        {
            // Create new spawn task
            var task = new SpawnTask(GenerateSpawnTaskId(), spawner, options, customOptions);

            // List this task
            spawnTasksList[task.Id] = task;

            // Add this task to queue
            spawner.AddTaskToQueue(task);

            logger.Debug("Spawner was found, and spawn task created: " + task);

            return(task);
        }
Esempio n. 3
0
        public void SetGameSpawnTask(SpawnTask task)
        {
            if (task == null)
            {
                return;
            }

            if (gameSpawnTask == task)
            {
                return;
            }

            if (gameSpawnTask != null)
            {
                // Unsubscribe from previous game
                gameSpawnTask.OnStatusChangedEvent -= OnSpawnServerStatusChanged;
                gameSpawnTask.Abort();
            }

            gameSpawnTask = task;

            task.OnStatusChangedEvent += OnSpawnServerStatusChanged;
        }
Esempio n. 4
0
        /// <summary>
        /// Fired whe connected client has made request to spawn process
        /// </summary>
        /// <param name="message"></param>
        protected virtual void ClientsSpawnRequestHandler(IIncomingMessage message)
        {
            // Parse data from message
            var spawnRequestData = message.Deserialize(new ClientsSpawnRequestPacket());
            var peer             = message.Peer;

            logger.Info($"Client {peer.Id} requested to spawn room with options: {spawnRequestData}");

            if (spawnersList.Count == 0)
            {
                logger.Error("But no registered spawner was found!");
                message.Respond("No registered spawner was found", ResponseStatus.Failed);
                return;
            }

            // Check if current request is authorized
            if (!CanClientSpawn(peer, spawnRequestData))
            {
                logger.Error("Unauthorized request");
                // Client can't spawn
                message.Respond("Unauthorized", ResponseStatus.Unauthorized);
                return;
            }

            // Try to find existing request to prevent new one
            SpawnTask prevRequest = peer.GetProperty((int)MstPeerPropertyCodes.ClientSpawnRequest) as SpawnTask;

            if (prevRequest != null && !prevRequest.IsDoneStartingProcess)
            {
                logger.Warn("And he already has an active request");
                // Client has unfinished request
                message.Respond("You already have an active request", ResponseStatus.Failed);
                return;
            }

            // Create a new spawn task
            var task = Spawn(spawnRequestData.Options, spawnRequestData.Options.AsString(MstDictKeys.ROOM_REGION), spawnRequestData.CustomOptions);

            // If spawn task is not created
            if (task == null)
            {
                logger.Warn("But all the servers are busy. Let him try again later");
                message.Respond("All the servers are busy. Try again later".ToBytes(), ResponseStatus.Failed);
                return;
            }

            // Save spawn task requester
            task.Requester = message.Peer;

            // Save the task as peer property
            peer.SetProperty((int)MstPeerPropertyCodes.ClientSpawnRequest, task);

            // Listen to status changes
            task.OnStatusChangedEvent += (status) =>
            {
                // Send status update
                var msg = Mst.Create.Message((short)MstMessageCodes.SpawnRequestStatusChange, new SpawnStatusUpdatePacket()
                {
                    SpawnId = task.Id,
                    Status  = status
                });

                if (task.Requester != null && task.Requester.IsConnected)
                {
                    message.Peer.SendMessage(msg);
                }
            };

            message.Respond(task.Id, ResponseStatus.Success);
        }
Esempio n. 5
0
 public void AddTaskToQueue(SpawnTask task)
 {
     _queue.Enqueue(task);
 }