Пример #1
0
        public string PrintGame(DotaBotGame game)
        {
            var s = $"```\nZespół na {game.Time.Hour}:{game.Time.Minute:D2}\n";
            int i = 1;

            foreach (var player in game.Players)
            {
                string added_by = player.AddedBy == null ? "" : $" (dodany przez: {player.AddedBy})";
                string note     = player.Note == null ? "" : $" ({player.Note})";
                s += $" {i}) {player.Name}{added_by}{note}\n";
                i++;
            }
            s += "```";

            return(s);
        }
Пример #2
0
        public void SendReminder(DotaBotGame game)
        {
            discord.DownloadUsersAsync(new List <Discord.IGuild> {
            }).Wait();
            var users = discord.GetGuild(game.GuildId).Users;

            List <string> mentions = new List <string>();

            foreach (var player in game.Players)
            {
                var found = users.FirstOrDefault(x => x.Username == player.Name);
                if (found != null)
                {
                    mentions.Add(found.Mention);
                }
            }

            SendMessage($"Zaraz gramy! {String.Join(", ", mentions)}\n{PrintGame(game)}");
        }
Пример #3
0
        private void ExecuteInner(Command command, string requester)
        {
            var player = new Player {
                Name    = command.as_player == null ? requester : NormalizeAsPlayer(command.as_player),
                Note    = command.note,
                AddedBy = command.as_player == null ? null : requester
            };

            if (command.action == Command.Action.Add)
            {
                var existing_game = Games.Where(x => x.Time == command.time).FirstOrDefault();
                if (existing_game == null)
                {
                    var new_game = new DotaBotGame
                    {
                        ChannelId = channel_id,
                        GuildId   = guild_id,
                        Players   = new List <Player> {
                            player
                        },
                        Time = command.time
                    };
                    db.DotaBotGames.Add(new_game);
                    SendMessage($"Będzie Dotka!\n{PrintGame(new_game)}");
                }
                else
                {
                    // replace same player added by someone else
                    // TODO: test
                    var added_by_someone_else = existing_game.Players.FindIndex(x =>
                                                                                x.Name.ToLower() == player.Name.ToLower() &&
                                                                                x.AddedBy != null);
                    if (added_by_someone_else != -1 && player.AddedBy == null)
                    {
                        existing_game.Players[added_by_someone_else] = player;
                        SendMessage($"{player.Name} sam włączył się do gry\n{PrintGame(existing_game)}");
                    }
                    else if (existing_game.Players.All(x => x.Name != player.Name))
                    {
                        existing_game.Players.Add(player);
                        SendMessage($"{player.Name} dołączył do gry\n{PrintGame(existing_game)}");
                    }
                    db.Entry(existing_game).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }
            }
            else if (command.action == Command.Action.Remove)
            {
                var game = Games.FirstOrDefault(x => x.Time == command.time);
                if (game != null)
                {
                    var player_to_remove = game.Players.FirstOrDefault(x =>
                                                                       x.Name == player.Name &&
                                                                       x.AddedBy == player.AddedBy);
                    if (player_to_remove != null)
                    {
                        game.Players.Remove(player_to_remove);
                        db.Entry(game).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        if (game.Players.Count == 0)
                        {
                            SendMessage($"Brak chętnych na Dotkę o {game.Time.Hour}:{game.Time.Minute:D2} :(");
                            db.DotaBotGames.Remove(game);
                        }
                        else
                        {
                            SendMessage($"{player.Name} zrezygnował z gry\n{PrintGame(game)}");
                        }
                    }
                }
            }
            else if (command.action == Command.Action.JoinLatestGame)
            {
                if (Games.Count() == 0)
                {
                    SendMessage($"Nie ma żadnych gier, żebyś mógł dołączyć");
                }
                else
                {
                    // Recursive call!
                    ExecuteInner(new Command
                    {
                        action    = Command.Action.Add,
                        as_player = command.as_player,
                        note      = command.note,
                        time      = Games.OrderBy(x => x.Id).ToList().Last().Time
                    },
                                 requester);
                }
            }
            else if (command.action == Command.Action.RemoveAll)
            {
                foreach (var game in db.DotaBotGames.ToList())
                {
                    // Recursive call!
                    ExecuteInner(new Command
                    {
                        action    = Command.Action.Remove,
                        as_player = command.as_player,
                        time      = game.Time
                    },
                                 requester);
                }
            }
            else if (command.action == Command.Action.ShowGames)
            {
                if (Games.Count() == 0)
                {
                    SendMessage("Nie ma żadnych zaplanowanych gier. Zaproponuj swoją!");
                }
                else
                {
                    var s = "Szykuje się granie:";
                    foreach (var game in Games)
                    {
                        s += $"{PrintGame(game)}\n";
                    }
                    SendMessage(s);
                }
            }
            else
            {
                Log($"Unhandled action: {command.action}");
            }
        }