Skip to content

Commit

Permalink
feat: add merchant price checking
Browse files Browse the repository at this point in the history
now if no price on the TP is found, we should check the local merchant options, and use their price
instead of calculations
  • Loading branch information
deadpyxel committed Oct 23, 2023
1 parent e47b927 commit 53b1413
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
24 changes: 23 additions & 1 deletion crafter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,34 @@ func NewCrafter(gw2APIClient APIClient, localCache LocalCache) *Crafter {
return &Crafter{gw2APIClient: gw2APIClient, localCache: localCache}
}

type NoPurchasingOptionsFoundError struct {
ItemID int
Message string
}

func (e *NoPurchasingOptionsFoundError) Error() string {
return fmt.Sprintf("%s for ItemID=%d", e.Message, e.ItemID)
}

func (crafter *Crafter) fetchItemTPPrice(itemID int) (*ItemPrice, error) {
itemPrice, err := crafter.gw2APIClient.FetchItemPrice(itemID)
if err != nil {
apiErr, ok := err.(*APIError)
if ok && apiErr.StatusCode == http.StatusNotFound {
logger.Error("Item Price not found on TP", "itemID", itemID)
logger.Warn("Item Price not found on TP, checking merchant options", "itemID", itemID)
hasPurchaseOption, err := crafter.localCache.HasPurchaseOptionWithCurrency(itemID, "Coin")
if err != nil {
return nil, fmt.Errorf("Failed to check for purchasing options: %w", err)
}
if !hasPurchaseOption {
return nil, &NoPurchasingOptionsFoundError{ItemID: itemID, Message: "No purchasing options found"}
}
merchantPrice, err := crafter.localCache.GetMerchantItemPrice(itemID, "Coin")
if err != nil {
return nil, err
}
logger.Info("Found merchant price", "itemID", itemID)
return merchantPrice, nil
}
return nil, err
}
Expand Down
27 changes: 27 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"errors"
"fmt"

"github.com/jmoiron/sqlx"
)
Expand Down Expand Up @@ -112,3 +113,29 @@ func (lc *LocalCache) HasPurchaseOptionWithCurrency(itemId int, currencyName str
}
return count > 0, nil
}

func (lc *LocalCache) GetMerchantItemPrice(itemID int, currencyName string) (*ItemPrice, error) {
currencyId, err := lc.GetCurrencyIDByName(currencyName)
if err != nil {
return nil, err
}
var currencyPrice int
err = lc.db.QueryRowx(
`SELECT mp.count FROM merchant_prices merchant_prices
JOIN purchase_options po ON mp.purchase_option_id = po.recipe_id
WHERE po.item_id = ? AND mp.currency_id = ?`, itemID, currencyId).Scan(&currencyPrice)
if err != nil {
if err == sql.ErrNoRows {
// No price was found with given currency
return nil, fmt.Errorf("No price found in %s for itemID %d", currencyName, itemID)
}
// An error has occurred during the query
return nil, err
}
// Price found, return a custom ItemPrice
return &ItemPrice{
ID: itemID,
Buys: TradingPostPrice{UnitPrice: currencyPrice},
Sells: TradingPostPrice{UnitPrice: currencyPrice},
}, nil
}

0 comments on commit 53b1413

Please sign in to comment.