forked from SerbiaStrong-220/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paper auto form tweaks (SerbiaStrong-220#2461)
- Loading branch information
Showing
5 changed files
with
125 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Content.Shared/SS220/Paper/PaperAutoFormDatasetPrototype.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = %должность |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |