GameUuid as a part of GameState interface

This commit is contained in:
djmil 2023-09-18 11:34:12 +02:00
parent e26cfe0d91
commit bd6612f3e6
17 changed files with 60 additions and 54 deletions

View File

@ -335,6 +335,7 @@ public class CordaClient {
} }
if (responseBody.isFlowCompleted() && responseBody.flowResult() != null) { if (responseBody.isFlowCompleted() && responseBody.flowResult() != null) {
System.out.println("resp: "+ responseBody.flowResult());
return responseBody.flowResult(); return responseBody.flowResult();
} }
} }

View File

@ -14,8 +14,8 @@ public record GameBoard(
Map<Integer, Piece> board, Map<Integer, Piece> board,
GameBoardCommand previousCommand, GameBoardCommand previousCommand,
String message, String message,
UUID id) UUID gameUuid)
implements CordaState { implements GameState {
} }

View File

@ -10,6 +10,8 @@ public record GameProposal(
String acquier, String acquier,
Piece.Color acquierColor, Piece.Color acquierColor,
String message, String message,
UUID id) implements CordaState { UUID gameUuid)
implements GameState {
} }

View File

@ -9,8 +9,8 @@ public record GameResult(
String whitePlayerName, String whitePlayerName,
String blackPlayerName, String blackPlayerName,
Piece.Color victoryColor, Piece.Color victoryColor,
UUID id) UUID gameUuid)
implements CordaState { implements GameState {
} }

View File

@ -2,6 +2,6 @@ package djmil.cordacheckers.cordaclient.dao;
import java.util.UUID; import java.util.UUID;
public interface CordaState { public interface GameState {
public UUID id(); public UUID gameUuid();
} }

View File

