using System.Collections;
using System.IO;
using NUnit.Framework;
using TKS.OutGame;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace TKS.Tests
{
///
/// 전투 중 전리품 연출 · 결과 창 전리품 줄(2026-11-23 스테이지 보상 드랍 요청서 §2-2 · §2-3) — 적 처치 → 금화 · 상자가 표시칸으로 날아가 합계가 오르고,
/// 전부 처치하면 표시칸 = 그 판 골드 총량 · 상자 수 = 결과 보상. 캡처(Explicit) = Logs/loot_capture/(월드맵 표식 · 스테이지 정보 · 전투 드랍 · 결과 창).
/// 기대값은 표 · 규칙 함수에서 읽는다(C45).
///
public class LootFxTests
{
OutGameRoot og;
BattleManager bm;
WorldMapScreen wm;
bool unlockWas;
[SetUp]
public void SetUp()
{
unlockWas = TestOptions.UnlockAllStages;
}
[TearDown]
public void TearDown()
{
TestOptions.UnlockAllStages = unlockWas; // 이 PC 의 플레이 테스트 옵션을 그대로 돌려놓는다
SaveStore.UseMemory(false);
}
IEnumerator Boot()
{
WorldProgress.Reset();
SaveStore.UseMemory(true);
SceneManager.LoadScene("Battle");
yield return null;
yield return null;
og = OutGameRoot.Instance;
bm = BattleManager.Instance;
wm = WorldMapScreen.Instance;
Assert.IsNotNull(og);
}
static IEnumerator Wait(float sec)
{
float t0 = Time.realtimeSinceStartup;
while (Time.realtimeSinceStartup - t0 < sec) yield return null;
}
[UnityTest]
public IEnumerator Loot_KillDrops_ChipTotals_ResultSpoils()
{
yield return Boot();
var s = og.Session;
og.OpenWorldMap();
yield return null;
s.Save.stamina = EconomyRules.StaminaMax(s.Db, s.Save);
wm.EnterStage(0, 0);
yield return null;
var fx = og.LootFx;
Assert.IsNotNull(fx, "전리품 연출");
Assert.IsTrue(fx.Active, "출정 스테이지 = 표시칸 켜짐");
int gold, exp;
EconomyRules.StageGoldExp(s.Db, s.Save, bm.Stage, true, out gold, out exp);
Assert.AreEqual(gold, fx.GoldTotal, "표시칸 총량 = 그 판 골드(첫 클리어 · 지역 배율)");
Assert.AreEqual(s.PendingLoot != null ? s.PendingLoot.gearIds.Count : 0, fx.ChestTotal, "상자 수 = 출전 때 굴린 전리품");
Assert.AreEqual(0, fx.GoldShown);
// 1기 처치 → 금화가 튀어나와 표시칸으로
Unit first = null;
foreach (var u in bm.AliveUnits(Faction.Enemy)) { first = u; break; }
first.TakeDamage(first.Hp);
Assert.Greater(fx.FlyCount, 0, "처치 = 떨어지는 그림");
yield return Wait(2f);
Assert.AreEqual(0, fx.FlyCount, "날아가 사라짐");
Assert.Greater(fx.GoldShown, 0, "표시칸 골드가 오름");
Assert.Less(fx.GoldShown, fx.GoldTotal, "1기 몫 = 총량의 일부");
// 전부 처치 → 표시칸 = 총량 = 결과 보상
foreach (var u in bm.AliveUnits(Faction.Enemy)) u.TakeDamage(u.Hp);
bm.CheckBattleEnd();
yield return null;
Assert.AreEqual(BattlePhase.StageClear, bm.Phase);
var r = s.LastReward;
Assert.IsNotNull(r);
Assert.AreEqual(r.gold, fx.GoldShown, "표시칸 골드 = 결과 골드");
Assert.AreEqual(r.gearIds.Count, fx.ChestsShown, "표시칸 상자 = 결과 전리품 수");
Assert.IsNull(s.PendingLoot, "미리 굴린 전리품은 지급하고 비움");
Assert.IsTrue(og.ResultVisible);
bm.ContinueAfterResult();
yield return null;
Assert.IsFalse(fx.Active, "전투를 떠나면 표시칸 숨김");
}
/// 실행: test run --mode playmode --filter Capture_Loot --include-explicit --async-tests → Logs/loot_capture/*.png
[UnityTest, Explicit]
public IEnumerator Capture_Loot()
{
LogAssert.ignoreFailingMessages = true;
TestOptions.UnlockAllStages = true; // 보스 스테이지(첫 클리어 확정 상자)에 바로 들어가기 위해 · TearDown 에서 되돌림
yield return Boot();
string dir = Path.Combine(Path.GetDirectoryName(Application.dataPath), "Logs/loot_capture");
Directory.CreateDirectory(dir);
og.OpenWorldMap();
yield return Wait(0.6f);
yield return Shot(dir, "01_worldmap");
int bossNode = 0;
for (int n = 0; n < 30; n++)
{
int si = wm.StageIndexOf(0, n);
if (si < 0) break;
if (bm.Config.stages[si].kind == StageKind.Boss) { bossNode = n; break; }
}
wm.OpenPopup(bossNode);
yield return Wait(0.4f);
yield return Shot(dir, "02_stage_popup");
wm.ConfirmPopup();
float t0 = Time.realtimeSinceStartup; // 보스 등장 연출이 끝나 아군 턴이 될 때까지
while (bm.Phase != BattlePhase.PlayerTurn && Time.realtimeSinceStartup - t0 < 30f) yield return null;
yield return Wait(1.0f);
var fx = og.LootFx;
Unit boss = null;
int killed = 0;
foreach (var u in bm.AliveUnits(Faction.Enemy))
{
if (u.IsBoss) { boss = u; continue; }
if (killed++ < 2) u.TakeDamage(u.Hp);
}
if (boss != null) boss.TakeDamage(boss.Hp);
yield return Wait(0.42f);
yield return Shot(dir, "03_battle_drop");
yield return Wait(1.6f);
yield return Shot(dir, "04_battle_chip");
foreach (var u in bm.AliveUnits(Faction.Enemy)) u.TakeDamage(u.Hp);
bm.CheckBattleEnd();
yield return Wait(1.6f);
yield return Shot(dir, "05_result");
Debug.Log("[TKS] loot capture gold " + (fx != null ? fx.GoldShown + "/" + fx.GoldTotal : "-") + " chests " + (fx != null ? fx.ChestsShown + "/" + fx.ChestTotal : "-"));
}
static IEnumerator Shot(string dir, string name)
{
string path = Path.Combine(dir, name + ".png");
if (File.Exists(path)) File.Delete(path);
ScreenCapture.CaptureScreenshot(path);
float t0 = Time.realtimeSinceStartup;
while (!File.Exists(path) && Time.realtimeSinceStartup - t0 < 5f) yield return null;
yield return null;
}
}
}