116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package game
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func IsContentCommand(op string) bool {
|
|
return op == "grant-item" || op == "remove-item" || op == "grant-title" || op == "revoke-title"
|
|
}
|
|
|
|
// Commands identify a stack definition or one exact equipment instance.
|
|
func ParseItemChange(value string) (string, int64, error) {
|
|
id, amount, ok := strings.Cut(value, ":")
|
|
n, err := strconv.ParseInt(amount, 10, 64)
|
|
if !ok || id == "" || len(id) > 64 || err != nil || n < 1 || n > 100 {
|
|
return "", 0, errors.New("choose an item and a quantity from 1 to 100")
|
|
}
|
|
return id, n, nil
|
|
}
|
|
|
|
// Called on the transaction's staged world, never through a player intent.
|
|
func (w *World) AdminItems(c *Character, op, value string) error {
|
|
id, n, err := ParseItemChange(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if op == "grant-item" {
|
|
known := false
|
|
for key := range (content.Catalog{Building: w.Balance.Building}).BuildingItems() {
|
|
if key == id {
|
|
known = true
|
|
}
|
|
}
|
|
for key := range (content.Catalog{Powers: w.Balance.Powers}).PowerItems() {
|
|
if key == id {
|
|
known = true
|
|
}
|
|
}
|
|
if w.Balance.Gathering != nil {
|
|
for _, r := range w.Balance.Gathering.Resources {
|
|
if r.Output == "self" && r.ID == id {
|
|
known = true
|
|
}
|
|
}
|
|
for _, v := range w.Balance.Gathering.Consumables {
|
|
if v.ID == id {
|
|
known = true
|
|
}
|
|
}
|
|
}
|
|
for _, key := range []string{"axe", "flint_and_steel", "fishing_pole", "wood", "ore", "herb", "bandage", "escape_stone"} {
|
|
if key == id {
|
|
known = true
|
|
}
|
|
}
|
|
if w.Balance.Crafting != nil {
|
|
for _, m := range w.Balance.Crafting.Materials {
|
|
if m.ID == id {
|
|
known = true
|
|
}
|
|
}
|
|
}
|
|
for _, definition := range w.Balance.Items {
|
|
if definition.ID == id {
|
|
known = true
|
|
}
|
|
}
|
|
if !known {
|
|
return errors.New("unknown item definition")
|
|
}
|
|
return w.grantItems(c, id, n)
|
|
}
|
|
if op != "remove-item" {
|
|
return errors.New("unknown item operation")
|
|
}
|
|
for index, item := range c.View.Items {
|
|
if item.ID != id {
|
|
continue
|
|
}
|
|
if n != 1 {
|
|
return errors.New("equipment removal requires one exact instance")
|
|
}
|
|
c.View.Items = append(c.View.Items[:index], c.View.Items[index+1:]...)
|
|
for slot, held := range c.View.Equipment {
|
|
if held == id {
|
|
delete(c.View.Equipment, slot)
|
|
}
|
|
}
|
|
w.RefreshStats(c)
|
|
return nil
|
|
}
|
|
if c.View.Inventory[id] < n {
|
|
return errors.New("player no longer holds that item quantity")
|
|
}
|
|
TakeStack(c, id, n)
|
|
if c.View.Inventory[id] == 0 {
|
|
delete(c.View.Inventory, id)
|
|
}
|
|
w.RefreshStats(c)
|
|
return nil
|
|
}
|
|
|
|
func (w *World) AdminTitle(id string) (p.TitleAward, error) {
|
|
for _, title := range w.Balance.Titles {
|
|
if title.ID == id {
|
|
return p.TitleAward{ID: title.ID, Name: title.Name, Description: title.Description, Scope: title.Scope, WorldID: w.ID, EarnedAt: w.now.UTC().Format(time.RFC3339Nano), Effects: p.TitleEffects{Damage: title.Effects.Damage, MoveSpeed: title.Effects.MoveSpeed, Modifiers: protocolModifiers(title.Effects.Modifiers)}}, nil
|
|
}
|
|
}
|
|
return p.TitleAward{}, errors.New("unknown title definition")
|
|
}
|