Skip to content

Commit

Permalink
added task worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Jackson committed Aug 26, 2024
1 parent ed7c0f1 commit 1846eb5
Show file tree
Hide file tree
Showing 25 changed files with 814 additions and 71 deletions.
Empty file added build/crafting/Dockerfile
Empty file.
10 changes: 10 additions & 0 deletions build/worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:1.22.5-alpine3.20

WORKDIR /go/src/worker

COPY . /go/src/worker

RUN go get ./...
RUN go build ./cmd/worker

ENTRYPOINT ["./worker"]
27 changes: 9 additions & 18 deletions cmd/miner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,26 @@ func main() {
continue
}
if c.Data.MiningLevel < models.IronLevel {
x = models.CopperX
y = models.CopperY
x, y = artifacts.Client.FindRocks(models.Copper)
} else if c.Data.MiningLevel < models.CoalLevel {
x = models.IronX
y = models.IronY
x, y = artifacts.Client.FindRocks(models.Iron)
} else if c.Data.MiningLevel < models.GoldLevel {
x = models.CoalX
y = models.CoalY
x, y = artifacts.Client.FindRocks(models.Coal)
} else {
x = models.GoldX
y = models.GoldY
x, y = artifacts.Client.FindRocks(models.Copper)
}
} else {
switch miningResource {
case models.Copper:
x = models.CopperX
y = models.CopperY
x, y = artifacts.Client.FindRocks(models.Copper)
case models.Iron:
x = models.IronX
y = models.IronY
x, y = artifacts.Client.FindRocks(models.Iron)
case models.Coal:
x = models.CoalX
y = models.CoalY
x, y = artifacts.Client.FindRocks(models.Coal)
case models.Gold:
x = models.GoldX
y = models.GoldY
x, y = artifacts.Client.FindRocks(models.Coal)
default:
x = models.CopperX
y = models.CopperY
x, y = artifacts.Client.FindRocks(models.Copper)
}
}
c, err := artifacts.Client.GetCharacter(*artifacts.Client.CharacterName)
Expand Down
7 changes: 5 additions & 2 deletions cmd/woodcutting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ func main() {
} else if c.Data.WoodcuttingLevel < models.BirchTreeLevel {
x = models.SpruceTreeX
y = models.SpruceTreeY
} else {
} else if c.Data.WoodcuttingLevel < models.DeadTreeLevel {
x = models.BirchTreeX
y = models.BirchTreeY
x = models.BirchTreeY
} else {
x = models.DeadTreeX
y = models.DeadTreeY
}
} else {
switch woodResource {
Expand Down
78 changes: 78 additions & 0 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"errors"
"github.com/wizedkyle/artifactsmmo/v2/internal/artifacts"
"github.com/wizedkyle/artifactsmmo/v2/internal/controllers"
"github.com/wizedkyle/artifactsmmo/v2/internal/database"
"github.com/wizedkyle/artifactsmmo/v2/internal/models"
"github.com/wizedkyle/artifactsmmo/v2/internal/utils"
"go.uber.org/zap"
"time"
)

func main() {
utils.LoggerInit()
artifacts.Init()
database.Init()
for {
tasks, err := database.Client.ListTasks("", *artifacts.Client.CharacterName, 1, models.TaskStatusPending)
if errors.Is(err, utils.ErrTasksNotFound) {
utils.Logger.Info("no crafting tasks found", zap.Error(err))
time.Sleep(10 * time.Second)
continue
} else if err != nil {
utils.Logger.Error("failed to get tasks from database", zap.Error(err))
continue
}
for _, task := range *tasks {
switch task.ActionCategory {
case models.Combat:
reason, err := controllers.CompleteCombatOrder(task)
if err != nil {
utils.Logger.Error("failed to complete combat order", zap.Error(err))
err = database.Client.UpdateTask(task.Id, reason, models.TaskStatusSuccess)
if err != nil {
utils.Logger.Error("failed to update task status", zap.Error(err))
continue
}
continue
}
err = database.Client.UpdateTask(task.Id, "", models.TaskStatusSuccess)
if err != nil {
utils.Logger.Error("failed to update task status", zap.Error(err))
continue
}
continue
case models.Crafting:
reason, err := controllers.CompleteCraftingOrder(task)
if err != nil {
utils.Logger.Error("failed to complete crafting order", zap.Error(err))
err = database.Client.UpdateTask(task.Id, reason, models.TaskStatusSuccess)
if err != nil {
utils.Logger.Error("failed to update task status", zap.Error(err))
continue
}
continue
}
err = database.Client.UpdateTask(task.Id, "", models.TaskStatusSuccess)
if err != nil {
utils.Logger.Error("failed to update task status", zap.Error(err))
continue
}
default:
utils.Logger.Info("no valid task action", zap.String("action", task.Action))
err := database.Client.UpdateTask(task.Id, "no valid task action", models.TaskStatusError)
if err != nil {
utils.Logger.Error("failed to update task status", zap.Error(err))
continue
}
}
}
time.Sleep(10 * time.Second)
}
// list all pending tasks from the database with a type of crafting
// if no results continue
// if results get the first array record
// for number of quantity do crafting task
}
59 changes: 36 additions & 23 deletions internal/artifacts/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"net/http"
)

