Skip to content

Commit

Permalink
feat: more stuff (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
promiseofcake authored Aug 3, 2024
1 parent 97d8ffd commit cdd4121
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 24 deletions.
52 changes: 52 additions & 0 deletions cmd/craft.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"fmt"
"github.com/spf13/viper"
"log/slog"
"time"

"github.com/promiseofcake/artifactsmmo-engine/internal/actions"
"github.com/spf13/cobra"
)

var (
craftCode string
craftQty int
)

// craftCmd represents the gather command
var craftCmd = &cobra.Command{
Use: "craft",
Short: "Start a craft loop in your current location",
RunE: func(cmd *cobra.Command, args []string) error {
character := viper.GetViper().GetString("character")
if character == "" {
return fmt.Errorf("you must specify a character")
}
r := cmd.Context().Value(runnerKey).(*actions.Runner)
for {
slog.Info("about to craft", "code", craftCode, "quantity", craftQty)

resp, err := r.Craft(cmd.Context(), character, craftCode, craftQty)
if err != nil {
slog.Error("failed to craft", "error", err.Error())
return fmt.Errorf("failed to craft: %w", err)
}

sec := resp.GetRemainingCooldown()
slog.Info("craft results",
"xp", resp.SkillInfo.Xp,
"items", resp.SkillInfo.Items,
"cooldown", sec,
)
time.Sleep(time.Duration(sec) * time.Second)
}
},
}

func init() {
craftCmd.Flags().StringVar(&craftCode, "code", "", "The code of your item to craft")
craftCmd.Flags().IntVar(&craftQty, "qty", 0, "The quantity to craft")
rootCmd.AddCommand(craftCmd)
}
44 changes: 44 additions & 0 deletions cmd/fight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"github.com/spf13/viper"
"log/slog"
"time"

"github.com/promiseofcake/artifactsmmo-engine/internal/actions"
"github.com/spf13/cobra"
)

var fightCmd = &cobra.Command{
Use: "fight",
Short: "fight something",
RunE: func(cmd *cobra.Command, args []string) error {
character := viper.GetViper().GetString("character")
if character == "" {
return fmt.Errorf("you must specify a character")
}
r := cmd.Context().Value(runnerKey).(*actions.Runner)
for {
slog.Info("about to fight")

resp, err := r.Fight(cmd.Context(), character)
if err != nil {
slog.Error("failed to fight", "error", err.Error())
return fmt.Errorf("failed to fight: %w", err)
}

sec := resp.GetRemainingCooldown()
slog.Info("fight results",
"results", resp.FightResponse,
"cooldown", sec,
)
time.Sleep(time.Duration(sec) * time.Second)
return nil
}
},
}

func init() {
rootCmd.AddCommand(fightCmd)
}
14 changes: 3 additions & 11 deletions cmd/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@ package cmd

import (
"fmt"
"github.com/spf13/viper"
"log/slog"
"time"

"github.com/promiseofcake/artifactsmmo-engine/internal/actions"
"github.com/spf13/cobra"
)

var characterName string

// gatherCmd represents the gather command
var gatherCmd = &cobra.Command{
Use: "gather",
Short: "Start a gather loop in your current location",
RunE: func(cmd *cobra.Command, args []string) error {
character, err := cmd.PersistentFlags().GetString("character")
if err != nil {
return fmt.Errorf("failed to get character: %w", err)
}

character := viper.GetViper().GetString("character")
if character == "" {
return fmt.Errorf("you must specify a character")
}
Expand All @@ -32,23 +27,20 @@ var gatherCmd = &cobra.Command{
resp, err := r.Gather(cmd.Context(), character)
if err != nil {
slog.Error("failed to gather", "error", err.Error())
return fmt.Errorf("failed to get gather: %w", err)
return fmt.Errorf("failed to gather: %w", err)
}

sec := resp.GetRemainingCooldown()
slog.Info("gather results",
"status", resp.StatusCode,
"xp", resp.SkillInfo.Xp,
"items", resp.SkillInfo.Items,
"cooldown", sec,
)
time.Sleep(time.Duration(sec) * time.Second)
}
return nil
},
}

func init() {
gatherCmd.PersistentFlags().StringVar(&characterName, "character", "", "The name of your character")
rootCmd.AddCommand(gatherCmd)
}
48 changes: 48 additions & 0 deletions cmd/move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"fmt"
"github.com/spf13/viper"
"log/slog"
"time"

"github.com/promiseofcake/artifactsmmo-engine/internal/actions"
"github.com/spf13/cobra"
)

var (
x int
y int
)

