Brak opisu

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package funmow
  2. import (
  3. "fmt"
  4. "github.com/mgutz/ansi"
  5. "sort"
  6. "strings"
  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.db = f.db
  61. o.SetFlag("jump_ok", true)
  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. o.SetFlag("enter_ok", true)
  70. return o
  71. }
  72. func (o *Object) Commit() error {
  73. return o.db.StoreObject(*o, o.ID)
  74. }
  75. func (o *Object) Refresh() bool {
  76. refreshed, found := o.db.RetrieveObject(o.ID)
  77. if found {
  78. *o = refreshed
  79. }
  80. return found
  81. }
  82. func (o Object) String() string {
  83. return fmt.Sprintf("id %d: %s", o.ID, o.Name)
  84. }
  85. func (o *Object) Remove(c *Object) error {
  86. return o.db.Unlink(o.ID, c.ID)
  87. }
  88. func (o *Object) Delete() error {
  89. err := o.db.Delete(o.ID)
  90. if err == nil {
  91. *o = Object{}
  92. }
  93. return err
  94. }
  95. func (o *Object) Contains(c *Object) error {
  96. return o.db.Link(o.ID, c.ID, c.Type)
  97. }
  98. func (o *Object) MatchExitNames(name string) ObjectList { // so much copypasta
  99. r := make(ObjectList, 0)
  100. children := o.db.GetChildren(o.ID) // map[DBRef]string
  101. for childID, childType := range children {
  102. if childType != "exit" {
  103. continue
  104. }
  105. o, found := o.db.Fetch(childID)
  106. if found {
  107. idName := fmt.Sprintf("#%d", childID)
  108. if strings.EqualFold(o.Name, name) || name == idName {
  109. r = append(r, o)
  110. break // have a match. Don't need to look for any more.
  111. }
  112. aliases := strings.Split(o.Name, ";")
  113. for _, v := range aliases {
  114. if v == name {
  115. r = append(r, o)
  116. break // no need to look at other aliases
  117. }
  118. }
  119. }
  120. }
  121. return r
  122. }
  123. func (o *Object) DetailedName() string {
  124. var label = fmt.Sprintf("%s <#%d>", o.Name, o.ID)
  125. colour := o.GetProp("color")
  126. if colour != "" {
  127. label = ansi.Color(label, colour)
  128. }
  129. return label
  130. }
  131. func (o *Object) ColorName() string {
  132. colour := o.GetProp("color")
  133. if colour != "" {
  134. return ansi.Color(o.Name, colour)
  135. }
  136. return o.Name
  137. }
  138. func (o *Object) GetContents(exclude DBRef, showDark bool) []string {
  139. r := make([]string, 0)
  140. children := o.db.GetChildren(o.ID)
  141. for childID, linkType := range children {
  142. if childID == exclude {
  143. continue
  144. }
  145. if !(linkType == "player" || linkType == "thing") {
  146. continue
  147. }
  148. child, found := o.db.Fetch(childID)
  149. if !found {
  150. continue
  151. }
  152. if linkType == "player" && !child.GetFlag("online") {
  153. continue
  154. }
  155. if child.GetFlag("dark") && !showDark {
  156. continue
  157. }
  158. r = append(r, child.DetailedName())
  159. }
  160. sort.Strings(r)
  161. return r
  162. }
  163. func (o *Object) GetExits(parseAliases bool, showDark bool) []string {
  164. r := make([]string, 0)
  165. children := o.db.GetChildren(o.ID)
  166. for exitID, linkType := range children {
  167. if linkType != "exit" {
  168. continue
  169. }
  170. exit, found := o.db.Fetch(exitID)
  171. if !found {
  172. continue
  173. }
  174. if exit.GetFlag("dark") && !showDark {
  175. continue
  176. }
  177. if parseAliases {
  178. r = append(r, exit.FirstAlias())
  179. } else {
  180. r = append(r, exit.DetailedName())
  181. }
  182. }
  183. sort.Strings(r)
  184. return r
  185. }
  186. func (o *Object) FirstAlias() string {
  187. aliases := strings.Split(o.Name, ";")
  188. colour := o.GetProp("color")
  189. if colour != "" {
  190. return ansi.Color(aliases[0], colour)
  191. } else {
  192. return aliases[0]
  193. }
  194. }
  195. func (o *Object) SetFlag(flag string, value bool) {
  196. if o.Flags == nil {
  197. o.Flags = make(map[string]bool)
  198. }
  199. o.Flags[flag] = value
  200. }
  201. func (o *Object) GetFlag(flag string) bool {
  202. if o.Flags == nil {
  203. return false
  204. }
  205. value, ok := o.Flags[flag]
  206. if !ok {
  207. return false
  208. }
  209. return value
  210. }
  211. func (o *Object) SetProp(key string, value string) {
  212. if o.Properties == nil {
  213. o.Properties = make(map[string]string)
  214. }
  215. o.Properties[key] = value
  216. }
  217. func (o *Object) GetProp(key string) string {
  218. if o.Properties == nil {
  219. return ""
  220. }
  221. value, ok := o.Properties[key]
  222. if !ok {
  223. return ""
  224. }
  225. return value
  226. }
  227. type ObjectList []Object
  228. func (l ObjectList) First() Object {
  229. if len(l) > 0 {
  230. return l[0]
  231. } else {
  232. return Object{}
  233. }
  234. }
  235. func (l ObjectList) ExactlyOne() (Object, int) {
  236. c := len(l)
  237. if c == 0 {
  238. return Object{}, MatchNone
  239. } else if c == 1 {
  240. return l[0], MatchOne
  241. } else {
  242. return Object{}, MatchMany
  243. }
  244. }