-
Notifications
You must be signed in to change notification settings - Fork 1
/
items.go
93 lines (80 loc) · 2.31 KB
/
items.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
items.go contains Item types and funcs,
along with an ItemManager type which
provides item-related functions for ThingManager
Item implements the Thing interface.
ItemManager is a ThingManager
*/
package main
import (
"fmt"
)
type ItemLocationType int32
const (
ilRoom = iota
ilPlayer
ilNpc
)
type Item struct {
id identifier
name string
brief string
Location identifier
LocationType ItemLocationType ///< @todo ? remove this ? it isn't strictly necessary, as we can type assert to find the type
Items map[identifier]bool
}
func (i *Item) Id() identifier {
return i.id
}
func (i *Item) SetId(newId identifier) {
i.id = newId
}
func (i *Item) Name() string {
return i.name
}
func (i *Item) Brief() string {
return i.brief
}
type ItemManager ThingManager
/// @todo remove this, after changing things which call it to store Accessors rather than IDs
/// @todo change this to return an error object with an err string, rather than printing the err and returning bool
func (m ItemManager) GetById(id identifier) (*Item, bool) {
accessor := ThingManager(m).GetThingAccessor(id)
if accessor.ThingGetter == nil {
fmt.Println("ItemManager.GetById error: ThingGetter nil " + id.String())
return &Item{}, false
}
thing, ok := <-accessor.ThingGetter
if !ok {
fmt.Println("ItemManager.GetById error: item ThingGetter closed " + id.String())
return &Item{}, false
}
item, ok := thing.(*Item)
if !ok {
fmt.Println("ItemManager.GetById error: item accessor returned non-item " + id.String())
return &Item{}, false
}
return item, ok
}
/// @todo change this to return an error object with an err string, rather than printing the err and returning bool
func (m ItemManager) ChangeById(id identifier, modify func(p *Item)) bool {
accessor := ThingManager(m).GetThingAccessor(id)
if accessor.ThingGetter == nil {
fmt.Println("ItemManager.ChangeById error: ThingGetter nil " + id.String())
return false
}
setMsg, ok := <-accessor.ThingSetter
if !ok {
fmt.Println("ItemManager.ChangeById error: item ThingGetter closed " + id.String())
return false
}
setMsg.chainTime <- NotChaining
item, ok := setMsg.it.(*Item)
if !ok {
fmt.Println("ItemManager.ChangeById error: item accessor returned non-item " + id.String())
return false
}
modify(item)
setMsg.set <- item
return true
}