diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/GameEngineController.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/GameEngineController.java index 3695ad0..92a9a22 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/GameEngineController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/minigames/GameEngineController.java @@ -1,8 +1,11 @@ package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.minigames; import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType; import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.WidgetEnum; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.util.Duration; @@ -12,9 +15,13 @@ public final class GameEngineController extends ViewController { private int currentScore = 0; private int frameCounter = 0; private int secondsRemaining = 60; + private char currentRank = 'E'; + private String rankEffectString; + private double fortuneToSet; private GameGimmick activeGimmick; private Timeline engineLoop; private Stock chosenStock; + private boolean reportActive = false; public GameEngineController(final GameEngineView viewElement, final EventManager eventManager, @@ -31,23 +38,29 @@ public GameEngineController(final GameEngineView viewElement, frameCounter = 0; refreshMetrics(); } + if (secondsRemaining <= 0) { + reportActive = true; + stopGameSession(reportActive); + } })); engineLoop.setCycleCount(Timeline.INDEFINITE); } @Override protected void initInteractions() { - getViewElement().setOnAction(MiniGamesActions.INGAME_QUIT, this::stopGameSession); + getViewElement().setOnAction(MiniGamesActions.INGAME_QUIT, this::returnToOverview); + getViewElement().setOnAction(MiniGamesActions.INGAME_RETURN, this::returnToOverview); } public void launchGimmickSession(final GameGimmick gimmick) { - this.activeGimmick = gimmick; - this.currentScore = 0; - this.frameCounter = 0; - this.secondsRemaining = 60; + activeGimmick = gimmick; + currentScore = 0; + frameCounter = 0; + currentRank = 'E'; + reportActive = false; + secondsRemaining = 60; refreshMetrics(); - // Connect interactions. getViewElement().setGameGimmickContent(gimmick, (scoreDelta) -> { if (this.currentScore + scoreDelta >= 0) { this.currentScore += scoreDelta; @@ -67,20 +80,52 @@ private void refreshMetrics() { int seconds = secondsRemaining % 60; String formattedTime = String.format("%02d:%02d:%02d", hours, minutes, seconds); - // Dynamic Rank classification engine evaluations - String rank = "E"; - if (currentScore > 50) rank = "A"; - else if (currentScore > 30) rank = "B"; - else if (currentScore > 15) rank = "C"; - else if (currentScore > 5) rank = "D"; + if (currentScore > 400) { + currentRank = 'S'; + rankEffectString = "Extremely good fortune awaits " + chosenStock.getSymbol(); + fortuneToSet = 0.10; + + } else if (currentScore > 300) { + currentRank = 'A'; + rankEffectString = "Really good fortune awaits " + chosenStock.getSymbol(); + fortuneToSet = 0.5; + + } else if (currentScore > 200) { + currentRank = 'B'; + rankEffectString = "Good fortune awaits " + chosenStock.getSymbol(); + fortuneToSet = 0.2; + + } else if (currentScore > 150) { + currentRank = 'C'; + rankEffectString = "Bad fortune awaits " + chosenStock.getSymbol(); + fortuneToSet = -0.2; + + } else if (currentScore > 100) { + currentRank = 'D'; + rankEffectString = "Really bad fortune awaits " + chosenStock.getSymbol(); + fortuneToSet = -0.5; - getViewElement().updateMetadataDisplay(currentScore, formattedTime, rank); + } else { + currentRank = 'E'; + rankEffectString = "Extremely bad fortune awaits " + chosenStock.getSymbol(); + fortuneToSet = -0.10; + } + + getViewElement().updateMetadataDisplay(currentScore, formattedTime, currentRank); } - private void stopGameSession() { + private void stopGameSession(final boolean showReport) { if (engineLoop != null) { engineLoop.stop(); + if(showReport) { + chosenStock.setFortune(fortuneToSet); + getViewElement().showGameReport(currentRank, rankEffectString); + } } - // Fire architecture EventManager updates here to go back to the menu view cleanly + } + + private void returnToOverview() { + EventData eventData = new EventData<>(EventType.CHANGE_INGAME_CENTER, WidgetEnum.MINIGAMES_OVERVIEW); + invoke(eventData); } }