Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MapChooserSharp.API.MapCycleController;

/// <summary>
/// Map extending management
/// </summary>
public interface IMcsMapCycleExtendControllerApi
{
/// <summary>
/// Extends a current map.
/// </summary>
/// <param name="extendTime">extends a round, timelimit and roundtime. It depends on a map type</param>
/// <returns>McsMapCycleExtendResult.Extended if map is extended successfully</returns>
public McsMapCycleExtendResult ExtendCurrentMap(int extendTime);
}
22 changes: 22 additions & 0 deletions MapChooserSharp.API/MapCycleController/McsMapCycleExtendResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace MapChooserSharp.API.MapCycleController;

/// <summary>
/// Extend result of McsMapCycleExtendController
/// </summary>
public enum McsMapCycleExtendResult
{
/// <summary>
/// When map extended
/// </summary>
Extended,

/// <summary>
/// When failed to extend map somehow
/// </summary>
FailedToExtend,

/// <summary>
/// If map time is minus when extended
/// </summary>
FailedTimeCannotBeZeroOrNegative
}
2 changes: 2 additions & 0 deletions MapChooserSharp/MapChooserSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ protected override void TncssOnPluginLoad(bool hotReload)

RegisterModule<McsMapCycleController>(hotReload);
RegisterModule<McsMapCycleCommands>();
RegisterModule<McsMapCycleExtendController>();
RegisterModule<McsMapCycleExtendCommands>();

#if DEBUG
RegisterModule<McsDebugCommands>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MapChooserSharp.API.MapCycleController;

namespace MapChooserSharp.Modules.MapCycle.Interfaces;

internal interface IMcsInternalMapCycleExtendControllerApi: IMcsMapCycleExtendControllerApi
{

}
147 changes: 147 additions & 0 deletions MapChooserSharp/Modules/MapCycle/McsMapCycleExtendCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using MapChooserSharp.API.MapCycleController;
using MapChooserSharp.API.MapVoteController;
using MapChooserSharp.Interfaces;
using MapChooserSharp.Modules.MapCycle.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TNCSSPluginFoundation.Models.Plugin;
using TNCSSPluginFoundation.Utils.Entity;

namespace MapChooserSharp.Modules.MapCycle;

