コード例 #1
0
        /// <summary>
        /// Occurs when a player wins the game.
        /// </summary>
        public static async Task PlayerWon(Player player)
        {
            TimeSpan ts = DateTime.Now - startTime;

            if (PlayersInGame.FirstOrDefault(p => p.Id == player.Id) != null && !isBuilding)
            {
                if (PlayersSafe.Count == 0) // First
                {
                    firstPerson = DateTime.Now;

                    int    k = Math.Max(35 - round * 5, 10);
                    string s = $"{k} seconds left!";
                    await Say($"{player.Name.ToUpper()} {(PlayersInGame.Count > 2 ? "finished! " + s : "won!")}");

                    if (!isTrainMode && PlayersInGame.Count == 2)
                    {
                        Profiles[player.Name].Wins++;
                    }

                    Eliminator.Start();
                }

                string elapsedTime = TimeToString(ts);
                PlayersSafe.Add(player, ts);

                //await SayCommand($"reset {player.Name}");
                await SayCommand($"effect {player.Name} clear");

                //await SayCommand($"tp {player.Name} {TopLeftShiftCoord.X + 16} {TopLeftShiftCoord.Y + 22}");

                await SayPrivate(player, $"Your Time: {elapsedTime}s");
                await UpdateSign();

                Formatter[] format =
                {
                    new Formatter(PlayersSafe.Count, Color.Gold),
                    new Formatter(player.Name,       Color.Green),
                    new Formatter(elapsedTime,       Color.Green),
                };
                Console.WriteLineFormatted("    {0}. {1} in {2}", Color.Silver, format);

                if (!isTrainMode)
                {
                    Profiles[player.Name].Rounds++;
                    saveProfiles();
                }

                if (2 * PlayersSafe.Count >= PlayersInGame.Count) // 50 % completed - eliminate (For 5 players: 3, For 16 players: 8; Minimum: 3 Players, wait for 2)
                {
                    if (PlayersSafe.Count > 1)
                    {
                        await Say($"Round over! Players not finished are eliminated!");
                    }
                    await ContinueGame();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Start or continue a running game process
        /// </summary>
        public static async Task ContinueGame()
        {
            try
            {
                if (!isBuilding)
                {
                    isBuilding    = true;
                    isGameAborted = new CancellationTokenSource();

                    if (Tick != null)
                    {
                        Tick.Stop();
                    }
                    if (Eliminator != null)
                    {
                        Eliminator.Stop();
                    }
                    if (TimeLimit != null)
                    {
                        TimeLimit.Stop();
                    }

                    await HideMapVoters();
                    await ClearGameArea();

                    await Task.Delay(6000, isGameAborted.Token);

                    int maxVotes = 0;
                    var chose    = new List <MapInfo>();
                    foreach (MapVote mv in MapVoteSigns)
                    {
                        if (maxVotes < mv.Voters.Count)
                        {
                            maxVotes = mv.Voters.Count;
                            chose.Clear();
                            chose.Add(Maps.FirstOrDefault(map => map.Id == mv.MapId));
                        }
                        else if (maxVotes == mv.Voters.Count)
                        {
                            chose.Add(Maps.FirstOrDefault(map => map.Id == mv.MapId));
                        }
                    }

                    MapInfo newMap = Maps.ElementAt(0);
                    if (chose.Count == 1)
                    {
                        newMap = chose.ElementAt(0);
                    }
                    else if (chose.Count > 1)
                    {
                        newMap = chose.ElementAt(new Random().Next(0, chose.Count));
                    }

                    await PlaceSign(TopLeftShiftCoord.X + 16, TopLeftShiftCoord.Y + 22, 58, $"You'll be playing now:\n{newMap.Title}\nBy {newMap.Creator}\n[Difficulty: {newMap.Difficulty}]", 1);
                    await BuildMap(newMap, isGameAborted.Token);
                    await CreateExit();

                    {
                        Formatter[] format =
                        {
                            new Formatter(newMap.Title,         Color.Cyan),
                            new Formatter(newMap.Id.ToString(), Color.Gray),
                            new Formatter(newMap.Creator,       Color.Cyan),
                        };

                        Console.WriteLineFormatted("  Map {0} ({1}) By {2}", Color.Silver, format);
                    }


                    if (PlayersSafe.Count < 2)
                    {
                        round = 1;

                        PlayersInGame = new List <Player>();
                        PlayersSafe   = new Dictionary <Player, TimeSpan>();

                        foreach (Player p in Players)
                        {
                            if (!p.Afk)
                            {
                                await SayCommand($"reset {p.Name}");
                                await SayCommand($"tp {p.Name} {TopLeftShiftCoord.X + 16} {TopLeftShiftCoord.Y + 22}");

                                PlayersInGame.Add(p);
                            }
                        }

                        if (PlayersInGame.Count < 4)
                        {
                            isTrainMode = true;
                            await Say($"Not enough players! Traning mode is enabled! Statistics will not be updated!");
                        }
                        else
                        {
                            isTrainMode = !isSavingData; // If you are saving data (true) then training mode is not (false), then data is saved!
                            if (isTrainMode)
                            {
                                await Say($"This bot does not save players statistics.");
                            }
                        }

                        foreach (Player p in Players)
                        {
                            if (!isTrainMode)
                            {
                                Profiles[p.Name].Plays++;
                            }
                        }

                        Console.Write("* ", Color.Silver);
                        Console.Write("NEW GAME! ");
                        Console.WriteLine($"{Players.Count} in world, {PlayersInGame.Count} joined", Color.DarkGray);
                    }
                    else
                    {
                        round++;

                        int playersBefore = PlayersInGame.Count;
                        PlayersInGame = new List <Player>();

                        foreach (KeyValuePair <Player, TimeSpan> p in PlayersSafe)
                        {
                            PlayersInGame.Add(p.Key);
                        }

                        PlayersSafe = new Dictionary <Player, TimeSpan>();

                        Formatter[] format =
                        {
                            new Formatter("Round",                             Color.White),
                            new Formatter(round,                               Color.Gold),
                            new Formatter(PlayersInGame.Count,                 Color.Green),
                            new Formatter(playersBefore - PlayersInGame.Count, Color.Green),
                        };

                        Console.WriteLineFormatted("    {0} {1}! {2} joined the round, {3} eliminated", Color.Silver, format);
                    }

                    TimeLimit          = new System.Timers.Timer(150 * 1000);
                    TimeLimit.Elapsed += async(object s, ElapsedEventArgs e) =>
                    {
                        await Say($"Time's over!");
                        await ContinueGame();
                    };

                    int k = Math.Max(35 - round * 5, 10);
                    Eliminator          = new System.Timers.Timer(k * 1000);
                    Eliminator.Elapsed += async(object s, ElapsedEventArgs e) =>
                    {
                        if (PlayersSafe.Count == 1)
                        {
                            await Say($"{PlayersSafe.ElementAt(0).Key.Name.ToUpper()} won!");

                            if (!isTrainMode)
                            {
                                Profiles[PlayersSafe.ElementAt(0).Key.Name].Wins++;
                            }
                        }
                        else
                        {
                            await Say($"Time's over!");
                        }
                        await ContinueGame();
                    };

                    await Task.Delay(5000, isGameAborted.Token);
                    await MakeGravity();
                    await OpenEntrance();

                    await Task.Delay(500, isGameAborted.Token);
                    await ReleasePlayers();

                    startTime = DateTime.Now;
                    TimeLimit.Start();

                    await Task.Delay(2000, isGameAborted.Token);
                    await CreateSafeArea();

                    foreach (Player p in Players)
                    {
                        p.Vote = -1;
                    }
                    await RegenerateMapVoters();

                    Tick.Start();
                    isBuilding = false;
                }
            }
            catch (TaskCanceledException)
            {
                await ResetGame();
            }
        }
コード例 #3
0
ファイル: Player.cs プロジェクト: Anatoly03/ShiftBot
        public async static Task AdminMessageHandler(Command c)
        {
            Player player = c.Sender;

            switch (c.Cmd)
            {
            case "help":
                await player.Tell(".fullname, .name - set the world name to.. (.name renames the prefix)");

                await player.Tell(".scanner worldid - opens a scanner in a new world");

                await player.Tell(".kill, .start, .resume - continue the game process (eliminate players or start game)");

                await player.Tell(".stop, .pause - abort the game process");
                await DefaultMessageHandler(c);     //  For complete informations without rewriting code.

                break;

            case "fullname":
                string fulltitle = c[0];
                await SayCommand($"title {fulltitle}");

                Console.Write("@ ", Color.BlueViolet);
                Console.Write(player.Name, Color.Orange);
                Console.Write($" set title to ");
                Console.WriteLine(fulltitle, Color.Cyan);
                break;

            case "name":
                string title = c[0];
                await SayCommand($"title {title}: CC Shift");

                Console.Write("@ ", Color.BlueViolet);
                Console.Write(player.Name, Color.Orange);
                Console.Write($" set title to ");
                Console.WriteLine(title + ": CC Shift", Color.Cyan);
                break;

            case "kill":
            case "start":
            case "resume":
                Console.Write("@ ", Color.BlueViolet);
                Console.Write(player.Name, Color.Orange);

                await Task.Run(async() =>
                {
                    if (!isBuilding)
                    {
                        if (PlayersInGame.Count > 0)
                        {
                            if (PlayersSafe.Count == 0)
                            {
                                Console.WriteLine(" forced to eliminate everyone!");
                                await Say($"Round was forced to end! Everyone's eliminated!");
                            }
                            else if (PlayersSafe.Count == 1)
                            {
                                Console.WriteLine(" forced to end the round!");
                                await Say($"Round was forced to end! {PlayersSafe.ElementAt(0).Key.Name.ToUpper()} won!");

                                if (!isTrainMode)
                                {
                                    Profiles[PlayersSafe.ElementAt(0).Key.Name].Wins++;
                                    saveProfiles();
                                }
                            }
                            else
                            {
                                Console.WriteLine(" forced to end the round!");
                                await Say($"Round was forced to end! Players not finished are eliminated!");
                            }
                        }
                        else
                        {
                            Console.WriteLine(" started the game!");
                        }
                        await ContinueGame();
                    }
                });

                break;

            case "stop":
            case "abort":
            case "pause":
                Console.Write("@ ", Color.BlueViolet);
                Console.Write(player.Name, Color.Orange);
                Console.WriteLine(" aborted the game", Color.Cyan);
                await AbortGame();

                break;

            case "clear":
                Console.Write("@ ", Color.BlueViolet);
                Console.Write(player.Name, Color.Orange);
                Console.WriteLine(" cleared the map!", Color.Cyan);
                await ClearGameArea();

                break;

            case "build":
                if (c.ArgNum > 0)
                {
                    Console.Write("@ ", Color.BlueViolet);
                    Console.Write(player.Name, Color.Orange);
                    Console.WriteLine(" built map", Color.Cyan);
                    await BuildMap(int.Parse(c[0]));
                }
                break;

            case "scanner":
                if (c.ArgNum > 0)
                {
                    Console.Write("@ ", Color.BlueViolet);
                    Console.Write(player.Name, Color.Orange);
                    Console.WriteLine(" proposed to open a scanner", Color.Cyan);
                    await OpenScanner(c[0], player);
                }
                break;

            case "regen":
                switch (c[0])
                {
                case "maps":
                    await RegenerateMapVoters();

                    break;
                }
                break;

            default:
                await DefaultMessageHandler(c);

                break;
            }
        }