Exemple #1
0
        /// <summary>
        /// Checks if a hider is in the specified room.
        /// </summary>
        /// <param name="seeker">The seeker who is guessing.</param>
        /// <param name="playerName">The name of the hider.</param>
        /// <param name="roomName">The name of the room.</param>
        /// <returns>Returns true if the seeker has correctly guessed the room the hider was hiding in.</returns>
        public async Task <bool> GuessRoomAsync(Player seeker, string playerName, string roomName)
        {
            // If host is handling,
            // request data from hider
            if (_IsHost)
            {
                Hider hider = _hiders.Where(player => player.PlayerName == playerName).FirstOrDefault();
                return(bool.Parse(await hider.SendCommand(ERemoteCommand.GuessRoom, roomName)));
            }
            else
            {
                bool result = bool.Parse(await SendCommandToHost(playerName, EHostCommand.GuessRoom, roomName));

                if (result)
                {
                    await seeker.SendCommand(ERemoteCommand.AddPoints, 10.ToString());

                    // End game if all hiders are found.
                    if (_hiders.Count(hider => !hider.IsFound) <= 0)
                    {
                        _ = Task.Run(async() => await EndGame());
                    }
                }

                return(result);
            }
        }
Exemple #2
0
 /// <summary>
 /// Gets the position of a <see cref="Hider"/>.
 /// </summary>
 /// <param name="hiderUsername">The identifier of the hider.</param>
 /// <returns>The <see cref="Room"/> a <see cref="Hider"/> is currently in.</returns>
 public async Task <Room> GetHiderLocation(string hiderUsername)
 {
     if (_IsHost)
     {
         Hider hider = _hiders.Where(player => player.PlayerName == hiderUsername).FirstOrDefault();
         return(JsonConvert.DeserializeObject <Room>(await hider.SendCommand(ERemoteCommand.GetPosition, string.Empty)));
     }
     else
     {
         return(JsonConvert.DeserializeObject <Room>(await SendCommandToHost(hiderUsername, EHostCommand.GetHiderPosition, string.Empty)));
     }
 }
Exemple #3
0
        /// <summary>
        /// Execute incomming commands on host
        /// </summary>
        /// <param name="target">The user target.</param>
        /// <param name="command">The command to send.</param>
        /// <param name="arguments">Additional arguments to send.</param>
        /// <returns>Returns the command results.</returns>
        private async Task <string> ExecuteHostCommands(string target, string command, string arguments)
        {
            if (!Enum.TryParse(command, out EHostCommand hostCommand))
            {
                return(string.Empty);
            }

            switch (hostCommand)
            {
            case EHostCommand.Disconnect:
                Player player = _hiders.FirstOrDefault(p => p.PlayerName == target) ?? (Player)_seekers.FirstOrDefault(p => p.PlayerName == target);
                player?.Client.Close();
                break;

            case EHostCommand.EndGame:
                await EndGame();

                break;

            case EHostCommand.GetRemainingTime:
                return(_remainingHidingTime.ToString());

            case EHostCommand.GuessRoom:
                Hider hider  = _hiders.FirstOrDefault(p => p.PlayerName == target);
                bool  result = await hider?.SendCommand(ERemoteCommand.GetPosition, string.Empty) == arguments;

                return(result.ToString());

            case EHostCommand.ViewMaps:
                return(JsonConvert.SerializeObject(await ViewMapsAsync()));

            default:
                return(string.Empty);
            }

            return(string.Empty);
        }