Commit 61cb88ac authored by Joshua Gutow's avatar Joshua Gutow Committed by GitHub

Add IsDuplicate to game state (#6173)

Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 6d87eb7c
......@@ -49,9 +49,8 @@ func (a *Agent) move(claim, parent Claim) error {
if err != nil || move == nil {
return err
}
// TODO(CLI-4123): Don't send duplicate responses
// if a.game.IsDuplicate(move) {
// return nil
// }
if a.game.IsDuplicate(*move) {
return nil
}
return a.responder.Respond(context.TODO(), *move)
}
......@@ -22,6 +22,8 @@ type Game interface {
claim Claim
parent Claim
}
IsDuplicate(claim Claim) bool
}
// Node is a node in the game state tree.
......@@ -33,17 +35,21 @@ type Node struct {
// gameState is a struct that represents the state of a dispute game.
// The game state implements the [Game] interface.
type gameState struct {
root Node
root Node
claims map[ClaimData]Claim
}
// NewGameState returns a new game state.
// The provided [Claim] is used as the root node.
func NewGameState(root Claim) *gameState {
claims := make(map[ClaimData]Claim)
claims[root.ClaimData] = root
return &gameState{
root: Node{
self: root,
children: make([]*Node, 0),
},
claims: claims,
}
}
......@@ -116,10 +122,16 @@ func (g *gameState) Put(claim Claim) error {
// Add the node to the tree.
found.children = append(found.children, &node)
g.claims[claim.ClaimData] = claim
return nil
}
func (g *gameState) IsDuplicate(claim Claim) bool {
_, ok := g.claims[claim.ClaimData]
return ok
}
// recurseTreePairs recursively walks down the tree from the root node
// returning a list of claim and parent pairs.
func (g *gameState) recurseTreePairs(current *Node) []struct {
......
......@@ -35,6 +35,21 @@ func createTestClaims() (Claim, Claim, Claim) {
return top, middle, bottom
}
func TestIsDuplicate(t *testing.T) {
// Setup the game state.
top, middle, bottom := createTestClaims()
g := NewGameState(top)
err := g.Put(middle)
require.NoError(t, err)
// Top + Middle should be duplicates
require.True(t, g.IsDuplicate(top))
require.True(t, g.IsDuplicate(middle))
// Bottom should not be a duplicate
require.False(t, g.IsDuplicate(bottom))
}
// TestGame_Put_RootAlreadyExists tests the [Game.Put] method using a [gameState]
// instance errors when the root claim already exists in state.
func TestGame_Put_RootAlreadyExists(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment