Draft: RankingFlow +Test

This commit is contained in:
djmil 2023-09-22 16:07:44 +02:00
parent 74176ecf45
commit fe1708ad32
7 changed files with 140 additions and 1 deletions

View File

@ -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<Rank> 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

View File

@ -0,0 +1,9 @@
package djmil.cordacheckers.cordaclient.dao;
public record Rank(
String name,
Integer gamesPlayed,
Integer gamesWon
) {
}

View File

@ -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<Rank> successStatus,
String failureStatus) implements Rsp<List<Rank>> {
}

View File

@ -48,7 +48,7 @@ public class HoldingIdentityResolverTests {
*/
@Test
void testCheckUsers() {
void testPlayer() {
assertDoesNotThrow( () -> {
holdingIdentityResolver.getByUsername("alice");
holdingIdentityResolver.getByUsername("BOB");

View File

@ -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;
}
}

View File

@ -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<GameResultState> gameStateResutList = getGameResultsList();
final List<Rank> board = new LinkedList<Rank>(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<GameResultState> getGameResultsList() {
return utxoLedgerService
.findUnconsumedStatesByType(GameResultState.class)
.stream()
.map( stateAndRef -> stateAndRef.getState().getContractState() )
.collect(Collectors.toList());
}
}

View File

@ -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<Rank> successStatus;
public final String failureStatus;
public RankingFlowResponce(List<Rank> 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);
}
}