Add audited admin player item and title management
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
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 []string{"axe", "flint_and_steel", "fishing_pole", "wood", "ore", "herb", "bandage"} {
|
||||
if key == 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")
|
||||
}
|
||||
c.View.Inventory[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")
|
||||
}
|
||||
Reference in New Issue
Block a user