@ -16,7 +16,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import djmil.cordacheckers.cordaclient.dao.CordaState; import djmil.cordacheckers.cordaclient.dao.GameState;
import djmil.cordacheckers.cordaclient.dao.GameBoard; import djmil.cordacheckers.cordaclient.dao.GameBoard;
import djmil.cordacheckers.cordaclient.dao.GameProposal; import djmil.cordacheckers.cordaclient.dao.GameProposal;
import djmil.cordacheckers.cordaclient.dao.GameResult; import djmil.cordacheckers.cordaclient.dao.GameResult;
@ -153,7 +153,7 @@ public class CordaClientTest {
List<GameBoard> gbListIssuer = cordaClient.gameBoardList( List<GameBoard> gbListIssuer = cordaClient.gameBoardList(
holdingIdentityResolver.getByUsername(gpIssuer)); holdingIdentityResolver.getByUsername(gpIssuer));
GameBoard gbAlice = findByUuid(gbListIssuer, gbState.id()); GameBoard gbAlice = findByUuid(gbListIssuer, gbState.gameUuid());
assertThat(gbAlice).isNotNull(); assertThat(gbAlice).isNotNull();
assertThat(gbAlice.opponentName()).isEqualToIgnoringCase(gpAcquier); assertThat(gbAlice.opponentName()).isEqualToIgnoringCase(gpAcquier);
assertThat(gbAlice.opponentColor()).isEqualByComparingTo(gpAcquierColor); assertThat(gbAlice.opponentColor()).isEqualByComparingTo(gpAcquierColor);
@ -163,7 +163,7 @@ public class CordaClientTest {
List<GameBoard> gbListAcquier = cordaClient.gameBoardList( List<GameBoard> gbListAcquier = cordaClient.gameBoardList(
holdingIdentityResolver.getByUsername(gpAcquier)); holdingIdentityResolver.getByUsername(gpAcquier));
GameBoard bgBob = findByUuid(gbListAcquier, gbState.id()); GameBoard bgBob = findByUuid(gbListAcquier, gbState.gameUuid());
assertThat(bgBob).isNotNull(); assertThat(bgBob).isNotNull();
assertThat(bgBob.opponentName()).isEqualToIgnoringCase(gpIssuer); assertThat(bgBob.opponentName()).isEqualToIgnoringCase(gpIssuer);
assertThat(bgBob.opponentColor()).isEqualByComparingTo(Piece.Color.BLACK); assertThat(bgBob.opponentColor()).isEqualByComparingTo(Piece.Color.BLACK);
@ -201,15 +201,15 @@ public class CordaClientTest {
hiBob, gpUuid hiBob, gpUuid
); );
System.out.println("New GameBoard UUID "+ gbState.id()); System.out.println("New GameBoard UUID "+ gbState.gameUuid());
assertThatThrownBy(() -> { // Alice can not surrender, since it is Bob's turn assertThatThrownBy(() -> { // Alice can not surrender, since it is Bob's turn
cordaClient.gameBoardSurrender( cordaClient.gameBoardSurrender(
hiAlice, gbState.id()); hiAlice, gbState.gameUuid());
}); });
final GameResult gameResult = cordaClient.gameBoardSurrender( final GameResult gameResult = cordaClient.gameBoardSurrender(
hiBob, gbState.id()); hiBob, gbState.gameUuid());
assertThat(gameResult.whitePlayerName()).isEqualTo(hiBob.getName()); assertThat(gameResult.whitePlayerName()).isEqualTo(hiBob.getName());
assertThat(gameResult.blackPlayerName()).isEqualTo(hiAlice.getName()); assertThat(gameResult.blackPlayerName()).isEqualTo(hiAlice.getName());
@ -233,21 +233,21 @@ public class CordaClientTest {
hiBob, gpUuid hiBob, gpUuid
); );
System.out.println("New GameBoard UUID "+ gbState.id()); System.out.println("New GameBoard UUID "+ gbState.gameUuid());
assertThatThrownBy(() -> { // Alice can not move, since it is Bob's turn assertThatThrownBy(() -> { // Alice can not move, since it is Bob's turn
cordaClient.gameBoardMove( cordaClient.gameBoardMove(
hiAlice, gbState.id(), hiAlice, gbState.gameUuid(),
Arrays.asList(1, 2)); Arrays.asList(1, 2));
}); });
final GameBoard gameBoard = cordaClient.gameBoardMove( final GameBoard gameBoard = cordaClient.gameBoardMove(
hiBob, gbState.id(), Arrays.asList(1, 2)); hiBob, gbState.gameUuid(), Arrays.asList(1, 2));
} }
private <T extends CordaState> T findByUuid(List<T> statesList, UUID uuid) { private <T extends GameState> T findByUuid(List<T> statesList, UUID uuid) {
for (T state : statesList) { for (T state : statesList) {
if (state.id().compareTo(uuid) == 0) if (state.gameUuid().compareTo(uuid) == 0)
return state; return state;
}; };
return null; return null;

View File

@ -8,10 +8,13 @@ import net.corda.v5.base.annotations.CordaSerializable;
import net.corda.v5.base.types.MemberX500Name; import net.corda.v5.base.types.MemberX500Name;
@CordaSerializable @CordaSerializable
public interface Counterparty { public interface Game {
@NotNull @NotNull
MemberX500Name getCounterpartyName(MemberX500Name myName) throws NotInvolved; MemberX500Name getCounterpartyName(MemberX500Name myName) throws NotInvolved;
@NotNull
UUID getGameUuid();
public static class NotInvolved extends RuntimeException { public static class NotInvolved extends RuntimeException {
public <T> NotInvolved(MemberX500Name myName, Class<T> clazz, UUID uuid) { public <T> NotInvolved(MemberX500Name myName, Class<T> clazz, UUID uuid) {

View File

@ -14,7 +14,7 @@ import net.corda.v5.ledger.utxo.BelongsToContract;
import net.corda.v5.ledger.utxo.ContractState; import net.corda.v5.ledger.utxo.ContractState;
@BelongsToContract(GameBoardContract.class) @BelongsToContract(GameBoardContract.class)
public class GameBoardState implements ContractState, Counterparty { public class GameBoardState implements ContractState, Game {
private final MemberX500Name whitePlayerName; private final MemberX500Name whitePlayerName;
private final MemberX500Name blackPlayerName; private final MemberX500Name blackPlayerName;
@ -24,7 +24,7 @@ public class GameBoardState implements ContractState, Counterparty {
private final Map<Integer, Piece> board; private final Map<Integer, Piece> board;
private final String message; private final String message;
private final UUID id; private final UUID gameUuid;
private final List<PublicKey> participants; private final List<PublicKey> participants;
public GameBoardState( public GameBoardState(
@ -39,7 +39,7 @@ public class GameBoardState implements ContractState, Counterparty {
this.board = new LinkedHashMap<Integer, Piece>(initialBoard); this.board = new LinkedHashMap<Integer, Piece>(initialBoard);
this.message = null; this.message = null;
this.id = gameProposalState.getId(); this.gameUuid = gameProposalState.getGameUuid();
this.participants = gameProposalState.getParticipants(); this.participants = gameProposalState.getParticipants();
} }
@ -55,21 +55,21 @@ public class GameBoardState implements ContractState, Counterparty {
this.board = newBoard; this.board = newBoard;
this.message = null; this.message = null;
this.id = oldGameBoardState.getId(); this.gameUuid = oldGameBoardState.getGameUuid();
this.participants = oldGameBoardState.getParticipants(); this.participants = oldGameBoardState.getParticipants();
} }
@ConstructorForDeserialization @ConstructorForDeserialization
public GameBoardState(MemberX500Name whitePlayerName, MemberX500Name blackPlayerName, public GameBoardState(MemberX500Name whitePlayerName, MemberX500Name blackPlayerName,
Color moveColor, Integer moveNumber, Map<Integer, Piece> board, String message, Color moveColor, Integer moveNumber, Map<Integer, Piece> board, String message,
UUID id, List<PublicKey> participants) { UUID gameUuid, List<PublicKey> participants) {
this.whitePlayerName = whitePlayerName; this.whitePlayerName = whitePlayerName;
this.blackPlayerName = blackPlayerName; this.blackPlayerName = blackPlayerName;
this.moveColor = moveColor; this.moveColor = moveColor;
this.moveNumber = moveNumber; this.moveNumber = moveNumber;
this.board = board; this.board = board;
this.message = message; this.message = message;
this.id = id; this.gameUuid = gameUuid;
this.participants = participants; this.participants = participants;
} }
@ -97,8 +97,8 @@ public class GameBoardState implements ContractState, Counterparty {
return message; return message;
} }
public UUID getId() { public UUID getGameUuid() {
return id; return gameUuid;
} }
public List<PublicKey> getParticipants() { public List<PublicKey> getParticipants() {
@ -113,7 +113,7 @@ public class GameBoardState implements ContractState, Counterparty {
if (blackPlayerName.compareTo(myName) == 0) if (blackPlayerName.compareTo(myName) == 0)
return whitePlayerName; return whitePlayerName;
throw new Counterparty.NotInvolved(myName, GameBoardState.class, this.id); throw new Game.NotInvolved(myName, GameBoardState.class, this.gameUuid);
} }
public Piece.Color getCounterpartyColor(MemberX500Name myName) throws NotInvolved { public Piece.Color getCounterpartyColor(MemberX500Name myName) throws NotInvolved {

View File

@ -11,13 +11,13 @@ import net.corda.v5.ledger.utxo.BelongsToContract;
import net.corda.v5.ledger.utxo.ContractState; import net.corda.v5.ledger.utxo.ContractState;
@BelongsToContract(GameProposalContract.class) @BelongsToContract(GameProposalContract.class)
public class GameProposalState implements ContractState, Counterparty { public class GameProposalState implements ContractState, Game {
private final MemberX500Name issuer; private final MemberX500Name issuer;
private final MemberX500Name acquier; private final MemberX500Name acquier;
private final Piece.Color acquierColor; private final Piece.Color acquierColor;
private final String message; private final String message;
private final UUID id; private final UUID gameUuid;
private final List<PublicKey> participants; private final List<PublicKey> participants;
@ConstructorForDeserialization @ConstructorForDeserialization
@ -26,14 +26,14 @@ public class GameProposalState implements ContractState, Counterparty {
MemberX500Name acquier, MemberX500Name acquier,
Piece.Color acquierColor, Piece.Color acquierColor,
String message, String message,
UUID id, UUID gameUuid,
List<PublicKey> participants List<PublicKey> participants
) { ) {
this.issuer = issuer; this.issuer = issuer;
this.acquier = acquier; this.acquier = acquier;
this.acquierColor = acquierColor; this.acquierColor = acquierColor;
this.message = message; this.message = message;
this.id = id; this.gameUuid = gameUuid;
this.participants = participants; this.participants = participants;
} }
@ -53,8 +53,8 @@ public class GameProposalState implements ContractState, Counterparty {
return message; return message;
} }
public UUID getId() { public UUID getGameUuid() {
return id; return gameUuid;
} }
public List<PublicKey> getParticipants() { public List<PublicKey> getParticipants() {
@ -77,7 +77,7 @@ public class GameProposalState implements ContractState, Counterparty {
if (acquier.compareTo(myName) == 0) if (acquier.compareTo(myName) == 0)
return issuer; return issuer;
throw new Counterparty.NotInvolved(myName, GameProposalState.class, this.id); throw new Game.NotInvolved(myName, GameProposalState.class, this.gameUuid);
} }
} }

View File

@ -11,23 +11,23 @@ import net.corda.v5.ledger.utxo.BelongsToContract;
import net.corda.v5.ledger.utxo.ContractState; import net.corda.v5.ledger.utxo.ContractState;
@BelongsToContract(GameResultContract.class) @BelongsToContract(GameResultContract.class)
public class GameResultState implements ContractState, Counterparty { public class GameResultState implements ContractState, Game {
private final MemberX500Name whitePlayerName; private final MemberX500Name whitePlayerName;
private final MemberX500Name blackPlayerName; private final MemberX500Name blackPlayerName;
private final Piece.Color victoryColor; private final Piece.Color victoryColor;
private final UUID id; private final UUID gameUuid;
private final List<PublicKey> participants; private final List<PublicKey> participants;
@ConstructorForDeserialization @ConstructorForDeserialization
public GameResultState(MemberX500Name whitePlayerName, MemberX500Name blackPlayerName, Piece.Color victoryColor, public GameResultState(MemberX500Name whitePlayerName, MemberX500Name blackPlayerName, Piece.Color victoryColor,
UUID id, List<PublicKey> participants) { UUID gameUuid, List<PublicKey> participants) {
this.whitePlayerName = whitePlayerName; this.whitePlayerName = whitePlayerName;
this.blackPlayerName = blackPlayerName; this.blackPlayerName = blackPlayerName;
this.victoryColor = victoryColor; this.victoryColor = victoryColor;
this.id = id; this.gameUuid = gameUuid;
this.participants = participants; this.participants = participants;
} }
@ -36,7 +36,7 @@ public class GameResultState implements ContractState, Counterparty {
this.blackPlayerName = stateGameBoard.getBlackPlayerName(); this.blackPlayerName = stateGameBoard.getBlackPlayerName();
this.victoryColor = victoryColor; this.victoryColor = victoryColor;
this.id = stateGameBoard.getId(); this.gameUuid = stateGameBoard.getGameUuid();
this.participants = stateGameBoard.getParticipants(); this.participants = stateGameBoard.getParticipants();
} }
@ -52,8 +52,8 @@ public class GameResultState implements ContractState, Counterparty {
return victoryColor; return victoryColor;
} }
public UUID getId() { public UUID getGameUuid() {
return id; return gameUuid;
} }
@Override @Override
@ -69,7 +69,7 @@ public class GameResultState implements ContractState, Counterparty {
if (blackPlayerName.compareTo(myName) == 0) if (blackPlayerName.compareTo(myName) == 0)
return whitePlayerName; return whitePlayerName;
throw new Counterparty.NotInvolved(myName, GameResultState.class, this.id); throw new Game.NotInvolved(myName, GameResultState.class, this.gameUuid);
} }
} }

View File

@ -85,7 +85,7 @@ public class CommandFlow implements ClientStartableFlow {
return this.utxoLedgerService return this.utxoLedgerService
.findUnconsumedStatesByType(GameBoardState.class) .findUnconsumedStatesByType(GameBoardState.class)
.stream() .stream()
.filter(sar -> sar.getState().getContractState().getId().equals(gameBoardUuid)) .filter(sar -> sar.getState().getContractState().getGameUuid().equals(gameBoardUuid))
.reduce((a, b) -> {throw new IllegalStateException("Multiple states: " +a +", " +b);}) .reduce((a, b) -> {throw new IllegalStateException("Multiple states: " +a +", " +b);})
.get(); .get();
} }

View File

@ -5,7 +5,7 @@ import java.util.UUID;
import djmil.cordacheckers.contracts.GameBoardCommand; import djmil.cordacheckers.contracts.GameBoardCommand;
import djmil.cordacheckers.contracts.UtxoLedgerTransactionUtil; import djmil.cordacheckers.contracts.UtxoLedgerTransactionUtil;
import djmil.cordacheckers.states.Counterparty.NotInvolved; import djmil.cordacheckers.states.Game.NotInvolved;
import djmil.cordacheckers.states.GameBoardState; import djmil.cordacheckers.states.GameBoardState;
import djmil.cordacheckers.states.Piece; import djmil.cordacheckers.states.Piece;
import net.corda.v5.base.types.MemberX500Name; import net.corda.v5.base.types.MemberX500Name;
@ -21,7 +21,7 @@ public class GameBoardView {
public final Map<Integer, Piece> board; public final Map<Integer, Piece> board;
public final GameBoardCommand previousCommand; public final GameBoardCommand previousCommand;
public final String message; public final String message;
public final UUID id; public final UUID gameUuid;
/* /*
* GameStatus enum: * GameStatus enum:
@ -40,7 +40,7 @@ public class GameBoardView {
this.board = null; this.board = null;
this.previousCommand = null; this.previousCommand = null;
this.message = null; this.message = null;
this.id = null; this.gameUuid = null;
} }
// A view from a perspective of a concrete player, on a ledger transaction that has // A view from a perspective of a concrete player, on a ledger transaction that has
@ -62,7 +62,7 @@ public class GameBoardView {
this.moveNumber = stateGameBoard.getMoveNumber(); this.moveNumber = stateGameBoard.getMoveNumber();
this.board = stateGameBoard.getBoard(); this.board = stateGameBoard.getBoard();
this.message = stateGameBoard.getMessage(); this.message = stateGameBoard.getMessage();
this.id = stateGameBoard.getId(); this.gameUuid = stateGameBoard.getGameUuid();
} }
} }

View File

@ -8,7 +8,7 @@ import org.slf4j.LoggerFactory;
import djmil.cordacheckers.FlowResult; import djmil.cordacheckers.FlowResult;
import djmil.cordacheckers.states.GameBoardState; import djmil.cordacheckers.states.GameBoardState;
import djmil.cordacheckers.states.Counterparty.NotInvolved; import djmil.cordacheckers.states.Game.NotInvolved;
import net.corda.v5.application.flows.ClientRequestBody; import net.corda.v5.application.flows.ClientRequestBody;
import net.corda.v5.application.flows.ClientStartableFlow; import net.corda.v5.application.flows.ClientStartableFlow;
import net.corda.v5.application.flows.CordaInject; import net.corda.v5.application.flows.CordaInject;

View File

@ -10,7 +10,7 @@ import djmil.cordacheckers.contracts.GameProposalCommand;
import djmil.cordacheckers.gameboard.GameBoardView; import djmil.cordacheckers.gameboard.GameBoardView;
import djmil.cordacheckers.states.GameBoardState; import djmil.cordacheckers.states.GameBoardState;
import djmil.cordacheckers.states.GameProposalState; import djmil.cordacheckers.states.GameProposalState;
import djmil.cordacheckers.states.Counterparty.NotInvolved; import djmil.cordacheckers.states.Game.NotInvolved;
import net.corda.v5.application.flows.ClientRequestBody; import net.corda.v5.application.flows.ClientRequestBody;
import net.corda.v5.application.flows.ClientStartableFlow; import net.corda.v5.application.flows.ClientStartableFlow;
import net.corda.v5.application.flows.CordaInject; import net.corda.v5.application.flows.CordaInject;
@ -86,7 +86,7 @@ public class CommandFlow implements ClientStartableFlow {
return this.utxoLedgerService return this.utxoLedgerService
.findUnconsumedStatesByType(GameProposalState.class) .findUnconsumedStatesByType(GameProposalState.class)
.stream() .stream()
.filter(sar -> sar.getState().getContractState().getId().equals(uuidGameProposal)) .filter(sar -> sar.getState().getContractState().getGameUuid().equals(uuidGameProposal))
.reduce((a, b) -> {throw new IllegalStateException("Multiple states: " +a +", " +b);}) .reduce((a, b) -> {throw new IllegalStateException("Multiple states: " +a +", " +b);})
.get(); .get();
} }

View File

@ -62,7 +62,7 @@ public class CreateFlow implements ClientStartableFlow{
final SecureHash trxId = this.flowEngine final SecureHash trxId = this.flowEngine
.subFlow(new CommitSubFlow(trx, command.getRespondent(newGameProposal))); .subFlow(new CommitSubFlow(trx, command.getRespondent(newGameProposal)));
return new FlowResult(newGameProposal.getId(), trxId) return new FlowResult(newGameProposal.getGameUuid(), trxId)
.toJsonEncodedString(jsonMarshallingService); .toJsonEncodedString(jsonMarshallingService);
} }
catch (Exception e) { catch (Exception e) {

View File

@ -31,6 +31,6 @@ public class ListItem {
this.acquier = state.getAcquier().getCommonName(); this.acquier = state.getAcquier().getCommonName();
this.acquierColor = state.getAcquierColor(); this.acquierColor = state.getAcquierColor();
this.message = state.getMessage(); this.message = state.getMessage();
this.id = state.getId(); this.id = state.getGameUuid();
} }
} }

View File

@ -23,7 +23,7 @@ public class GameResultView {
this.whitePlayerName = gameResultState.getWhitePlayerName().getCommonName(); this.whitePlayerName = gameResultState.getWhitePlayerName().getCommonName();
this.blackPlayerName = gameResultState.getBlackPlayerName().getCommonName(); this.blackPlayerName = gameResultState.getBlackPlayerName().getCommonName();
this.victoryColor = gameResultState.getVictoryColor(); this.victoryColor = gameResultState.getVictoryColor();
this.id = gameResultState.getId(); this.id = gameResultState.getGameUuid();
} }
} }