Skip to content

Commit

Permalink
feat: better organization of game character - #5
Browse files Browse the repository at this point in the history
  • Loading branch information
SomethingSexy committed Oct 14, 2024
1 parent 89d3642 commit a4a01b0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
27 changes: 27 additions & 0 deletions internal/chronicle/core/domain/game/vtm_game_character.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package game

import (
"os"
)

// TODO: I think this needs another sub property to work with the validator
type VtmGameCharacter struct {
Name string `json:"name"`
Disciplines []Discipline `json:"disciplines,omitempty"`
}

type Discipline struct {
Name string `json:"name"`
Level int `json:"level"`
Powers []Power `json:"powers"`
}

type Power struct {
Name string `json:"name"`
Level int `json:"level"`
Description string `json:"description,omitempty"`
}

func (g VtmGameCharacter) Schema() ([]byte, error) {
return os.ReadFile("./vtm_v5_character_schema.json")
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package domain_test
package game_test

import (
"testing"

"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain"
"github.com/goccy/go-json"
"github.com/SomethingSexy/chronicle/internal/chronicle/core/domain/game"
)

func TestGameCharacter_Validate_Valid(t *testing.T) {
var data interface{}

if err := json.Unmarshal([]byte(`{
"name": "John Doe"
}`), &data); err != nil {
t.Fatalf("Failed to unmarshal test cases: %v", err)
}

gameCharacter := domain.GameCharacter[any]{
Data: data,
gameCharacter := domain.GameCharacter[game.VtmGameCharacter]{
Data: game.VtmGameCharacter{
Name: "John Doe",
},
}

valid, err := gameCharacter.Validate()
Expand All @@ -32,13 +26,13 @@ func TestGameCharacter_Validate_Valid(t *testing.T) {
}

func TestGameCharacter_Validate_Disciplines_Valid(t *testing.T) {
gameCharacter := domain.GameCharacter[domain.VtmGameCharacter]{
Data: domain.VtmGameCharacter{
gameCharacter := domain.GameCharacter[game.VtmGameCharacter]{
Data: game.VtmGameCharacter{
Name: "John Doe",
Disciplines: []domain.Discipline{{
Disciplines: []game.Discipline{{
Name: "Protean",
Level: 1,
Powers: []domain.Power{{
Powers: []game.Power{{
Name: "Eyes of the Beast",
Level: 1,
Description: "See perfectly in total darkness with glowing red eyes.",
Expand Down
44 changes: 16 additions & 28 deletions internal/chronicle/core/domain/game_character.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package domain

import (
"fmt"
"os"
"log"

"github.com/goccy/go-json"
"github.com/kaptinlin/jsonschema"
)

type GameCharacterValidator interface {
Schema() ([]byte, error)
}

// This should probably be generic, need to
// test this against the schema compiler though.
// Maybe after it is valid, we can marshall to a strict type
type GameCharacter[D interface{}] struct {
type GameCharacter[D GameCharacterValidator] struct {
Data D
Type GameType
}

func (g GameCharacter[D]) Validate() (bool, error) {
// For now just read on load (for testing purposes), we can deal with caching this later
var vtmV5CharacterSchema, err = os.ReadFile("./game/vtm_v5_character_schema.json")
characterSchema, err := g.Data.Schema()
if err != nil {
return false, err
}

compiler := jsonschema.NewCompiler()
schema, err := compiler.Compile(vtmV5CharacterSchema)
schema, err := compiler.Compile(characterSchema)
if err != nil {
return false, err
}
Expand All @@ -33,6 +36,7 @@ func (g GameCharacter[D]) Validate() (bool, error) {
// For now we are going to marshall and unmarshall back into
// a generic interface of maps so we can validate the schema
// Hide it all here
log.Println(g.Data)
dataAsByte, err := json.Marshal(g.Data)
if err != nil {
return false, err
Expand All @@ -46,30 +50,14 @@ func (g GameCharacter[D]) Validate() (bool, error) {

result := schema.Validate(dataAsInterface)
if !result.IsValid() {
details, err := json.MarshalIndent(result.ToList(), "", " ")
if err != nil {
return false, err
}
fmt.Println(string(details))
log.Println(result)
// details, err := json.MarshalIndent(result.ToList(), "", " ")
// if err != nil {
// return false, err
// }
// fmt.Println(string(details))
return false, nil
}

return true, nil
}

type VtmGameCharacter struct {
Name string `json:"name"`
Disciplines []Discipline `json:"disciplines"`
}

type Discipline struct {
Name string `json:"name"`
Level int `json:"level"`
Powers []Power `json:"powers"`
}

type Power struct {
Name string `json:"name"`
Level int `json:"level"`
Description string `json:"description,omitempty"`
}

0 comments on commit a4a01b0

Please sign in to comment.