// craftCmd represents the gather command
var moveCmd = &cobra.Command{
Use: "move",
Short: "move position",
RunE: func(cmd *cobra.Command, args []string) error {
character := viper.GetViper().GetString("character")
if character == "" {
return fmt.Errorf("you must specify a character")
}
r := cmd.Context().Value(runnerKey).(*actions.Runner)

resp, err := r.Move(cmd.Context(), character, x, y)
if err != nil {
slog.Error("failed to move", "error", err.Error())
return fmt.Errorf("failed to move: %w", err)
}

sec := resp.GetRemainingCooldown()
slog.Info("move results",
"cooldown", sec,
)
time.Sleep(time.Duration(sec) * time.Second)
return nil
},
}

func init() {
moveCmd.Flags().IntVar(&x, "x", 0, "The x position")
moveCmd.Flags().IntVar(&y, "y", 0, "The y position")
rootCmd.AddCommand(moveCmd)
}
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const (
)

var (
cfgFile string
cfgFile string
characterName string
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -49,6 +50,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.artifactsmmo-engine.yaml)")
rootCmd.PersistentFlags().StringVar(&characterName, "character", "", "The name of your character")
viper.BindPFlags(rootCmd.PersistentFlags())
}

Expand All @@ -70,6 +72,7 @@ func initConfig() {

viper.SetEnvPrefix("mmo")
viper.BindEnv("token")
viper.BindEnv("character")
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
Expand Down
51 changes: 43 additions & 8 deletions internal/actions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (r *Runner) GetMyCharacterInfo(ctx context.Context, character string) (*Cha
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{
Expand All @@ -52,39 +56,68 @@ func (r *Runner) GetMyCharacterInfo(ctx context.Context, character string) (*Cha
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,
Quantity: &quantity,
}

resp, err := r.Client.ActionCraftingMyNameActionCraftingPostWithResponse(ctx, character, req)
if err != nil {
return nil, fmt.Errorf("failed to craft %s (%d): %w", code, quantity, err)
}

if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("failed to craft: %s (%d)", resp.Body, resp.StatusCode())
}

return &SkillResponse{
SkillInfo: resp.JSON200.Data.Details,
Response: Response{
CharacterResponse: CharacterResponse{resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
},
}, nil

}

// Fight attacks the mob at the current position for the given character
func (r *Runner) Fight(ctx context.Context, character string) (*FightResponse, error) {
resp, err := r.Client.ActionFightMyNameActionFightPostWithResponse(ctx, character)
if err != nil {
return nil, fmt.Errorf("failed to attack: %w", err)
}

if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("failed to attack: %s (%d)", resp.Body, resp.StatusCode())
}

return &FightResponse{
FightResponse: resp.JSON200.Data.Fight,
Response: Response{
CharacterResponse: CharacterResponse{resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
StatusCode: resp.StatusCode(),
StatusText: resp.Status(),
},
}, nil

}

// Gather performs resource gathering at the present position for the given character
func (r *Runner) Gather(ctx context.Context, character string) (*GatherResponse, error) {
func (r *Runner) Gather(ctx context.Context, character string) (*SkillResponse, error) {
resp, err := r.Client.ActionGatheringMyNameActionGatheringPostWithResponse(ctx, character)
if err != nil {
return nil, fmt.Errorf("failed to gather: %w", err)
}

return &GatherResponse{
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("failed to gather: %s (%d)", resp.Body, resp.StatusCode())
}

return &SkillResponse{
SkillInfo: resp.JSON200.Data.Details,
Response: Response{
CharacterResponse: CharacterResponse{resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
StatusCode: resp.StatusCode(),
StatusText: resp.Status(),
},
}, nil
}
Expand All @@ -99,10 +132,12 @@ func (r *Runner) Move(ctx context.Context, character string, x, y int) (*Respons
return nil, fmt.Errorf("failed to move: %w", err)
}

if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("failed to move: %s (%d)", resp.Body, resp.StatusCode())
}

return &Response{
CharacterResponse: CharacterResponse{resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
StatusCode: resp.StatusCode(),
StatusText: resp.Status(),
}, nil
}
6 changes: 2 additions & 4 deletions internal/actions/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type Coords struct {
type Response struct {
CharacterResponse CharacterResponse
CooldownSchema client.CooldownSchema
StatusCode int
StatusText string
}

// GetRemainingCooldown returns the remaining cooldown based upon the last action
Expand Down Expand Up @@ -43,8 +41,8 @@ type FightResponse struct {
FightResponse client.FightSchema
}

// GatherResponse wraps a generic Response with Skill related data
type GatherResponse struct {
// SkillResponse wraps a generic Response with Skill related data
type SkillResponse struct {
Response
SkillInfo client.SkillInfoSchema
}

0 comments on commit cdd4121

Please sign in to comment.