/
session.go
280 lines (233 loc) · 6.2 KB
/
session.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package realm
import (
"fmt"
"sync"
"time"
"github.com/Gophercraft/core/bnet"
"github.com/Gophercraft/core/i18n"
"github.com/Gophercraft/core/packet/area"
"github.com/Gophercraft/core/packet/auth"
"github.com/Gophercraft/core/packet/query"
"github.com/Gophercraft/core/packet/update"
"github.com/Gophercraft/core/realm/wdb"
"github.com/Gophercraft/core/realm/wdb/models"
"github.com/Gophercraft/log"
"github.com/Gophercraft/core/guid"
"github.com/superp00t/etc"
"github.com/Gophercraft/core/home/config"
"github.com/Gophercraft/core/home/rpcnet"
"github.com/Gophercraft/core/packet"
)
type SessionState int8
const Ended SessionState = -1
const (
Handshaking SessionState = iota
Authed
Waiting
CharacterSelectMenu
InWorld
)
type PlayerSession struct {
*update.ValuesBlock
MovementManager
Char *models.Character
CharacterProps []models.CharacterProp
GuardInventory sync.Mutex
Inventory []models.Inventory
Items map[guid.GUID]*Item
// In-world data
JoinedWorldAt time.Time
LevelJoinTime time.Time
CurrentPhase string
CurrentMap *Map
CurrentArea uint32
// CurrentChunkIndex *terrain.TileChunkLookupIndex
ZoneID uint32
AuraState AuraState
SpellManager *SpellManager
// currently tracked objects
Camera Sight
GuardTrackedGUIDs sync.Mutex
TrackedGUIDs []guid.GUID
Flying bool
// Social
Group *Group
GroupInvite guid.GUID
TeleportSummons *Summons
gameMode models.GameMode
}
type Session struct {
// Account data
Server *Server
Connection *packet.Connection
AuthChallenge *auth.Challenge
GuardSession sync.Mutex
ending bool
state SessionState
Tier rpcnet.Tier
Locale i18n.Locale
Account uint64
GameAccount uint64
SessionKey []byte
Processes []*Process
PacketPipe chan *packet.WorldPacket
// In-world Data: better kept as pointer
// Otherwise we're allocating EVERYTHING when someone opens a socket
*PlayerSession
}
func (s *Session) Log(msg ...any) {
text := fmt.Sprintln(
append([]any{s.String()}, msg...))
log.DefaultLogger.LogLine(&log.Line{
Time: time.Now(),
Text: text,
Category: "session",
})
}
func (s *Session) ClientOrigin() string {
return s.Connection.Conn.RemoteAddr().String()
}
// error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
func (s *Session) State() SessionState {
// s.GuardSession.Lock()
state := s.state
// s.GuardSession.Unlock()
return state
}
func (s *Session) HasState(ss SessionState) bool {
if s.State() == Ended {
if ss == Ended {
return true
}
return false
}
return s.State() >= ss
}
func (s *Session) SetState(ss SessionState) {
s.GuardSession.Lock()
s.state = ss
for i := len(s.Processes) - 1; i >= 0; i-- {
proc := s.Processes[i]
if proc.CreationState > s.state {
s.killProcess(i)
}
}
s.GuardSession.Unlock()
}
func (session *Session) SendAuthSuccess() {
exp := uint8(session.Build().Exp())
var as auth.Response
as.Result = bnet.ERROR_OK
as.SuccessInfo = &auth.SuccessInfo{
ActiveExpansionLevel: exp,
AccountExpansionLevel: exp,
// AvailableClasses: session.Server.AvailableClasses(),
}
session.Send(&as)
}
func (s *Session) TypeID() guid.TypeID {
// activeplayer
return guid.TypePlayer
}
func (s *Session) GUID() guid.GUID {
if s.Char == nil {
return guid.Nil
}
return guid.RealmSpecific(guid.Player, s.Server.RealmID(), s.Char.ID)
}
func (s *PlayerSession) Values() *update.ValuesBlock {
return s.ValuesBlock
}
func (s *Session) HandlePing(pi *packet.Ping) {
log.Println("Ping: ", pi.Ping, "Latency", pi.Latency)
s.Send(&packet.Pong{
Ping: pi.Ping,
})
}
func (s *Session) DB() *wdb.Core {
return s.Server.DB
}
func (s *Session) InitPlayerSession(char *models.Character) {
if s.PlayerSession == nil {
s.PlayerSession = &PlayerSession{
Char: char,
}
} else {
s.PlayerSession.Char = char
}
}
func (s *Session) HandlePetNameQuery(p *query.PetName) {
var resp query.PetNameResponse
s.Send(&resp)
log.Println("Sent pet response")
}
func (s *Session) Alertf(format string, args ...interface{}) {
s.SendAlertText(fmt.Sprintf(format, args...))
}
func (s *Session) SendAlertText(data string) {
s.Send(&area.TriggerMessage{
Message: data,
})
}
func (s *Session) Map() *Map {
return s.CurrentMap
}
func (s *Session) MapID() uint32 {
return s.CurrentMap.ID
}
func (s *Session) Phase() *Phase {
return s.Server.Phase(s.CurrentPhase)
}
func (s *Session) GetPlayerClass() models.Class {
return models.Class(s.ValuesBlock.Get("Class").Byte())
}
func (s *Session) Config() *config.World {
return s.Server.Config
}
func (s *Session) HandleRealmSplit(split *etc.Buffer) {
splitReq := split.ReadInt32() // realm ID perhaps?
log.Println("User requested realm split", splitReq)
response := packet.NewWorldPacket(packet.SMSG_REALM_SPLIT)
response.WriteInt32(splitReq)
response.WriteInt32(0) // split state
response.WriteCString("01/01/01")
s.SendPacket(response)
}
func (s *Session) HandleUITimeRequest() {
resp := packet.NewWorldPacket(packet.SMSG_UI_TIME)
resp.WriteUint32(uint32(time.Now().Unix()))
s.SendPacket(resp)
}
func (s *Session) DebugGUID(dbg guid.GUID) string {
switch dbg.HighType() {
case guid.Player:
plyr, err := s.Server.GetSessionByGUID(dbg)
if err != nil {
return dbg.String()
}
return dbg.String() + " (" + plyr.PlayerName() + ")"
case guid.Item:
it, ok := s.Items[dbg]
if !ok {
return dbg.String()
}
var tpl *models.ItemTemplate
s.DB().Lookup(wdb.BucketKeyStringID, it.ItemID, &tpl)
if tpl == nil {
return dbg.String()
}
return fmt.Sprintf("%s (%s)", dbg, tpl.Name.GetLocalized(s.Locale))
default:
return dbg.String()
}
}
func (s *Session) TimeInWorld() (time.Duration, error) {
if !s.HasState(InWorld) {
return 0, fmt.Errorf("realm: TimeInWorld(): not currently in world")
}
return time.Since(s.JoinedWorldAt), nil
}