From 3420ee1208aa9285f34a8c0bf560ac04f16d783a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Roberge?= Date: Sat, 6 Apr 2024 00:30:25 -0400 Subject: [PATCH] Start working on making the state machine accessible via the api --- .../api/MahjongGameOrchestrator.java | 46 +++++++++++++++++++ .../api/MahjongGameOrchestratorTest.java | 31 +++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestrator.java create mode 100644 src/test/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestratorTest.java diff --git a/src/main/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestrator.java b/src/main/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestrator.java new file mode 100644 index 0000000..06bd4d7 --- /dev/null +++ b/src/main/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestrator.java @@ -0,0 +1,46 @@ +package com.monsieurmahjong.commonjong.api; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import com.monsieurmahjong.commonjong.game.mahjong.MahjongGame; +import com.monsieurmahjong.commonjong.rules.riichi.RiichiStandardRuleset; +import com.monsieurmahjong.commonjong.rules.riichi.RiichiTileset; +import com.monsieurmahjong.commonjong.rules.riichi.scoring.RiichiScoring; + +public class MahjongGameOrchestrator +{ + private static MahjongGameOrchestrator instance; + private Map ongoingMahjongGames; + + private MahjongGameOrchestrator() + { + ongoingMahjongGames = new HashMap<>(); + } + + protected static MahjongGameOrchestrator getInstance() + { + if (instance == null) + { + instance = new MahjongGameOrchestrator(); + } + return instance; + } + + public static UUID createRiichiGame() + { + var mahjongGame = new MahjongGame(null, new RiichiStandardRuleset(new RiichiTileset(), new RiichiScoring())); + var orchestrator = getInstance(); + var uuid = UUID.randomUUID(); + + orchestrator.ongoingMahjongGames.put(uuid, mahjongGame); + + return uuid; + } + + public static boolean hasMahjongGame(UUID uuid) + { + return getInstance().ongoingMahjongGames.get(uuid) != null; + } +} diff --git a/src/test/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestratorTest.java b/src/test/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestratorTest.java new file mode 100644 index 0000000..c506462 --- /dev/null +++ b/src/test/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestratorTest.java @@ -0,0 +1,31 @@ +package com.monsieurmahjong.commonjong.api; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.UUID; + +import org.junit.jupiter.api.Test; + +public class MahjongGameOrchestratorTest +{ + @Test + public void whenInvalidUUIDIsGiven_thenNoGameShouldBeFoundWithThisUUID() + { + var uuid = UUID.randomUUID(); + + var hasMahjongGame = MahjongGameOrchestrator.hasMahjongGame(uuid); + + assertFalse(hasMahjongGame); + } + + @Test + public void whenCreatingMahjongGame_thenThatGameShouldBeFoundInTheOrchestrator() + { + var uuid = MahjongGameOrchestrator.createRiichiGame(); + + var hasMahjongGame = MahjongGameOrchestrator.hasMahjongGame(uuid); + + assertTrue(hasMahjongGame); + } +}