Geen omschrijving

object.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package funmow
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/mgutz/ansi"
  7. )
  8. const (
  9. MatchNone = iota
  10. MatchOne
  11. MatchMany
  12. )
  13. type PlayerMeta struct {
  14. ID DBRef `json:"id"`
  15. Password string `json:"password"`
  16. }
  17. type Object struct {
  18. ID DBRef `json:"id"`
  19. Type string `json:"type"`
  20. Owner DBRef `json:"owner"`
  21. Next DBRef `json:"next"`
  22. Name string `json:"name"`
  23. Description string `json:"description"`
  24. Flags map[string]bool `json:"flags"`
  25. Properties map[string]string `json:"properties"`
  26. db *DB
  27. }
  28. type ObjectFactory struct {
  29. db *DB
  30. }
  31. func NewObjectFactory(db *DB) *ObjectFactory {
  32. return &ObjectFactory{db: db}
  33. }
  34. func (f *ObjectFactory) NewObject() Object {
  35. o := Object{}
  36. o.ID, _ = f.db.Allocate()
  37. o.db = f.db
  38. return o
  39. }
  40. func (f *ObjectFactory) NewRawObject() Object {
  41. o := Object{}
  42. o.db = f.db
  43. return o
  44. }
  45. func (f *ObjectFactory) NewExit(name string, next DBRef, owner DBRef) Object {
  46. o := Object{
  47. Type: "exit",
  48. Name: name,
  49. Next: next,
  50. Owner: owner,
  51. db: f.db,
  52. }
  53. o.ID, _ = f.db.Allocate()
  54. return o
  55. }
  56. func (f *ObjectFactory) NewRoom() Object {
  57. o := Object{}
  58. o.ID, _ = f.db.Allocate()
  59. o.Type = "room"
  60. o.Flags = map[string]bool{"jump_ok": true}
  61. o.db = f.db
  62. return o
  63. }
  64. func (f *ObjectFactory) NewThing() Object {
  65. o := Object{}
  66. o.ID, _ = f.db.Allocate()
  67. o.Type = "thing"
  68. o.db = f.db
  69. return o
  70. }
  71. func (o *Object) Commit() error {
  72. return o.db.StoreObject(*o, o.ID)
  73. }
  74. func (o *Object) Refresh() bool {
  75. refreshed, found := o.db.RetrieveObject(o.ID)
  76. if found {
  77. *o = refreshed
  78. }
  79. return found
  80. }
  81. func (o Object) String() string {
  82. return fmt.Sprintf("id %d: %s", o.ID, o.Name)
  83. }
  84. func (o *Object) Remove(c *Object) error {
  85. return o.db.Unlink(o.ID, c.ID)
  86. }
  87. func (o *Object) Delete() error {
  88. err := o.db.Delete(o.ID)
  89. if err == nil {
  90. *o = Object{}
  91. }
  92. return err
  93. }
  94. func (o *Object) Contains(c *Object) error {
  95. return o.db.Link(o.ID, c.ID, c.Type)
  96. }
  97. func (o *Object) MatchExitNames(name string) ObjectList { // so much copypasta
  98. r := make(ObjectList, 0)
  99. children := o.db.GetChildren(o.ID) // map[DBRef]string
  100. for childID, childType := range children {
  101. if childType != "exit" {
  102. continue
  103. }
  104. o, found := o.db.Fetch(childID)
  105. if found {
  106. idName := fmt.Sprintf("#%d", childID)
  107. if strings.EqualFold(o.Name, name) || name == idName {
  108. r = append(r, o)
  109. break // have a match. Don't need to look for any more.
  110. }
  111. aliases := strings.Split(o.Name, ";")
  112. for _, v := range aliases {
  113. if v == name {
  114. r = append(r, o)
  115. break // no need to look at other aliases
  116. }
  117. }
  118. }
  119. }
  120. return r
  121. }
  122. func (o *Object) DetailedName() string {
  123. var label = fmt.Sprintf("%s <#%d>", o.Name, o.ID)
  124. colour := o.GetProp("color")
  125. if colour != "" {
  126. label = ansi.Color(label, colour)
  127. }
  128. return label
  129. }
  130. func (o *Object) ColorName() string {
  131. colour := o.GetProp("color")
  132. if colour != "" {
  133. return ansi.Color(o.Name, colour)
  134. }
  135. return o.Name
  136. }
  137. func (o *Object) GetContents(exclude DBRef) []string {
  138. r := make([]string, 0)
  139. children := o.db.GetChildren(o.ID)
  140. for childID, linkType := range children {
  141. if childID == exclude {
  142. continue
  143. }
  144. if !(linkType == "player" || linkType == "thing") {
  145. continue
  146. }
  147. o, found := o.db.Fetch(childID)
  148. if !found {
  149. continue
  150. }
  151. if linkType == "player" && !o.GetFlag("online") {
  152. continue
  153. }
  154. r = append(r, o.DetailedName())
  155. }
  156. sort.Strings(r)
  157. return r
  158. }
  159. func (o *Object) GetExits(parseAliases bool) []string {
  160. r := make([]string, 0)
  161. children := o.db.GetChildren(o.ID)
  162. for exitID, linkType := range children {
  163. if linkType != "exit" {
  164. continue
  165. }
  166. exit, found := o.db.Fetch(exitID)
  167. if !found {
  168. continue
  169. }
  170. if parseAliases {
  171. r = append(r, exit.FirstAlias())
  172. } else {
  173. r = append(r, exit.DetailedName())
  174. }
  175. }
  176. sort.Strings(r)
  177. return r
  178. }
  179. func (o *Object) FirstAlias() string {
  180. aliases := strings.Split(o.Name, ";")
  181. colour := o.GetProp("color")
  182. if colour != "" {
  183. return ansi.Color(aliases[0], colour)
  184. } else {
  185. return aliases[0]
  186. }
  187. }
  188. func (o *Object) SetFlag(flag string, value bool) {
  189. if o.Flags == nil {
  190. o.Flags = make(map[string]bool)
  191. }
  192. o.Flags[flag] = value
  193. }
  194. func (o *Object) GetFlag(flag string) bool {
  195. if o.Flags == nil {
  196. return false
  197. }
  198. value, ok := o.Flags[flag]
  199. if !ok {
  200. return false
  201. }
  202. return value
  203. }
  204. func (o *Object) SetProp(key string, value string) {
  205. if o.Properties == nil {
  206. o.Properties = make(map[string]string)
  207. }
  208. o.Properties[key] = value
  209. }
  210. func (o *Object) GetProp(key string) string {
  211. if o.Properties == nil {
  212. return ""
  213. }
  214. value, ok := o.Properties[key]
  215. if !ok {
  216. return ""
  217. }
  218. return value
  219. }
  220. type ObjectList []Object
  221. func (l ObjectList) First() Object {
  222. if len(l) > 0 {
  223. return l[0]
  224. } else {
  225. return Object{}
  226. }
  227. }
  228. func (l ObjectList) ExactlyOne() (Object, int) {
  229. c := len(l)
  230. if c == 0 {
  231. return Object{}, MatchNone
  232. } else if c == 1 {
  233. return l[0], MatchOne
  234. } else {
  235. return Object{}, MatchMany
  236. }
  237. }