150 lines
5.6 KiB
C#
150 lines
5.6 KiB
C#
using System;
|
|
using System.Numerics;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public static class Tools
|
|
{
|
|
public static Sprite GetIconForUser(BigInteger user)
|
|
{
|
|
if (user == 1) return Resources.Load<Sprite>("Icons/Icons/bird_-1");
|
|
else if (user == 2) return Resources.Load<Sprite>("Icons/Icons/bird_-2");
|
|
else if (user == 4) return Resources.Load<Sprite>("Icons/Icons/bird_-3");
|
|
else if (user == 3) return Resources.Load<Sprite>("Icons/Icons/bird_-4");
|
|
else return Resources.Load<Sprite>("Icons/Icons/bird_1");
|
|
}
|
|
|
|
public static string FormatWithCommas(string number)
|
|
{
|
|
try { return FormatWithCommas(BigInteger.Parse(number)); }
|
|
catch { return number; }
|
|
}
|
|
|
|
public static string FormatWithCommas(BigInteger number)
|
|
{
|
|
return string.Format("{0:N0}", number);
|
|
}
|
|
|
|
public static void UpdateStatusText(TMP_Text statusText, string message, Color color)
|
|
{
|
|
statusText.text = message;
|
|
statusText.color = color;
|
|
}
|
|
|
|
public static void RenderFromBase64(string base64, Image targetImage)
|
|
{
|
|
byte[] imageData = Convert.FromBase64String(base64);
|
|
Texture2D tex = new(2, 2, TextureFormat.ARGB32, false);
|
|
if (!tex.LoadImage(imageData)) return;
|
|
|
|
tex.filterMode = FilterMode.Point;
|
|
tex.Apply(false, false);
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new UnityEngine.Vector2(0.5f, 0.5f));
|
|
targetImage.sprite = sprite;
|
|
}
|
|
|
|
public static void RenderFromBase64(string base64, SpriteRenderer targetImage)
|
|
{
|
|
byte[] imageData = Convert.FromBase64String(base64);
|
|
Texture2D tex = new(2, 2, TextureFormat.ARGB32, false);
|
|
if (!tex.LoadImage(imageData)) return;
|
|
|
|
tex.filterMode = FilterMode.Point;
|
|
tex.Apply(false, false);
|
|
|
|
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new UnityEngine.Vector2(0.5f, 0.5f));
|
|
targetImage.sprite = sprite;
|
|
}
|
|
|
|
public static void RefreshHierarchy(GameObject root)
|
|
{
|
|
foreach (Transform child in root.transform) RefreshHierarchy(child.gameObject);
|
|
if (root.TryGetComponent<RectTransform>(out var rect)) LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
|
|
}
|
|
|
|
public static string Sha512Sum(byte[] data)
|
|
{
|
|
using var sha512 = SHA512.Create();
|
|
var hash = sha512.ComputeHash(data);
|
|
|
|
var sb = new StringBuilder(hash.Length * 2);
|
|
foreach (var b in hash)
|
|
sb.Append(b.ToString("x2"));
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static BigInteger CalculateXP(BigInteger normalBerries, BigInteger poisonBerries, BigInteger slowBerries, BigInteger ultraBerries, BigInteger speedyBerries, BigInteger coinBerries)
|
|
{
|
|
BigInteger totalXp = 0;
|
|
totalXp += normalBerries;
|
|
totalXp -= poisonBerries;
|
|
totalXp -= slowBerries;
|
|
totalXp += ultraBerries * 5;
|
|
totalXp += speedyBerries * 10;
|
|
totalXp += coinBerries * 10;
|
|
if (totalXp < 0) totalXp = 0;
|
|
return totalXp;
|
|
}
|
|
|
|
public static BigInteger CalculateXP()
|
|
{
|
|
return CalculateXP(
|
|
BazookaManager.Instance.GetGameStoreTotalNormalBerries(),
|
|
BazookaManager.Instance.GetGameStoreTotalPoisonBerries(),
|
|
BazookaManager.Instance.GetGameStoreTotalSlowBerries(),
|
|
BazookaManager.Instance.GetGameStoreTotalUltraBerries(),
|
|
BazookaManager.Instance.GetGameStoreTotalSpeedyBerries(),
|
|
BazookaManager.Instance.GetGameStoreTotalCoinBerries()
|
|
);
|
|
}
|
|
|
|
public static (BigInteger totalXp, double level, BigInteger currentXpInLevel, BigInteger totalXpForLevel, double percentDone) GetLevelInfo(BigInteger xp)
|
|
{
|
|
double levelDivisor = 50.0;
|
|
|
|
double discriminant = 95 * 95 + levelDivisor * 2 * (double)xp;
|
|
double level = (-95 + Math.Sqrt(discriminant)) / levelDivisor;
|
|
double flooredLevel = Math.Floor(level);
|
|
|
|
BigInteger xpAtStartOfLevel = (BigInteger)(levelDivisor / 2 * flooredLevel * flooredLevel + 95 * flooredLevel);
|
|
|
|
double nextLevel = flooredLevel + 1;
|
|
BigInteger xpAtNextLevel = (BigInteger)(levelDivisor / 2 * nextLevel * nextLevel + 95 * nextLevel);
|
|
|
|
BigInteger totalXpForLevel = xpAtNextLevel - xpAtStartOfLevel;
|
|
BigInteger currentXpInLevel = xp - xpAtStartOfLevel;
|
|
|
|
double percentDone = totalXpForLevel == 0 ? 0.0 : (double)currentXpInLevel / (double)totalXpForLevel * 100.0;
|
|
|
|
return (xp, flooredLevel + 1, currentXpInLevel, totalXpForLevel, percentDone);
|
|
}
|
|
|
|
public static (BigInteger totalXp, double level, BigInteger currentXpInLevel, BigInteger totalXpForLevel, double percentDone) GetLevelInfo()
|
|
{
|
|
var (totalXp, level, currentXpInLevel, totalXpForLevel, percentDone) = GetLevelInfo(CalculateXP());
|
|
return (totalXp, level, currentXpInLevel, totalXpForLevel, percentDone);
|
|
}
|
|
|
|
public static string FormatHumanTime(long unixTimestamp)
|
|
{
|
|
DateTime date = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).LocalDateTime;
|
|
DateTime now = DateTime.Now;
|
|
|
|
bool isToday = date.Year == now.Year && date.Month == now.Month && date.Day == now.Day;
|
|
DateTime yesterday = now.AddDays(-1);
|
|
bool isYesterday = date.Year == yesterday.Year && date.Month == yesterday.Month && date.Day == yesterday.Day;
|
|
|
|
string timeStr = date.ToString("t");
|
|
|
|
if (isToday) return timeStr;
|
|
if (isYesterday) return $"Yesterday at {timeStr}";
|
|
|
|
string dateStr = date.ToShortDateString();
|
|
return $"{dateStr}, {timeStr}";
|
|
}
|
|
} |