Skip to content

Commit

Permalink
Add Ability to Fetch Game State (#4)
Browse files Browse the repository at this point in the history
* feat: more commands

* feat: fix go.mod
  • Loading branch information
promiseofcake authored Aug 4, 2024
1 parent cdd4121 commit dda6094
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 31 deletions.
40 changes: 40 additions & 0 deletions cmd/state.go
Original file line number Diff line number Diff line change
@@ -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")
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
22 changes: 0 additions & 22 deletions internal/actions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions internal/actions/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
60 changes: 60 additions & 0 deletions internal/actions/state.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit dda6094

Please sign in to comment.