Skip to content

Commit

Permalink
added support for event mining
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Jackson committed Sep 4, 2024
1 parent 9295439 commit e970834
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 9 deletions.
17 changes: 16 additions & 1 deletion cmd/miner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,22 @@ func main() {
case models.Coal:
x, y = artifacts.Client.FindRocks(models.Coal)
case models.Gold:
x, y = artifacts.Client.FindRocks(models.Coal)
x, y = artifacts.Client.FindRocks(models.Gold)
case models.StrangeRocks:
events, err := artifacts.Client.ListEvents(models.GetAllEventsQueryParameters{})
if err != nil {
utils.Logger.Error("failed to list events", zap.Error(err))
x, y = artifacts.Client.FindRocks(models.Gold)
continue
}
for _, event := range events.Data {
if event.Name == "Strange Apparition" {
x = event.Map.X
y = event.Map.Y
} else {
x, y = artifacts.Client.FindRocks(models.Gold)
}
}
default:
x, y = artifacts.Client.FindRocks(models.Copper)
}
Expand Down
53 changes: 53 additions & 0 deletions internal/artifacts/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package artifacts

import (
"encoding/json"
"fmt"
"github.com/wizedkyle/artifactsmmo/v2/internal/models"
"github.com/wizedkyle/artifactsmmo/v2/internal/utils"
"io"
"net/http"
"strconv"
)

func (a *artifacts) ListEvents(params models.GetAllEventsQueryParameters) (*models.EventResponse, error) {
req, err := a.generateRequest(http.MethodGet, "/events", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
if params.Page != 0 {
q.Add("page", strconv.Itoa(params.Page))
}
if params.Size != 0 {
q.Add("size", strconv.Itoa(params.Size))
} else {
q.Add("size", strconv.Itoa(50))
}
req.URL.RawQuery = q.Encode()
resp, err := a.Client.Do(req)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var response models.EventResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return &response, nil
case 403:
return nil, utils.ErrNotAuthenticated
case 404:
return nil, utils.ErrItemNotFound
default:
fmt.Println(string(body))
return nil, utils.ErrGenericError
}
}
14 changes: 7 additions & 7 deletions internal/controllers/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ func WithdrawCraftingItems(quantity int, items *models.ItemDetails) (int, error)
if err != nil {
return 0, errors.New("failed to get character information")
}
maxCraftableItems = c.Data.InventoryMaxItems / totalResourcesRequired
if quantity < maxCraftableItems {
maxCraftableItems = quantity
}
if len(c.Data.Inventory) == 0 {
currentInventorySize = models.InventorySize
currentInventorySize = c.Data.InventoryMaxItems
} else {
for _, inventory := range c.Data.Inventory {
currentInventorySize += inventory.Quantity
}
spareInventorySize := models.InventorySize - currentInventorySize
if spareInventorySize < totalResourcesRequired {
spareInventorySize := c.Data.InventoryMaxItems - currentInventorySize
if spareInventorySize < totalResourcesRequired*maxCraftableItems {
err := Move(models.Bank)
if err != nil {
return 0, err
}
DepositAllInventory(c.Data.Inventory)
}
}
maxCraftableItems = models.InventorySize / totalResourcesRequired
if quantity < maxCraftableItems {
maxCraftableItems = quantity
}
for _, item := range items.Craft.Items {
err := Move(models.Bank)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/combat.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func CompleteCombatOrder(task models.Task) (string, error) {
utils.Logger.Error("failed to fight", zap.Error(err))
time.Sleep(utils.CalculateTimeDifference(resp.Data.Cooldown.StartedAt, resp.Data.Cooldown.Expiration))
}
fmt.Printf("%s fought %s and %s. It dropped %v. %d remaining to fight\n", *artifacts.Client.CharacterName, task.Monster, resp.Data.Fight.Result, resp.Data.Fight.Drops, i)
fmt.Printf("%s fought %s and %s. It dropped %v. Fight number %d\n", *artifacts.Client.CharacterName, task.Monster, resp.Data.Fight.Result, resp.Data.Fight.Drops, i)
time.Sleep(utils.CalculateTimeDifference(resp.Data.Cooldown.StartedAt, resp.Data.Cooldown.Expiration))
}
return "", nil
Expand Down
38 changes: 38 additions & 0 deletions internal/models/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package models

import "time"

type EventResponse struct {
Data []ActiveEvent `json:"data"`
Total int `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
Pages int `json:"pages"`
}

type ActiveEvent struct {
Name string `json:"name"`
Map MapSchema `json:"map"`
PreviousSkin string `json:"previous_skin"`
Duration int `json:"duration"`
Expiration time.Time `json:"expiration"`
CreatedAt time.Time `json:"created_at"`
}

type MapSchema struct {
Name string `json:"name"`
Skin string `json:"skin"`
X int `json:"x"`
Y int `json:"y"`
Content MapContent `json:"content"`
}

type MapContent struct {
Type string `json:"type"`
Code string `json:"code"`
}

type GetAllEventsQueryParameters struct {
Page int `json:"page"`
Size int `json:"size"`
}
2 changes: 2 additions & 0 deletions internal/models/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
MiningWorkshop string = "mining_workshop"
MiningWorkshopX int = 1
MiningWorkshopY int = 5
MagicTree string = "magic_tree"
Shrimp string = "shrimp"
ShrimpLevel int = 10
ShrimpX int = 5
Expand All @@ -68,6 +69,7 @@ const (
SpruceTreeLevel int = 10
SpruceTreeX int = 2
SpruceTreeY int = 6
StrangeRocks string = "strange_rocks"
Trout string = "trout"
TroutLevel int = 20
TroutX int = 7
Expand Down

0 comments on commit e970834

Please sign in to comment.