package game import ( "fmt" "strings" "thehouseoficarus/internal/net" "thehouseoficarus/internal/player" ) func (g *Game) doDropAll(sess *net.Session) { p := sess.Player.(*player.Player) g.CancelAction(p) p.ActionState = &ActionState{Type: ActionDropping, TargetName: "inventory"} count := 0 for _, slot := range p.Inventory { if slot != nil && slot.Quantity > 0 { count++ } } if count == 0 { sess.WriteLine("You have nothing to drop.") return } sess.State = net.StateDropAllConfirm sess.WriteLine(fmt.Sprintf("\nDrop all %d items? [y/N]", count)) } func (g *Game) doDropAllNamed(sess *net.Session, input string) { p := sess.Player.(*player.Player) g.CancelAction(p) matches := g.findInventoryMatches(input, p) if len(matches) == 0 { sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", input)) return } unique := uniqueItemNames(matches) if len(unique) > 1 { g.showWhichOne(sess, matches) return } itemID := matches[0].ID def, _ := g.ItemStore.Load(itemID) name := itemID if def != nil { name = def.Name } coloredName := g.itemColorize(sess, def, name) p.ActionState = &ActionState{Type: ActionDropping, TargetName: name} if def != nil && def.Stackable { slot := p.InvSlot(matches[0].Slot) qty := slot.Quantity p.SetInvSlot(matches[0].Slot, nil) g.World.AddGroundItem(p.RoomID, itemID, qty) g.AccountStore.SaveCharacter(p) if qty == 1 { sess.WriteLine(fmt.Sprintf("You drop a %s.", coloredName)) } else { sess.WriteLine(fmt.Sprintf("You drop %d x %s.", qty, coloredName)) } return } total := 0 for _, match := range matches { slot := p.InvSlot(match.Slot) if slot == nil || slot.ItemID != itemID || slot.Quantity <= 0 { continue } total += slot.Quantity p.SetInvSlot(match.Slot, nil) g.World.AddGroundItem(p.RoomID, itemID, slot.Quantity) } g.AccountStore.SaveCharacter(p) if total == 1 { sess.WriteLine(fmt.Sprintf("You drop a %s.", coloredName)) } else { sess.WriteLine(fmt.Sprintf("You drop %d x %s.", total, coloredName)) } } func (g *Game) doDrop(sess *net.Session, input string) { p := sess.Player.(*player.Player) g.CancelAction(p) qty, itemName := parseQty(input) matches := g.findInventoryMatches(itemName, p) if len(matches) == 0 { sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", itemName)) return } unique := uniqueItemNames(matches) if len(unique) > 1 { g.showWhichOne(sess, matches) return } itemID := matches[0].ID def, _ := g.ItemStore.Load(itemID) name := itemID if def != nil { name = def.Name } p.ActionState = &ActionState{Type: ActionDropping, TargetName: name} if qty == 0 { if def != nil && def.Stackable { for i := 0; i < 28; i++ { slot := p.InvSlot(i) if slot != nil && slot.ItemID == itemID && slot.Quantity > 0 { qty = slot.Quantity break } } } else { qty = 1 } } if qty <= 0 { sess.WriteLine(fmt.Sprintf("You don't have any %s.", name)) return } if def != nil && def.Stackable { slot := p.InvSlot(matches[0].Slot) if qty > slot.Quantity { qty = slot.Quantity } if slot.Quantity > qty { slot.Quantity -= qty } else { p.SetInvSlot(matches[0].Slot, nil) } g.World.AddGroundItem(p.RoomID, itemID, qty) g.AccountStore.SaveCharacter(p) if qty == 1 { sess.WriteLine(fmt.Sprintf("You drop a %s.", g.itemColorize(sess, def, name))) } else { sess.WriteLine(fmt.Sprintf("You drop %d x %s.", qty, g.itemColorize(sess, def, name))) } return } remaining := qty for _, match := range matches { if remaining <= 0 { break } slot := p.InvSlot(match.Slot) if slot == nil || slot.ItemID != itemID || slot.Quantity <= 0 { continue } take := 1 if slot.Quantity > take { slot.Quantity -= take } else { p.SetInvSlot(match.Slot, nil) } g.World.AddGroundItem(p.RoomID, itemID, take) remaining -= take } dropped := qty - remaining g.AccountStore.SaveCharacter(p) if dropped == 0 { sess.WriteLine(fmt.Sprintf("You don't have any %s.", g.itemColorize(sess, def, name))) } else if dropped == 1 { sess.WriteLine(fmt.Sprintf("You drop a %s.", g.itemColorize(sess, def, name))) } else { sess.WriteLine(fmt.Sprintf("You drop %d x %s.", dropped, g.itemColorize(sess, def, name))) } } func (g *Game) handleDropAllConfirm(sess *net.Session, input string) { p, ok := sess.Player.(*player.Player) if !ok { sess.State = net.StateGame g.writePrompt(sess) return } g.CancelAction(p) input = strings.TrimSpace(strings.ToLower(input)) if input != "y" && input != "yes" { sess.State = net.StateGame g.writePrompt(sess) return } var dropped []string for i := 0; i < 28; i++ { slot := p.InvSlot(i) if slot == nil || slot.Quantity <= 0 { continue } g.World.AddGroundItem(p.RoomID, slot.ItemID, slot.Quantity) def, _ := g.ItemStore.Load(slot.ItemID) name := slot.ItemID if def != nil { name = def.Name } dropped = append(dropped, g.itemColorize(sess, def, name)) p.SetInvSlot(i, nil) } g.AccountStore.SaveCharacter(p) sess.State = net.StateGame if len(dropped) == 0 { sess.WriteLine("\nYou have nothing to drop.") } else { sess.Write(fmt.Sprintf("\nYou drop your ")) formatPickupList(sess, dropped) } g.writePrompt(sess) }