ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Arena/LeagueRules.cs

161 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace TKS.OutGame
{
/// <summary>등급 투기장 세이브(컨텐츠 모드 ④ · 기기 세이브 모드 · 서버 모드 = GET /tks/modes/arena 로 덮는다).</summary>
[Serializable]
public class LeagueState
{
public int score = -1;
public int plays, wins, losses;
public string day = "";
public int used, bought, refreshUsed;
public bool dailyClaimed;
[Tooltip("상대 3명 = arena.samples 의 순번 + 종류(strong · even · weak)")]
public List<int> pick = new List<int>();
public List<string> kinds = new List<string>();
public bool serverView;
public int tier;
public int rank, rankTotal;
public int season;
}
/// <summary>
/// 등급 투기장 규칙(기기 세이브 모드 · 서버 `league` 와 같은 점수 · 등급 · 횟수) — 표 = content_modes_config arena.* · arena_tiers.
/// 상대 3명 = 강한 · 비슷한 · 약한(arena.candidates · 기기 = 표본 상대에서 점수로 고름 · 서버 = 실제 유저) · 이기면 score_win[종류] · 지면 score_lose[종류] · 메달 = arena.medal.
/// </summary>
public static class LeagueRules
{
public const string Kind = "league";
const string M = "arena";
static readonly string[] Kinds = { "strong", "even", "weak" };
static void Roll(OutGameData db, SaveData d, DateTime nowUtc)
{
var s = d.league;
if (s.score < 0) s.score = ContentTables.Int(M, "score_start");
var day = EconomyRules.DayKey(nowUtc);
if (s.day != day && !s.serverView)
{
s.day = day;
s.used = s.bought = s.refreshUsed = 0;
s.dailyClaimed = false;
}
ArenaRules.EnsureSeason(db, d, nowUtc, new System.Random(Environment.TickCount));
if (s.pick.Count == 0 && !s.serverView) Pick(d);
}
/// <summary>표본에서 상대 3명(점수 높은 · 가까운 · 낮은).</summary>
static void Pick(SaveData d)
{
var s = d.league;
s.pick.Clear();
s.kinds.Clear();
var list = d.arena.samples;
if (list.Count == 0) return;
var idx = new List<int>();
for (int i = 0; i < list.Count; i++) idx.Add(i);
idx.Sort((a, b) => list[b].score.CompareTo(list[a].score));
int n = Mathf.Min(ContentTables.Int(M, "candidates"), idx.Count);
for (int k = 0; k < n && k < Kinds.Length; k++)
{
int at = n <= 1 ? 0 : Mathf.RoundToInt(k * (idx.Count - 1) / (float)(n - 1));
s.pick.Add(idx[at]);
s.kinds.Add(Kinds[k]);
}
}
public static int Tier(int score)
{
int t = 1;
foreach (var r in ContentTables.Rows("arena_tiers"))
if (score >= r.I("n_score_min")) t = Mathf.Max(t, r.I("n_tier"));
return t;
}
public static string TierName(int tier)
{
var r = ContentTables.Find("arena_tiers", "n_tier", tier.ToString());
return r != null ? r.S("s_name_ko") : tier.ToString();
}
public static int Left(OutGameData db, SaveData d, DateTime nowUtc)
{
Roll(db, d, nowUtc);
return Mathf.Max(0, ContentTables.Int(M, "entries_per_day") + d.league.bought - d.league.used);
}
public static bool Unlocked(SaveData d)
{
if (d.league.serverView) return true;
int ch, st;
if (!ArenaRules.ParseStageKey(ContentTables.Str(M, "unlock_stage"), out ch, out st)) return true;
var s = d.Stage(ch, st, false);
return s != null && s.clears > 0;
}
public static string Can(OutGameData db, SaveData d, int k, DateTime nowUtc)
{
if (ArenaRules.Settling(d, ArenaRules.Arena, nowUtc)) return ArenaRules.SettleNotice;
if (!Unlocked(d)) return ContentTables.Str(M, "unlock_stage") + " 클리어 뒤 열림";
Roll(db, d, nowUtc);
if (k < 0 || k >= d.league.pick.Count) return "없는 상대";
if (Left(db, d, nowUtc) <= 0) return "오늘 입장을 모두 썼습니다";
return null;
}
public static string Enter(OutGameData db, SaveData d, int k, DateTime nowUtc)
{
var why = Can(db, d, k, nowUtc);
if (why != null) return why;
d.league.used++;
return null;
}
public static string Refresh(OutGameData db, SaveData d, DateTime nowUtc)
{
Roll(db, d, nowUtc);
if (d.league.refreshUsed >= ContentTables.Int(M, "refresh_free_per_day")) return "오늘 무료 새로고침을 모두 썼습니다";
d.league.refreshUsed++;
d.arena.samples.Clear();
ArenaRules.EnsureSeason(db, d, nowUtc, new System.Random(Environment.TickCount));
Pick(d);
return null;
}
public static string BuyEntry(OutGameData db, SaveData d, DateTime nowUtc)
{
Roll(db, d, nowUtc);
int gem = ContentTables.Int(M, "buy_entry_gem");
if (d.league.bought >= ContentTables.Int(M, "buy_entries_per_day")) return "오늘 구매 한도";
if (d.gem < gem) return "보옥 부족 " + d.gem + " / " + gem;
d.gem -= gem;
d.league.bought++;
return null;
}
public static ModeResult Result(OutGameData db, SaveData d, int k, bool win, List<string> lines, DateTime nowUtc)
{
var s = d.league;
string kind = k >= 0 && k < s.kinds.Count ? s.kinds[k] : "even";
float delta;
ContentTables.Map(M, win ? "score_win" : "score_lose").TryGetValue(kind, out delta);
float medal;
ContentTables.Map(M, "medal").TryGetValue(win ? "win" : "lose", out medal);
int before = s.score;
s.score = Mathf.Max(0, s.score + Mathf.RoundToInt(delta));
s.plays++;
if (win) s.wins++;
else s.losses++;
d.arena.medals += Mathf.RoundToInt(medal);
var m = new ModeResult { kind = Kind, win = win, title = "투기장 " + (win ? "승리" : "패배") };
if (lines != null) m.lines.AddRange(lines);
m.lines.Add("점수 " + before + " → " + s.score + " (" + (delta >= 0 ? "+" : "") + Mathf.RoundToInt(delta) + " · " + kind + ") · " + TierName(Tier(s.score)));
m.lines.Add("메달 +" + Mathf.RoundToInt(medal) + " · 오늘 남은 입장 " + Left(db, d, nowUtc));
return m;
}
}
}