forked from xiaonanln/goworld
/
SpaceService.go
164 lines (138 loc) · 4.16 KB
/
SpaceService.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
package main
import (
"time"
"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"
)
const (
_MAX_AVATAR_COUNT_PER_SPACE = 100
)
type enterSpaceReq struct {
avatarId common.EntityID
kind int
}
type _SpaceKindInfo struct {
spaceEntities map[common.EntityID]*_SpaceEntityInfo
}
func (ki *_SpaceKindInfo) choose() *_SpaceEntityInfo {
var best *_SpaceEntityInfo
for _, ei := range ki.spaceEntities {
if ei.AvatarNum >= _MAX_AVATAR_COUNT_PER_SPACE { // space is full
continue
}
if best == nil || best.AvatarNum < ei.AvatarNum { // choose the space with more avatars
best = ei
}
}
return best
}
func (ki *_SpaceKindInfo) remove(spaceID common.EntityID) {
delete(ki.spaceEntities, spaceID)
}
type _SpaceEntityInfo struct {
EntityID common.EntityID
Kind int
LastEnterTime time.Time
AvatarNum int
}
// SpaceService is the service entity for space management
type SpaceService struct {
entity.Entity
spaceKinds map[int]*_SpaceKindInfo
pendingRequests []enterSpaceReq
}
func (s *SpaceService) DescribeEntityType(desc *entity.EntityTypeDesc) {
}
func (s *SpaceService) getSpaceKindInfo(kind int) *_SpaceKindInfo {
ki := s.spaceKinds[kind]
if ki == nil {
ki = &_SpaceKindInfo{
spaceEntities: map[common.EntityID]*_SpaceEntityInfo{},
}
s.spaceKinds[kind] = ki
}
return ki
}
func (s *SpaceService) getSpaceEntityInfo(kind int, spaceID common.EntityID) *_SpaceEntityInfo {
kindinfo := s.getSpaceKindInfo(kind)
return kindinfo.spaceEntities[spaceID]
}
// OnInit initializes SpaceService
func (s *SpaceService) OnInit() {
s.spaceKinds = map[int]*_SpaceKindInfo{}
s.pendingRequests = []enterSpaceReq{}
}
// OnCreated is called when entity is created
func (s *SpaceService) OnCreated() {
gwlog.Infof("Registering SpaceService ...")
}
// EnterSpace is called by avatar to enter space by kind
func (s *SpaceService) EnterSpace(avatarId common.EntityID, kind int) {
if consts.DEBUG_SPACES {
gwlog.Infof("%s.EnterSpace: avatar=%s, kind=%d", s, avatarId, kind)
}
spaceKindInfo := s.getSpaceKindInfo(kind)
spaceInfo := spaceKindInfo.choose()
if spaceInfo != nil {
// space already exists, tell the avatar
spaceInfo.LastEnterTime = time.Now()
s.Call(avatarId, "DoEnterSpace", kind, spaceInfo.EntityID)
} else {
s.pendingRequests = append(s.pendingRequests, enterSpaceReq{
avatarId, kind,
})
// create the space
goworld.CreateSpaceAnywhere(kind)
}
}
// NotifySpaceLoaded is called when space is loaded
func (s *SpaceService) NotifySpaceLoaded(loadKind int, loadSpaceID common.EntityID) {
if consts.DEBUG_SPACES {
gwlog.Infof("%s: space is loaded: kind=%d, loadSpaceID=%s", s, loadKind, loadSpaceID)
}
spaceKindInfo := s.getSpaceKindInfo(loadKind)
spaceKindInfo.spaceEntities[loadSpaceID] = &_SpaceEntityInfo{
Kind: loadKind,
EntityID: loadSpaceID,
LastEnterTime: time.Now(),
}
// notify all pending requests
leftPendingReqs := []enterSpaceReq{}
satisfyingReqs := []enterSpaceReq{}
for _, req := range s.pendingRequests {
if req.kind == loadKind {
// this req can be satisfied
satisfyingReqs = append(satisfyingReqs, req)
} else {
// this req can not be satisfied
leftPendingReqs = append(leftPendingReqs, req)
}
}
if len(satisfyingReqs) > 0 {
// if some req is satisfied
s.pendingRequests = leftPendingReqs
for _, req := range satisfyingReqs {
s.Call(req.avatarId, "DoEnterSpace", loadKind, loadSpaceID)
}
}
}
// RequestDestroy is RPC request for Spaces to request for destroying self
func (s *SpaceService) RequestDestroy(kind int, spaceID common.EntityID) {
if consts.DEBUG_SPACES {
gwlog.Infof("Space %s kind %d is requesting destroy ...", spaceID, kind)
}
//spaceKindInfo := s.getSpaceKindInfo(kind)
spaceInfo := s.getSpaceEntityInfo(kind, spaceID)
if spaceInfo == nil { // You don't exists
s.Call(spaceID, "ConfirmRequestDestroy", true)
return
}
if time.Now().After(spaceInfo.LastEnterTime.Add(time.Second * 60)) {
s.getSpaceKindInfo(kind).remove(spaceID)
s.Call(spaceID, "ConfirmRequestDestroy", true)
return
}
}