Traveller/scripts/SloganProvider.cs

65 lines
1.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using ColdMint.scripts.utils;
using Godot;
using FileAccess = Godot.FileAccess;
2024-04-28 13:55:19 +00:00
namespace ColdMint.scripts;
/// <summary>
/// <para>SloganProvider</para>
/// <para>标语提供器</para>
/// </summary>
public static class SloganProvider
{
private static string _csvPath = "res://locals/slogan.csv";
private static string[]? _sloganKeys;
2024-04-28 13:55:19 +00:00
/// <summary>
/// <para>Loading CSV file</para>
/// <para>加载CSV文件</para>
2024-04-28 13:55:19 +00:00
/// </summary>
public static void LoadSloganCsv()
{
var exists = FileAccess.FileExists(_csvPath);
if (!exists)
{
return;
}
using var file = FileAccess.Open(_csvPath, FileAccess.ModeFlags.Read);
var content = file.GetAsText();
var lineStrings = content.Split('\n');
var keys = new List<string>();
foreach (var lineString in lineStrings)
{
var index = lineString.IndexOf(',');
if (index > -1)
{
keys.Add(lineString[..index]);
}
}
if (keys.Count > 0)
{
keys.RemoveAt(0);
}
_sloganKeys = keys.ToArray();
}
2024-04-28 13:55:19 +00:00
/// <summary>
/// <para>Swipe the machine to get a slogan</para>
/// <para>刷机获取一个标语</para>
/// </summary>
/// <returns></returns>
public static string? GetSlogan()
2024-04-28 13:55:19 +00:00
{
if (_sloganKeys == null || _sloganKeys.Length == 0)
{
return null;
}
return TranslationServerUtils.Translate(_sloganKeys[GD.Randi() % _sloganKeys.Length]);
2024-04-28 13:55:19 +00:00
}
}