JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/Misc/StringProcessor.cs

148 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Localization;
using UnityEngine;
namespace Misc
{
public static class StringProcessor
{
// in-game strings have bracketed markers for icons or other annotations (e.g. [icon:GEN-BACK])
// these are not supported in the mobile app, so we just remove them
public static string removeMarkers(string str)
{
var bracketRegex = new Regex(@"\[[^\]]*\]\s*");
return bracketRegex.Replace(str, string.Empty);
}
public static string formatCustomizableString(string text, Dictionary<string, string> customValues)
{
// make sure var is in lower case
var varRegex = new Regex(@"\[var:", RegexOptions.IgnoreCase);
text = varRegex.Replace(text, "[var:");
return customValues.Aggregate(text, (current, customValue) => current.Replace("[var:" + customValue.Key + "]", customValue.Value));
}
public static string applyEllipsis(string text, GUIStyle style, float maxWidth)
{
var ellipsisWidth = style.CalcSize(new GUIContent("...")).x;
var textWidth = style.CalcSize(new GUIContent(text)).x;
// use an ellipsis only if the text doesn't fit
if (textWidth >= maxWidth)
{
int ellipsisPos;
for (ellipsisPos = 0; ellipsisPos < text.Length; ellipsisPos++)
{
var width = style.CalcSize(new GUIContent(text.Substring(0, ellipsisPos + 1))).x;
if (width > maxWidth - ellipsisWidth)
break;
}
return text.Substring(0, ellipsisPos) + "...";
}
else
{
return text;
}
}
// group numbers for better readability
public static string formatScore(int score)
{
return score.ToString("N0", new CultureInfo("fr-FR"));
}
// round to 2 decimals
public static string formatCalories(double calories)
{
return calories.ToString("F2", CultureInfo.InvariantCulture);
}
public static string makeRankString(int rank)
{
string suffix;
switch (rank)
{
case 1: suffix = LocalizationManager.Instance.GetLocalizedString(11433); break; // "st"
case 2: suffix = LocalizationManager.Instance.GetLocalizedString(11434); break; // "nd"
case 3: suffix = LocalizationManager.Instance.GetLocalizedString(11435); break; // "rd"
default: suffix = LocalizationManager.Instance.GetLocalizedString(11432); break; // "th"
}
return rank.ToString() + suffix;
}
public static string parseIPAddress(string textValue)
{
// check format first
var ipFormat = new Regex(@"^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{1,5})?\s*$");
if (!ipFormat.IsMatch(textValue))
return null;
// remove extra spaces
return textValue.Trim();
}
public static string GetLocalizedDateDifference(DateTime date)
{
var timeDifference = DateTime.UtcNow - date;
// if the phone clock is not completely aligned with the server one,
// or the time is too recent (e.g. 1s), ensure the time is always at least 1min
var minimumTimeDifference = TimeSpan.FromMinutes(1.0);
timeDifference = timeDifference < minimumTimeDifference ? minimumTimeDifference : timeDifference;
var years = timeDifference.Days / 365; // wrong but near enough
var months = timeDifference.Days / 30; // wrong but near enough
var weeks = timeDifference.Days / 7;
var timeValue = new Dictionary<string, string>();
string timeString;
int locId;
if (years > 0)
{
timeString = years.ToString();
locId = 13856;
}
else if (months > 0)
{
timeString = months.ToString();
locId = 13857;
}
else if (weeks > 0)
{
timeString = weeks.ToString();
locId = 13858;
}
else if (timeDifference.Days > 0)
{
timeString = timeDifference.Days.ToString();
locId = 13859;
}
else if (timeDifference.Hours > 0)
{
timeString = timeDifference.Hours.ToString();
locId = 13860;
}
else
{
// if (timeDifference.Minutes > 0)
timeString = timeDifference.Minutes.ToString();
locId = 13861;
}
timeValue.Add("TIME_UNIT", timeString);
return LocalizationManager.Instance.GetLocalizedString(locId, timeValue);
}
}
}