/
Avatar.go
263 lines (226 loc) · 8.73 KB
/
Avatar.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"github.com/xiaonanln/goworld"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/entity"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/ext/pubsub"
"github.com/xiaonanln/typeconv"
)
// Avatar entity which is the player itself
type Avatar struct {
entity.Entity // Entity type should always inherit entity.Entity
}
func (a *Avatar) DescribeEntityType(desc *entity.EntityTypeDesc) {
desc.SetPersistent(true).SetUseAOI(true)
desc.DefineAttr("name", "AllClients", "Persistent")
desc.DefineAttr("level", "AllClients", "Persistent")
desc.DefineAttr("prof", "AllClients", "Persistent")
desc.DefineAttr("exp", "Client", "Persistent")
desc.DefineAttr("mails", "Client", "Persistent")
desc.DefineAttr("spaceKind", "Persistent")
desc.DefineAttr("lastMailID", "Persistent")
desc.DefineAttr("testListField", "AllClients")
desc.DefineAttr("enteringNilSpace")
}
func (a *Avatar) OnInit() {
}
func (a *Avatar) OnAttrsReady() {
a.setDefaultAttrs()
gwlog.Debugf("Avatar %s is ready: client=%s, mails=%d", a, a.GetClient(), a.Attrs.GetMapAttr("mails").Size())
//a.Msgbox.SetMsgHandler(a.handleMsgboxMsg)
}
// OnCreated is called when avatar is created
func (a *Avatar) OnCreated() {
//gwlog.Debugf("Found OnlineService: %s", onlineServiceEid)
goworld.CallService("OnlineService", "CheckIn", a.ID, a.Attrs.GetStr("name"), a.Attrs.GetInt("level"))
for _, subject := range _TEST_PUBLISH_SUBSCRIBE_SUBJECTS { // subscribe all subjects
goworld.CallService(pubsub.ServiceName, "Subscribe", a.ID, subject)
}
//a.AddTimer(time.Second, "PerSecondTick", 1, "")
}
// PerSecondTick is ticked per second, if timer is setup
func (a *Avatar) PerSecondTick(arg1 int, arg2 string) {
fmt.Fprint(os.Stderr, "!")
}
func (a *Avatar) setDefaultAttrs() {
a.Attrs.SetDefaultStr("name", "无名")
a.Attrs.SetDefaultInt("level", 1)
a.Attrs.SetDefaultInt("exp", 0)
a.Attrs.SetDefaultInt("prof", int64(1+rand.Intn(4)))
a.Attrs.SetDefaultInt("spaceKind", int64(1+rand.Intn(100)))
a.Attrs.SetDefaultInt("lastMailID", 0)
a.Attrs.SetDefaultMapAttr("mails", goworld.MapAttr())
a.Attrs.SetDefaultListAttr("testListField", goworld.ListAttr())
a.Attrs.SetDefaultBool("enteringNilSpace", false)
}
// TestListField_Client is a test RPC for client
func (a *Avatar) TestListField_Client() {
testListField := a.GetListAttr("testListField")
if testListField.Size() > 0 && rand.Float32() < 0.3333333333 {
testListField.PopInt()
} else if testListField.Size() > 0 && rand.Float32() < 0.5 {
testListField.SetInt(rand.Intn(testListField.Size()), int64(rand.Intn(100)))
} else {
testListField.AppendInt(int64(rand.Intn(100)))
}
a.CallClient("OnTestListField", testListField.ToList())
}
func (a *Avatar) enterSpace(spaceKind int) {
if a.Space.Kind == spaceKind {
return
}
if consts.DEBUG_SPACES {
gwlog.Infof("%s enter space from %d => %d", a, a.Space.Kind, spaceKind)
}
goworld.CallService("SpaceService", "EnterSpace", a.ID, spaceKind)
}
// OnClientConnected is called when client is connected
func (a *Avatar) OnClientConnected() {
a.SetClientFilterProp("spaceKind", strconv.Itoa(int(a.GetInt("spaceKind"))))
a.SetClientFilterProp("level", strconv.Itoa(int(a.GetInt("level"))))
a.SetClientFilterProp("prof", strconv.Itoa(int(a.GetInt("prof"))))
a.SetClientFilterProp("online", "0")
a.SetClientFilterProp("online", "1")
a.enterSpace(int(a.GetInt("spaceKind")))
}
// OnClientDisconnected is called when client is lost
func (a *Avatar) OnClientDisconnected() {
gwlog.Infof("%s client disconnected", a)
a.Destroy()
}
// EnterSpace_Client is enter space RPC for client
func (a *Avatar) EnterSpace_Client(kind int) {
a.enterSpace(kind)
}
// DoEnterSpace is called by SpaceService to notify avatar entering specified space
func (a *Avatar) DoEnterSpace(kind int, spaceID common.EntityID) {
// let the avatar enter space with spaceID
a.EnterSpace(spaceID, a.randomPosition())
}
func (a *Avatar) randomPosition() entity.Vector3 {
minCoord, maxCoord := -400, 400
return entity.Vector3{
X: entity.Coord(minCoord + rand.Intn(maxCoord-minCoord)),
Y: 0,
Z: entity.Coord(minCoord + rand.Intn(maxCoord-minCoord)),
}
}
// OnEnterSpace is called when avatar enters a space
func (a *Avatar) OnEnterSpace() {
if consts.DEBUG_SPACES {
gwlog.Infof("%s ENTER SPACE %s", a, a.Space)
}
}
// GetSpaceID is a server RPC to query avatar space ID
func (a *Avatar) GetSpaceID(callerID common.EntityID) {
a.Call(callerID, "OnGetAvatarSpaceID", a.ID, a.Space.ID)
}
func (a *Avatar) EnterRandomNilSpace_Client() {
gameIDs := goworld.ListGameIDs()
gameid := gameIDs[rand.Intn(len(gameIDs))]
nilSpaceID := goworld.GetNilSpaceID(gameid)
gwlog.Debugf("%s EnterRandomNilSpace: %s on game%d", a, nilSpaceID, gameid)
a.Attrs.SetBool("enteringNilSpace", true)
//goworld.GetEntity()
if goworld.GetSpace(nilSpaceID) != nil {
// the nil space is local
a.Attrs.SetBool("enteringNilSpace", false)
a.EnterSpace(nilSpaceID, goworld.Vector3{})
a.CallClient("OnEnterRandomNilSpace")
} else {
a.EnterSpace(nilSpaceID, goworld.Vector3{})
}
}
// OnDestroy is called when avatar is destroying
func (a *Avatar) OnMigrateIn() {
gwlog.Debugf("%s OnMigrateIn ...", a)
if a.Attrs.GetBool("enteringNilSpace") {
a.Attrs.Del("enteringNilSpace")
a.CallClient("OnEnterRandomNilSpace")
}
}
// OnDestroy is called when avatar is destroying
func (a *Avatar) OnDestroy() {
goworld.CallService("OnlineService", "CheckOut", a.ID)
// unsubscribe all subjects
goworld.CallService(pubsub.ServiceName, "UnsubscribeAll", a.ID)
}
// SendMail_Client is a client RPC to send mail to others
func (a *Avatar) SendMail_Client(targetID common.EntityID, mail MailData) {
goworld.CallService("MailService", "SendMail", a.ID, a.GetStr("name"), targetID, mail)
}
// OnSendMail is called by MailService to notify sending mail succeed
func (a *Avatar) OnSendMail(ok bool) {
a.CallClient("OnSendMail", ok)
}
// NotifyReceiveMail is called by MailService to notify Avatar of receiving any mail
func (a *Avatar) NotifyReceiveMail() {
//goworld.CallService("MailService", "GetMails", a.ID)
}
// GetMails_Client is a RPC for clients to retrive mails
func (a *Avatar) GetMails_Client() {
goworld.CallService("MailService", "GetMails", a.ID, a.GetInt("lastMailID"))
}
// OnGetMails is called by MailService to send mails to avatar
func (a *Avatar) OnGetMails(lastMailID int, mails []interface{}) {
//gwlog.Infof("%s.OnGetMails: lastMailID=%v/%v, mails=%v", a, a.GetInt("lastMailID"), lastMailID, mails)
if lastMailID != int(a.GetInt("lastMailID")) {
gwlog.Warnf("%s.OnGetMails: lastMailID mismatch: local=%v, return=%v", a, a.GetInt("lastMailID"), lastMailID)
a.CallClient("OnGetMails", false)
return
}
mailsAttr := a.Attrs.GetMapAttr("mails")
for _, _item := range mails {
item := _item.([]interface{})
mailId := int(typeconv.Int(item[0]))
if mailId <= int(a.GetInt("lastMailID")) {
gwlog.Panicf("mail ID should be increasing")
}
if mailsAttr.HasKey(strconv.Itoa(mailId)) {
gwlog.Errorf("mail %d received multiple times", mailId)
continue
}
mail := typeconv.String(item[1])
mailsAttr.SetStr(strconv.Itoa(mailId), mail)
a.Attrs.SetInt("lastMailID", int64(mailId))
}
a.CallClient("OnGetMails", true)
}
// Say_Client is client RPC for chatting
func (a *Avatar) Say_Client(channel string, content string) {
gwlog.Debugf("Say @%s: %s", channel, content)
if channel == "world" {
a.CallFilteredClients("", "=", "", "OnSay", a.ID, a.GetStr("name"), channel, content)
} else if channel == "prof" {
profStr := strconv.Itoa(int(a.GetInt("prof")))
a.CallFilteredClients("prof", "=", profStr, "OnSay", a.ID, a.GetStr("name"), channel, content)
} else {
gwlog.Panicf("%s.Say_Client: invalid channel: %s", a, channel)
}
}
// Move_Client is client RPC for moving
func (a *Avatar) Move_Client(pos entity.Vector3) {
gwlog.Debugf("Move from %s -> %s", a.GetPosition(), pos)
a.SetPosition(pos)
}
var _TEST_PUBLISH_SUBSCRIBE_SUBJECTS = []string{"monster", "npc", "item", "avatar", "boss_*"}
// TestPublish_Client is client RPC for Publish/Subscribe testing
func (a *Avatar) TestPublish_Client() {
subject := _TEST_PUBLISH_SUBSCRIBE_SUBJECTS[rand.Intn(len(_TEST_PUBLISH_SUBSCRIBE_SUBJECTS))]
if subject[len(subject)-1] == '*' {
subject = subject[:len(subject)-1] + strconv.Itoa(rand.Intn(100))
}
goworld.CallService(pubsub.ServiceName, "Publish", subject, fmt.Sprintf("%s: hello %s, this is a test publish message", a.ID, subject))
}
func (a *Avatar) OnPublish(subject string, content string) {
var publisher common.EntityID
publisher = common.EntityID(content[:common.ENTITYID_LENGTH])
gwlog.Debugf("OnPublish: publisher=%s, subject=%s, content=%s", publisher, subject, content)
a.CallClient("OnTestPublish", publisher, subject, content)
}