Skip to content

Commit

Permalink
Dynamic Hostname System (#121)
Browse files Browse the repository at this point in the history
* first

* Update Resources/Locale/ru-RU/_CorvaxNext/dynamichostname/hostname.ftl
  • Loading branch information
AwareFoxy authored Dec 15, 2024
1 parent fc5a222 commit 53b0503
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Content.Server.GameTicking;
using Content.Server.Maps;
using Content.Shared.CCVar;
using Content.Shared.GameTicking;
using Robust.Shared;
using Robust.Shared.Configuration;

namespace Content.Server.DynamicHostname;


/// <summary>
/// This handles dynamically updating hostnames.
/// </summary>
public sealed class DynamicHostnameSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _configuration = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly IGameMapManager _mapManager = default!;

private string OriginalHostname { get; set; } = string.Empty;

/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<GameRunLevelChangedEvent>(OnRunLevelChanged);
SubscribeLocalEvent<RoundStartedEvent>(OnRoundStarted);

OriginalHostname = _configuration.GetCVar(CVars.GameHostName);
AttemptUpdateHostname();
}

private void OnRunLevelChanged(GameRunLevelChangedEvent ev) => AttemptUpdateHostname();
private void OnRoundStarted(RoundStartedEvent ev) => AttemptUpdateHostname();

private void OnValueChanged(bool newValue)
{
if (!newValue)
_configuration.SetCVar(CVars.GameHostName, OriginalHostname);

AttemptUpdateHostname();
}

private void AttemptUpdateHostname()
{
var currentMapName = _mapManager.GetSelectedMap()?.MapName;
var currentPresetName = _gameTicker.CurrentPreset?.ModeTitle;

UpdateHostname(currentMapName, currentPresetName);
}

private string GetLocId()
{
switch (_gameTicker.RunLevel)
{
case GameRunLevel.InRound:
return "in-round";
case GameRunLevel.PostRound:
return "post-round";
default:
return "in-lobby";
}
}

private void UpdateHostname(string? currentMapName = null, string? currentPresetName = null)
{
var locId = GetLocId();
var presetName = "No preset";

if (currentPresetName != null)
presetName = Loc.GetString(currentPresetName);

var hostname = Loc.GetString($"dynamic-hostname-{locId}-hostname",
("originalHostName", OriginalHostname),
("preset", presetName),
("mapName", currentMapName ?? "No Map"));

_configuration.SetCVar(CVars.GameHostName, hostname);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dynamic-hostname-in-lobby-hostname = { $originalHostName } | В Лобби
dynamic-hostname-in-round-hostname = { $originalHostName } | Идёт раунд на { $mapName }!
dynamic-hostname-post-round-hostname = { $originalHostName } | Раунд закончился!

0 comments on commit 53b0503

Please sign in to comment.