-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ability to Fetch Game State (#4)
* feat: more commands * feat: fix go.mod
- Loading branch information
1 parent
cdd4121
commit dda6094
Showing
6 changed files
with
120 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |