示例#1
0
        private void Deserialize(string directory)
        {
            if (!Directory.Exists(directory))
            {
                return;
            }

            DiscordSocketClient client  = _services.GetRequiredService <DiscordSocketClient>();
            WerewolfManager     manager = _services.GetRequiredService <WerewolfManager>();

            foreach (string dir in Directory.GetDirectories(directory))
            {
                ulong guildId = Convert.ToUInt64(Path.GetFileName(dir), 16);

                SocketGuild    guild      = client.GetGuild(guildId);
                JsonSerializer serializer = new JsonSerializer();
                try
                {
                    using (StreamReader sr = new StreamReader(Path.Combine(dir, WerewolfManager.SETTINGS_FILE)))
                        using (JsonReader jr = new JsonTextReader(sr))
                        {
                            WerewolfSettings loadedSettings = serializer.Deserialize <WerewolfSettings>(jr);
                            loadedSettings.Restore(guild);
                            manager.Settings.Add(guild, loadedSettings);
                        }
                }
                catch (IOException ex)
                {
                    _log.Error("Could not load Settings", ex);
                }
            }
        }
示例#2
0
        public async Task WerewolfConfigAsync(params string[] args)
        {
            if (args.Length == 0)
            {
                await ReplyAsync("Command `werewolf` needs to be followed by a subcommand, possible subcommands: start, set.");

                return;
            }
            switch (args[0])
            {
            case "start":
                if (args.Length != 1)
                {
                    return;
                }
                if (WerewolfManager.Games.ContainsKey(Context.Guild))
                {
                    await ReplyAsync($"The server `{Context.Guild.Name}` is already playing a game of werewolf.");
                }
                else
                {
                    if (!WerewolfManager.Settings.ContainsKey(Context.Guild))
                    {
                        await ReplyAsync("You cannot start a game without specifying the Game Channel first, use `werewolf set #<channel>`");

                        return;
                    }
                    if (WerewolfManager.Settings[Context.Guild].Channel == null)
                    {
                        await ReplyAsync("You cannot start a game without specifying the Game Channel first, use `werewolf set #<channel>`");

                        return;
                    }
                    WerewolfManager.Games.Add(Context.Guild, new WerewolfGame());
                    await WerewolfManager.Settings[Context.Guild].Channel.SendMessageAsync($"{Context.User.Username} has started a new game of werewolf, use `!join` or `@werewolf join` to join in.");
                }
                break;

            case "set":
                if (!((Context.User as SocketGuildUser).GuildPermissions.ManageChannels))
                {
                    await ReplyAsync("Setting the Werewolf Channel requires permission `ManageChannels`.");

                    return;
                }
                if (args.Length < 2)
                {
                    await ReplyAsync("What do you want to set? (`channel`)");

                    return;
                }
                else
                {
                    switch (args[1])
                    {
                    case "channel":
                        if (args.Length != 3 || (Context.Message.MentionedChannels.FirstOrDefault() == null))
                        {
                            await ReplyAsync("Usage: `werewolf set channel #<channel>`.");

                            return;
                        }
                        WerewolfSettings   settings = WerewolfManager.GetOrCreateSettings(Context.Guild);
                        SocketGuildChannel channel  = Context.Message.MentionedChannels.FirstOrDefault();
                        if (!(channel is ISocketMessageChannel))
                        {
                            //this should probably not even happen
                            await ReplyAsync($"Specified channel `{channel.Name}` is not a Text Channel.");

                            return;
                        }

                        settings.Channel = channel as ISocketMessageChannel;
                        await ReplyAsync($"Game Channel set to `{settings.Channel.Name}`");

                        break;
                    }
                }
                break;

            case "begin":
                if (args.Length != 1)
                {
                    return;
                }
                if (!WerewolfManager.Games.ContainsKey(Context.Guild))
                {
                    await ReplyAsync("Cannot begin the game, because none was started yet, use `werewolf start`.");

                    return;
                }
                if (WerewolfManager.Games[Context.Guild].Phase == ManagerPhase.JOIN)
                {
                    WerewolfManager.Games[Context.Guild].Phase = ManagerPhase.PLAY;

                    //Start async game while still listening for commands
                    WerewolfManager.Games[Context.Guild].DoGame(WerewolfManager.Settings[Context.Guild], Context.Guild);
                }
                break;
            }
        }