public Startup(string[] args) { var builder = new ConfigurationBuilder() // Create a new instance of the config builder .SetBasePath(AppContext.BaseDirectory); // Specify the default location for the config file Configuration = builder.Build(); // Build the configuration Games = new List <Mafia.Game>(); // Instantiate our internal games list DefaultRoles = new List <Mafia.PlayerRole>(); // Instantiate our default role list // Load the list of default roles at runtime try { string json = File.ReadAllText("DefaultRoles.json"); var defaultRoles = JsonConvert.DeserializeObject <Mafia.DefaultRoleCollection>(json); Array Powers = Enum.GetValues(typeof(Mafia.PowerFlags)); Array Alignments = Enum.GetValues(typeof(Mafia.PlayerAlignment)); foreach (Mafia.DefaultRole role in defaultRoles.Roles) { Mafia.PowerFlags drPowers = Mafia.PowerFlags.None; Mafia.PlayerAlignment drAlignment = Mafia.PlayerAlignment.Town; // Set alignment to town by default in case the specified alignment is invalid foreach (string thisPower in role.Powers) // Set power flags for each power specified in the JSON file { foreach (Mafia.PowerFlags val in Powers) { string powerFlag = Enum.GetName(typeof(Mafia.PowerFlags), val); if (powerFlag == thisPower) { drPowers |= val; } } } foreach (Mafia.PlayerAlignment val in Alignments) // Set alignment as specified in the JSON file { if (Enum.GetName(typeof(Mafia.PlayerAlignment), val) == role.Alignment) { drAlignment = val; } } DefaultRoles.Add(new Mafia.PlayerRole(role.Name, role.Description, drPowers, role.Charges, drAlignment)); } } catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.Message); } }
public async Task AddCustomRole([Remainder] string RoleString) { Game CurrentGame = null; foreach (Mafia.Game game in Startup.Games) // Find the game this user started { if ((game.Mod.Username + game.Mod.Discriminator.ToString()) == (Context.User.Username + Context.User.Discriminator.ToString())) { CurrentGame = game; } } if (CurrentGame != null && CurrentGame.State != GameState.Signups) // Ignore command if game is already running { await Context.User.SendMessageAsync("This game is already running!"); return; } if (CurrentGame != null) { try { string[] newRole = RoleString.Split('|'); // Attempt to instantiate a new custom role within the current game if (newRole.Length != 5) { await Context.User.SendMessageAsync("Role string is not in the proper format!\nex: ##addcustomrole Doctor Miller|A doctor who is also a miller!|Miller,Doctor|-1,-1|Town"); } else { Array Powers = Enum.GetValues(typeof(Mafia.PowerFlags)); string[] customPowers = newRole[2].Trim().Split(','); Mafia.PowerFlags customPowerFlags = Mafia.PowerFlags.None; // Compare each specified power to existing enumerated powers and add to the custom power flags if found if (customPowers.Length > 0) { foreach (string thisPower in customPowers) { foreach (Mafia.PowerFlags val in Powers) { string powerFlag = Enum.GetName(typeof(Mafia.PowerFlags), val); if (powerFlag == thisPower) { customPowerFlags |= val; } } } } Mafia.PlayerAlignment Alignment = Mafia.PlayerAlignment.Town; // Set alignment to town by default in case the specified alignment is invalid Array Alignments = Enum.GetValues(typeof(Mafia.PlayerAlignment)); foreach (Mafia.PlayerAlignment val in Alignments) { if (Enum.GetName(typeof(Mafia.PlayerAlignment), val) == newRole[4]) { Alignment = val; } } // Add new custom role to current game int[] Charges = newRole[3].Split(',').Select(x => int.Parse(x)).ToArray <int>(); CurrentGame.PlayerRoles.Add(new PlayerRole(newRole[0], newRole[1], customPowerFlags, Charges, Alignment)); await Context.User.SendMessageAsync($"Custom role \'{newRole[0]}\' added."); } } catch (Exception err) { await Context.User.SendMessageAsync(err.Message); } } else { await Context.User.SendMessageAsync("There is no game running on this server or you are not the mod of the current game!"); } }