예제 #1
0
        /// <summary>
        /// Removes player with given name from the game
        /// </summary>
        /// <param name="name">Use alias</param>
        public void Leave(string name, bool died)
        {
            Console.WriteLine($"{name} {(died ? "died" : "left")}!");
            if (callbacks.ContainsKey(name))
            {
                callbacks.Remove(name);

                NotifyPlayers();

                //Only reset the round if the round isn't in progress
                if (gameInProgress)
                {
                    //Reset the round as someone might have selected them as a target
                    playerRounds.Clear();

                    if (!died && callbacks.Count > 1)
                    {
                        foreach (var cb in callbacks.Values)
                        {
                            cb.ResetRound();
                        }
                    }
                    else if (callbacks.Count == 1)
                    {
                        PlayerRound round = new PlayerRound("You are the last player standing!");
                        callbacks.First().Value.SendRoundResults(round);
                    }
                    else if (callbacks.Count == 0)
                    {
                        gameInProgress = false;
                    }
                    Console.WriteLine($"Players remaining: {callbacks.Count}");
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Processes all shots taken and then returns round results to each client
        /// </summary>
        public void ProcessRound()
        {
            // If there are still players who are deciding, do not complete round
            if (playerRounds.Count != callbacks.Count)
            {
                return;
            }

            // Process all shots taken by players
            foreach (var playerRound in playerRounds.Values)
            {
                if (playerRound.Action == PlayerActions.Shoot)
                {
                    playerRound.ShotHit = playerRounds[playerRound.Target].ReceiveShot();
                }
            }

            // Report results to all players
            // List<string> results = new List<string>();
            foreach (var cb in callbacks)
            {
                Regex       playerName = new Regex(cb.Key);
                PlayerRound round      = playerRounds[cb.Key];
                round.Results.Add(round.GetResult(true));
                foreach (var callback in callbacks)
                {
                    if (callback.Key == round.PlayerName)
                    {
                        continue;
                    }
                    string playerResult = playerRounds[callback.Key].GetResult();
                    if (playerName.IsMatch(playerResult))
                    {
                        playerResult = playerName.Replace(playerResult, "you");
                    }
                    round.Results.Add(playerResult);
                }
            }

            // Report results to all players
            foreach (var cb in callbacks)
            {
                cb.Value.SendRoundResults(playerRounds[cb.Key]);
            }

            //Update the players on each client as some might have left/died during the turn
            foreach (var cb in callbacks)
            {
                cb.Value.SendAllPlayers(callbacks.Keys.ToArray());
            }

            playerRounds.Clear();

            //Notify the last user
            if (callbacks.Count == 1)
            {
                var         cb    = callbacks.First();
                PlayerRound round = playerRounds[cb.Key];
                round.Results.Clear();
                round.Results.Add("You are the last player standing!");
                callbacks.Values.First().SendRoundResults(round);
            }
        }