using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using DevilLink2.Core; using DevilLink2.Interaction; using NUnit.Framework; namespace DevilLink2.Gameplay.Tests { public sealed class GameFlowTests { private static GameFlowController Create(ManualClock clock, GameMode mode = GameMode.Classic, int level = 1, PlayerEconomy economy = null, ModeProgress progress = null) { progress = progress ?? new ModeProgress(); if (level > 1) progress.RecordClear(mode, level - 1); return new GameFlowController(mode, progress, economy ?? new PlayerEconomy(), clock, 2026); } private static IEnumerable Tiles(Board board) { for(int r=0;r(object source,string name) where T:class => typeof(GameSessionRuntime).GetField(name,BindingFlags.Instance|BindingFlags.NonPublic).GetValue(source) as T; public static IEnumerable AllLevels { get { foreach(GameMode mode in Enum.GetValues(typeof(GameMode))) foreach(var level in LevelCatalog.GetAll(mode)) yield return new TestCaseData(mode,level.Level).SetName("FullVictoryRewards_"+mode+"_"+level.Level); } } [TestCaseSource(nameof(AllLevels))] public void FullVictoryGrantsOnlyCatalogRewardsAndIndependentProgress(GameMode mode,int level) { var clock=new ManualClock(); using(var flow=Create(clock,mode,level)) { int settlements=0; flow.Settled += _=>settlements++; Assert.That(flow.Start(),Is.True); Win(flow,clock); var definition=LevelCatalog.Get(mode,level); Assert.That(flow.Economy.Coins,Is.EqualTo(2000L+definition.ClearCoinReward)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(level)); foreach(GameMode other in Enum.GetValues(typeof(GameMode))) if(other!=mode) Assert.That(flow.Progress.GetHighestCleared(other),Is.Zero); Assert.That(flow.Result.CoinReward,Is.EqualTo(definition.ClearCoinReward)); Assert.That(flow.Result.SpinTicketReward,Is.EqualTo(1)); Assert.That(flow.Result.FinalScore,Is.EqualTo(flow.Runtime.Score.TotalScore)); Assert.That(flow.Result.ClearBonusScore,Is.EqualTo(flow.Runtime.Score.ClearBonus)); Assert.That(flow.Result.IsSettled,Is.True); Assert.That(settlements,Is.EqualTo(1)); } } [Test] public void ReadyDoesNotTickOrAcceptInputAndStartTransitionsOnce() { var clock=new ManualClock(); using(var flow=Create(clock)) { var states=new List(); flow.StateChanged += (a,b)=>states.Add(b); clock.Seconds=20; flow.Update(); Assert.That(flow.State,Is.EqualTo(GameFlowState.Ready)); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(180)); Assert.That(flow.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.Pause() || flow.Resume() || flow.RestartCurrentLevel() || flow.Retry() || flow.NextLevel(),Is.False); Assert.That(flow.Start(),Is.True); Assert.That(flow.Start(),Is.False); clock.Seconds=20.25; flow.Update(); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(179.75)); Assert.That(states,Is.EqualTo(new[]{GameFlowState.Running})); } } [Test] public void PauseResumePreserveBoardScoreAndSelectionButComboUsesRealTime() { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.Start(); Match(flow,clock); clock.Seconds+=.25; flow.Update(); var tile=Tiles(flow.Runtime.Session.Board).First(); flow.Click(tile.Row,tile.Column); var board=flow.Runtime.Session.Board; var remaining=flow.Runtime.Timer.RemainingTime; var states=new List(); flow.StateChanged += (a,b)=>states.Add(b); Assert.That(flow.Pause(),Is.True); Assert.That(flow.Pause(),Is.False); clock.Seconds+=10; flow.Update(); Assert.That(flow.Click(tile.Row,tile.Column),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.Runtime.Score.ComboCount,Is.Zero); Assert.That(flow.Runtime.Score.TotalScore,Is.EqualTo(100)); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(remaining)); Assert.That(flow.Runtime.Interaction.Selected,Is.SameAs(tile)); Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero); Assert.That(flow.Progress.GetHighestCleared(GameMode.Classic),Is.Zero); Assert.That(flow.Resume(),Is.True); Assert.That(flow.Resume(),Is.False); Assert.That(flow.Runtime.Session.Board,Is.SameAs(board)); Assert.That(flow.Runtime.Score.TotalScore,Is.EqualTo(100)); clock.Seconds+=.25; flow.Update(); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(remaining-.25)); Assert.That(states,Is.EqualTo(new[]{GameFlowState.Paused,GameFlowState.Running})); } } [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] [TestCase(GameMode.Zen)] public void PausedRestartCreatesCleanRunAndKeepsPlayerData(GameMode mode) { var clock=new ManualClock(); using(var flow=Create(clock,mode,3)) { flow.Start(); Match(flow,clock); clock.Seconds+=.25; flow.Update(); var tile=Tiles(flow.Runtime.Session.Board).First(); flow.Click(tile.Row,tile.Column); flow.Pause(); var previous=flow.Runtime; var states=new List(); flow.StateChanged += (a,b)=>states.Add(b); Assert.That(flow.RestartCurrentLevel(),Is.True); Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(previous.Session.Board)); Assert.That(flow.Runtime.Session.Id,Is.Not.EqualTo(previous.Session.Id)); Assert.That(flow.Runtime.Session.Definition,Is.SameAs(previous.Session.Definition)); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Runtime.Score.ComboCount,Is.Zero); Assert.That(flow.Runtime.Timer.ElapsedTime,Is.Zero); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(flow.Runtime.Timer.InitialTime)); Assert.That(flow.Runtime.Interaction.Selected,Is.Null); Assert.That(previous.Interaction.Selected,Is.Null); Assert.That(flow.Runtime.Interaction.IsLocked,Is.False); Assert.That(previous.Timer.IsRunning,Is.False); Assert.That(previous.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(2)); Assert.That(flow.Result,Is.Null); Assert.That(states,Is.EqualTo(new[]{GameFlowState.Ready,GameFlowState.Running})); } } [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] public void DefeatPreservesScoreAndSnapshotWithoutRewardsAndRetryKeepsLevel(GameMode mode) { var clock=new ManualClock(); using(var flow=Create(clock,mode,4)) { flow.Start(); Match(flow,clock); int settled=0; flow.Settled += _=>settled++; clock.Seconds+=1000; flow.Update(); var result=flow.Result; var runtime=flow.Runtime; Assert.That(flow.State,Is.EqualTo(GameFlowState.Defeat)); Assert.That(result.Outcome,Is.EqualTo(GameOutcome.Defeat)); Assert.That(result.FinalScore,Is.EqualTo(100)); Assert.That(result.ClearBonusScore,Is.Zero); Assert.That(result.SessionId,Is.EqualTo(runtime.Session.Id)); Assert.That(result.GameMode,Is.EqualTo(mode)); Assert.That(result.LevelNumber,Is.EqualTo(4)); Assert.That(result.HasTimer,Is.True); Assert.That(result.InitialTime,Is.EqualTo(LevelCatalog.Get(mode,4).TimeLimitSeconds)); Assert.That(result.RemainingTime,Is.Zero); Assert.That(result.ElapsedTime,Is.EqualTo(result.InitialTime)); Assert.That(result.CoinReward+result.SpinTicketReward,Is.Zero); Assert.That(result.IsSettled,Is.True); Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(3)); Assert.That(flow.NextLevel() || flow.Start() || flow.Resume() || flow.Pause(),Is.False); Assert.That(flow.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.AddTime(30),Is.False); Notification(runtime,"TimedOut")(); Notification>(runtime,"ClearBonusAwarded")(1000); flow.Update(); Assert.That(flow.Result,Is.SameAs(result)); Assert.That(settled,Is.EqualTo(1)); Assert.That(flow.Retry(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(4)); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(runtime.Session.Board)); } } [Test] public void VictoryResultIsImmutableAndDuplicateOrOppositeEventsCannotSettleAgain() { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.Start(); var runtime=flow.Runtime; int events=0; flow.Settled += _=>events++; Win(flow,clock); var result=flow.Result; var balance=flow.Economy.Coins; Notification>(runtime,"ClearBonusAwarded")(runtime.Score.ClearBonus); Notification(runtime,"TimedOut")(); runtime.Interaction.Advance(10); flow.Update(); Assert.That(flow.State,Is.EqualTo(GameFlowState.Victory)); Assert.That(flow.Result,Is.SameAs(result)); Assert.That(flow.Economy.Coins,Is.EqualTo(balance)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(events,Is.EqualTo(1)); Assert.That(result.InitialTime,Is.EqualTo(180)); Assert.That(result.RemainingTime,Is.EqualTo(runtime.Timer.RemainingTime)); Assert.That(result.ElapsedTime,Is.EqualTo(runtime.Timer.ElapsedTime)); Assert.That(result.LevelNumber,Is.EqualTo(1)); Assert.That(result.GameMode,Is.EqualTo(GameMode.Classic)); Assert.That(result.Outcome,Is.EqualTo(GameOutcome.Victory)); Assert.That(result.ClearBonusScore,Is.EqualTo(1000+(int)Math.Ceiling(result.RemainingTime)*10)); Assert.That(typeof(GameResult).GetProperties().All(p=>p.SetMethod==null),Is.True); } } [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] [TestCase(GameMode.Zen)] public void VictoryRetryKeepsCurrentLevelAndEconomyWithoutAdvancingAgain(GameMode mode) { var clock=new ManualClock(); using(var flow=Create(clock,mode,5)) { flow.Start(); Win(flow,clock); var old=flow.Runtime; var coins=flow.Economy.Coins; Assert.That(flow.Retry(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(5)); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(5)); Assert.That(flow.Economy.Coins,Is.EqualTo(coins)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(old.Session.Board)); Assert.That(flow.Result,Is.Null); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); } } [TestCase(GameMode.Classic,1)] [TestCase(GameMode.Classic,19)] [TestCase(GameMode.TimeAttack,1)] [TestCase(GameMode.TimeAttack,19)] [TestCase(GameMode.Zen,1)] [TestCase(GameMode.Zen,19)] public void NextLevelUsesNewBoardAndPreservesEconomy(GameMode mode,int level) { var clock=new ManualClock(); using(var flow=Create(clock,mode,level)) { Assert.That(flow.NextLevel(),Is.False); flow.Start(); Win(flow,clock); var old=flow.Runtime; var coins=flow.Economy.Coins; Assert.That(flow.NextLevel(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(level+1)); Assert.That(flow.Runtime.Session.Board,Is.Not.SameAs(old.Session.Board)); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Runtime.Timer.RemainingTime,Is.EqualTo(LevelCatalog.Get(mode,level+1).TimeLimitSeconds)); Assert.That(flow.Economy.Coins,Is.EqualTo(coins)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(level)); } } [TestCase(GameMode.Classic)] [TestCase(GameMode.TimeAttack)] [TestCase(GameMode.Zen)] public void LevelTwentyAlwaysReplaysTwentyAndPaysFullReward(GameMode mode) { var clock=new ManualClock(); using(var flow=Create(clock,mode,20)) { flow.Start(); var definition=LevelCatalog.Get(mode,20); for(int i=1;i<=3;i++) { Win(flow,clock); Assert.That(flow.Economy.Coins,Is.EqualTo(2000L+i*definition.ClearCoinReward)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(i)); Assert.That(flow.Progress.GetHighestCleared(mode),Is.EqualTo(20)); Assert.That(flow.NextLevel(),Is.True); Assert.That(flow.Runtime.Session.Definition.Level,Is.EqualTo(20)); } } } [TestCase(false)] [TestCase(true)] public void ReturnToLobbyDisablesRunWithoutResultRewardsOrProgress(bool paused) { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.Start(); Match(flow,clock); if(paused) flow.Pause(); var runtime=flow.Runtime; var staleClear=Notification>(runtime,"ClearBonusAwarded"); var staleTimeout=Notification(runtime,"TimedOut"); int lobby=0,settled=0; flow.LobbyRequested += ()=>lobby++; flow.Settled += _=>settled++; clock.Seconds=1000; Assert.That(flow.ReturnToLobby(),Is.True); Assert.That(flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(runtime.Timer.IsRunning,Is.False); flow.Update(); staleClear(1000); staleTimeout(); runtime.Interaction.Advance(1000); Assert.That(flow.Click(0,0),Is.EqualTo(TileClickResult.Ignored)); Assert.That(flow.Result,Is.Null); Assert.That(flow.Start() || flow.Resume() || flow.RestartCurrentLevel() || flow.NextLevel() || flow.ReturnToLobby(),Is.False); Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); Assert.That(flow.Economy.SpinTickets,Is.Zero); Assert.That(flow.Progress.GetHighestCleared(GameMode.Classic),Is.Zero); Assert.That(lobby,Is.EqualTo(1)); Assert.That(settled,Is.Zero); Assert.That(Notification>(runtime,"ClearBonusAwarded"),Is.Null); Assert.That(Notification(runtime,"TimedOut"),Is.Null); } } [Test] public void OldQueuedCallbacksCannotMutateNewRunAcrossRepeatedRestarts() { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.Start(); int settled=0; flow.Settled += _=>settled++; for(int i=0;i<5;i++) { Match(flow,clock); var old=flow.Runtime; var stale=Notification>(old,"ClearBonusAwarded"); var staleTimeout=Notification(old,"TimedOut"); flow.Pause(); flow.RestartCurrentLevel(); old.Interaction.Advance(1000); stale(1000); staleTimeout(); old.Update(); Assert.That(flow.State,Is.EqualTo(GameFlowState.Running)); Assert.That(flow.Result,Is.Null); Assert.That(flow.Runtime.Score.TotalScore,Is.Zero); Assert.That(flow.Economy.Coins,Is.EqualTo(2000)); } Win(flow,clock); Assert.That(settled,Is.EqualTo(1)); Assert.That(flow.Economy.Coins,Is.EqualTo(3000)); } } [Test] public void SettlementCallbacksObserveCommittedDataAndCannotReenterCommands() { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.StateChanged += (old,next)=> { if(next!=GameFlowState.Victory) return; Assert.That(flow.Result.IsSettled,Is.True); Assert.That(flow.Economy.Coins,Is.EqualTo(3000)); Assert.That(flow.NextLevel() || flow.Retry() || flow.ReturnToLobby(),Is.False); }; flow.Settled += result=> { Assert.That(result,Is.SameAs(flow.Result)); Assert.That(flow.RestartCurrentLevel(),Is.False); }; flow.Start(); Win(flow,clock); Assert.That(flow.NextLevel(),Is.True); } } [Test] public void PauseAtDeadlineBecomesDefeatRatherThanPaused() { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.Start(); clock.Seconds=180; Assert.That(flow.Pause(),Is.False); Assert.That(flow.State,Is.EqualTo(GameFlowState.Defeat)); } } [Test] public void SharedPlayerDataSurvivesLeavingAndEnteringAnotherMode() { var clock=new ManualClock(); var economy=new PlayerEconomy(); var progress=new ModeProgress(); using(var first=Create(clock,GameMode.Classic,1,economy,progress)) { first.Start(); Win(first,clock); first.ReturnToLobby(); } using(var second=Create(clock,GameMode.Zen,1,economy,progress)) { Assert.That(economy.Coins,Is.EqualTo(3000)); Assert.That(economy.SpinTickets,Is.EqualTo(1)); Assert.That(progress.GetHighestCleared(GameMode.Classic),Is.EqualTo(1)); Assert.That(second.Runtime.Session.Definition.Level,Is.EqualTo(1)); second.Start(); Win(second,clock); Assert.That(economy.Coins,Is.EqualTo(4000)); Assert.That(economy.SpinTickets,Is.EqualTo(2)); } } [TestCase(false)] [TestCase(true)] public void LastPairDeadlineHasOneOutcome(bool atDeadline) { var clock=new ManualClock(); using(var flow=Create(clock)) { var board=flow.Runtime.Session.Board; new BoardMoveFinder().TryFindMove(board,out var pair); foreach(var tile in Tiles(board).ToArray()) if(tile!=pair.First && tile!=pair.Second) board.RemoveTile(tile); int settled=0; flow.Settled += _=>settled++; flow.Start(); flow.Click(pair.First.Row,pair.First.Column); clock.Seconds=atDeadline?180:179.75; var click=flow.Click(pair.Second.Row,pair.Second.Column); Assert.That(click,Is.EqualTo(atDeadline?TileClickResult.Ignored:TileClickResult.Matched)); Assert.That(flow.State,Is.EqualTo(atDeadline?GameFlowState.Defeat:GameFlowState.Victory)); Assert.That(flow.Result.FinalScore,Is.EqualTo(atDeadline?0:1110)); clock.Seconds=1000; flow.Update(); Assert.That(settled,Is.EqualTo(1)); Assert.That(flow.Economy.Coins,Is.EqualTo(atDeadline?2000:3000)); } } [Test] public void StaleVictoryNotificationFromCompletedRunCannotWinNextRun() { var clock=new ManualClock(); using(var flow=Create(clock)) { flow.Start(); Win(flow,clock); var old=flow.Runtime; var queued=Notification>(old,"ClearBonusAwarded"); var snapshot=flow.Result; flow.NextLevel(); queued(old.Score.ClearBonus); old.Interaction.Advance(10); Assert.That(flow.State,Is.EqualTo(GameFlowState.Running)); Assert.That(flow.Result,Is.Null); Assert.That(flow.Economy.Coins,Is.EqualTo(3000)); Assert.That(flow.Economy.SpinTickets,Is.EqualTo(1)); Assert.That(snapshot.SessionId,Is.Not.EqualTo(flow.Runtime.Session.Id)); Assert.That(snapshot.CoinReward,Is.EqualTo(1000)); } } [Test] public void DestroyingFlowInsideSettlementDefersCleanupUntilNotificationCompletes() { var clock=new ManualClock(); var flow=Create(clock); int abandoned=0; flow.StateChanged += (a,b)=> { if(b==GameFlowState.Abandoned) abandoned++; }; flow.Settled += _=>flow.Dispose(); flow.Start(); while(flow.State==GameFlowState.Running) Match(flow,clock); Assert.That(flow.State,Is.EqualTo(GameFlowState.Abandoned)); Assert.That(flow.Result.Outcome,Is.EqualTo(GameOutcome.Victory)); Assert.That(flow.Economy.Coins,Is.EqualTo(3000)); Assert.That(flow.Runtime.Timer.IsRunning,Is.False); Assert.That(abandoned,Is.EqualTo(1)); Assert.That(flow.Start(),Is.False); flow.Dispose(); } [Test] public void EconomyDefaultsAndInvalidBalancesAreChecked() { var player=new PlayerEconomy(); Assert.That(player.Coins,Is.EqualTo(2000)); Assert.That(player.SpinTickets,Is.Zero); Assert.Throws(()=>new PlayerEconomy(-1)); Assert.Throws(()=>new PlayerEconomy(0,-1)); } } }