internal sealed class McsMapCycleExtendCommands(IServiceProvider serviceProvider) : PluginModuleBase(serviceProvider)
{
public override string PluginModuleName => "McsMapCycleExtendCommands";
public override string ModuleChatPrefix => "unused";
protected override bool UseTranslationKeyInModuleChatPrefix => false;

private IMcsInternalMapCycleExtendControllerApi _mcsInternalMapCycleExtendController = null!;
private ITimeLeftUtil _timeLeftUtil = null!;

protected override void OnAllPluginsLoaded()
{
_mcsInternalMapCycleExtendController = ServiceProvider.GetRequiredService<IMcsInternalMapCycleExtendControllerApi>();
_timeLeftUtil = ServiceProvider.GetRequiredService<ITimeLeftUtil>();

Plugin.AddCommand("css_extend", "Extend a current map", CommandExtendMap);
}

protected override void OnUnloadModule()
{
Plugin.RemoveCommand("css_extend", CommandExtendMap);
}


[RequiresPermissions("css/root")]
private void CommandExtendMap(CCSPlayerController? player, CommandInfo info)
{
if (info.ArgCount < 2)
{
if (player == null)
{
Server.PrintToConsole(LocalizeString("MapCycleExtend.Command.Admin.Notification.MapExtended.Usage"));
}
else
{
player.PrintToChat(LocalizeWithPluginPrefixForPlayer(player, "MapCycleExtend.Command.Admin.Notification.MapExtended.Usage"));
}
return;
}

string arg1 = info.ArgByIndex(1);

if (!int.TryParse(info.ArgByIndex(1), out var extendTime))
{
if (player == null)
{
Server.PrintToConsole(LocalizeString("General.Notification.InvalidArgument.WithParam", arg1));
}
else
{
player.PrintToChat(LocalizeWithPluginPrefixForPlayer(player, "General.Notification.InvalidArgument.WithParam", arg1));
}
return;
}

McsMapCycleExtendResult result = _mcsInternalMapCycleExtendController.ExtendCurrentMap(extendTime);


if (result == McsMapCycleExtendResult.FailedToExtend)
{
if (player == null)
{
Server.PrintToConsole(LocalizeString("MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend"));
}
else
{
player.PrintToConsole(LocalizeWithPluginPrefixForPlayer(player, "MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend"));
}

Logger.LogWarning("Failed to extend current map, this may be caused by a misconfigured map time type.");
return;
}
else if (result == McsMapCycleExtendResult.FailedTimeCannotBeZeroOrNegative)
{
if (player == null)
{
Server.PrintToConsole(LocalizeString("MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend.FailedTimeCannotBeZeroOrNegative"));
}
else
{
player.PrintToChat(LocalizeWithPluginPrefixForPlayer(player, "MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend.FailedTimeCannotBeZeroOrNegative"));
}
return;
}

string executorName = PlayerUtil.GetPlayerName(player);
int extendTimeAbs = Math.Abs(extendTime);

bool isExtend = extendTime > 0;

switch (_timeLeftUtil.ExtendType)
{
case McsMapExtendType.TimeLimit:
if (isExtend)
{
PrintLocalizedChatToAll("MapCycleExtend.Command.Admin.Broadcast.MapExtended.TimeLeft", executorName, extendTimeAbs);
Logger.LogInformation($"Admin {executorName} extended mp_timelimit by {extendTimeAbs} minutes");
}
else
{
PrintLocalizedChatToAll("MapCycleExtend.Command.Admin.Broadcast.MapShortened.TimeLeft", executorName, extendTimeAbs);
Logger.LogInformation($"Admin {executorName} shortened mp_timelimit by {extendTimeAbs} minutes");
}
break;

case McsMapExtendType.Rounds:
if (isExtend)
{
PrintLocalizedChatToAll("MapCycleExtend.Command.Admin.Broadcast.MapExtended.Rounds", executorName, extendTimeAbs);
Logger.LogInformation($"Admin {executorName} extended mp_maxrounds by {extendTimeAbs} rounds");
}
else
{
PrintLocalizedChatToAll("MapCycleExtend.Command.Admin.Broadcast.MapShortened.Rounds", executorName, extendTimeAbs);
Logger.LogInformation($"Admin {executorName} shortened mp_maxrounds by {extendTimeAbs} rounds");
}
break;

case McsMapExtendType.RoundTime:
if (isExtend)
{
PrintLocalizedChatToAll("MapCycleExtend.Command.Admin.Broadcast.MapExtended.RoundTime", executorName, extendTimeAbs);
Logger.LogInformation($"Admin {executorName} extended mp_roundtime by {extendTimeAbs} minutes");
}
else
{
PrintLocalizedChatToAll("MapCycleExtend.Command.Admin.Broadcast.MapShortened.RoundTime", executorName, extendTimeAbs);
Logger.LogInformation($"Admin {executorName} shortened mp_roundtime by {extendTimeAbs} minutes");
}
break;
}
}
}
70 changes: 70 additions & 0 deletions MapChooserSharp/Modules/MapCycle/McsMapCycleExtendController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using CounterStrikeSharp.API;
using MapChooserSharp.API.MapCycleController;
using MapChooserSharp.API.MapVoteController;
using MapChooserSharp.Interfaces;
using MapChooserSharp.Modules.MapCycle.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using TNCSSPluginFoundation.Models.Plugin;

namespace MapChooserSharp.Modules.MapCycle;

