Skip to content

Commit

Permalink
Start working on making the state machine accessible via the api
Browse files Browse the repository at this point in the history
  • Loading branch information
bonarc712 committed Apr 6, 2024
1 parent f7acd67 commit 3420ee1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
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;
}
}
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);
}
}

0 comments on commit 3420ee1

Please sign in to comment.