Finished shop system
This commit is contained in:
1
main.go
1
main.go
@ -129,6 +129,7 @@ func configure(app *aero.Application) *aero.Application {
|
|||||||
app.Ajax("/shop", shop.Get)
|
app.Ajax("/shop", shop.Get)
|
||||||
app.Ajax("/inventory", inventory.Get)
|
app.Ajax("/inventory", inventory.Get)
|
||||||
app.Ajax("/charge", charge.Get)
|
app.Ajax("/charge", charge.Get)
|
||||||
|
app.Post("/api/shop/buy/:item/:quantity", shop.BuyItem)
|
||||||
|
|
||||||
// Admin
|
// Admin
|
||||||
app.Ajax("/admin", admin.Get)
|
app.Ajax("/admin", admin.Get)
|
||||||
|
@ -20,7 +20,9 @@ component Sidebar(user *arn.User)
|
|||||||
//- SidebarButton("Search", "/search", "search")
|
//- SidebarButton("Search", "/search", "search")
|
||||||
|
|
||||||
if user != nil
|
if user != nil
|
||||||
SidebarButton("Shop", "/shop", "shopping-cart")
|
if user.Role == "admin" || user.Role == "editor"
|
||||||
|
SidebarButton("Shop", "/shop", "shopping-cart")
|
||||||
|
|
||||||
SidebarButton("Statistics", "/statistics", "pie-chart")
|
SidebarButton("Statistics", "/statistics", "pie-chart")
|
||||||
SidebarButton("Settings", "/settings", "cog")
|
SidebarButton("Settings", "/settings", "cog")
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
component Inventory(inventory *arn.Inventory, viewUser *arn.User, user *arn.User)
|
component Inventory(inventory *arn.Inventory, viewUser *arn.User, user *arn.User)
|
||||||
ShopTabs(user)
|
ShopTabs(user)
|
||||||
|
|
||||||
|
h1.page-title Inventory
|
||||||
|
|
||||||
.inventory(data-api="/api/inventory/" + viewUser.ID)
|
.inventory(data-api="/api/inventory/" + viewUser.ID)
|
||||||
for index, slot := range inventory.Slots
|
for index, slot := range inventory.Slots
|
||||||
if slot.ItemID == ""
|
if slot.ItemID == ""
|
||||||
|
@ -3,6 +3,7 @@ package shop
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
|
|
||||||
@ -11,6 +12,8 @@ import (
|
|||||||
"github.com/animenotifier/notify.moe/utils"
|
"github.com/animenotifier/notify.moe/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var itemBuyMutex sync.Mutex
|
||||||
|
|
||||||
// Get shop page.
|
// Get shop page.
|
||||||
func Get(ctx *aero.Context) string {
|
func Get(ctx *aero.Context) string {
|
||||||
user := utils.GetUser(ctx)
|
user := utils.GetUser(ctx)
|
||||||
@ -31,3 +34,56 @@ func Get(ctx *aero.Context) string {
|
|||||||
|
|
||||||
return ctx.HTML(components.Shop(user, items))
|
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)
|
component Shop(user *arn.User, items []*arn.Item)
|
||||||
h1.page-title Shop
|
|
||||||
|
|
||||||
ShopTabs(user)
|
ShopTabs(user)
|
||||||
|
|
||||||
|
h1.page-title Shop
|
||||||
|
|
||||||
.widgets.shop-items
|
.widgets.shop-items
|
||||||
each item in items
|
each item in items
|
||||||
ShopItem(item)
|
ShopItem(item)
|
||||||
@ -21,6 +21,6 @@ component ShopItem(item *arn.Item)
|
|||||||
//- span.shop-item-duration= " " + duration
|
//- span.shop-item-duration= " " + duration
|
||||||
.shop-item-description!= aero.Markdown(item.Description)
|
.shop-item-description!= aero.Markdown(item.Description)
|
||||||
.buttons.shop-buttons
|
.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
|
span.shop-item-price= item.Price
|
||||||
Icon("diamond")
|
Icon("diamond")
|
22
patches/add-balance/add-balance.go
Normal file
22
patches/add-balance/add-balance.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/animenotifier/arn"
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
color.Yellow("Adding balance to all users")
|
||||||
|
|
||||||
|
// Get a stream of all users
|
||||||
|
allUsers, err := arn.StreamUsers()
|
||||||
|
arn.PanicOnError(err)
|
||||||
|
|
||||||
|
// Iterate over the stream
|
||||||
|
for user := range allUsers {
|
||||||
|
user.Balance += 100000
|
||||||
|
arn.PanicOnError(user.Save())
|
||||||
|
}
|
||||||
|
|
||||||
|
color.Green("Finished.")
|
||||||
|
}
|
@ -16,19 +16,19 @@ func main() {
|
|||||||
|
|
||||||
// Iterate over the stream
|
// Iterate over the stream
|
||||||
for user := range allUsers {
|
for user := range allUsers {
|
||||||
// exists, err := arn.DB.Exists("Inventory", user.ID)
|
exists, err := arn.DB.Exists("Inventory", user.ID)
|
||||||
|
|
||||||
// if err != nil || exists {
|
if err != nil || exists {
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
|
|
||||||
fmt.Println(user.Nick)
|
fmt.Println(user.Nick)
|
||||||
|
|
||||||
inventory := arn.NewInventory(user.ID)
|
inventory := arn.NewInventory(user.ID)
|
||||||
|
|
||||||
// TEST
|
// // TEST
|
||||||
inventory.AddItem("anime-support-ticket", 50)
|
// inventory.AddItem("anime-support-ticket", 50)
|
||||||
inventory.AddItem("pro-account-24", 30)
|
// inventory.AddItem("pro-account-24", 30)
|
||||||
|
|
||||||
err = arn.DB.Set("Inventory", inventory.UserID, inventory)
|
err = arn.DB.Set("Inventory", inventory.UserID, inventory)
|
||||||
|
|
||||||
|
@ -336,6 +336,35 @@ export function chargeUp(arn: AnimeNotifier, button: HTMLElement) {
|
|||||||
.then(() => arn.loading(false))
|
.then(() => arn.loading(false))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Buy item
|
||||||
|
export function buyItem(arn: AnimeNotifier, button: HTMLElement) {
|
||||||
|
let itemId = button.dataset.itemId
|
||||||
|
let itemName = button.dataset.itemName
|
||||||
|
let price = button.dataset.price
|
||||||
|
|
||||||
|
if(!confirm(`Would you like to buy ${itemName} for ${price} gems?`)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
arn.loading(true)
|
||||||
|
|
||||||
|
fetch(`/api/shop/buy/${itemId}/1`, {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "same-origin"
|
||||||
|
})
|
||||||
|
.then(response => response.text())
|
||||||
|
.then(body => {
|
||||||
|
if(body !== "ok") {
|
||||||
|
throw body
|
||||||
|
}
|
||||||
|
|
||||||
|
return arn.reloadContent()
|
||||||
|
})
|
||||||
|
.then(() => arn.statusMessage.showInfo(`You bought ${itemName} for ${price} gems. Check out your inventory to confirm the purchase.`, 4000))
|
||||||
|
.catch(err => arn.statusMessage.showError(err))
|
||||||
|
.then(() => arn.loading(false))
|
||||||
|
}
|
||||||
|
|
||||||
// Chrome extension installation
|
// Chrome extension installation
|
||||||
export function installExtension(arn: AnimeNotifier, button: HTMLElement) {
|
export function installExtension(arn: AnimeNotifier, button: HTMLElement) {
|
||||||
let browser: any = window["chrome"]
|
let browser: any = window["chrome"]
|
||||||
|
@ -471,6 +471,10 @@ export class AnimeNotifier {
|
|||||||
element.removeEventListener(oldAction.trigger, oldAction.handler)
|
element.removeEventListener(oldAction.trigger, oldAction.handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!(actionName in actions)) {
|
||||||
|
this.statusMessage.showError(`Action '${actionName}' has not been defined`)
|
||||||
|
}
|
||||||
|
|
||||||
let actionHandler = e => {
|
let actionHandler = e => {
|
||||||
actions[actionName](this, element, e)
|
actions[actionName](this, element, e)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user