Improved shop
This commit is contained in:
@ -9,7 +9,8 @@ component Inventory(inventory *arn.Inventory, viewUser *arn.User, user *arn.User
|
||||
.inventory-slot.mountable(draggable="false", data-index=index)
|
||||
else
|
||||
.inventory-slot.mountable(title=slot.Item().Name, draggable="true", data-index=index, data-item-id=slot.ItemID, data-consumable=slot.Item().Consumable)
|
||||
Icon(slot.Item().Icon)
|
||||
.item-icon
|
||||
Icon(slot.Item().Icon)
|
||||
if slot.Quantity > 1
|
||||
.inventory-slot-quantity= slot.Quantity
|
||||
|
||||
|
@ -16,12 +16,45 @@ inventory-slot-size = 64px
|
||||
display flex
|
||||
align-items center
|
||||
justify-content center
|
||||
font-size 2rem
|
||||
font-size 2.5rem
|
||||
|
||||
[draggable="true"]
|
||||
:hover
|
||||
cursor pointer
|
||||
|
||||
.item-icon
|
||||
animation hover-item 1s infinite ease-in-out
|
||||
|
||||
.icon
|
||||
margin 0
|
||||
pointer-events none
|
||||
|
||||
// [data-item-id="pro-account-3"]
|
||||
// .item-icon
|
||||
// opacity 0.7
|
||||
|
||||
// [data-item-id="pro-account-6"]
|
||||
// .item-icon
|
||||
// opacity 0.8
|
||||
|
||||
// [data-item-id="pro-account-12"]
|
||||
// .item-icon
|
||||
// opacity 0.9
|
||||
|
||||
// [data-item-id="pro-account-24"]
|
||||
// .item-icon
|
||||
// opacity 1.0
|
||||
|
||||
animation hover-item
|
||||
0%
|
||||
transform rotateZ(0)
|
||||
20%
|
||||
transform rotateZ(5deg)
|
||||
80%
|
||||
transform rotateZ(-5deg)
|
||||
100%
|
||||
transform rotateZ(0)
|
||||
|
||||
.inventory-slot-quantity
|
||||
position absolute
|
||||
bottom 0.25rem
|
||||
|
72
pages/shop/buyitem.go
Normal file
72
pages/shop/buyitem.go
Normal file
@ -0,0 +1,72 @@
|
||||
package shop
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
var itemBuyMutex sync.Mutex
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Save purchase
|
||||
err = arn.NewPurchase(user.ID, itemID, quantity, int(item.Price), "gem").Save()
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusInternalServerError, "Error saving purchase", err)
|
||||
}
|
||||
|
||||
return "ok"
|
||||
}
|
32
pages/shop/history.go
Normal file
32
pages/shop/history.go
Normal file
@ -0,0 +1,32 @@
|
||||
package shop
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
// PurchaseHistory ...
|
||||
func PurchaseHistory(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
|
||||
if user == nil {
|
||||
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
|
||||
}
|
||||
|
||||
purchases, err := arn.AllPurchases()
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusInternalServerError, "Error fetching shop item data", err)
|
||||
}
|
||||
|
||||
sort.Slice(purchases, func(i, j int) bool {
|
||||
return purchases[i].Date > purchases[j].Date
|
||||
})
|
||||
|
||||
return ctx.HTML(components.PurchaseHistory(purchases, user))
|
||||
}
|
22
pages/shop/history.pixy
Normal file
22
pages/shop/history.pixy
Normal file
@ -0,0 +1,22 @@
|
||||
component PurchaseHistory(purchases []*arn.Purchase, user *arn.User)
|
||||
ShopTabs(user)
|
||||
|
||||
h1.page-title Purchase History
|
||||
|
||||
table
|
||||
thead
|
||||
tr.mountable
|
||||
th Icon
|
||||
th Item
|
||||
th.history-quantity Quantity
|
||||
th.history-price Price
|
||||
th.history-date Date
|
||||
tbody
|
||||
each purchase in purchases
|
||||
tr.shop-item.mountable(data-item-id=purchase.ItemID)
|
||||
td.item-icon
|
||||
Icon(purchase.Item().Icon)
|
||||
td= purchase.Item().Name
|
||||
td.history-quantity= purchase.Quantity
|
||||
td.history-price= purchase.Price
|
||||
td.history-date.utc-date(data-date=purchase.Date)
|
2
pages/shop/history.scarlet
Normal file
2
pages/shop/history.scarlet
Normal file
@ -0,0 +1,2 @@
|
||||
.history-price, .history-date, .history-quantity
|
||||
text-align right
|
@ -3,7 +3,6 @@ package shop
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/animenotifier/arn"
|
||||
|
||||
@ -12,8 +11,6 @@ import (
|
||||
"github.com/animenotifier/notify.moe/utils"
|
||||
)
|
||||
|
||||
var itemBuyMutex sync.Mutex
|
||||
|
||||
// Get shop page.
|
||||
func Get(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
@ -34,56 +31,3 @@ 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"
|
||||
}
|
||||
|
@ -11,12 +11,14 @@ component ShopTabs(user *arn.User)
|
||||
.tabs
|
||||
Tab("Shop", "shopping-cart", "/shop")
|
||||
Tab("Inventory", "briefcase", "/inventory")
|
||||
Tab("History", "history", "/shop/history")
|
||||
Tab(strconv.Itoa(user.Balance), "diamond", "/charge")
|
||||
|
||||
component ShopItem(item *arn.Item)
|
||||
.widget.shop-item.mountable
|
||||
.widget.shop-item.mountable(data-item-id=item.ID)
|
||||
h3.widget-title.shop-item-name
|
||||
Icon(item.Icon)
|
||||
.item-icon
|
||||
Icon(item.Icon)
|
||||
span= item.Name
|
||||
//- span.shop-item-duration= " " + duration
|
||||
.shop-item-description!= aero.Markdown(item.Description)
|
||||
|
@ -1,11 +1,33 @@
|
||||
item-color-pro-account = hsl(0, 100%, 71%)
|
||||
item-color-anime-support-ticket = hsl(217, 64%, 50%)
|
||||
|
||||
.shop-items
|
||||
// ...
|
||||
|
||||
.shop-item
|
||||
// ...
|
||||
.item-icon
|
||||
display inline-block
|
||||
|
||||
.shop-item-name
|
||||
// ...
|
||||
// Colors
|
||||
.shop-item, .inventory-slot
|
||||
[data-item-id="pro-account-3"]
|
||||
.item-icon
|
||||
color item-color-pro-account
|
||||
|
||||
[data-item-id="pro-account-6"]
|
||||
.item-icon
|
||||
color item-color-pro-account
|
||||
|
||||
[data-item-id="pro-account-12"]
|
||||
.item-icon
|
||||
color item-color-pro-account
|
||||
|
||||
[data-item-id="pro-account-24"]
|
||||
.item-icon
|
||||
color item-color-pro-account
|
||||
|
||||
[data-item-id="anime-support-ticket"]
|
||||
.item-icon
|
||||
color item-color-anime-support-ticket
|
||||
|
||||
.shop-item-price
|
||||
// ...
|
||||
|
Reference in New Issue
Block a user