using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
namespace TKS.OutGame
{
/// 임무 표 5장 해석·검증(2026-11-20 · 개발 요청서 「임무 메뉴」 §1) — 형식·참조 오류는 errors 에 모아 임포트를 막는다.
public static partial class OutGameTables
{
public static readonly string[] MissionFiles = { "mission_config.csv", "mission_list.csv", "mission_point_rewards.csv", "achievements.csv", "mission_counters.csv" };
/// s_goto 낱말(요청서 §1-1) — 화면 바로가기를 코드가 아는 것만.
public static readonly string[] MissionGoto = { "stage", "supply", "patrol", "hero", "tower", "arena", "raid", "gacha", "treasure", "none" };
/// 구간 보상 낱말(기존 보상 낱말 · 상자 = 「<상자 id>_chest」).
public static readonly string[] MissionRewardKeys = { "gold", "gem", "skill_book", "stamina", "iron", "blueprint" };
/// 임무 표만 해석(texts = MissionFiles 순서) — 상자 id 검사는 chests 가 있을 때만.
public static MissionDb ParseMissionsOnly(string[] texts, ChestDb chests, List e)
{
var rows = new Dictionary>>();
for (int i = 0; i < MissionFiles.Length; i++)
{
var text = texts != null && i < texts.Length ? texts[i] : null;
if (text == null)
{
e.Add(MissionFiles[i] + ": 파일 없음");
rows[MissionFiles[i]] = new List>();
continue;
}
rows[MissionFiles[i]] = StageTable.ReadCsv(text, MissionFiles[i], e);
}
return ParseMission(rows, chests, e);
}
static MissionDb ParseMission(Dictionary>> rows, ChestDb chests, List e)
{
var t = ScriptableObject.CreateInstance();
// mission_counters
const string fc = "mission_counters.csv";
var rc = rows[fc];
if (Cols(rc, fc, e, "s_key", "s_name_ko", "e_count", "s_code_now"))
{
var seen = new HashSet();
foreach (var r in rc)
{
var c = new MissionDb.Counter { key = Get(r, "s_key"), nameKo = Get(r, "s_name_ko"), count = Get(r, "e_count"), codeNow = Get(r, "s_code_now") };
if (!Unique(seen, c.key, fc, r, e)) continue;
if (c.count != MissionDb.Add && c.count != MissionDb.State && c.count != MissionDb.Day) e.Add(fc + " 행 " + Get(r, "#") + ": e_count 「" + c.count + "」 = Add · State · Daily 가 아님");
if (c.count == MissionDb.State && Array.IndexOf(MissionRules.StateKeys, c.key) < 0) e.Add(fc + " 행 " + Get(r, "#") + ": State 카운터 「" + c.key + "」 = 세이브에서 다시 계산하는 법을 코드가 모름(MissionRules.StateKeys)");
t.counters.Add(c);
}
}
// mission_config
const string fg = "mission_config.csv";
var rg = rows[fg];
if (Cols(rg, fg, e, "s_key", "s_value"))
{
var seen = new HashSet();
foreach (var r in rg)
{
var c = new MissionDb.Cfg { key = Get(r, "s_key"), value = Get(r, "s_value") };
if (!Unique(seen, c.key, fg, r, e)) continue;
t.config.Add(c);
}
foreach (var k in new[] { "unlock_stage", "daily_reset", "weekly_reset", "daily_point_max", "weekly_point_max" })
if (!seen.Contains(k)) e.Add(fg + ": 「" + k + "」 줄 없음");
int ch, st;
if (!ArenaRules.ParseStageKey(t.Value("unlock_stage"), out ch, out st)) e.Add(fg + ": unlock_stage 「" + t.Value("unlock_stage") + "」 = 장-판 형식이 아님");
DayOfWeek dow;
int hour;
if (!MissionRules.ParseWeekly(t.Value("weekly_reset"), out dow, out hour)) e.Add(fg + ": weekly_reset 「" + t.Value("weekly_reset") + "」 = 요일;시 형식이 아님");
}
// mission_list
const string fl = "mission_list.csv";
var rl = rows[fl];
if (Cols(rl, fl, e, "n_id", "e_kind", "n_slot", "e_role", "s_counter", "n_goal", "n_point", "s_unlock", "n_fallback_id", "s_goto", "s_title_ko", "s_hint_ko"))
{
var seen = new HashSet();
foreach (var r in rl)
{
var m = new MissionDb.Mission
{
id = Int(r, "n_id", fl, e), kind = Get(r, "e_kind"), slot = Int(r, "n_slot", fl, e), role = Get(r, "e_role"), counter = Get(r, "s_counter"),
goal = Int(r, "n_goal", fl, e), point = Int(r, "n_point", fl, e), unlock = Get(r, "s_unlock"), fallbackId = Int(r, "n_fallback_id", fl, e),
go = Get(r, "s_goto"), titleKo = Get(r, "s_title_ko"), hintKo = Get(r, "s_hint_ko"),
};
string at = fl + " 행 " + Get(r, "#");
if (!Unique(seen, m.id.ToString(CultureInfo.InvariantCulture), fl, r, e)) continue;
if (m.kind != MissionDb.Daily && m.kind != MissionDb.Weekly) e.Add(at + ": e_kind 「" + m.kind + "」 = Daily · Weekly 가 아님");
if (m.role != MissionDb.Main && m.role != MissionDb.Fallback) e.Add(at + ": e_role 「" + m.role + "」 = Main · Fallback 이 아님");
if (t.CounterOf(m.counter) == null) e.Add(at + ": s_counter 「" + m.counter + "」 = mission_counters 에 없음");
if (m.goal <= 0) e.Add(at + ": n_goal 이 0 이하");
if (Array.IndexOf(MissionGoto, m.go) < 0) e.Add(at + ": s_goto 「" + m.go + "」 = 아는 화면이 아님(" + string.Join(" · ", MissionGoto) + ")");
if (m.unlock != "none" && MissionRules.UnlockMode(m.unlock) == null) e.Add(at + ": s_unlock 「" + m.unlock + "」 = none 또는 cfg:<모드>.unlock_stage 가 아님");
t.missions.Add(m);
}
foreach (var kind in new[] { MissionDb.Daily, MissionDb.Weekly })
{
int sum = 0;
var slots = new HashSet();
foreach (var m in t.missions)
{
if (m.kind != kind) continue;
if (m.IsMain)
{
sum += m.point;
if (!slots.Add(m.slot)) e.Add(fl + ": " + kind + " Main 칸 " + m.slot + " 중복");
if (m.fallbackId != 0)
{
var fb = t.Get(m.fallbackId);
if (fb == null) e.Add(fl + ": " + m.id + " n_fallback_id " + m.fallbackId + " 없음");
else if (fb.role != MissionDb.Fallback || fb.kind != kind || fb.slot != m.slot) e.Add(fl + ": " + m.id + " 대체 임무 " + fb.id + " = 같은 종류 · 같은 칸의 Fallback 이 아님");
}
}
else if (t.MainOf(m) == null) e.Add(fl + ": Fallback " + m.id + " 를 가리키는 Main 이 없음");
}
int max = t.PointMax(kind);
if (max <= 0) e.Add(fg + ": " + kind + " 포인트 상한이 0 이하");
else if (sum != max) e.Add(fl + ": " + kind + " Main 포인트 합 " + sum + " ≠ 상한 " + max + "(mission_config)");
}
}
// mission_point_rewards
const string fr = "mission_point_rewards.csv";
var rr = rows[fr];
if (Cols(rr, fr, e, "e_kind", "n_step", "n_point_need", "l_reward"))
{
foreach (var r in rr)
{
var p = new MissionDb.PointReward { kind = Get(r, "e_kind"), step = Int(r, "n_step", fr, e), need = Int(r, "n_point_need", fr, e), reward = Get(r, "l_reward") };
string at = fr + " 행 " + Get(r, "#");
if (p.kind != MissionDb.Daily && p.kind != MissionDb.Weekly) e.Add(at + ": e_kind 「" + p.kind + "」 = Daily · Weekly 가 아님");
CheckReward(p.reward, chests, at, e);
t.rewards.Add(p);
}
foreach (var kind in new[] { MissionDb.Daily, MissionDb.Weekly })
{
var steps = t.Steps(kind);
if (steps.Count == 0) e.Add(fr + ": " + kind + " 구간 없음");
for (int i = 0; i < steps.Count; i++)
{
if (steps[i].step != i + 1) e.Add(fr + ": " + kind + " n_step 이 1부터 연속이 아님(" + (i + 1) + "번째 = " + steps[i].step + ")");
if (i > 0 && steps[i].need <= steps[i - 1].need) e.Add(fr + ": " + kind + " n_point_need 가 커지지 않음(단계 " + steps[i].step + ")");
if (steps[i].need > t.PointMax(kind)) e.Add(fr + ": " + kind + " 단계 " + steps[i].step + " 필요 포인트 " + steps[i].need + " > 상한 " + t.PointMax(kind));
}
}
}
// achievements
const string fa = "achievements.csv";
var ra = rows[fa];
if (Cols(ra, fa, e, "s_id", "e_category", "n_order", "s_name_ko", "s_desc_ko", "s_counter", "l_goal", "l_gem", "n_repeat_add", "n_repeat_gem"))
{
var seen = new HashSet();
foreach (var r in ra)
{
var a = new MissionDb.Achievement
{
id = Get(r, "s_id"), category = Get(r, "e_category"), order = Int(r, "n_order", fa, e), nameKo = Get(r, "s_name_ko"), descKo = Get(r, "s_desc_ko"),
counter = Get(r, "s_counter"), goals = IntList(Get(r, "l_goal")), gems = IntList(Get(r, "l_gem")),
repeatAdd = Int(r, "n_repeat_add", fa, e), repeatGem = Int(r, "n_repeat_gem", fa, e),
};
string at = fa + " 행 " + Get(r, "#") + " " + a.id;
if (!Unique(seen, a.id, fa, r, e)) continue;
if (a.category.Length == 0) e.Add(at + ": e_category 비었음");
var c = t.CounterOf(a.counter);
if (c == null) e.Add(at + ": s_counter 「" + a.counter + "」 = mission_counters 에 없음");
else if (c.count == MissionDb.Day) e.Add(at + ": s_counter 「" + a.counter + "」 = 하루 1(Daily) 카운터는 누적 업적에 못 씀");
if (a.goals == null || a.gems == null || a.goals.Length == 0 || a.goals.Length != a.gems.Length) e.Add(at + ": l_goal · l_gem 길이가 다르거나 비었음");
else
for (int i = 1; i < a.goals.Length; i++)
if (a.goals[i] <= a.goals[i - 1]) e.Add(at + ": l_goal 이 커지지 않음(" + (i + 1) + "단계)");
if (a.repeatAdd < 0 || (a.repeatAdd > 0 && a.repeatGem <= 0)) e.Add(at + ": 반복 단계 n_repeat_add · n_repeat_gem 값 이상");
if (!a.descKo.Contains("{n}")) e.Add(at + ": s_desc_ko 에 {n} 자리 없음");
t.achievements.Add(a);
}
}
return t;
}
static void CheckReward(string reward, ChestDb chests, string at, List e)
{
var map = ContentTables.ParseMap(reward);
if (map.Count == 0) e.Add(at + ": l_reward 비었음 또는 형식 오류 「" + reward + "」");
foreach (var kv in map)
{
if (Array.IndexOf(MissionRewardKeys, kv.Key) >= 0) continue;
if (kv.Key.EndsWith("_chest"))
{
string id = kv.Key.Substring(0, kv.Key.Length - "_chest".Length);
if (chests != null && chests.Get(id) == null) e.Add(at + ": 상자 「" + id + "」 = chests 에 없음");
continue;
}
e.Add(at + ": 보상 낱말 「" + kv.Key + "」 모름");
}
}
static int[] IntList(string s)
{
var res = new List();
foreach (var p in (s ?? "").Split(';'))
{
int v;
if (p.Trim().Length == 0) continue;
res.Add(int.TryParse(p.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out v) ? v : -1);
}
return res.ToArray();
}
}
}