// ActionDepositBank
// Instructs the character to deposit the specified resources in the bank.
func (a *artifacts) ActionDepositBank(character string, deposit models.ActionDepositBank) (*models.ActionDepositBankResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/bank/deposit", deposit)
// ActionWithdrawBank
// Instructs the character to withdraw the specified resources from the bank.
func (a *artifacts) ActionWithdrawBank(character string, withdrawal models.ActionWithdrawBank) (*models.WithdrawBankResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/bank/withdraw", withdrawal)
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
if err != nil {
return nil, err
Expand All @@ -24,7 +27,7 @@ func (a *artifacts) ActionDepositBank(character string, deposit models.ActionDep
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var response models.ActionDepositBankResponse
var response models.WithdrawBankResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
Expand All @@ -38,6 +41,8 @@ func (a *artifacts) ActionDepositBank(character string, deposit models.ActionDep
return nil, utils.ErrItemMissingOrInsufficientQuantity
case 486:
return nil, utils.ErrCharacterLockedActionInProgress
case 497:
return nil, utils.ErrCharacterInventoryFull
case 498:
return nil, utils.ErrCharacterNotFound
case 499:
Expand All @@ -50,10 +55,13 @@ func (a *artifacts) ActionDepositBank(character string, deposit models.ActionDep
}
}

// ActionGathering
// Instructs the character to gather resources at the current location.
func (a *artifacts) ActionGathering(character string) (*models.CharacterGatheringResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/gathering", nil)
// ActionDepositBank
// Instructs the character to deposit the specified resources in the bank.
func (a *artifacts) ActionDepositBank(character string, deposit models.ActionDepositBank) (*models.ActionDepositBankResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/bank/deposit", deposit)
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
if err != nil {
return nil, err
Expand All @@ -65,20 +73,20 @@ func (a *artifacts) ActionGathering(character string) (*models.CharacterGatherin
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var response models.CharacterGatheringResponse
var response models.ActionDepositBankResponse
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
case 478:
return nil, utils.ErrItemMissingOrInsufficientQuantity
case 486:
return nil, utils.ErrCharacterLockedActionInProgress
case 493:
return nil, utils.ErrCharacterNotAtSkillLevel
case 497:
return nil, utils.ErrCharacterInventoryFull
case 498:
return nil, utils.ErrCharacterNotFound
case 499:
Expand All @@ -91,10 +99,13 @@ func (a *artifacts) ActionGathering(character string) (*models.CharacterGatherin
}
}

// ActionMove
// Instructs the character to move to the specified location.
func (a *artifacts) ActionMove(character string, location models.ActionMove) (*models.CharacterMovementResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/move", location)
// ActionGathering
// Instructs the character to gather resources at the current location.
func (a *artifacts) ActionGathering(character string) (*models.CharacterGatheringResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/gathering", nil)
if err != nil {
return nil, err
}
resp, err := a.Client.Do(req)
if err != nil {
return nil, err
Expand All @@ -106,24 +117,26 @@ func (a *artifacts) ActionMove(character string, location models.ActionMove) (*m
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var response models.CharacterMovementResponse
var response models.CharacterGatheringResponse
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.ErrMapNotFound
case 486:
return nil, utils.ErrCharacterLockedActionInProgress
case 490:
return nil, utils.ErrCharacterAtDestination
case 493:
return nil, utils.ErrCharacterNotAtSkillLevel
case 497:
return nil, utils.ErrCharacterInventoryFull
case 498:
return nil, utils.ErrCharacterNotFound
case 499:
return nil, utils.ErrCharacterCooldown
case 598:
return nil, utils.ErrResourceNotFound
default:
fmt.Println(string(body))
return nil, utils.ErrGenericError
Expand Down
90 changes: 90 additions & 0 deletions internal/artifacts/characters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/wizedkyle/artifactsmmo/v2/internal/utils"
"io"
"net/http"
"strconv"
)

// GetCharacter
Expand Down Expand Up @@ -40,6 +41,8 @@ func (a *artifacts) GetCharacter(character string) (*models.CharacterResponse, e
}
}

// CreateCharacter
// Creates a new character.
func (a *artifacts) CreateCharacter(character models.CreateCharacter) (*models.CharacterResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/characters/create", character)
resp, err := a.Client.Do(req)
Expand Down Expand Up @@ -70,3 +73,90 @@ func (a *artifacts) CreateCharacter(character models.CreateCharacter) (*models.C
return nil, utils.ErrGenericError
}
}

// ListBankInventory
// Returns all items in the bank.
func (a *artifacts) ListBankInventory(params models.BankInventoryParams) (*models.BankInventoryResponse, error) {
req, err := a.generateRequest(http.MethodGet, "/my/bank/items", nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
if params.ItemCode != "" {
q.Add("item_code", params.ItemCode)
}
if params.Page != 0 {
q.Add("page", strconv.Itoa(params.Page))
}
if params.Size != 0 {
q.Add("size", strconv.Itoa(params.Size))
}
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.BankInventoryResponse
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
}
}

// ActionMove
// Instructs the character to move to the specified location.
func (a *artifacts) ActionMove(character string, location models.ActionMove) (*models.CharacterMovementResponse, error) {
req, err := a.generateRequest(http.MethodPost, "/my/"+character+"/action/move", location)
if err != nil {
return nil, err
}
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.CharacterMovementResponse
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.ErrMapNotFound
case 486:
return nil, utils.ErrCharacterLockedActionInProgress
case 490:
return nil, utils.ErrCharacterAtDestination
case 498:
return nil, utils.ErrCharacterNotFound
case 499:
return nil, utils.ErrCharacterCooldown
default:
fmt.Println(string(body))
return nil, utils.ErrGenericError
}
}
Loading

0 comments on commit 1846eb5

Please sign in to comment.