-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start working on making the state machine accessible via the api
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UUID, MahjongGame> 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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/com/monsieurmahjong/commonjong/api/MahjongGameOrchestratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |