Improved inventory

This commit is contained in:
Eduard Urbach 2017-10-05 09:39:37 +02:00
parent 77ae39d330
commit 09d28e146c
5 changed files with 69 additions and 15 deletions

View File

@ -14,22 +14,17 @@ import (
// Get inventory page.
func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx)
viewUser := user
if user == nil {
return ctx.Error(http.StatusUnauthorized, "Not logged in", nil)
}
inventory, err := arn.GetInventory(user.ID)
inventory, err := arn.GetInventory(viewUser.ID)
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error fetching inventory data", err)
}
// TEST
inventory.AddItem("anime-support-ticket", 35)
inventory.AddItem("pro-account-24", 20)
inventory.AddItem("anime-support-ticket", 15)
inventory.AddItem("pro-account-24", 10)
return ctx.HTML(components.Inventory(inventory))
return ctx.HTML(components.Inventory(inventory, viewUser))
}

View File

@ -1,11 +1,11 @@
component Inventory(inventory *arn.Inventory)
component Inventory(inventory *arn.Inventory, viewUser *arn.User)
ShopTabs
.inventory
.inventory(data-api="/api/inventory/" + viewUser.ID)
for index, slot := range inventory.Slots
if slot.ItemID == ""
.inventory-slot.mountable(draggable="false", data-index=index)
else
.inventory-slot.mountable(title=slot.Item().Name, draggable="true", data-index=index)
.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)
if slot.Quantity > 1
.inventory-slot-quantity= slot.Quantity

View File

@ -16,15 +16,20 @@ func main() {
// Iterate over the stream
for user := range allUsers {
exists, err := arn.DB.Exists("Inventory", user.ID)
// exists, err := arn.DB.Exists("Inventory", user.ID)
if err != nil || exists {
continue
}
// if err != nil || exists {
// continue
// }
fmt.Println(user.Nick)
inventory := arn.NewInventory(user.ID)
// TEST
inventory.AddItem("anime-support-ticket", 50)
inventory.AddItem("pro-account-24", 30)
err = arn.DB.Set("Inventory", inventory.UserID, inventory)
if err != nil {

View File

@ -302,6 +302,25 @@ export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElemen
.then(() => arn.loading(false))
}
// Use item
// export function useItem(arn: AnimeNotifier, button: HTMLElement) {
// let slotIndex = ""
// let parent = button
// while(parent = parent.parentElement) {
// if(parent.dataset.index !== undefined) {
// slotIndex = parent.dataset.index
// break
// }
// }
// let apiEndpoint = arn.findAPIEndpoint(button)
// arn.post(apiEndpoint + "/use/" + slotIndex, "")
// .then(() => arn.reloadContent())
// .catch(err => arn.statusMessage.showError(err))
// }
// Chrome extension installation
export function installExtension(arn: AnimeNotifier, button: HTMLElement) {
let browser: any = window["chrome"]

View File

@ -248,11 +248,32 @@ export class AnimeNotifier {
dragAndDrop() {
for(let element of findAll("inventory-slot")) {
// Skip elements that have their event listeners attached already
if(element["listeners-attached"]) {
continue
}
// If the slot has an item and is therefore draggable
if(element.draggable) {
element.addEventListener("dragstart", e => {
let element = e.target as HTMLElement
e.dataTransfer.setData("text", element.dataset.index)
}, false)
element.addEventListener("dblclick", e => {
let itemName = element.title
if(element.dataset.consumable !== "true") {
return this.statusMessage.showError(itemName + " is not a consumable item.")
}
let apiEndpoint = this.findAPIEndpoint(element)
this.post(apiEndpoint + "/use/" + element.dataset.index, "")
.then(() => this.reloadContent())
.then(() => this.statusMessage.showInfo(`You used ${itemName}.`))
.catch(err => this.statusMessage.showError(err))
}, false)
}
element.addEventListener("dragenter", e => {
@ -283,12 +304,22 @@ export class AnimeNotifier {
return
}
// Swap in database
let apiEndpoint = this.findAPIEndpoint(inventory)
this.post(apiEndpoint + "/swap/" + fromIndex + "/" + toIndex, "")
.catch(err => this.statusMessage.showError(err))
// Swap in UI
toElement.classList.remove("drag-enter")
swapElements(fromElement, toElement)
fromElement.dataset.index = toIndex
toElement.dataset.index = fromIndex
}, false)
// Prevent re-attaching the same listeners
element["listeners-attached"] = true
}
}
@ -660,6 +691,10 @@ export class AnimeNotifier {
}
findAPIEndpoint(element: HTMLElement) {
if(element.dataset.api !== undefined) {
return element.dataset.api
}
let apiObject: HTMLElement
let parent = element