Improved shop

This commit is contained in:
2017-10-06 12:44:55 +02:00
parent 7e5acf5a97
commit bb39234f2d
14 changed files with 253 additions and 77 deletions

72
pages/shop/buyitem.go Normal file
View 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
View 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
View 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)

View File

@ -0,0 +1,2 @@
.history-price, .history-date, .history-quantity
text-align right

View File

@ -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"
}

View File

@ -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)

View File

@ -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
// ...