Files

65 lines
2.2 KiB
C#
Raw Permalink Normal View History

2026-04-29 13:10:00 +07:00
using UnityEngine;
namespace Hallucinate.Game
{
2026-06-09 22:46:32 +07:00
/// <summary>
/// Pure logic for Elo rating calculations.
/// Follows the 1v1 competitive formula with dynamic K-factor.
/// </summary>
2026-04-29 13:10:00 +07:00
public static class EloSystem
{
2026-06-09 22:46:32 +07:00
public const int RATING_FLOOR = 100;
public const int PLACEMENT_GAMES = 30;
2026-04-29 13:10:00 +07:00
public static EloResult Calculate(
int ratingA, int ratingB,
int gamesPlayedA, int gamesPlayedB,
float resultA) // 1=win, 0=lose, 0.5=draw
{
2026-06-09 22:46:32 +07:00
// 1. Expected Scores
2026-04-29 13:10:00 +07:00
float eA = 1f / (1f + Mathf.Pow(10f, (ratingB - ratingA) / 400f));
2026-06-09 22:46:32 +07:00
float eB = 1f - eA;
// 2. K-Factors
2026-04-29 13:10:00 +07:00
int kA = GetK(ratingA, gamesPlayedA);
int kB = GetK(ratingB, gamesPlayedB);
2026-06-09 22:46:32 +07:00
// 3. New Ratings
int nA = Mathf.Max(RATING_FLOOR, Mathf.RoundToInt(ratingA + kA * (resultA - eA)));
int nB = Mathf.Max(RATING_FLOOR, Mathf.RoundToInt(ratingB + kB * ((1f - resultA) - eB)));
2026-04-29 13:10:00 +07:00
return new EloResult(nA, nB, nA - ratingA, nB - ratingB);
}
2026-06-09 22:46:32 +07:00
private static int GetK(int rating, int gamesPlayed)
{
if (gamesPlayed < PLACEMENT_GAMES) return 40;
if (rating < 1200) return 32;
if (rating < 2000) return 24;
return 16;
}
2026-04-29 13:10:00 +07:00
public static string GetRank(int rating)
{
if (rating < 800) return "Iron";
if (rating < 1000) return "Bronze";
if (rating < 1200) return "Silver";
if (rating < 1500) return "Gold";
if (rating < 1800) return "Platinum";
if (rating < 2100) return "Diamond";
return "Master";
}
public static string GetRankColor(int rating)
{
2026-06-09 22:46:32 +07:00
if (rating < 800) return "#8A8A8A"; // Iron
if (rating < 1000) return "#CD7F32"; // Bronze
if (rating < 1200) return "#C0C0C0"; // Silver
if (rating < 1500) return "#FFD700"; // Gold
if (rating < 1800) return "#4DC8A0"; // Platinum
if (rating < 2100) return "#7B6EE8"; // Diamond
return "#E84D8A"; // Master
2026-04-29 13:10:00 +07:00
}
}
}