From fe1708ad324b7aa8f6ae413f0bedceb10dc643de Mon Sep 17 00:00:00 2001 From: djmil Date: Fri, 22 Sep 2023 16:07:44 +0200 Subject: [PATCH] Draft: RankingFlow +Test --- .../cordaclient/CordaClient.java | 13 ++++ .../cordacheckers/cordaclient/dao/Rank.java | 9 +++ .../dao/flow/arguments/RspRankList.java | 11 ++++ .../HoldingIdentityResolverTests.java | 2 +- .../djmil/cordacheckers/gameresult/Rank.java | 20 ++++++ .../cordacheckers/gameresult/RankingFlow.java | 61 +++++++++++++++++++ .../gameresult/RankingFlowResponce.java | 25 ++++++++ 7 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/djmil/cordacheckers/cordaclient/dao/Rank.java create mode 100644 backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/RspRankList.java create mode 100644 corda/workflows/src/main/java/djmil/cordacheckers/gameresult/Rank.java create mode 100644 corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlow.java create mode 100644 corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlowResponce.java diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java index f863cd6..1d9952a 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import djmil.cordacheckers.cordaclient.dao.GameState; import djmil.cordacheckers.cordaclient.dao.HoldingIdentity; import djmil.cordacheckers.cordaclient.dao.Piece; +import djmil.cordacheckers.cordaclient.dao.Rank; import djmil.cordacheckers.cordaclient.dao.VirtualNode; import djmil.cordacheckers.cordaclient.dao.VirtualNodeList; import djmil.cordacheckers.cordaclient.dao.flow.RequestBody; @@ -29,6 +30,7 @@ import djmil.cordacheckers.cordaclient.dao.flow.arguments.ReqGameBoardMove; import djmil.cordacheckers.cordaclient.dao.flow.arguments.ReqGameProposalCreate; import djmil.cordacheckers.cordaclient.dao.flow.arguments.RspGameState; import djmil.cordacheckers.cordaclient.dao.flow.arguments.RspGameStateList; +import djmil.cordacheckers.cordaclient.dao.flow.arguments.RspRankList; @Service public class CordaClient { @@ -58,6 +60,17 @@ public class CordaClient { ).virtualNodes(); } + // use custodian holding identity to get ranking table of all players + public List fetchRanking(HoldingIdentity holdingIdentity) { + final RequestBody requestBody = new RequestBody( + "ranking-" +UUID.randomUUID(), + "djmil.cordacheckers.gameresult.RankingFlow", + new Req()); + + return cordaFlowExecute(holdingIdentity, requestBody, RspRankList.class) + .getResponce(requestBody); + } + /** * @param holdingIdentity * @return list of unconsumed (active) GameStateViews diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/Rank.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/Rank.java new file mode 100644 index 0000000..3dda122 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/Rank.java @@ -0,0 +1,9 @@ +package djmil.cordacheckers.cordaclient.dao; + +public record Rank( + String name, + Integer gamesPlayed, + Integer gamesWon +) { + +} diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/RspRankList.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/RspRankList.java new file mode 100644 index 0000000..78a3c10 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/RspRankList.java @@ -0,0 +1,11 @@ +package djmil.cordacheckers.cordaclient.dao.flow.arguments; + +import java.util.List; + +import djmil.cordacheckers.cordaclient.dao.Rank; + +public record RspRankList( + List successStatus, + String failureStatus) implements Rsp> { + +} diff --git a/backend/src/test/java/djmil/cordacheckers/HoldingIdentityResolverTests.java b/backend/src/test/java/djmil/cordacheckers/HoldingIdentityResolverTests.java index b39a7e7..3dc846d 100644 --- a/backend/src/test/java/djmil/cordacheckers/HoldingIdentityResolverTests.java +++ b/backend/src/test/java/djmil/cordacheckers/HoldingIdentityResolverTests.java @@ -48,7 +48,7 @@ public class HoldingIdentityResolverTests { */ @Test - void testCheckUsers() { + void testPlayer() { assertDoesNotThrow( () -> { holdingIdentityResolver.getByUsername("alice"); holdingIdentityResolver.getByUsername("BOB"); diff --git a/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/Rank.java b/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/Rank.java new file mode 100644 index 0000000..dc427b6 --- /dev/null +++ b/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/Rank.java @@ -0,0 +1,20 @@ +package djmil.cordacheckers.gameresult; + +public class Rank { + public final String name; + public final Integer gamesPlayed; + public final Integer gamesWon; + + // Serialisation service requires a default constructor + Rank() { + name = null; + gamesPlayed = null; + gamesWon = null; + } + + public Rank(String name, Integer gamesPlayed, Integer gamesWon) { + this.name = name; + this.gamesPlayed = gamesPlayed; + this.gamesWon = gamesWon; + } +} diff --git a/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlow.java b/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlow.java new file mode 100644 index 0000000..3514631 --- /dev/null +++ b/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlow.java @@ -0,0 +1,61 @@ +package djmil.cordacheckers.gameresult; + +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import djmil.cordacheckers.states.GameResultState; +import net.corda.v5.application.flows.ClientRequestBody; +import net.corda.v5.application.flows.ClientStartableFlow; +import net.corda.v5.application.flows.CordaInject; +import net.corda.v5.application.flows.FlowEngine; +import net.corda.v5.application.marshalling.JsonMarshallingService; +import net.corda.v5.base.annotations.Suspendable; +import net.corda.v5.ledger.utxo.UtxoLedgerService; + +public class RankingFlow implements ClientStartableFlow { + + private final static Logger log = LoggerFactory.getLogger(RankingFlow.class); + + @CordaInject + public UtxoLedgerService utxoLedgerService; + + @CordaInject + public JsonMarshallingService jsonMarshallingService; + + @CordaInject + public FlowEngine flowEngine; + + @Suspendable + @Override + public String call(ClientRequestBody requestBody) { + try { + final List gameStateResutList = getGameResultsList(); + + final List board = new LinkedList(List.of( + new Rank("alice", 42, 1), + new Rank("bobik", 25, 78) + )); + + return new RankingFlowResponce(board) + .toJsonEncodedString(jsonMarshallingService); + } catch (Exception e) { + log.warn("Exception during processing " + requestBody + " request: " + e.getMessage()); + return new RankingFlowResponce(e) + .toJsonEncodedString(jsonMarshallingService); + } + } + + @Suspendable + List getGameResultsList() { + return utxoLedgerService + .findUnconsumedStatesByType(GameResultState.class) + .stream() + .map( stateAndRef -> stateAndRef.getState().getContractState() ) + .collect(Collectors.toList()); + } + +} diff --git a/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlowResponce.java b/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlowResponce.java new file mode 100644 index 0000000..2dcc36b --- /dev/null +++ b/corda/workflows/src/main/java/djmil/cordacheckers/gameresult/RankingFlowResponce.java @@ -0,0 +1,25 @@ +package djmil.cordacheckers.gameresult; + +import java.util.List; + +import net.corda.v5.application.marshalling.JsonMarshallingService; + +public class RankingFlowResponce { + public final List successStatus; + public final String failureStatus; + + public RankingFlowResponce(List success) { + this.successStatus = success; + this.failureStatus = null; + } + + public RankingFlowResponce(Exception exception) { + this.successStatus = null; + this.failureStatus = exception.getMessage(); + } + + public String toJsonEncodedString(JsonMarshallingService jsonMarshallingService) { + return jsonMarshallingService.format(this); + } + +} \ No newline at end of file