IW4M-Admin/WebfrontCore/Controllers/ActionController.cs

380 lines
13 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Configuration;
move all the deployment setup into 2.4 pr (#85) * don't run build commands in release * fix test file * Set up CI with Azure Pipelines [skip ci] * Include fonts and fix automessage hidden command * more project changes * migration from bower to libman * more lib man changes * project update for sneaky commands * add missing canvas.js dep update projects not to have stupid extra dlls include in previous * update pipeline file * update post publish script and pipeline definition * fix broken yaml * move encoding conversion to seperate script * remove extra uneeded rank icons remove garbage language files being created remove frontend lib when done * fix publish script path * grab localizations through powershell * fix broken batch :shrug: * actually fixed * only include runtime compilation in debug mode for webfront * don't deploy un minified css use full jquery version * add step to download the scss for open iconic change the font path * update mkdir for iconic path * don't include old iconic css * correct font path for real now * copy script plugins * lots of changes for deployment * build the projects * use projectdir instead of solution dir * nerf script commands plugin fix live radar left over command * actually kill script command post build * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * fix the font file copy (I think) * maybe fix delete folder issue * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines
2019-12-02 16:52:36 -05:00
using SharedLibraryCore.Interfaces;
using WebfrontCore.ViewModels;
using static SharedLibraryCore.Database.Models.EFClient;
namespace WebfrontCore.Controllers
{
public class ActionController : BaseController
{
private readonly ApplicationConfiguration _appConfig;
move all the deployment setup into 2.4 pr (#85) * don't run build commands in release * fix test file * Set up CI with Azure Pipelines [skip ci] * Include fonts and fix automessage hidden command * more project changes * migration from bower to libman * more lib man changes * project update for sneaky commands * add missing canvas.js dep update projects not to have stupid extra dlls include in previous * update pipeline file * update post publish script and pipeline definition * fix broken yaml * move encoding conversion to seperate script * remove extra uneeded rank icons remove garbage language files being created remove frontend lib when done * fix publish script path * grab localizations through powershell * fix broken batch :shrug: * actually fixed * only include runtime compilation in debug mode for webfront * don't deploy un minified css use full jquery version * add step to download the scss for open iconic change the font path * update mkdir for iconic path * don't include old iconic css * correct font path for real now * copy script plugins * lots of changes for deployment * build the projects * use projectdir instead of solution dir * nerf script commands plugin fix live radar left over command * actually kill script command post build * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * fix the font file copy (I think) * maybe fix delete folder issue * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines
2019-12-02 16:52:36 -05:00
public ActionController(IManager manager) : base(manager)
{
_appConfig = manager.GetApplicationSettings().Configuration();
move all the deployment setup into 2.4 pr (#85) * don't run build commands in release * fix test file * Set up CI with Azure Pipelines [skip ci] * Include fonts and fix automessage hidden command * more project changes * migration from bower to libman * more lib man changes * project update for sneaky commands * add missing canvas.js dep update projects not to have stupid extra dlls include in previous * update pipeline file * update post publish script and pipeline definition * fix broken yaml * move encoding conversion to seperate script * remove extra uneeded rank icons remove garbage language files being created remove frontend lib when done * fix publish script path * grab localizations through powershell * fix broken batch :shrug: * actually fixed * only include runtime compilation in debug mode for webfront * don't deploy un minified css use full jquery version * add step to download the scss for open iconic change the font path * update mkdir for iconic path * don't include old iconic css * correct font path for real now * copy script plugins * lots of changes for deployment * build the projects * use projectdir instead of solution dir * nerf script commands plugin fix live radar left over command * actually kill script command post build * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * fix the font file copy (I think) * maybe fix delete folder issue * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines
2019-12-02 16:52:36 -05:00
}
public IActionResult BanForm()
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_BAN_NAME"],
Name = "Ban",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name = "Reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
},
new InputInfo()
{
Name ="Duration",
Label=Localization["WEBFRONT_ACTION_LABEL_DURATION"],
Type="select",
Values = new Dictionary<string, string>()
{
{"1", $"1 {Localization["GLOBAL_TIME_HOUR"]}" },
{"2", $"6 {Localization["GLOBAL_TIME_HOURS"]}" },
{"3", $"1 {Localization["GLOBAL_TIME_DAY"]}" },
{"4", $"2 {Localization["GLOBAL_TIME_DAYS"]}" },
{"5", $"1 {Localization["GLOBAL_TIME_WEEK"]}" },
{"6", $"{Localization["WEBFRONT_ACTION_SELECTION_PERMANENT"]}" },
}
}
},
2019-08-04 18:06:07 -04:00
Action = "BanAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> BanAsync(int targetId, string Reason, int Duration)
{
string duration = string.Empty;
var loc = Utilities.CurrentLocalization.LocalizationIndex;
switch (Duration)
{
case 1:
duration = $"1{loc["GLOBAL_TIME_HOURS"][0]}";
break;
case 2:
duration = $"6{loc["GLOBAL_TIME_HOURS"][0]}";
break;
case 3:
duration = $"1{loc["GLOBAL_TIME_DAYS"][0]}";
break;
case 4:
duration = $"2{loc["GLOBAL_TIME_DAYS"][0]}";
break;
case 5:
duration = $"1{loc["GLOBAL_TIME_WEEKS"][0]}";
break;
}
string command = Duration == 6 ?
$"{_appConfig.CommandPrefix}ban @{targetId} {Reason}" :
$"{_appConfig.CommandPrefix}tempban @{targetId} {duration} {Reason}";
var server = Manager.GetServers().First();
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = server.EndPoint,
command
}));
}
public IActionResult UnbanForm()
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_UNBAN_NAME"],
Name = "Unban",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name = "Reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
}
},
2019-08-04 18:06:07 -04:00
Action = "UnbanAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> UnbanAsync(int targetId, string Reason)
{
var server = Manager.GetServers().First();
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}unban @{targetId} {Reason}"
}));
}
public IActionResult LoginForm()
{
var login = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LOGIN_NAME"],
Name = "Login",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name = "clientId",
Label = Localization["WEBFRONT_ACTION_LABEL_ID"]
},
new InputInfo()
{
Name = "Password",
Label = Localization["WEBFRONT_ACTION_LABEL_PASSWORD"],
Type = "password",
}
},
Action = "LoginAsync"
};
return View("_ActionForm", login);
}
public async Task<IActionResult> LoginAsync(int clientId, string password)
{
return await Task.FromResult(RedirectToAction("LoginAsync", "Account", new { clientId, password }));
}
public IActionResult EditForm()
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LABEL_EDIT"],
Name = "Edit",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name ="level",
Label=Localization["WEBFRONT_PROFILE_LEVEL"],
Type="select",
Values = Enum.GetValues(typeof(Permission)).OfType<Permission>()
.Where(p => p <= Client.Level)
.Where(p => p != Permission.Banned)
.Where(p => p != Permission.Flagged)
.ToDictionary(p => p.ToString(), p=> p.ToLocalizedLevelName())
},
},
2019-08-04 18:06:07 -04:00
Action = "EditAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> EditAsync(int targetId, string level)
{
var server = Manager.GetServers().First();
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}setlevel @{targetId} {level}"
}));
}
public IActionResult GenerateLoginTokenForm()
{
var info = new ActionInfo()
{
ActionButtonLabel = "Generate",
Name = "GenerateLoginToken",
Action = "GenerateLoginTokenAsync",
Inputs = new List<InputInfo>()
};
return View("_ActionForm", info);
}
[Authorize]
public string GenerateLoginTokenAsync()
{
var state = Manager.TokenAuthenticator.GenerateNextToken(Client.NetworkId);
return string.Format(Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_GENERATETOKEN_SUCCESS"], state.Token, $"{state.RemainingTime} {Utilities.CurrentLocalization.LocalizationIndex["GLOBAL_MINUTES"]}", Client.ClientId);
}
public IActionResult ChatForm(long id)
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LABEL_SUBMIT_MESSAGE"],
Name = "Chat",
Inputs = new List<InputInfo>
{
new InputInfo()
{
Name = "message",
Type = "text",
Label = Localization["WEBFRONT_ACTION_LABEL_MESSAGE"]
},
new InputInfo()
{
Name = "id",
Value = id.ToString(),
Type = "hidden"
}
},
Action = "ChatAsync"
};
return View("_ActionForm", info);
}
public async Task<IActionResult> ChatAsync(long id, string message)
{
var server = Manager.GetServers().First(_server => _server.EndPoint == id);
server.ChatHistory.Add(new SharedLibraryCore.Dtos.ChatInfo()
{
ClientId = Client.ClientId,
Message = message,
Name = Client.Name,
ServerGame = server.GameName,
Time = DateTime.Now
});
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}say {message}"
}));
}
public async Task<IActionResult> RecentClientsForm()
{
var clients = await Manager.GetClientService().GetRecentClients();
return View("~/Views/Shared/Components/Client/_RecentClients.cshtml", clients);
}
2019-08-04 18:06:07 -04:00
public IActionResult FlagForm()
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_FLAG_NAME"],
Name = "Flag",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name = "reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
}
},
Action = "FlagAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> FlagAsync(int targetId, string reason)
{
var server = Manager.GetServers().First();
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}flag @{targetId} {reason}"
2019-08-04 18:06:07 -04:00
}));
}
public IActionResult UnflagForm()
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_UNFLAG_NAME"],
Name = "Unflag",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name = "reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
}
},
Action = "UnflagAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> UnflagAsync(int targetId, string reason)
{
var server = Manager.GetServers().First();
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}unflag @{targetId} {reason}"
2019-08-04 18:06:07 -04:00
}));
}
public IActionResult KickForm(int id)
{
var info = new ActionInfo()
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_KICK_NAME"],
Name = "Kick",
Inputs = new List<InputInfo>()
{
new InputInfo()
{
Name = "reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
},
new InputInfo()
{
Name = "targetId",
Type = "hidden",
Value = id.ToString()
}
},
Action = "KickAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> KickAsync(int targetId, string reason)
{
var client = Manager.GetActiveClients().FirstOrDefault(_client => _client.ClientId == targetId);
if (client == null)
{
return BadRequest(Localization["WEBFRONT_ACTION_KICK_DISCONNECT"]);
}
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{
serverId = client.CurrentServer.EndPoint,
command = $"{_appConfig.CommandPrefix}kick {client.ClientNumber} {reason}"
}));
}
}
}