Finished shop system
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
component Inventory(inventory *arn.Inventory, viewUser *arn.User, user *arn.User)
|
||||
ShopTabs(user)
|
||||
|
||||
h1.page-title Inventory
|
||||
|
||||
.inventory(data-api="/api/inventory/" + viewUser.ID)
|
||||
for index, slot := range inventory.Slots
|
||||
if slot.ItemID == ""
|
||||
|
@ -3,6 +3,7 @@ package shop
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/animenotifier/arn"
|
||||
|
||||
@ -11,6 +12,8 @@ import (
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
var itemBuyMutex sync.Mutex
|
||||
|
||||
// Get shop page.
|
||||
func Get(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
@ -31,3 +34,56 @@ func Get(ctx *aero.Context) string {
|
||||
|
||||
return ctx.HTML(components.Shop(user, items))
|
||||
}
|
||||
|
||||
// BuyItem ...
|
||||
func BuyItem(ctx *aero.Context) string {
|
||||
// Lock via mutex to prevent race conditions
|
||||
itemBuyMutex.Lock()
|
||||
defer itemBuyMutex.Unlock()
|
||||
|
||||
// Logged in user
|
||||
user := utils.GetUser(ctx)
|
||||
|
||||
if user == nil {
|
||||
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
|
||||
}
|
||||
|
||||
// Item ID and quantity
|
||||
itemID := ctx.Get("item")
|
||||
quantity, err := ctx.GetInt("quantity")
|
||||
|
||||
if err != nil || quantity == 0 {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid item quantity", err)
|
||||
}
|
||||
|
||||
item, err := arn.GetItem(itemID)
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusInternalServerError, "Error fetching item data", err)
|
||||
}
|
||||
|
||||
// Calculate total price and subtract balance
|
||||
totalPrice := int(item.Price) * quantity
|
||||
|
||||
if user.Balance < totalPrice {
|
||||
return ctx.Error(http.StatusBadRequest, "Not enough gems", nil)
|
||||
}
|
||||
|
||||
user.Balance -= totalPrice
|
||||
err = user.Save()
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusInternalServerError, "Error saving user data", err)
|
||||
}
|
||||
|
||||
// Add item to user inventory
|
||||
inventory := user.Inventory()
|
||||
inventory.AddItem(itemID, uint(quantity))
|
||||
err = inventory.Save()
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusInternalServerError, "Error saving inventory", err)
|
||||
}
|
||||
|
||||
return "ok"
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
component Shop(user *arn.User, items []*arn.Item)
|
||||
h1.page-title Shop
|
||||
|
||||
ShopTabs(user)
|
||||
|
||||
h1.page-title Shop
|
||||
|
||||
.widgets.shop-items
|
||||
each item in items
|
||||
ShopItem(item)
|
||||
@ -21,6 +21,6 @@ component ShopItem(item *arn.Item)
|
||||
//- span.shop-item-duration= " " + duration
|
||||
.shop-item-description!= aero.Markdown(item.Description)
|
||||
.buttons.shop-buttons
|
||||
button.shop-button-buy
|
||||
button.shop-button-buy.action(data-item-id=item.ID, data-item-name=item.Name, data-price=item.Price, data-trigger="click", data-action="buyItem")
|
||||
span.shop-item-price= item.Price
|
||||
Icon("diamond")
|
Reference in New Issue
Block a user