From de98c2aec2c072abeb6c81c74b830d72e29ba0c8 Mon Sep 17 00:00:00 2001 From: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com> Date: Sat, 30 Nov 2024 13:28:24 -0400 Subject: [PATCH] Dynamic Hostname System (#1296) # Description Change your hostname whenever map, preset, or runlevel changes! Variables given on all hostname locale strings: - {$mapName} - The name of the current map. - {$preset} - The name of the current preset. - {$originalHostname} - What you originally had game.hostname set to, before updating. ---

Media

![image](https://github.com/user-attachments/assets/97cba40b-5ceb-40f8-91e4-93450cd22b78) ![image](https://github.com/user-attachments/assets/2cf27e65-b493-4916-bedd-aa69eaa0135b) ![image](https://github.com/user-attachments/assets/234f5be0-7a8c-41e1-abdf-10e9c6700a90) ![image](https://github.com/user-attachments/assets/522525d6-fb23-45b9-b559-deffb3f37a58)

--- # Changelog nah --- .../DynamicHostname/DynamicHostnameSystem.cs | 86 +++++++++++++++++++ Content.Shared/CCVar/CCVars.cs | 8 ++ .../Locale/en-US/dynamichostname/hostname.ftl | 3 + 3 files changed, 97 insertions(+) create mode 100644 Content.Server/DynamicHostname/DynamicHostnameSystem.cs create mode 100644 Resources/Locale/en-US/dynamichostname/hostname.ftl diff --git a/Content.Server/DynamicHostname/DynamicHostnameSystem.cs b/Content.Server/DynamicHostname/DynamicHostnameSystem.cs new file mode 100644 index 0000000000..386447534c --- /dev/null +++ b/Content.Server/DynamicHostname/DynamicHostnameSystem.cs @@ -0,0 +1,86 @@ +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); + + Subs.CVar(_configuration, CCVars.UseDynamicHostname, OnValueChanged); + + 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() + { + if (!_configuration.GetCVar(CCVars.UseDynamicHostname)) + return; + + 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/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index 40c1696c13..d597ca04f7 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -2790,5 +2790,13 @@ public static readonly CVarDef /// Requires auto voting to be enabled. public static readonly CVarDef PresetAutoVoteEnabled = CVarDef.Create("vote.preset_autovote_enabled", true, CVar.SERVERONLY); + + /// + /// Set to true to enable the dynamic hostname system. + /// Automatically updates the hostname to include current map and preset. + /// Configure what that looks like for you in Resources/Prototypes/Locale/en-US/dynamichostname/hostname.ftl + /// + public static readonly CVarDef UseDynamicHostname = + CVarDef.Create("game.use_dynamic_hostname", false, CVar.SERVERONLY); } } diff --git a/Resources/Locale/en-US/dynamichostname/hostname.ftl b/Resources/Locale/en-US/dynamichostname/hostname.ftl new file mode 100644 index 0000000000..66aa2f13f9 --- /dev/null +++ b/Resources/Locale/en-US/dynamichostname/hostname.ftl @@ -0,0 +1,3 @@ +dynamic-hostname-in-lobby-hostname = { $originalHostName } | Sitting in lobby +dynamic-hostname-in-round-hostname = { $originalHostName } | Playing { $preset } on { $mapName } +dynamic-hostname-post-round-hostname = { $originalHostName } | Round over \ No newline at end of file