Skip to content

Commit

Permalink
Merge pull request #99 from einaskoi/per/clean
Browse files Browse the repository at this point in the history
weekend implimentation
  • Loading branch information
einaskoi authored May 13, 2026
2 parents 18fa3be + 47db121 commit 4b019e2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.GameState;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -13,20 +12,10 @@
* Controller for managing the game state and timing.
*/
public final class GameController {
private final Player player;
private final List<AppController> appControllers = new ArrayList<>();
private Timer timer;
private GameState gameState;

/**
* Constructs a new GameController.
*
* @param player the player model
*/
public GameController(final Player player) {
this.player = player;
}

/**
* Adds an application controller to the list of controllers to be updated.
*
Expand All @@ -38,6 +27,7 @@ public void addAppController(final AppController appController) {

public void setGameState(GameState gameState) {
this.gameState = gameState;

}

/**
Expand All @@ -48,25 +38,20 @@ public void startGame() {
final int delay = 0;
final int period = 1000;

timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {

if (isWeekend()) {
stopGame();
}

for (AppController controller : appControllers) {
controller.nextTick();

}
}
}, delay, period);

}

public boolean isWeekend() {
return !(gameState == GameState.WORKDAY);
timer.scheduleAtFixedRate(
new TimerTask() {
@Override
public void run() {
for (AppController controller : appControllers) {
controller.nextTick();
}
if (isWeekend()) {
stopGame();
}
}
},
delay,
period);
}

/**
Expand All @@ -78,4 +63,8 @@ public void stopGame() {
timer.purge();
}
}

public boolean isWeekend() {
return !(gameState == GameState.WORKDAY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ public static void printMarket(final List<Stock> stocks) {
System.out.println(stock.getHistoricalPrices().toString());
}
}

public void advanceWeek() {
exchange.advance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Day;
import edu.ntnu.idi.idatt2003.gruppe42.Model.GameState;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

Expand All @@ -19,7 +15,8 @@
*/
public class TimeAndWeatherController implements AppController {
private final IntegerProperty hour = new SimpleIntegerProperty(0);
private final IntegerProperty dayIndex = new SimpleIntegerProperty(0); // 0 = Sunday, 1 = Monday, etc.
private final IntegerProperty dayIndex = new SimpleIntegerProperty(0);
private final IntegerProperty weekNumber = new SimpleIntegerProperty(0);
private final IntegerProperty temperature = new SimpleIntegerProperty(15);
private final StringProperty weather = new SimpleStringProperty("Sunny");
private final Random random = new Random();
Expand All @@ -37,10 +34,12 @@ public TimeAndWeatherController(GameController gameController) {
public void nextTick() {
int nextHour = hour.get() + 1;
if (nextHour >= 24) {
hour.set(0);
System.out.println("This is the day" + dayIndex);
System.out.println("This is the hour" + hour);
dayIndex.set((dayIndex.get() + 1) % 7);
updateWeather();
updateGameState();
updateWeather();
hour.set(0);
} else {
hour.set(nextHour);
}
Expand Down Expand Up @@ -75,6 +74,17 @@ public IntegerProperty dayIndexProperty() {
return dayIndex;
}

public IntegerProperty weekIndexProperty() {
return weekNumber;
}

public void advanceWeek() {
weekNumber.set(weekNumber.get() + 1);
dayIndex.set(1);
updateGameState();
gameController.startGame();
}

public IntegerProperty temperatureProperty() {
return temperature;
}
Expand All @@ -91,11 +101,18 @@ public String getDayOfWeekString() {
return DAYS[dayIndex.get()].toString();
}

public String getWeekString() {
return "Week " + weekNumber.get();
}

public void updateGameState() {
if (dayIndex.equals(new SimpleIntegerProperty(6))) {

if (dayIndex.get() == 6) {
System.out.println("det er lørdag");
gameController.setGameState(GameState.RENTDAY);
return;
} else if (dayIndex.equals(new SimpleIntegerProperty(0))) {
} else if (dayIndex.get() == 0) {
System.out.println("det er søndag");
gameController.setGameState(GameState.FREEDAY);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public DesktopViewController(
popupsController.getPopups().stream()
.map(Popup::getRoot).toArray(Pane[]::new)
);

this.desktopView.getNextWeekButton().setOnAction( event -> {
marketController.advanceWeek();
timeAndWeatherController.advanceWeek();
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void start(Stage stage) throws Exception {

public void initGame(String username, Difficulty difficulty) {
player = GameFactory.createPlayer(username, difficulty);
gameController = new GameController(player);
gameController = new GameController();
gameController.startGame();
switchToDesktopView();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public final class DesktopView {
private final DesktopViewController controller;
private final BorderPane root;

private Button nextWeekButton;

public DesktopView(DesktopViewController controller) {
this.controller = controller;
this.root = new BorderPane();
Expand Down Expand Up @@ -95,10 +97,17 @@ private HBox createBottomBar() {
playerLabel.getStyleClass().add("desktop-label-bold");
playerLabel.setMinWidth(Region.USE_PREF_SIZE);

Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
nextWeekButton = new Button("Advance to next week");

Region spacerLeft = new Region();
HBox.setHgrow(spacerLeft, Priority.ALWAYS);

Region spacerRight = new Region();
HBox.setHgrow(spacerRight, Priority.ALWAYS);

HBox bottomBar = new HBox(15, settingsButton, playerLabel, spacer, createStatusBox());
HBox bottomBar = new HBox(15,
settingsButton, playerLabel, spacerLeft, nextWeekButton, spacerRight, createStatusBox()
);
bottomBar.setAlignment(Pos.CENTER_LEFT);
bottomBar.setPadding(new Insets(5, 15, 5, 15));
bottomBar.getStyleClass().add("desktop-bottom-bar");
Expand All @@ -112,23 +121,27 @@ private HBox createStatusBox() {
Label weather = styledLabel("desktop-label");
Label temp = styledLabel("desktop-label");
Label day = styledLabel("desktop-label-bold");
Label week = styledLabel("desktop-label-bold");
Label clock = styledLabel("desktop-label-bold");

weather.setText(twc.weatherProperty().get());
temp.setText(twc.temperatureProperty().get() + "°C");
day.setText(twc.getDayOfWeekString());
week.setText(twc.getWeekString());
clock.setText(twc.getTimeString());

twc.hourProperty().addListener((obs, o, n)
-> Platform.runLater(() -> clock.setText(twc.getTimeString())));
twc.dayIndexProperty().addListener((obs, o, n)
-> Platform.runLater(() -> day.setText(twc.getDayOfWeekString())));
twc.weekIndexProperty().addListener((obs, o, n)
-> Platform.runLater(() -> week.setText(twc.getWeekString())));
twc.weatherProperty().addListener((obs, o, n)
-> Platform.runLater(() -> weather.setText(n)));
twc.temperatureProperty().addListener((obs, o, n)
-> Platform.runLater(() -> temp.setText(n + "°C")));

HBox box = new HBox(10, weather, temp, day, clock);
HBox box = new HBox(10, weather, temp, day, week, clock);
box.setAlignment(Pos.CENTER_RIGHT);
box.setMinWidth(Region.USE_PREF_SIZE);
return box;
Expand All @@ -143,4 +156,8 @@ private Label styledLabel(String styleClass) {
private String resource(String path) {
return getClass().getResource(path).toExternalForm();
}

public Button getNextWeekButton() {
return nextWeekButton;
}
}

0 comments on commit 4b019e2

Please sign in to comment.