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. // Get inventory page.
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
viewUser := user
if user == nil { if user == nil {
return ctx.Error(http.StatusUnauthorized, "Not logged in", 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 { if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error fetching inventory data", err) return ctx.Error(http.StatusInternalServerError, "Error fetching inventory data", err)
} }
// TEST return ctx.HTML(components.Inventory(inventory, viewUser))
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))
} }

View File

@ -1,11 +1,11 @@
component Inventory(inventory *arn.Inventory) component Inventory(inventory *arn.Inventory, viewUser *arn.User)
ShopTabs ShopTabs
.inventory .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 == ""
.inventory-slot.mountable(draggable="false", data-index=index) .inventory-slot.mountable(draggable="false", data-index=index)
else 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) Icon(slot.Item().Icon)
if slot.Quantity > 1 if slot.Quantity > 1
.inventory-slot-quantity= slot.Quantity .inventory-slot-quantity= slot.Quantity

View File

@ -16,15 +16,20 @@ 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
inventory.AddItem("anime-support-ticket", 50)
inventory.AddItem("pro-account-24", 30)
err = arn.DB.Set("Inventory", inventory.UserID, inventory) err = arn.DB.Set("Inventory", inventory.UserID, inventory)
if err != nil { if err != nil {

View File

@ -302,6 +302,25 @@ export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElemen
.then(() => arn.loading(false)) .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 // 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"]

View File

@ -248,11 +248,32 @@ export class AnimeNotifier {
dragAndDrop() { dragAndDrop() {
for(let element of findAll("inventory-slot")) { 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) { if(element.draggable) {
element.addEventListener("dragstart", e => { element.addEventListener("dragstart", e => {
let element = e.target as HTMLElement let element = e.target as HTMLElement
e.dataTransfer.setData("text", element.dataset.index) e.dataTransfer.setData("text", element.dataset.index)
}, false) }, 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 => { element.addEventListener("dragenter", e => {
@ -283,12 +304,22 @@ export class AnimeNotifier {
return 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") toElement.classList.remove("drag-enter")
swapElements(fromElement, toElement) swapElements(fromElement, toElement)
fromElement.dataset.index = toIndex fromElement.dataset.index = toIndex
toElement.dataset.index = fromIndex toElement.dataset.index = fromIndex
}, false) }, false)
// Prevent re-attaching the same listeners
element["listeners-attached"] = true
} }
} }
@ -660,6 +691,10 @@ export class AnimeNotifier {
} }
findAPIEndpoint(element: HTMLElement) { findAPIEndpoint(element: HTMLElement) {
if(element.dataset.api !== undefined) {
return element.dataset.api
}
let apiObject: HTMLElement let apiObject: HTMLElement
let parent = element let parent = element