Skip to content

Commit

Permalink
Dynamic Hostname System (#1296)
Browse files Browse the repository at this point in the history
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

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.

---

<!--
This is default collapsed, readers click to expand it and see all your
media
The PR media section can get very large at times, so this is a good way
to keep it clean
The title is written using HTML tags
The title must be within the <summary> tags or you won't see it
-->

<details><summary><h1>Media</h1></summary>
<p>


![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)



</p>
</details>

---

# Changelog

<!--
You can add an author after the `:cl:` to change the name that appears
in the changelog (ex: `:cl: Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->
nah
  • Loading branch information
sleepyyapril authored Nov 30, 2024
1 parent c7f1b4f commit de98c2a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Content.Server/DynamicHostname/DynamicHostnameSystem.cs
Original file line number Diff line number Diff line change
@@ -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;


/// <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);

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);
}
}
8 changes: 8 additions & 0 deletions Content.Shared/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2790,5 +2790,13 @@ public static readonly CVarDef<float>
/// Requires auto voting to be enabled.
public static readonly CVarDef<bool> PresetAutoVoteEnabled =
CVarDef.Create("vote.preset_autovote_enabled", true, CVar.SERVERONLY);

/// <summary>
/// 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
/// </summary>
public static readonly CVarDef<bool> UseDynamicHostname =
CVarDef.Create("game.use_dynamic_hostname", false, CVar.SERVERONLY);
}
}
3 changes: 3 additions & 0 deletions Resources/Locale/en-US/dynamichostname/hostname.ftl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de98c2a

Please sign in to comment.