123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- package funmow
-
- import (
- "fmt"
- "github.com/mgutz/ansi"
- "sort"
- "strings"
- )
-
- const (
- MatchNone = iota
- MatchOne
- MatchMany
- )
-
- type PlayerMeta struct {
- ID DBRef `json:"id"`
- Password string `json:"password"`
- }
-
- type Object struct {
- ID DBRef `json:"id"`
- Type string `json:"type"`
- Owner DBRef `json:"owner"`
- Next DBRef `json:"next"`
- Name string `json:"name"`
- Description string `json:"description"`
- Flags map[string]bool `json:"flags"`
- Properties map[string]string `json:"properties"`
-
- db *DB
- }
-
- type ObjectFactory struct {
- db *DB
- }
-
- func NewObjectFactory(db *DB) *ObjectFactory {
- return &ObjectFactory{db: db}
- }
-
- func (f *ObjectFactory) NewObject() Object {
- o := Object{}
- o.ID, _ = f.db.Allocate()
- o.db = f.db
- return o
- }
-
- func (f *ObjectFactory) NewRawObject() Object {
- o := Object{}
- o.db = f.db
- return o
- }
-
- func (f *ObjectFactory) NewExit(name string, next DBRef, owner DBRef) Object {
- o := Object{
- Type: "exit",
- Name: name,
- Next: next,
- Owner: owner,
- db: f.db,
- }
- o.ID, _ = f.db.Allocate()
- return o
- }
-
- func (f *ObjectFactory) NewRoom() Object {
- o := Object{}
- o.ID, _ = f.db.Allocate()
- o.Type = "room"
- o.db = f.db
- o.SetFlag("jump_ok", true)
- return o
- }
-
- func (f *ObjectFactory) NewThing() Object {
- o := Object{}
- o.ID, _ = f.db.Allocate()
- o.Type = "thing"
- o.db = f.db
- o.SetFlag("enter_ok", true)
- return o
- }
-
- func (o *Object) Commit() error {
- return o.db.StoreObject(*o, o.ID)
- }
-
- func (o *Object) Refresh() bool {
- refreshed, found := o.db.RetrieveObject(o.ID)
- if found {
- *o = refreshed
- }
- return found
- }
-
- func (o Object) String() string {
- return fmt.Sprintf("id %d: %s", o.ID, o.Name)
- }
-
- func (o *Object) Remove(c *Object) error {
- return o.db.Unlink(o.ID, c.ID)
- }
-
- func (o *Object) Delete() error {
- err := o.db.Delete(o.ID)
- if err == nil {
- *o = Object{}
- }
- return err
- }
-
- func (o *Object) Contains(c *Object) error {
- return o.db.Link(o.ID, c.ID, c.Type)
- }
-
- func (o *Object) MatchExitNames(name string) ObjectList { // so much copypasta
- r := make(ObjectList, 0)
-
- children := o.db.GetChildren(o.ID) // map[DBRef]string
-
- for childID, childType := range children {
- if childType != "exit" {
- continue
- }
- o, found := o.db.Fetch(childID)
- if found {
- idName := fmt.Sprintf("#%d", childID)
-
- if strings.EqualFold(o.Name, name) || name == idName {
- r = append(r, o)
- break // have a match. Don't need to look for any more.
- }
-
- aliases := strings.Split(o.Name, ";")
-
- for _, v := range aliases {
- if v == name {
- r = append(r, o)
- break // no need to look at other aliases
- }
- }
- }
- }
-
- return r
- }
-
- func (o *Object) DetailedName() string {
-
- var label = fmt.Sprintf("%s <#%d>", o.Name, o.ID)
-
- colour := o.GetProp("color")
-
- if colour != "" {
- label = ansi.Color(label, colour)
- }
-
- return label
- }
-
- func (o *Object) ColorName() string {
-
- colour := o.GetProp("color")
-
- if colour != "" {
- return ansi.Color(o.Name, colour)
- }
-
- return o.Name
- }
-
- func (o *Object) GetContents(exclude DBRef, showDark bool) []string {
- r := make([]string, 0)
- children := o.db.GetChildren(o.ID)
- for childID, linkType := range children {
- if childID == exclude {
- continue
- }
- if !(linkType == "player" || linkType == "thing") {
- continue
- }
- child, found := o.db.Fetch(childID)
- if !found {
- continue
- }
- if linkType == "player" && !child.GetFlag("online") {
- continue
- }
- if child.GetFlag("dark") && !showDark {
- continue
- }
- r = append(r, child.DetailedName())
- }
- sort.Strings(r)
- return r
- }
-
- func (o *Object) GetExits(parseAliases bool, showDark bool) []string {
- r := make([]string, 0)
- children := o.db.GetChildren(o.ID)
- for exitID, linkType := range children {
- if linkType != "exit" {
- continue
- }
- exit, found := o.db.Fetch(exitID)
- if !found {
- continue
- }
- if exit.GetFlag("dark") && !showDark {
- continue
- }
- if parseAliases {
- r = append(r, exit.FirstAlias())
- } else {
- r = append(r, exit.DetailedName())
- }
- }
-
- sort.Strings(r)
- return r
- }
-
- func (o *Object) FirstAlias() string {
-
- aliases := strings.Split(o.Name, ";")
- colour := o.GetProp("color")
-
- if colour != "" {
- return ansi.Color(aliases[0], colour)
- } else {
- return aliases[0]
- }
- }
-
- func (o *Object) SetFlag(flag string, value bool) {
- if o.Flags == nil {
- o.Flags = make(map[string]bool)
- }
- o.Flags[flag] = value
- }
-
- func (o *Object) GetFlag(flag string) bool {
-
- if o.Flags == nil {
- return false
- }
- value, ok := o.Flags[flag]
- if !ok {
- return false
- }
-
- return value
- }
-
- func (o *Object) SetProp(key string, value string) {
- if o.Properties == nil {
- o.Properties = make(map[string]string)
- }
- o.Properties[key] = value
- }
-
- func (o *Object) GetProp(key string) string {
-
- if o.Properties == nil {
- return ""
- }
- value, ok := o.Properties[key]
- if !ok {
- return ""
- }
-
- return value
- }
-
- type ObjectList []Object
-
- func (l ObjectList) First() Object {
- if len(l) > 0 {
- return l[0]
- } else {
- return Object{}
- }
- }
-
- func (l ObjectList) ExactlyOne() (Object, int) {
- c := len(l)
-
- if c == 0 {
- return Object{}, MatchNone
- } else if c == 1 {
- return l[0], MatchOne
- } else {
- return Object{}, MatchMany
- }
- }
|