Skip to content

Commit

Permalink
replace root validation element
Browse files Browse the repository at this point in the history
  • Loading branch information
nlsui committed Jun 11, 2024
1 parent 52ea0a9 commit a238e31
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
7 changes: 7 additions & 0 deletions cardobject.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "."
}
]
}
1 change: 1 addition & 0 deletions cardobject/validatePrimitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const maxTagCount int = 3
const minTagCount int = 1
const maxWisdom int = 32
const minWisdom int = 0
const maxKeywords int = 9

type Attack jsonschema.BasicInt

Expand Down
61 changes: 42 additions & 19 deletions keywords/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,72 @@ package keywords

import (
"errors"
"strconv"

"github.com/DecentralCardGame/cardobject/cardobject"
"github.com/DecentralCardGame/cardobject/jsonschema"
)

//Card Represents the data-structure of a crowd control card in the blockchain
type Card struct {
Action *action `json:",omitempty"`
Entity *entity `json:",omitempty"`
Place *place `json:",omitempty"`
Headquarter *headquarter `json:",omitempty"`
const maxKeywords int = 8

type CardRootValidator struct {
Keywords int
ClassProvider cardobject.ClassProvider
Type string
}

//Validate Ensures that the card was built correctly and returns errors otherwise
func (c Card) Validate() error {
return c.ValidateType(c)
// Validate Ensures that the card was built correctly and returns errors otherwise
func (c CardRootValidator) Validate() error {
return nil
}

//ValidateClasses Checks if the given classes are covered by the card
func (c Card) ValidateClasses(cb jsonschema.ClassBound) error {
i, _ := c.FindImplementer()
if i.(cardobject.ClassProvider).ClassRestriction().Contains(cb.Classes()) {
// ValidateClasses Checks if the given classes are covered by the card
func (c CardRootValidator) ValidateClasses(cb jsonschema.ClassBound) error {
if c.ClassProvider.ClassRestriction().Contains(cb.Classes()) {
return nil
}
return errors.New("Classes are not covered by the cards classes")
}

//ValidateTarget Checks if the given classes are covered by the card
func (c Card) ValidateTarget(t jsonschema.Targeting) error {
func (c CardRootValidator) ValidateKeywordCount() error {
c.Keywords++
if c.Keywords <= maxKeywords {
return nil
}
return errors.New("too many keywords, max is " + strconv.Itoa(maxKeywords))
}

// ValidateTarget Checks if the given classes are covered by the card
func (c CardRootValidator) ValidateTarget(t jsonschema.Targeting) error {
ty, tm := t.Targets()
if tm == "THIS" {
for _, v := range ty {
if v == c.GetType() {
if v == c.Type {
return nil
}
}
return errors.New("Class " + c.GetType() + " is not covered by the target classes")
return errors.New("Class " + c.Type + " is not covered by the target classes")
} else {
return nil
}
}

//ValidateType Ensures that the type "Card" is build correctly in the context of the RootElement
// Card Represents the data-structure of a crowd control card in the blockchain
type Card struct {
Action *action `json:",omitempty"`
Entity *entity `json:",omitempty"`
Place *place `json:",omitempty"`
Headquarter *headquarter `json:",omitempty"`
}

// Validate Ensures that the card was built correctly and returns errors otherwise
func (c Card) Validate() error {
classProvider, _ := c.FindImplementer()
cardRoot := CardRootValidator{0, classProvider.(cardobject.ClassProvider), c.GetType()}
return c.ValidateType(cardRoot)
}

// ValidateType Ensures that the type "Card" is build correctly in the context of the RootElement
func (c Card) ValidateType(r jsonschema.RootElement) error {
implementer, err := c.FindImplementer()
if err != nil {
Expand All @@ -53,7 +76,7 @@ func (c Card) ValidateType(r jsonschema.RootElement) error {
return implementer.ValidateType(r)
}

//FindImplementer Returns which of its children "implement" the type "Card"
// FindImplementer Returns which of its children "implement" the type "Card"
func (c Card) FindImplementer() (jsonschema.Validateable, error) {
return jsonschema.FindImplementer(c)
}
Expand Down
31 changes: 15 additions & 16 deletions keywords/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ import (
"github.com/DecentralCardGame/cardobject/cardobject"
)

func allClassesTestCard() Card {
return Card{
&action{
CardName: "",
CastingCost: 3,
AdditionalCost: nil,
Class: cardobject.Class{
Nature: true,
Mysticism: true,
Technology: true,
Culture: true},
Effects: nil,
FlavourText: "",
Tags: nil,
Keywords: nil},
nil, nil, nil}
func allClassesTestCard() CardRootValidator {

return CardRootValidator{9, &action{
CardName: "",
CastingCost: 3,
AdditionalCost: nil,
Class: cardobject.Class{
Nature: true,
Mysticism: true,
Technology: true,
Culture: true},
Effects: nil,
FlavourText: "",
Tags: nil,
Keywords: nil}, "ACTION"}
}

func TestUnmarshaling(t *testing.T) {
Expand Down

0 comments on commit a238e31

Please sign in to comment.