internal class McsMapCycleExtendController(IServiceProvider serviceProvider) : PluginModuleBase(serviceProvider), IMcsInternalMapCycleExtendControllerApi
{
public override string PluginModuleName => "McsMapCycleExtendController";
public override string ModuleChatPrefix => "unused";
protected override bool UseTranslationKeyInModuleChatPrefix => false;

private ITimeLeftUtil _timeLeftUtil = null!;

public override void RegisterServices(IServiceCollection services)
{
services.AddSingleton<IMcsInternalMapCycleExtendControllerApi>(this);
}

protected override void OnAllPluginsLoaded()
{
_timeLeftUtil = ServiceProvider.GetRequiredService<ITimeLeftUtil>();
}

protected override void OnUnloadModule()
{
}



public McsMapCycleExtendResult ExtendCurrentMap(int extendTime)
{
_timeLeftUtil.ReDetermineExtendType();
switch (_timeLeftUtil.ExtendType)
{
case McsMapExtendType.TimeLimit:
if (_timeLeftUtil.TimeLimit + extendTime < 1)
return McsMapCycleExtendResult.FailedTimeCannotBeZeroOrNegative;

if (_timeLeftUtil.ExtendTimeLimit(extendTime))
return McsMapCycleExtendResult.Extended;

break;

case McsMapExtendType.Rounds:
if (_timeLeftUtil.RoundsLeft + extendTime < 1)
return McsMapCycleExtendResult.FailedTimeCannotBeZeroOrNegative;

if (_timeLeftUtil.ExtendRounds(extendTime))
return McsMapCycleExtendResult.Extended;

break;

case McsMapExtendType.RoundTime:
if (_timeLeftUtil.RoundTimeLeft + extendTime < 1)
return McsMapCycleExtendResult.FailedTimeCannotBeZeroOrNegative;

if (_timeLeftUtil.ExtendRoundTime(extendTime))
return McsMapCycleExtendResult.Extended;

break;
}

return McsMapCycleExtendResult.FailedToExtend;
}
}
17 changes: 13 additions & 4 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
"MapVote.Broadcast.VoteResult.NextMapConfirmed": "Next map is {lime}{0}{default} ({1}% of {2} votes)",
"MapVote.Broadcast.VoteResult.NotChanging": "Map is not changing ({0}% of {1} votes)",
"MapVote.Broadcast.VoteResult.Extend": "Map will extend ({0}% of {1} votes)",
"MapVote.Broadcast.VoteResult.ExtendForTime": "Map is extended for {lime}{0}{default} minuets",
"MapVote.Broadcast.VoteResult.ExtendForRound": "Map is extended for {lime}{0}{default} rounds",
"MapVote.Broadcast.VoteResult.ExtendForTime": "Map is extended by {lime}{0}{default} minuets",
"MapVote.Broadcast.VoteResult.ExtendForRound": "Map is extended by {lime}{0}{default} rounds",
"MapCycle.Broadcast.Admin.SetNextMap": "Admin {lime}{0}{default} set next map to {lime}{1}{default}",
"MapCycle.Broadcast.Admin.ChangedNextMap": "Admin {lime}{0}{default} changed next map to {lime}{1}{default}",
"MapCycle.Command.Admin.Notification.SetNextMap.Usage": "Usage: !setnextmap <MapName>",
Expand Down Expand Up @@ -115,5 +115,14 @@
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Hours": "{0} hours {1} minutes {2} seconds",
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Minutes": "{0} minutes {1} seconds",
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Seconds": "{0} seconds",
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Rounds": "{0} rounds"
}
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Rounds": "{0} rounds",
"MapCycleExtend.Command.Admin.Notification.MapExtended.Usage": "Usage: !extend <minutes|rounds>",
"MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend": "Failed to extend current map! check server logs for details!",
"MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend.FailedTimeCannotBeZeroOrNegative": "Map time cannot be zero or negative!",
"MapCycleExtend.Command.Admin.Broadcast.MapExtended.TimeLeft": "Admin {lime}{0}{default} has extended map time by {lime}{1}{default} minutes",
"MapCycleExtend.Command.Admin.Broadcast.MapExtended.Rounds": "Admin {lime}{0}{default} has extended round by {lime}{1}{default} rounds",
"MapCycleExtend.Command.Admin.Broadcast.MapExtended.RoundTime": "Admin {lime}{0}{default} has extended round time by {lime}{1}{default} minutes",
"MapCycleExtend.Command.Admin.Broadcast.MapShortened.TimeLeft": "Admin {lime}{0}{default} shortened map by {lime}{1}{default} minutes",
"MapCycleExtend.Command.Admin.Broadcast.MapShortened.Rounds": "Admin {lime}{0}{default} shortened round by {lime}{1}{default} rounds",
"MapCycleExtend.Command.Admin.Broadcast.MapShortened.RoundTime": "Admin {lime}{0}{default} shortened round time by {lime}{1}{default} minutes"
}
13 changes: 11 additions & 2 deletions lang/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,14 @@
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Hours": "{0} 時間 {1} 分 {2} 秒",
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Minutes": "{0} 分 {1} 秒",
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Seconds": "{0} 秒",
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Rounds": "{0} ラウンド"
}
"MapCycle.Command.Notification.TimeLeft.TimeFormat.Rounds": "{0} ラウンド",
"MapCycleExtend.Command.Admin.Notification.MapExtended.Usage": "使い方: !extend <分|ラウンド>",
"MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend": "現在のマップの延長に失敗しました! 詳細はサーバーログをご確認ください!",
"MapCycleExtend.Command.Admin.Notification.MapExtended.FailedToExtend.FailedTimeCannotBeZeroOrNegative": "マップの時間は0とマイナスには設定できません!",
"MapCycleExtend.Command.Admin.Broadcast.MapExtended.TimeLeft": "管理者 {lime}{0}{default} がマップの時間を {lime}{1}{default} 分延長しました",
"MapCycleExtend.Command.Admin.Broadcast.MapExtended.Rounds": "管理者 {lime}{0}{default} がラウンドを {lime}{1}{default} ラウンド延長しました",
"MapCycleExtend.Command.Admin.Broadcast.MapExtended.RoundTime": "管理者 {lime}{0}{default} がラウンドの時間を {lime}{1}{default} 分延長しました",
"MapCycleExtend.Command.Admin.Broadcast.MapShortened.TimeLeft": "管理者 {lime}{0}{default} がマップの時間を {lime}{1}{default} 分短縮しました",
"MapCycleExtend.Command.Admin.Broadcast.MapShortened.Rounds": "管理者 {lime}{0}{default} がラウンドを {lime}{1}{default} ラウンド短縮しました",
"MapCycleExtend.Command.Admin.Broadcast.MapShortened.RoundTime": "管理者 {lime}{0}{default} がラウンドの時間を {lime}{1}{default} 分短縮しました"
}