Files

124 lines
3.9 KiB
Go

package store
import (
"aeternalis/server/internal/game"
p "aeternalis/server/internal/protocol"
"slices"
)
func houseItemAction(w *game.World, c *game.Character, h *houseRecord, r HouseRequest, allowed map[string]bool) error {
rules := w.Balance.Housing
switch r.Op {
case "place":
if !allowed["placeFurniture"] {
return houseError("placing furniture is not permitted")
}
if len(h.View.Furniture) >= rules.MaxFurniture {
return houseError("furniture limit")
}
for i, item := range c.View.Items {
if item.ID == r.Item {
if !game.FurnitureFits(h.View, "", item, r.X, r.Z) {
return houseError("furniture overlaps or leaves the room")
}
h.View.Furniture[item.ID] = p.HouseFurniture{Item: item, X: r.X, Z: r.Z, Contents: game.EmptyHouseInventory()}
c.View.Items = append(c.View.Items[:i:i], c.View.Items[i+1:]...)
return nil
}
}
return houseError("carried furniture required")
case "move":
if !allowed["moveFurniture"] {
return houseError("moving furniture is not permitted")
}
f, ok := h.View.Furniture[r.Target]
if !ok || !game.FurnitureFits(h.View, r.Target, f.Item, r.X, r.Z) {
return houseError("invalid furniture position")
}
f.X = r.X
f.Z = r.Z
h.View.Furniture[r.Target] = f
return nil
case "pickup-furniture":
if !allowed["pickupItems"] {
return houseError("picking up items is not permitted")
}
f, ok := h.View.Furniture[r.Target]
if !ok || len(c.View.Items) >= rules.MaxItems {
return houseError("furniture unavailable or inventory full")
}
if len(f.Contents.Items) > 0 {
return houseError("empty the furniture first")
}
for _, n := range f.Contents.Inventory {
if n > 0 {
return houseError("empty the furniture first")
}
}
c.View.Items = append(c.View.Items, f.Item)
delete(h.View.Furniture, r.Target)
return nil
case "deposit", "withdraw", "drop", "pickup":
permission := map[string]string{"deposit": "depositItems", "withdraw": "pickupItems", "drop": "dropItems", "pickup": "pickupItems"}[r.Op]
if !allowed[permission] || r.Op == "withdraw" && !allowed["openFurniture"] {
return houseError("item movement is not permitted")
}
player := game.HouseInventoryOf(c)
contents := h.View.Floor
f := p.HouseFurniture{}
if r.Op == "deposit" || r.Op == "withdraw" {
var ok bool
f, ok = h.View.Furniture[r.Target]
if !ok || f.Item.Furniture == nil || !f.Item.Furniture.Container {
return houseError("choose storage furniture")
}
contents = f.Contents
}
if r.Op == "deposit" || r.Op == "drop" {
// Do not permit the house's access keys to become locked inside that same house.
for _, i := range player.Items {
if i.ID == r.Item && i.HouseID != nil && *i.HouseID == h.View.ID {
return houseError("keep this house's key outside its own storage and floor")
}
}
if len(contents.Items) >= rules.MaxItems {
return houseError("storage full")
}
if e := game.MoveHouseItem(&player, &contents, r.Item, r.Quantity); e != nil {
return houseError(e.Error())
}
if c.View.DestroyKey != nil && c.View.DestroyKey.KeyID == r.Item {
c.View.DestroyKey = nil
}
} else {
if len(player.Items) >= rules.MaxItems {
return houseError("inventory full")
}
if e := game.MoveHouseItem(&contents, &player, r.Item, r.Quantity); e != nil {
return houseError(e.Error())
}
}
seen := map[string]bool{}
if e := game.ValidateHouseInventory(player, seen); e != nil {
return houseError(e.Error())
}
if e := game.ValidateHouseInventory(contents, seen); e != nil {
return houseError(e.Error())
}
game.SetHouseInventory(c, player)
for slot, id := range c.View.Equipment {
if !slices.ContainsFunc(c.View.Items, func(i p.ItemInstance) bool { return i.ID == id }) {
delete(c.View.Equipment, slot)
}
}
if r.Op == "deposit" || r.Op == "withdraw" {
f.Contents = contents
h.View.Furniture[r.Target] = f
} else {
h.View.Floor = contents
}
return nil
}
return houseError("unknown house action")
}