using System;
using System.Collections.Generic;
using UnityEngine;
namespace TKS.OutGame
{
/// 맹장 토벌전 세이브(기기 세이브 모드 · 서버 모드 = GET /tks/modes/raid 응답으로 덮는다).
[Serializable]
public class RaidState
{
public int week;
public string entryDay = "";
public List entries = new List();
public long mainDamage;
public long subDamage;
public long score;
public List milestones = new List();
public List shopBought = new List();
[Tooltip("토벌 훈장(raid_coin · 서버 모드 = 재화에서 온다)")]
public int coins;
[Tooltip("서버가 준 이번 주 메인 · 서브 · 순위(서버 모드)")]
public bool serverView;
public string main = "";
public string sub = "";
public int rank;
public int rankTotal;
public int mainHp;
public int subHp;
}
///
/// 맹장 토벌전 규칙(기기 세이브 모드 · 서버 `domain/warlord.rules.ts` 와 같은 일정 · 횟수 · 점수 · 누적 단계 · 훈장 상점) — 순위 보상은 서버 모드만(실제 유저 순위).
/// 값은 전부 표(RaidData · raid_rewards · raid_shop · content_modes_config raid.*).
///
public static class RaidRules
{
public const string Kind = "raid2";
static void Roll(SaveData d, DateTime nowUtc)
{
var s = d.raid;
if (s.serverView) return; // 서버 모드 = 서버가 주 · 날을 넘긴다
int week;
string main, sub;
RaidData.Schedule(nowUtc, out week, out main, out sub);
if (s.week != week)
{
s.week = week;
s.mainDamage = s.subDamage = s.score = 0;
s.milestones.Clear();
s.shopBought.Clear();
}
s.main = main;
s.sub = sub;
var day = EconomyRules.DayKey(nowUtc);
if (s.entryDay != day)
{
s.entryDay = day;
s.entries.Clear();
}
}
public static string BossOf(SaveData d, string slot, DateTime nowUtc)
{
Roll(d, nowUtc);
return slot == RaidData.Main ? d.raid.main : d.raid.sub;
}
public static bool Unlocked(SaveData d)
{
if (d.raid.serverView) return true;
int ch, st;
if (!ArenaRules.ParseStageKey(ContentTables.Str(RaidData.Mode, "unlock_stage"), out ch, out st)) return true;
var s = d.Stage(ch, st, false);
return s != null && s.clears > 0;
}
public static int Used(SaveData d, string slot, DateTime nowUtc)
{
Roll(d, nowUtc);
return Counts.Get(d.raid.entries, slot);
}
public static string Can(SaveData d, string slot, bool practice, DateTime nowUtc)
{
if (ArenaRules.Settling(d, ArenaRules.Raid, nowUtc)) return ArenaRules.SettleNotice;
if (!Unlocked(d)) return ContentTables.Str(RaidData.Mode, "unlock_stage") + " 클리어 뒤 열림";
if (string.IsNullOrEmpty(BossOf(d, slot, nowUtc))) return "오늘은 맹장이 없습니다";
if (practice) return ContentTables.Str(RaidData.Mode, "practice") == "unlimited" ? null : "연습 없음";
int limit = RaidData.EntryLimit(slot);
if (Used(d, slot, nowUtc) >= limit) return "오늘 기록 도전 " + limit + "회 완료";
return null;
}
/// 입장 — 기록 도전은 자리마다 하루 횟수 1 사용(연습은 세지 않음).
public static string Enter(SaveData d, string slot, bool practice, DateTime nowUtc)
{
var why = Can(d, slot, practice, nowUtc);
if (why != null) return why;
if (!practice) Counts.Add(d.raid.entries, slot, 1);
return null;
}
/// 끝(기기 세이브 모드) — 피해(상한 n_hp) → 주간 점수 → 누적 단계 보상.
public static ModeResult Result(OutGameData db, SaveData d, string slot, bool practice, string bossId, long damage, List lines, DateTime nowUtc, System.Random rng)
{
var b = RaidData.Get(bossId);
var m = new ModeResult { kind = Kind, win = true, title = "맹장 토벌전 — " + (b != null ? b.nameKo : bossId) + (practice ? " (연습)" : "") };
if (lines != null) m.lines.AddRange(lines);
if (practice) return m;
Roll(d, nowUtc);
long cap = b != null && b.hpCap > 0 ? b.hpCap : long.MaxValue;
damage = Math.Min(cap, Math.Max(0, damage));
if (slot == RaidData.Main) d.raid.mainDamage += damage;
else d.raid.subDamage += damage;
d.raid.score = (long)Math.Floor(d.raid.mainDamage * RaidData.ScoreMult(RaidData.Main) + d.raid.subDamage * RaidData.ScoreMult(RaidData.Sub) + 1e-6);
m.lines.Add("이번 주 점수 " + d.raid.score.ToString("N0") + " (메인 " + d.raid.mainDamage.ToString("N0") + " × " + RaidData.ScoreMult(RaidData.Main) + " + 서브 " + d.raid.subDamage.ToString("N0") + ")");
foreach (var r in ContentTables.Rows(RaidData.Rewards))
{
if (r.S("e_kind") != "milestone") continue;
int step = r.I("n_step");
if (r.I("n_score_min") <= 0 || d.raid.score < r.I("n_score_min") || d.raid.milestones.Contains(step)) continue;
d.raid.milestones.Add(step);
m.lines.Add(r.S("s_band_ko") + " 달성 — " + GivePrize(db, d, r, rng));
}
return m;
}
/// 보상 1줄(보옥 · 토벌 훈장 · 상자 「등급:수」) 지급 → 받은 것 글.
static string GivePrize(OutGameData db, SaveData d, ContentRow r, System.Random rng)
{
var got = new List();
int gem = r.I("n_gem"), coin = r.I("n_raid_coin");
if (gem > 0)
{
d.gem += gem;
got.Add("보옥 +" + gem);
}
if (coin > 0)
{
d.raid.coins += coin;
got.Add("토벌 훈장 +" + coin);
}
foreach (var kv in ContentTables.ParseMap(r.S("s_chest")))
for (int i = 0; i < Mathf.RoundToInt(kv.Value); i++)
{
var id = TreasureRules.AddChest(db, d, kv.Key, rng);
if (id != null) got.Add(db.Chests.Get(id).nameKo);
}
return string.Join(" · ", got.ToArray());
}
// ---------- 훈장 상점 ----------
public static int Bought(SaveData d, string item, DateTime nowUtc)
{
Roll(d, nowUtc);
return Counts.Get(d.raid.shopBought, item);
}
/// 구매(기기 세이브 모드) — pick = 고르는 물품(영웅 조각 = 보유 영웅 · 보물 조각 = 보유 보물).
public static string Buy(OutGameData db, SaveData d, string item, string pick, DateTime nowUtc, System.Random rng)
{
var r = ContentTables.Find(RaidData.Shop, "s_item", item);
if (r == null) return "없는 물품";
if (Bought(d, item, nowUtc) >= r.I("n_weekly_limit")) return "이번 주 " + r.I("n_weekly_limit") + "회 완료";
int price = r.I("n_price");
if (d.raid.coins < price) return "토벌 훈장 부족 " + d.raid.coins + " / " + price;
int n = r.I("n_amount");
switch (item)
{
case "general_chest":
for (int i = 0; i < n; i++) TreasureRules.AddChest(db, d, "general", rng);
break;
case "skill_book":
d.inv.books.skill += n;
break;
case "hero_shard_pick":
{
var h = d.Hero(pick);
if (h == null) return "보유 영웅을 고르세요";
h.shards += n;
break;
}
case "arcana_shard":
{
TreasureState t = null;
foreach (var x in d.inv.treasures)
if (x.id == pick) t = x;
if (t == null) return "보유 보물을 고르세요";
t.shards += n;
break;
}
default:
return "구현되지 않은 물품 " + item;
}
d.raid.coins -= price;
Counts.Add(d.raid.shopBought, item, 1);
return null;
}
}
}