Example #1
0
        public static async Task ResetGame()
        {
            isBuilding = true;
            await Say("Game canceled!");

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

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

            await ClearGameArea();

            isBuilding = false;
        }
Example #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();
            }
        }