Skip to content

Commit

Permalink
#24 Help page;
Browse files Browse the repository at this point in the history
Yakuza-only mode
  • Loading branch information
WhiteCloud21 committed Feb 1, 2018
1 parent 27e754c commit 72968b6
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 44 deletions.
29 changes: 24 additions & 5 deletions DiscordMafia/Config/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Xml;
using System.Xml.Schema;
using DiscordMafia.Config.Lang;
using System.IO;

namespace DiscordMafia.Config
{
Expand All @@ -23,6 +24,7 @@ public class GameSettings : IXmlSerializable
public bool UseMuteBlacklist { get; protected set; }
public bool StartFromNight { get; protected set; }
public bool ShowNightActions { get; protected set; }
public bool IsMafiaEnabled { get; protected set; }
public bool IsYakuzaEnabled { get; protected set; }
public int PlayerCollectingTime { get; protected set; }
public int PauseTime { get; protected set; }
Expand All @@ -36,8 +38,11 @@ public class GameSettings : IXmlSerializable
public Points Points { get; private set; }
public Roles Roles { get; private set; }

public GameSettings(string gametype)
protected string ConfigPath { get; private set; }

public GameSettings(MainSettings mainSettings, string gametype)
{
ConfigPath = mainSettings.ConfigPath;
GameType = gametype;

// Значения по умолчанию
Expand All @@ -63,16 +68,27 @@ public GameSettings(string gametype)
MaxUsersToNotify = 50;
MinNotificationInterval = 7200;
ShowNightActions = true;
IsMafiaEnabled = true;
IsYakuzaEnabled = false;

ReadConfig();
Validate();

Points = Points.GetInstance(GetFilePath("points.xml"));
Console.WriteLine("Points configuration successfully loaded");
Roles = Roles.GetInstance(GetFilePath("roles.xml"));
Console.WriteLine("Role configuration successfully loaded");
}

private void Validate()
{
if (!IsMafiaEnabled && !IsYakuzaEnabled)
{
Console.Error.WriteLine("Mafia or Yakuza must be enabled. Enabling Mafia...");
IsMafiaEnabled = true;
}
}

protected void ReadConfig()
{
using (var stream = new System.IO.FileStream(GetFilePath("gameSettings.xml"), System.IO.FileMode.Open))
Expand All @@ -87,16 +103,16 @@ protected void ReadConfig()

protected string GetFilePath(string fileName)
{
if (GameType != null && System.IO.File.Exists($"Config/Gametypes/{GameType}/{fileName}"))
if (GameType != null && System.IO.File.Exists(Path.Combine(ConfigPath, $"Gametypes/{GameType}/{fileName}")))
{
return $"Config/Gametypes/{GameType}/{fileName}";
return Path.Combine(ConfigPath, $"Gametypes/{GameType}/{fileName}");
}
return $"Config/{fileName}";
return Path.Combine(ConfigPath, $"{fileName}");
}

public bool IsValidGametype(string gameType)
{
return System.IO.Directory.Exists($"Config/Gametypes/{gameType}");
return System.IO.Directory.Exists(Path.Combine(ConfigPath, $"Gametypes/{gameType}"));
}

public XmlSchema GetSchema()
Expand Down Expand Up @@ -132,6 +148,9 @@ public void ReadXml(XmlReader reader)
case "ShowNightActions":
ShowNightActions = bool.Parse(reader.ReadElementContentAsString());
break;
case "IsMafiaEnabled":
IsMafiaEnabled = bool.Parse(reader.ReadElementContentAsString());
break;
case "IsYakuzaEnabled":
IsYakuzaEnabled = bool.Parse(reader.ReadElementContentAsString());
break;
Expand Down
28 changes: 0 additions & 28 deletions DiscordMafia/Config/Gametypes/wtf/roles.xml

This file was deleted.

5 changes: 5 additions & 0 deletions DiscordMafia/Config/Gametypes/yakuza-only/gameSettings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<Settings>
<IsMafiaEnabled>False</IsMafiaEnabled>
<IsYakuzaEnabled>True</IsYakuzaEnabled>
</Settings>
93 changes: 93 additions & 0 deletions DiscordMafia/Config/Gametypes/yakuza-only/roles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0"?>
<Roles>
<Role Id="Commissioner">
<IsEnabled>true</IsEnabled>
<IsRandom>false</IsRandom>
<MinPlayers>0</MinPlayers>
</Role>
<Role Id="Doctor">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Wench">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Judge">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Elder">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Highlander">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Homeless">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Sheriff">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Prosecutor">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>3</MinPlayers>
</Role>
<Role Id="Mirror">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>10</MinPlayers>
</Role>
<Role Id="Sergeant">
<IsEnabled>true</IsEnabled>
<IsRandom>false</IsRandom>
<MinPlayers>13</MinPlayers>
</Role>
<Role Id="Spy">
<IsEnabled>true</IsEnabled>
<IsRandom>false</IsRandom>
<MinPlayers>15</MinPlayers>
</Role>
<Role Id="Hoodlum">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>10</MinPlayers>
</Role>
<Role Id="Ninja">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>10</MinPlayers>
</Role>
<Role Id="Maniac">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>8</MinPlayers>
</Role>
<Role Id="RobinHood">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>8</MinPlayers>
</Role>
<Role Id="Warlock">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>8</MinPlayers>
</Role>
<Role Id="Hacker">
<IsEnabled>true</IsEnabled>
<IsRandom>true</IsRandom>
<MinPlayers>12</MinPlayers>
</Role>
</Roles>
10 changes: 8 additions & 2 deletions DiscordMafia/Config/MainSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ public class MainSettings : IXmlSerializable
public string LanguageStr { get; protected set; }
public Lang.Language Language { get; protected set; }

public string ConfigPath { get; private set; }

public MainSettings(params string[] filenames)
{
foreach (var filename in filenames)
{
if (File.Exists(filename))
{
if (string.IsNullOrEmpty(ConfigPath))
{
ConfigPath = Path.GetDirectoryName(filename);
}
using (Stream stream = new FileStream(filename, FileMode.Open))
{
using (var reader = XmlReader.Create(stream))
Expand Down Expand Up @@ -108,7 +114,7 @@ public bool IsValidLanguage(string language)
{
return false;
}
return Directory.Exists($"Config/Lang/{language}");
return Directory.Exists(Path.Combine(ConfigPath, $"Lang/{LanguageStr}"));
}

public void LoadLanguage()
Expand All @@ -118,7 +124,7 @@ public void LoadLanguage()
throw new Exception("Language is not valid in mainSettings!");
}
Language = new Lang.Language();
Language.Load($"Config/Lang/{LanguageStr}");
Language.Load(Path.Combine(ConfigPath, $"Lang/{LanguageStr}"));
}
}
}
7 changes: 7 additions & 0 deletions DiscordMafia/Config/Roles.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
Expand Down Expand Up @@ -97,6 +98,12 @@ protected List<BaseRole> Filter(int playersCount, bool isRandom, bool onlyEnable
}
return result;
}

