Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Aug 20, 2023
1 parent 76160fd commit f2a9baa
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 251 deletions.
8 changes: 8 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33563,6 +33563,8 @@ paths:
default: AWon
coinsDistributed:
type: boolean
serverConfirmed:
type: boolean
default:
description: An unexpected error response.
schema:
Expand Down Expand Up @@ -33684,6 +33686,8 @@ paths:
default: AWon
coinsDistributed:
type: boolean
serverConfirmed:
type: boolean
default:
description: An unexpected error response.
schema:
Expand Down Expand Up @@ -68612,6 +68616,8 @@ definitions:
default: AWon
coinsDistributed:
type: boolean
serverConfirmed:
type: boolean
DecentralCardGame.cardchain.cardchain.MatchPlayer:
type: object
properties:
Expand Down Expand Up @@ -69116,6 +69122,8 @@ definitions:
default: AWon
coinsDistributed:
type: boolean
serverConfirmed:
type: boolean
DecentralCardGame.cardchain.cardchain.QueryQSellOffersResponse:
type: object
properties:
Expand Down
9 changes: 8 additions & 1 deletion proto/cardchain/cardchain/match.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ syntax = "proto3";
package DecentralCardGame.cardchain.cardchain;

option go_package = "github.com/DecentralCardGame/Cardchain/x/cardchain/types";
import "cardchain/cardchain/tx.proto";

message Match {

Expand All @@ -12,6 +11,7 @@ message Match {
MatchPlayer playerB = 4;
Outcome outcome = 7;
bool coinsDistributed = 10;
bool serverConfirmed = 8;
}

message MatchPlayer {
Expand All @@ -22,3 +22,10 @@ message MatchPlayer {
repeated uint64 deck = 5;
repeated uint64 votedCards = 6;
}

enum Outcome {
AWon = 0;
BWon = 1;
Draw = 2;
Aborted = 3;
}
7 changes: 1 addition & 6 deletions proto/cardchain/cardchain/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package DecentralCardGame.cardchain.cardchain;

import "gogoproto/gogo.proto";
import "cardchain/cardchain/council.proto";
import "cardchain/cardchain/match.proto";
import "cosmos/base/v1beta1/coin.proto";

// this line is used by starport scaffolding # proto/tx/import
Expand Down Expand Up @@ -161,12 +162,6 @@ message MsgReportMatch {
Outcome outcome = 5;
}

enum Outcome {
AWon = 0;
BWon = 1;
Draw = 2;
Aborted = 3;
}
message MsgReportMatchResponse {
uint64 matchId = 1;
}
Expand Down
44 changes: 44 additions & 0 deletions x/cardchain/keeper/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -129,3 +130,46 @@ func (k Keeper) GetOutcome(ctx sdk.Context, match types.Match) (outcome types.Ou
}
return
}

func (k Keeper) TryHandleMatchOutcome(ctx sdk.Context, match *types.Match) error {
if match.PlayerA.Confirmed && match.PlayerB.Confirmed && match.ServerConfirmed {
return k.HandleMatchOutcome(ctx, match)
}
return nil
}

func (k Keeper) HandleMatchOutcome(ctx sdk.Context, match *types.Match) error {
players := []*types.MatchPlayer{match.PlayerA, match.PlayerB}

// Evaluate Outcome
outcomes := []types.Outcome{match.Outcome, match.PlayerA.Outcome, match.PlayerB.Outcome}
slices.Sort(outcomes)
outcomes = slices.Compact(outcomes)
switch i := uint64(len(outcomes)); i {
case 1:
k.ReportServerMatch(ctx, match.Reporter, 1, true)
default:
k.ReportServerMatch(ctx, match.Reporter, i-1, false)
}

outcome, err := k.GetOutcome(ctx, *match)
match.Outcome = outcome

err = k.DistributeCoins(ctx, match, outcome)
if err != nil {
return err
}

for idx, player := range players {
// filter voted cards cards
var votedCards []uint64
for _, card := range player.VotedCards {
if slices.Contains(players[(idx+1)%2].Deck, card) {
votedCards = append(votedCards, card)
}
}
}

// TODO: Votes
return nil
}
14 changes: 6 additions & 8 deletions x/cardchain/keeper/msg_server_confirm_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ func (k msgServer) ConfirmMatch(goCtx context.Context, msg *types.MsgConfirmMatc
}

player.Outcome = msg.Outcome

player.VotedCards = msg.VotedCards
player.Confirmed = true

// Prefilter voted cards cards
var votedCards []uint64
for _, card := range msg.VotedCards {
if slices.Contains(otherPlayer.Deck, card) {
votedCards = append(votedCards, card)
}
err := k.TryHandleMatchOutcome(ctx, match)
if err != nil {
return nil, err
}

player.VotedCards = votedCards
player.Confirmed = true
k.Matches.Set(ctx, msg.MatchId, match)

return &types.MsgConfirmMatchResponse{}, nil
Expand Down
20 changes: 1 addition & 19 deletions x/cardchain/keeper/msg_server_report_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package keeper
import (
"context"

"golang.org/x/exp/slices"

"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -34,27 +32,11 @@ func (k msgServer) ReportMatch(goCtx context.Context, msg *types.MsgReportMatch)

match.Outcome = msg.Outcome

// Evaluate Outcome
outcomes := []types.Outcome{msg.Outcome, match.PlayerA.Outcome, match.PlayerB.Outcome}
slices.Sort(outcomes)
outcomes = slices.Compact(outcomes)
switch i := uint64(len(outcomes)); i {
case 1:
k.ReportServerMatch(ctx, match.Reporter, 1, true)
default:
k.ReportServerMatch(ctx, match.Reporter, i-1, false)
}

outcome, err := k.GetOutcome(ctx, *match)
match.Outcome = outcome

err = k.DistributeCoins(ctx, match, outcome)
err = k.TryHandleMatchOutcome(ctx, match)
if err != nil {
return nil, err
}

// TODO: Votes

k.Matches.Set(ctx, msg.MatchId, match)

return &types.MsgReportMatchResponse{}, nil
Expand Down
124 changes: 100 additions & 24 deletions x/cardchain/types/match.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f2a9baa

Please sign in to comment.