Skip to content

Commit

Permalink
Paper auto form tweaks (SerbiaStrong-220#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirus59 authored Jan 7, 2025
1 parent d6c38e3 commit 39fd69d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 66 deletions.
6 changes: 6 additions & 0 deletions Content.Shared/SS220/CCVars/CCVars220.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ public sealed class CCVars220
/// </summary>
public static readonly CVarDef<string> DiscordLinkApiKey =
CVarDef.Create("discord_auth.link_key", "", CVar.SERVERONLY | CVar.CONFIDENTIAL);

/// <summary>
/// How different is the game year from the real one
/// </summary>
public static readonly CVarDef<int> GameYearDelta =
CVarDef.Create("date.game_year_delta", 544, CVar.SERVER | CVar.REPLICATED);
}
15 changes: 15 additions & 0 deletions Content.Shared/SS220/Paper/PaperAutoFormDatasetPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt
using Robust.Shared.Prototypes;

namespace Content.Shared.SS220.Paper;

[Prototype("paperAutoFormDataset")]
public sealed partial class PaperAutoFormDatasetPrototype : IPrototype
{
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;

[DataField]
public Dictionary<string, ReplacedData> KeyWordsReplace = [];
}
148 changes: 82 additions & 66 deletions Content.Shared/SS220/Paper/PaperAutoFormSystem.cs
Original file line number Diff line number Diff line change
@@ -1,101 +1,117 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.GameTicking;
using Content.Shared.Inventory;
using Content.Shared.Paper;
using Content.Shared.PDA;
using Content.Shared.SS220.CCVars;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using System.Text.RegularExpressions;

namespace Content.Shared.SS220.Paper;

public sealed partial class PaperAutoFormSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedGameTicker _gameTicker = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;

private readonly Dictionary<string, ReplacedData> _keyWordsReplace = new()
private const string AutoFormDatasetId = "PaperAutoFormDataset";

private readonly Dictionary<string, ReplacedData> _keyWordsReplace = [];

private int _gameYearDelta;

public override void Initialize()
{
{ "%date", ReplacedData.Date },
{ "%дата", ReplacedData.Date },
{ "%time", ReplacedData.Time },
{ "%время", ReplacedData.Time },
{ "%name", ReplacedData.Name },
{ "%имя", ReplacedData.Name },
{ "%job", ReplacedData.Job },
{ "%должность", ReplacedData.Job }
};
base.Initialize();
_gameYearDelta = _configurationManager.GetCVar(CCVars220.GameYearDelta);

var dataset = _prototypeManager.Index<PaperAutoFormDatasetPrototype>(AutoFormDatasetId);
foreach (var (key, value) in dataset.KeyWordsReplace)
{
var locKey = Loc.GetString(key);
_keyWordsReplace.Add(locKey, value);
}
}

public string ReplaceKeyWords(Entity<PaperComponent> ent, string content)
{
// GenerateRegexAttribute cause errors on the client side and doesn't work
return Regex.Replace(content, "\\u0025\\b(\\w+)\\b", match =>
{
var word = match.Value.ToLower();
if (!_keyWordsReplace.TryGetValue(word, out var replacedData))
return word;

var writer = ent.Comp.Writer;
switch (replacedData)
return replacedData switch
{
case ReplacedData.Date:
{
var day = DateTime.UtcNow.AddHours(3).Day;
var month = DateTime.UtcNow.AddHours(3).Month;
var year = 2568;
return $"{day:00}.{month:00}.{year}";
}

case ReplacedData.Time:
{
var stationTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
return stationTime.ToString("hh\\:mm\\:ss");
}

case ReplacedData.Name when writer != null:
{
if (TryComp<MetaDataComponent>(writer.Value, out var metaData))
return metaData.EntityName;
break;
}

case ReplacedData.Job when writer != null:
{
if (_inventorySystem.TryGetSlotEntity(writer.Value, "id", out var idUid))
{
// PDA
if (EntityManager.TryGetComponent(idUid, out PdaComponent? pda) &&
TryComp<IdCardComponent>(pda.ContainedId, out var id) &&
id.LocalizedJobTitle != null)
{
return id.LocalizedJobTitle;
}

// ID Card
if (EntityManager.TryGetComponent(idUid, out id) &&
id.LocalizedJobTitle != null)
{
return id.LocalizedJobTitle;
}
}

break;
}

default:
break;
}

return word;
ReplacedData.Date => GetCurrentDate(),
ReplacedData.Time => GetStationTime(),
ReplacedData.Name => GetWriterName(writer) ?? word,
ReplacedData.Job => GetWriterJobByID(writer) ?? word,
_ => word
};
});
}

private enum ReplacedData : byte
private string GetCurrentDate()
{
Date,
Time,
Name,
Job,
var day = DateTime.UtcNow.AddHours(3).Day;
var month = DateTime.UtcNow.AddHours(3).Month;
var year = DateTime.UtcNow.AddHours(3).Year + _gameYearDelta;
return $"{day:00}.{month:00}.{year}";
}

private string GetStationTime()
{
var stationTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
return stationTime.ToString("hh\\:mm\\:ss");
}

private string? GetWriterName(EntityUid? writer)
{
if (writer is null ||
!TryComp<MetaDataComponent>(writer.Value, out var metaData))
return null;

return metaData.EntityName;
}

private string? GetWriterJobByID(EntityUid? writer)
{
if (writer is null ||
!_inventorySystem.TryGetSlotEntity(writer.Value, "id", out var idUid))
return null;

string? job = null;
// PDA
if (EntityManager.TryGetComponent(idUid, out PdaComponent? pda) &&
TryComp<IdCardComponent>(pda.ContainedId, out var id) &&
id.LocalizedJobTitle != null)
{
job = id.LocalizedJobTitle;
}
// ID Card
else if (EntityManager.TryGetComponent(idUid, out id) &&
id.LocalizedJobTitle != null)
{
job = id.LocalizedJobTitle;
}

return job;
}
}

public enum ReplacedData : byte
{
Date,
Time,
Name,
Job,
}
11 changes: 11 additions & 0 deletions Resources/Locale/ru-RU/ss220/datasets/paper_auto_form.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
paper_auto_form_date_1 = %date
paper_auto_form_date_2 = %дата
paper_auto_form_time_1 = %time
paper_auto_form_time_2 = %время
paper_auto_form_name_1 = %name
paper_auto_form_name_2 = %имя
paper_auto_form_job_1 = %job
paper_auto_form_job_2 = %должность
11 changes: 11 additions & 0 deletions Resources/Prototypes/SS220/Datasets/paper_auto_form.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- type: paperAutoFormDataset
id: PaperAutoFormDataset
keyWordsReplace:
paper_auto_form_date_1: Date
paper_auto_form_date_2: Date
paper_auto_form_time_1: Time
paper_auto_form_time_2: Time
paper_auto_form_name_1: Name
paper_auto_form_name_2: Name
paper_auto_form_job_1: Job
paper_auto_form_job_2: Job

0 comments on commit 39fd69d

Please sign in to comment.