/
inbound.go
663 lines (549 loc) · 15.2 KB
/
inbound.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// SPDX-FileCopyrightText: 2021 Softbear, Inc.
// SPDX-License-Identifier: AGPL-3.0-or-later
package server
import (
"github.com/SoftbearStudios/mk48/server/world"
"github.com/finnbear/moderation"
"math/rand"
"strings"
"time"
"unicode"
"unicode/utf8"
)
// Make sure to register in init function
type (
// AddToTeam allows Owners to add members that are requesting and
// outsiders to request to join.
AddToTeam struct {
TeamID world.TeamID `json:"teamID"`
PlayerID world.PlayerID `json:"playerID"`
}
// Aim sets your ship's aim target (for turrets, aircraft, etc.)
Aim struct {
Target world.Vec2f `json:"target"`
}
// CreateTeam creates a new team
CreateTeam struct {
Name string `json:"name"`
}
// Fire fires an armament
Fire struct {
world.Guidance
PositionTarget world.Vec2f `json:"positionTarget"`
Index int `json:"index"` // Armament index
}
// InvalidInbound means invalid message type from client (possibly out of date).
// NOTE: Do not register, otherwise client could send type "invalidInbound"
InvalidInbound struct {
messageType messageType
}
// Manual controls your ship's Guidance and optionally sets a TurretTarget.
// TODO embed AimTurrets
Manual struct {
world.Guidance
Active bool `json:"active"`
AngularVelocityTarget *world.Angle `json:"angVelTarget"` // angular velocity must be calculated on the server to avoid oscillations
AltitudeTarget *float32 `json:"altitudeTarget"`
AimTarget world.Vec2f `json:"aimTarget"`
EntityID world.EntityID `json:"entityID"` // Ignored for now, and completely optional. This may be used in the future to allow you to drive your weapons manually.
}
// Drop gold coins to transfer score (within limits)
Pay struct {
Position world.Vec2f `json:"position"`
}
// RemoveFromTeam either kicks someone from your team if you are Owner or leaves your team.
RemoveFromTeam struct {
PlayerID world.PlayerID `json:"playerID"`
}
// SendChat sends a chat message to global chat.
SendChat struct {
Message string `json:"message"`
Team bool `json:"team"`
}
// Spawn spawns your ship.
Spawn struct {
Name string `json:"name"`
Type world.EntityType `json:"type"`
Auth string `json:"auth"` // Auth unlocks certain names and removes some checks
Code world.TeamCode `json:"invite"` // Code automatically adds the Player to the team with that code
New bool `json:"new"` // Whether first time playing
}
// Trace sends debug info.
Trace struct {
FPS float32 `json:"fps"`
}
// Upgrade upgrades your ship.
Upgrade struct {
Type world.EntityType `json:"type"`
}
)
func init() {
registerInbound(
AddToTeam{},
Aim{},
CreateTeam{},
Fire{},
Manual{},
Pay{},
RemoveFromTeam{},
SendChat{},
Spawn{},
Trace{},
Upgrade{},
)
}
var reservedNames = [...]string{
"admin",
"administrator",
"console",
"editor",
"dev",
"developer",
"mod",
"moderator",
"npc",
"owner",
"root",
"server",
"staff",
"system",
}
func (data AddToTeam) Process(h *Hub, _ Client, player *Player) {
teamID := data.TeamID
if teamID == world.TeamIDInvalid {
teamID = player.TeamID
}
playerID := data.PlayerID
if playerID == world.PlayerIDInvalid {
playerID = player.PlayerID()
}
team := h.teams[teamID]
if team == nil {
return
}
// Owner moves people from JoinRequests to Members
// Everyone else can add themself to JoinRequests
if team.Owner() == &player.Player {
if team.Full() {
return // Team is full
}
joiningPlayer := team.JoinRequests.GetByID(playerID)
if joiningPlayer == nil {
return
}
// Shouldn't happen ever
if joiningPlayer.TeamID != world.TeamIDInvalid {
panic("player with active join request already in team")
}
// Removes for all teams including this one
h.clearTeamRequests(joiningPlayer)
team.Members.Add(joiningPlayer)
joiningPlayer.TeamID = player.TeamID
} else if player.PlayerID() == playerID {
if len(team.Members)+len(team.JoinRequests) >= world.TeamMembersMax {
return // Team with requests is full
}
// Player must be data.PlayerID's player
if player.TeamID != world.TeamIDInvalid {
return // Already on a team
}
team.JoinRequests.Add(&player.Player)
} // else possibly needed for voting system in future
}
func (data CreateTeam) Process(h *Hub, _ Client, player *Player) {
if player.TeamID != world.TeamIDInvalid {
return // Already on a team
}
// Validate name
name, ok := sanitize(data.Name, true, world.TeamIDLengthMin, world.TeamIDLengthMax)
// Invalid name
if !ok {
return
}
var teamID world.TeamID
err := teamID.UnmarshalText([]byte(name))
// Shouldn't happen because of validation
if err != nil {
panic(err.Error())
}
if existingTeam := h.teams[teamID]; existingTeam != nil {
return // Team already exists
}
player.TeamID = teamID
h.clearTeamRequests(&player.Player)
team := &Team{}
team.Create(&player.Player)
h.teams[teamID] = team
}
func (data RemoveFromTeam) Process(h *Hub, _ Client, player *Player) {
team := h.teams[player.TeamID]
if team == nil {
return
}
// Default to removing yourself
if data.PlayerID == world.PlayerIDInvalid {
data.PlayerID = player.PlayerID()
}
removePlayer := team.Members.GetByID(data.PlayerID)
// You can remove yourself or other if you are owner
if removePlayer != nil {
if &player.Player == team.Owner() || &player.Player == removePlayer {
h.leaveTeam(removePlayer)
}
} else if &player.Player == team.Owner() {
// Deny join request
removePlayer = team.JoinRequests.GetByID(data.PlayerID)
if removePlayer != nil {
team.JoinRequests.Remove(removePlayer)
}
}
}
func (data Spawn) Process(h *Hub, client Client, player *Player) {
h.world.EntityByID(player.EntityID, func(oldShip *world.Entity) (_ bool) {
if oldShip != nil {
return // can only have one ship
}
authed := h.auth != "" && data.Auth == h.auth
bot := client.Bot()
maxLevel := uint8(1)
if bot {
maxLevel = h.botMaxSpawnLevel
}
if d := data.Type.Data(); d.Kind != world.EntityKindBoat || ((d.Level > maxLevel || (d.NPC && !bot)) && !authed) {
return
}
name, ok := sanitize(data.Name, true, world.PlayerNameLengthMin, world.PlayerNameLengthMax)
// Invalid name
if !ok {
return
}
if authed {
player.Score += 1000
} else {
// Moderate name
lower := strings.ToLower(name)
for _, reservedName := range reservedNames {
if lower == reservedName {
println("blocked reserved name", name)
return // reserved
}
}
}
player.Name = name
// Team codes
if code := data.Code; code != world.TeamCodeInvalid && player.TeamID == world.TeamIDInvalid {
for teamID, team := range h.teams {
if team.Code == code {
if !team.Full() {
h.clearTeamRequests(&player.Player)
team.Members.Add(&player.Player)
player.TeamID = teamID
}
break
}
}
}
entity := &world.Entity{
Owner: &player.Player,
}
entity.Initialize(data.Type)
spawnRadius := h.worldRadius * 0.75
canTeam := player.CanRespawnWithTeam()
if team := h.teams[player.TeamID]; team != nil {
// Spawn near the first other team member with a ship
for _, member := range team.Members {
if member == &player.Player {
continue
}
var spawned bool
h.world.EntityByID(member.EntityID, func(memberShip *world.Entity) (_ bool) {
if memberShip == nil {
return
}
if memberShip.Position.DistanceSquared(player.DeathPos) < 1250*1250 && !canTeam {
return
}
entity.Position = memberShip.Position
spawnRadius = 200
spawned = true
return
})
if spawned {
break
}
}
}
if h.spawnEntity(entity, spawnRadius) == world.EntityIDInvalid {
// Spawn failed
return
}
player.ClearDeath()
if !bot {
h.cloud.IncrementPlaysStatistic()
if data.New {
h.cloud.IncrementNewPlayerStatistic()
}
}
return
})
}
func (data Upgrade) Process(h *Hub, _ Client, player *Player) {
h.world.EntityByID(player.EntityID, func(oldShip *world.Entity) (_ bool) {
if oldShip == nil {
return // hasn't spawned yet
}
newShipType := data.Type
if !oldShip.UpgradesTo(newShipType, player.Score) {
return
}
oldShip.Initialize(newShipType)
return
})
}
func (data Aim) Process(h *Hub, _ Client, player *Player) {
h.world.EntityByID(player.EntityID, func(entity *world.Entity) (_ bool) {
if entity == nil || entity.Owner != &player.Player {
return
}
entity.SetAimTarget(data.Target)
return
})
}
func (data Fire) Process(h *Hub, _ Client, player *Player) {
h.world.EntityByID(player.EntityID, func(entity *world.Entity) (_ bool) {
if entity == nil || entity.Owner != &player.Player {
return
}
shipData := entity.Data()
if data.Index >= len(shipData.Armaments) {
return
}
if entity.ArmamentConsumption()[data.Index] != 0 {
return
}
armamentData := shipData.Armaments[data.Index]
armamentEntityData := armamentData.Type.Data()
if shipData.SubKind == world.EntitySubKindSubmarine && entity.Altitude() < 0 {
// Submerged submarine
if armamentEntityData.SubKind == world.EntitySubKindShell || armamentEntityData.SubKind == world.EntitySubKindSAM {
return
}
}
if armamentData.Turret != nil {
turretData := shipData.Turrets[*armamentData.Turret]
if !turretData.CheckAzimuth(entity.TurretAngles()[*armamentData.Turret]) {
println("Invalid azimuth from client")
return
}
}
transform := entity.ArmamentTransform(data.Index)
failed := false
if armamentEntityData.SubKind == world.EntitySubKindDepositor {
// TODO find another way to calculate this
if data.PositionTarget.DistanceSquared(transform.Position) > 60*60 {
return
}
h.terrain.Sculpt(data.PositionTarget, 60)
} else {
armamentGuidance := data.Guidance
// Calculate angle on server, since Transform math is present
armamentGuidance.DirectionTarget = data.PositionTarget.Sub(transform.Position).Angle()
// Force target to be max speed to prevent any exploits
// (NOTE: change this if client ever legitimately sends something else)
armamentGuidance.VelocityTarget = armamentEntityData.Speed
if armamentData.Vertical {
// Vertically-launched armaments can be launched in any horizontal direction
transform.Direction = armamentGuidance.DirectionTarget
}
if armamentEntityData.SubKind == world.EntitySubKindRocket {
transform.Direction += world.ToAngle((rand.Float32() - 0.5) * 0.1)
}
armamentEntity := &world.Entity{
EntityType: armamentData.Type,
Owner: &player.Player,
Transform: transform,
Guidance: armamentGuidance,
}
if h.spawnEntity(armamentEntity, 0) == world.EntityIDInvalid {
failed = true
}
}
if !failed {
entity.ConsumeArmament(data.Index)
}
return
})
}
func (data Manual) Process(h *Hub, c Client, player *Player) {
// TODO: In the future, check data.EntityID (if controlling weapons is allowed).
h.world.EntityByID(player.EntityID, func(entity *world.Entity) (_ bool) {
if entity == nil || entity.Owner != &player.Player {
return
}
entity.Guidance = data.Guidance
if data.AngularVelocityTarget != nil {
// This only lasts one frame
entity.DirectionTarget = entity.Direction + world.ToAngle(data.AngularVelocityTarget.Float()/float32(world.TicksPerSecond))
}
if data.AltitudeTarget != nil {
entity.SetAltitudeTarget(*data.AltitudeTarget)
}
entity.SetActive(data.Active)
entity.SetAimTarget(data.AimTarget)
return
})
}
func (data Pay) Process(h *Hub, _ Client, player *Player) {
h.world.EntityByID(player.EntityID, func(entity *world.Entity) (_ bool) {
if entity == nil || entity.Owner != &player.Player {
return
}
entityData := entity.Data()
if data.Position.DistanceSquared(entity.Position) > square(entityData.Radius*2) {
// Out of range to pay
return
}
// Ammount being paid and amount being withdrawn
pay := int(world.EntityTypeCoin.Data().Level)
withdraw := pay * 2 // 50% of value lost during payment
if player.Score-withdraw < world.LevelToScore(entityData.Level) {
// Insufficient funds
return
}
paymentEntity := &world.Entity{
EntityType: world.EntityTypeCoin,
Owner: &player.Player,
Transform: world.Transform{
Position: data.Position,
},
}
if h.spawnEntity(paymentEntity, 1) != world.EntityIDInvalid {
// Payment successful, subtract funds
player.Score -= withdraw
}
return
})
}
func (data SendChat) Process(h *Hub, client Client, player *Player) {
h.world.EntityByID(player.EntityID, func(entity *world.Entity) (_ bool) {
if entity == nil || entity.Owner != &player.Player {
// You must have an alive ship to chat (at least until #124).
return
}
if len(data.Message) > 128 {
return
}
name := player.Name
// Allow spamming ones own team, since you can get kicked
msg, ok := player.ChatHistory.Update(data.Message, data.Team)
t := "user"
if client.Bot() {
t = "bot"
}
_ = AppendLog("/tmp/mk48-chat.log", []interface{}{
time.Now().UnixNano() / 1e6,
!ok,
name,
t,
data.Message,
msg,
})
if !ok {
return
}
msg, ok = sanitize(msg, false, 1, 128)
if !ok {
return
}
chat := Chat{Message: msg, PlayerData: player.PlayerData}
if data.Team {
team := h.teams[player.TeamID]
if team == nil {
return
}
team.Chats = append(team.Chats, chat)
} else {
h.chats = append(h.chats, chat)
}
return
})
}
func (trace Trace) Process(_ *Hub, _ Client, p *Player) {
if trace.FPS <= 0 {
return
}
// Clamp to 60 for people possibly playing above to not pollute average
if trace.FPS > 60 {
trace.FPS = 60
}
p.FPS = trace.FPS
_ = AppendLog("/tmp/mk48-trace.log", []interface{}{
time.Now().UnixNano() / 1e6,
p.Name,
trace.FPS,
})
}
func (data InvalidInbound) Process(_ *Hub, _ Client, _ *Player) {}
func trimUtf8(in string, low, high int) (str string, ok bool) {
if !utf8.ValidString(in) {
return "", false
}
// Remove spaces
str = strings.TrimSpace(in)
str = strings.TrimFunc(str, func(r rune) bool {
// NOTE: The following characters are not detected by
// unicode.IsSpace() but show up as blank
// https://www.compart.com/en/unicode/U+2800
// https://www.compart.com/en/unicode/U+200B
return r == 0x2800 || r == 0x200B
})
// Too long but can resize down
if len(str) > high {
var builder strings.Builder
for _, r := range str {
if builder.Len()+utf8.RuneLen(r) > high {
break
}
builder.WriteRune(r)
}
str = builder.String()
}
// Too short
if len(str) < low {
return "", false
}
ok = true
return
}
func sanitize(text string, name bool, low, high int) (string, bool) {
if name {
// Remove these characters
// Brackets are used in formatting
// * is used for censoring
const removals = "()[]{}*"
for i := 0; i < len(removals); i++ {
text = strings.ReplaceAll(text, removals[i:i+1], "")
}
}
text = strings.Map(func(r rune) rune {
if unicode.IsPrint(r) || unicode.IsGraphic(r) {
return r
}
return -1
}, text)
text, ok := trimUtf8(text, low, high)
if !ok {
return "", false
}
if name {
// Censor name
result := moderation.Scan(text)
if result.Is(moderation.Inappropriate) {
if result.Is(moderation.Inappropriate & moderation.Moderate) {
return "", false
}
text, _ = moderation.Censor(text, moderation.Inappropriate)
}
}
return text, true
}