Skip to content

Commit

Permalink
Cleanup Design (#10)
Browse files Browse the repository at this point in the history
* feat: remove duplicate schema

* feat: split up actions

* feat: more refactor

* feat: consistent logging / error returns for engine

* feat: remove shadows
  • Loading branch information
promiseofcake authored Aug 7, 2024
1 parent e9846db commit 9ddb398
Show file tree
Hide file tree
Showing 24 changed files with 485 additions and 435 deletions.
37 changes: 37 additions & 0 deletions internal/actions/craft.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package actions

import (
"context"
"fmt"
"net/http"

"github.com/promiseofcake/artifactsmmo-go-client/client"

"github.com/promiseofcake/artifactsmmo-engine/internal/models"
)

// Craft crafts the given item, with the given quantity and assumes the character is in the correct
// map position
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: models.Character{CharacterSchema: resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
},
}, nil
}
33 changes: 33 additions & 0 deletions internal/actions/deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package actions

import (
"context"
"fmt"

"github.com/promiseofcake/artifactsmmo-go-client/client"

"github.com/promiseofcake/artifactsmmo-engine/internal/models"
)

// Deposit deposits an item and quantity into the bank
func (r *Runner) Deposit(ctx context.Context, character string, code string, qty int) (*BankResponse, error) {
resp, err := r.Client.ActionDepositBankMyNameActionBankDepositPostWithResponse(
ctx,
character,
client.ActionDepositBankMyNameActionBankDepositPostJSONRequestBody{
Code: code,
Quantity: qty,
},
)
if err != nil {
return nil, fmt.Errorf("failed to deposit: %w", err)
}
return &BankResponse{
Item: resp.JSON200.Data.Item,
BankItems: resp.JSON200.Data.Bank,
Response: Response{
CharacterResponse: models.Character{CharacterSchema: resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
},
}, nil
}
30 changes: 30 additions & 0 deletions internal/actions/fight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package actions

import (
"context"
"fmt"
"net/http"

"github.com/promiseofcake/artifactsmmo-engine/internal/models"
)

// 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 fight: %w", err)
}

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

return &FightResponse{
FightResponse: resp.JSON200.Data.Fight,
Response: Response{
CharacterResponse: models.Character{CharacterSchema: resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
},
}, nil

}
29 changes: 29 additions & 0 deletions internal/actions/gather.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package actions

import (
"context"
"fmt"
"net/http"

"github.com/promiseofcake/artifactsmmo-engine/internal/models"
)

// Gather performs resource gathering at the current position for the given character
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)
}

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: models.Character{CharacterSchema: resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
},
}, nil
}
31 changes: 31 additions & 0 deletions internal/actions/move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package actions

import (
"context"
"fmt"
"net/http"

"github.com/promiseofcake/artifactsmmo-go-client/client"

"github.com/promiseofcake/artifactsmmo-engine/internal/models"
)

// Move changes the x, y position the given character
func (r *Runner) Move(ctx context.Context, character string, x, y int) (*Response, error) {
resp, err := r.Client.ActionMoveMyNameActionMovePostWithResponse(ctx, character, client.ActionMoveMyNameActionMovePostJSONRequestBody{
X: x,
Y: y,
})
if err != nil {
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: models.Character{CharacterSchema: resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
}, nil
}
42 changes: 42 additions & 0 deletions internal/actions/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package actions

import (
"time"

"github.com/promiseofcake/artifactsmmo-go-client/client"

"github.com/promiseofcake/artifactsmmo-engine/internal/models"
)

// Response is a generic return value for most actions
// it includes the most common return values for Character State
// and request cooldown
type Response struct {
CharacterResponse models.Character
CooldownSchema client.CooldownSchema
}

// GetCooldownDuration returns the duration for the Response cooldown
func (g *Response) GetCooldownDuration() time.Duration {
return time.Until(g.CooldownSchema.Expiration)
}

// FightResponse wraps a generic Response with Fight related data
type FightResponse struct {
Response
FightResponse client.FightSchema
}

// SkillResponse wraps a generic Response with Skill related data
// Used for craft, gather
type SkillResponse struct {
Response
SkillInfo client.SkillInfoSchema
}

// BankResponse wraps a generic Response with Banking related data
type BankResponse struct {
Response
BankItems []client.SimpleItemSchema
Item client.ItemSchema
}
111 changes: 1 addition & 110 deletions internal/actions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/promiseofcake/artifactsmmo-go-client/client"
)

// Runner is an executor for Actions
// Runner is an executor for various Actions (Character / State)
type Runner struct {
Client *client.ClientWithResponses
}
Expand All @@ -33,112 +33,3 @@ func NewRunnerWithClient(client *client.ClientWithResponses) *Runner {
Client: client,
}
}

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,
},
}, nil

}

// Gather performs resource gathering at the present position for the given character
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)
}

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,
},
}, nil
}

// Move changes the x, y position the given character
func (r *Runner) Move(ctx context.Context, character string, x, y int) (*Response, error) {
resp, err := r.Client.ActionMoveMyNameActionMovePostWithResponse(ctx, character, client.ActionMoveMyNameActionMovePostJSONRequestBody{
X: x,
Y: y,
})
if err != nil {
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,
}, nil
}

// Deposit deposits items into the bank
func (r *Runner) Deposit(ctx context.Context, character string, code string, qty int) (*BankResponse, error) {
resp, err := r.Client.ActionDepositBankMyNameActionBankDepositPostWithResponse(
ctx,
character,
client.ActionDepositBankMyNameActionBankDepositPostJSONRequestBody{
Code: code,
Quantity: qty,
},
)
if err != nil {
return nil, fmt.Errorf("failed to deposit: %w", err)
}
return &BankResponse{
Item: resp.JSON200.Data.Item,
BankItems: resp.JSON200.Data.Bank,
Response: Response{
CharacterResponse: CharacterResponse{resp.JSON200.Data.Character},
CooldownSchema: resp.JSON200.Data.Cooldown,
},
}, nil
}
Loading

0 comments on commit 9ddb398

Please sign in to comment.