From 53b05031b6e4092433ccd579988cfc9c12e40893 Mon Sep 17 00:00:00 2001
From: AwareFoxy <135021509+AwareFoxy@users.noreply.github.com>
Date: Sun, 15 Dec 2024 03:00:49 +0100
Subject: [PATCH] Dynamic Hostname System (#121)
* first
* Update Resources/Locale/ru-RU/_CorvaxNext/dynamichostname/hostname.ftl
---
.../DynamicHostname/DynamicHostnameSystem.cs | 81 +++++++++++++++++++
.../_CorvaxNext/dynamichostname/hostname.ftl | 3 +
2 files changed, 84 insertions(+)
create mode 100644 Content.Server/_CorvaxNext/DynamicHostname/DynamicHostnameSystem.cs
create mode 100644 Resources/Locale/ru-RU/_CorvaxNext/dynamichostname/hostname.ftl
diff --git a/Content.Server/_CorvaxNext/DynamicHostname/DynamicHostnameSystem.cs b/Content.Server/_CorvaxNext/DynamicHostname/DynamicHostnameSystem.cs
new file mode 100644
index 00000000000..b93f5e8fdb5
--- /dev/null
+++ b/Content.Server/_CorvaxNext/DynamicHostname/DynamicHostnameSystem.cs
@@ -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;
+
+
+///
+/// This handles dynamically updating hostnames.
+///
+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;
+
+ ///
+ public override void Initialize()
+ {
+ base.Initialize();
+
+ SubscribeLocalEvent(OnRunLevelChanged);
+ SubscribeLocalEvent(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);
+ }
+}
diff --git a/Resources/Locale/ru-RU/_CorvaxNext/dynamichostname/hostname.ftl b/Resources/Locale/ru-RU/_CorvaxNext/dynamichostname/hostname.ftl
new file mode 100644
index 00000000000..ef47a00d711
--- /dev/null
+++ b/Resources/Locale/ru-RU/_CorvaxNext/dynamichostname/hostname.ftl
@@ -0,0 +1,3 @@
+dynamic-hostname-in-lobby-hostname = { $originalHostName } | В Лобби
+dynamic-hostname-in-round-hostname = { $originalHostName } | Идёт раунд на { $mapName }!
+dynamic-hostname-post-round-hostname = { $originalHostName } | Раунд закончился!