public static IEnumerable<BaseRole> GetAllRoles()
{
var types = typeof(BaseRole).GetTypeInfo().Assembly.GetTypes();
return from t in types where typeof(BaseRole).IsAssignableFrom(t) && !t.GetTypeInfo().IsAbstract select GetRoleInstance(t.GetTypeInfo().GetConstructor(Type.EmptyTypes));
}

public static BaseRole GetRoleInstance(string roleName)
{
Expand Down
1 change: 0 additions & 1 deletion DiscordMafia/DiscordMafia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<ItemGroup>
<Folder Include="Activity" />
<Folder Include="Client" />
<Folder Include="Config\Gametypes\yakuza" />
<Folder Include="Messages" />
<Folder Include="Voting" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DiscordMafia/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Game(System.Threading.SynchronizationContext syncContext, DiscordSocketCl

internal void LoadSettings(string gametype = null)
{
Settings = new Config.GameSettings(gametype);
Settings = new Config.GameSettings(MainSettings, gametype);
MessageBuilder = new Config.MessageBuilder(MainSettings, client, PlayersList);
GameMode = gametype;
Console.WriteLine("Settings loaded");
Expand Down
3 changes: 2 additions & 1 deletion DiscordMafia/RoleAssigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void AssignRoles(IList<InGamePlayerInfo> players, Config.GameSettings set
{
this.players = players;
remainingPlayerCount = players.Count;
totalMafCount = remainingMafCount = (int)Math.Truncate(players.Count * (double)settings.MafPercent / 100);
totalMafCount = (int)Math.Truncate(players.Count * (double)settings.MafPercent / 100);
remainingMafCount = settings.IsMafiaEnabled ? totalMafCount : 0;
remainingYakuzaCount = settings.IsYakuzaEnabled ? totalMafCount : 0;
Console.WriteLine("Количество мафиози: {0}", remainingMafCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public static string[] GetNameCases(this BaseRole role, ILanguage language)
return Enumerable.Repeat($"#ROLE_NAME_{role.GetType().Name}", 6).ToArray();
}

public static string GetDescription(this BaseRole role, ILanguage language)
{
string description = language.Messages.get($"RoleHelp_{role.GetType().Name}");
return !string.IsNullOrEmpty(description) ? description : $"#ROLE_DESC_{role.GetType().Name}";
}

private static RoleMessages.RoleMessage GetItemLangInfo(this BaseRole role, ILanguage language)
{
if (language.RoleMessages.TryGetValue(role.GetType().Name, out var result))
Expand Down
56 changes: 56 additions & 0 deletions MafiaWeb/Controllers/HelpController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using cloudscribe.Web.Navigation;
using DiscordMafia.Services;
using MafiaWeb.ViewModels.Role;
using DiscordMafia.Config;
using System.IO;
using MafiaWeb.ViewModels.Gamemode;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace MafiaWeb.Controllers
{
public class HelpController : Controller
{
protected MainSettings settings;

public HelpController(MainSettings settings)
{
this.settings = settings;
}

public IActionResult Gamemodes()
{
ViewData["Title"] = "Режимы игры";

IEnumerable<string> directories = Directory.GetDirectories(Path.Combine(settings.ConfigPath, "Gametypes"));
directories = directories.Prepend(null);
return View(from d in directories select new Gamemode { Name = Path.GetFileName(d) });
}

public IActionResult Roles(string modeId = null)
{
ViewData["Title"] = "Роли";
var modeRegex = new Regex(@"^[a-zA-Z0-9\-]+$");
if (modeId != null && !modeRegex.IsMatch(modeId))
{
return NotFound();
}

ViewBag.ModeSettings = new GameSettings(settings, modeId);
if (modeId != null)
{
var currentCrumbAdjuster = new NavigationNodeAdjuster(Request.HttpContext)
{
KeyToAdjust = "HelpGamemodeView",
AdjustedText = modeId
};
currentCrumbAdjuster.AddToContext();
ViewData["Title"] = $"Роли режима {modeId}";
}
return View(from r in RoleInfo.AvailableRoles group r by r.Role.Team into g orderby g.Key select g);
}
}
}
8 changes: 4 additions & 4 deletions MafiaWeb/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void ConfigureServices(IServiceCollection services)
services.AddMvcGrid();

var dirSection = Configuration.GetSection("Directories");
var lang = new DiscordMafia.Config.Lang.Language();
// TODO Make language configurable
lang.Load(System.IO.Path.Combine(dirSection["Config"], "Lang/ru"));
services.AddSingleton(typeof(DiscordMafia.Services.ILanguage), lang);
var settings = new DiscordMafia.Config.MainSettings(System.IO.Path.Combine(dirSection["Config"], "mainSettings.xml"), System.IO.Path.Combine(dirSection["Config"], "Local/mainSettings.xml"));
settings.LoadLanguage();
services.AddSingleton(typeof(DiscordMafia.Services.ILanguage), settings.Language);
services.AddSingleton(typeof(DiscordMafia.Config.MainSettings), settings);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Loading

0 comments on commit 72968b6

Please sign in to comment.