From dda6094c07d16d6efbdbbc1ac26f76cd9aba721f Mon Sep 17 00:00:00 2001 From: Lucas Kacher Date: Sat, 3 Aug 2024 21:46:25 -1000 Subject: [PATCH] Add Ability to Fetch Game State (#4) * feat: more commands * feat: fix go.mod --- cmd/state.go | 40 +++++++++++++++++++++++++ go.mod | 6 ++-- go.sum | 12 ++++---- internal/actions/runner.go | 22 -------------- internal/actions/schema.go | 11 +++++++ internal/actions/state.go | 60 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 31 deletions(-) create mode 100644 cmd/state.go create mode 100644 internal/actions/state.go diff --git a/cmd/state.go b/cmd/state.go new file mode 100644 index 0000000..2d51b2d --- /dev/null +++ b/cmd/state.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "github.com/promiseofcake/artifactsmmo-engine/internal/actions" + "github.com/promiseofcake/artifactsmmo-go-client/client" + "github.com/spf13/cobra" +) + +var stateContent string + +// stateCmd represents the state command +var stateCmd = &cobra.Command{ + Use: "state", + RunE: func(cmd *cobra.Command, args []string) error { + r := cmd.Context().Value(runnerKey).(*actions.Runner) + + if stateContent == "" { + return fmt.Errorf("no map content provided") + } + + mc, err := r.GetMaps(cmd.Context(), client.GetAllMapsMapsGetParamsContentType(stateContent)) + if err != nil { + return fmt.Errorf("failed to get maps: %w", err) + } + + for _, m := range mc { + bts, _ := json.Marshal(m) + fmt.Println(string(bts)) + } + + return nil + }, +} + +func init() { + rootCmd.AddCommand(stateCmd) + stateCmd.Flags().StringVar(&stateContent, "content", "", "Type of map content to fetch") +} diff --git a/go.mod b/go.mod index 31bf66a..273a575 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/promiseofcake/artifactsmmo-engine go 1.22.5 require ( - github.com/promiseofcake/artifactsmmo-go-client v1.3.1 + github.com/promiseofcake/artifactsmmo-go-client v1.3.4 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 ) @@ -30,8 +30,8 @@ require ( go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.15.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 65bcfae..c5271f5 100644 --- a/go.sum +++ b/go.sum @@ -38,8 +38,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/promiseofcake/artifactsmmo-go-client v1.3.1 h1:r1lVoBxUkwmr1oi967EmMuHKjbpjHBtjikcWMGBe+I0= -github.com/promiseofcake/artifactsmmo-go-client v1.3.1/go.mod h1:u1/QFNndhH1PVIiD4thqZNHBmhetscbU1HfAS5fWGYw= +github.com/promiseofcake/artifactsmmo-go-client v1.3.4 h1:y7I/1rWoMnBN8JAw0AgG3uLRRxQUG/ZNKD2iJe0x7/0= +github.com/promiseofcake/artifactsmmo-go-client v1.3.4/go.mod h1:UPeTbBmAMzjX0EtFMxF4KVPdHrejptIwHASP4NxkjxI= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -78,10 +78,10 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= -golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/internal/actions/runner.go b/internal/actions/runner.go index 5d68516..4969f8f 100644 --- a/internal/actions/runner.go +++ b/internal/actions/runner.go @@ -34,28 +34,6 @@ func NewRunnerWithClient(client *client.ClientWithResponses) *Runner { } } -// GetMyCharacterInfo returns current info and status about your own specific character -func (r *Runner) GetMyCharacterInfo(ctx context.Context, character string) (*CharacterResponse, error) { - resp, err := r.Client.GetMyCharactersMyCharactersGetWithResponse(ctx) - if err != nil { - return nil, fmt.Errorf("failed to fetch characters: %w", err) - } - - if resp.StatusCode() != http.StatusOK { - return nil, fmt.Errorf("failed to fetch characters: %s (%d)", resp.Body, resp.StatusCode()) - } - - for _, c := range resp.JSON200.Data { - if c.Name == character { - return &CharacterResponse{ - CharacterSchema: c, - }, nil - } - } - - return nil, fmt.Errorf("failed to find character: %s", character) -} - func (r *Runner) Craft(ctx context.Context, character string, code string, quantity int) (*SkillResponse, error) { req := client.ActionCraftingMyNameActionCraftingPostJSONRequestBody{ Code: code, diff --git a/internal/actions/schema.go b/internal/actions/schema.go index e553329..6e6c4a9 100644 --- a/internal/actions/schema.go +++ b/internal/actions/schema.go @@ -46,3 +46,14 @@ type SkillResponse struct { Response SkillInfo client.SkillInfoSchema } + +type MapContent []Location + +type Location struct { + Name string `json:"name"` + Skin string `json:"skin"` + X int `json:"x"` + Y int `json:"y"` + Code string `json:"code"` + Type string `json:"type"` +} diff --git a/internal/actions/state.go b/internal/actions/state.go new file mode 100644 index 0000000..d90946d --- /dev/null +++ b/internal/actions/state.go @@ -0,0 +1,60 @@ +package actions + +import ( + "context" + "fmt" + "github.com/promiseofcake/artifactsmmo-go-client/client" + "log/slog" + "net/http" +) + +// GetMyCharacterInfo returns current info and status about your own specific character +func (r *Runner) GetMyCharacterInfo(ctx context.Context, character string) (*CharacterResponse, error) { + resp, err := r.Client.GetMyCharactersMyCharactersGetWithResponse(ctx) + if err != nil { + return nil, fmt.Errorf("failed to fetch characters: %w", err) + } + + if resp.StatusCode() != http.StatusOK { + return nil, fmt.Errorf("failed to fetch characters: %s (%d)", resp.Body, resp.StatusCode()) + } + + for _, c := range resp.JSON200.Data { + if c.Name == character { + return &CharacterResponse{ + CharacterSchema: c, + }, nil + } + } + + return nil, fmt.Errorf("failed to find character: %s", character) +} + +func (r *Runner) GetMaps(ctx context.Context, contentType client.GetAllMapsMapsGetParamsContentType) (MapContent, error) { + resp, err := r.Client.GetAllMapsMapsGetWithResponse(ctx, &client.GetAllMapsMapsGetParams{ + ContentType: &contentType, + }) + if err != nil { + return nil, fmt.Errorf("failed to fetch maps for content: %s %w", contentType, err) + } + + var mc MapContent + for _, m := range resp.JSON200.Data { + s, err := m.Content.AsMapContentSchema() + if err != nil { + slog.Error("failed to extract map content schema", "error", err) + } + + c := Location{ + Name: m.Name, + Skin: m.Skin, + X: m.X, + Y: m.Y, + Code: s.Code, + Type: s.Type, + } + + mc = append(mc, c) + } + return mc, nil +}