-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
clientActions.go
875 lines (797 loc) · 31 KB
/
clientActions.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
package gopher
import (
"github.com/gorilla/websocket"
"github.com/hewiefreeman/GopherGameServer/actions"
"github.com/hewiefreeman/GopherGameServer/core"
"github.com/hewiefreeman/GopherGameServer/database"
"github.com/hewiefreeman/GopherGameServer/helpers"
"sync"
)
const (
errorInvalidAction = "Invalid action"
errorLoggedIn = "You must be logged out"
errorNotLoggedIn = "You must be logged in"
errorFeatureDisabled = "Server feature not enabled"
errorRoomControl = "Clients cannot control rooms"
errorServerRoom = "Clients cannot control that room type"
errorNotOwner = "You are not the owner of the room"
errorRoomType = "Invalid room type"
errorIncorrectFormat = "Incorrect data format"
errorIncorrectFormatName = "Incorrect data format for user name"
errorIncorrectFormatPass = "Incorrect data format for password"
errorIncorrectFormatNewPass = "Incorrect data format for new password"
errorIncorrectFormatAction = "Incorrect data format for action"
errorIncorrectFormatCols = "Incorrect data format for custom columns"
errorIncorrectFormatRemember = "Incorrect data format for remember me"
errorIncorrectFormatGuest = "Incorrect data format for guest"
errorIncorrectFormatRoomName = "Incorrect data format for room name"
errorIncorrectFormatRoomType = "Incorrect data format for room type"
errorIncorrectFormatPrivateRoom = "Incorrect data format for private room"
errorIncorrectFormatMaxRoomUsers = "Incorrect data format for max room users"
errorIncorrectFormatVarKey = "Incorrect data format for variable key"
)
func clientActionHandler(action clientAction, user **core.User, conn *websocket.Conn,
deviceTag *string, devicePass *string, deviceUserID *int, connID *string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
switch action.A {
// Custom actions and voice streams
case helpers.ClientActionCustomAction:
return clientCustomAction(action.P, user, conn, *connID, clientMux)
case helpers.ClientActionVoiceStream:
return clientActionVoiceStream(action.P, user, conn, *connID, clientMux)
// User variables
case helpers.ClientActionSetVariable:
return clientActionSetVariable(action.P, user, *connID, clientMux)
case helpers.ClientActionSetVariables:
return clientActionSetVariables(action.P, user, *connID, clientMux)
// Chat
case helpers.ClientActionChatMessage:
return clientActionChatMessage(action.P, user, *connID, clientMux)
case helpers.ClientActionPrivateMessage:
return clientActionPrivateMessage(action.P, user, *connID, clientMux)
// Change user status
case helpers.ClientActionChangeStatus:
return clientActionChangeStatus(action.P, user, clientMux)
// Log in/out
case helpers.ClientActionLogin:
return clientActionLogin(action.P, user, deviceTag, devicePass, deviceUserID, conn, connID, clientMux)
case helpers.ClientActionLogout:
return clientActionLogout(user, deviceTag, devicePass, deviceUserID, connID, clientMux)
// Room actions
case helpers.ClientActionJoinRoom:
return clientActionJoinRoom(action.P, user, *connID, clientMux)
case helpers.ClientActionLeaveRoom:
return clientActionLeaveRoom(user, *connID, clientMux)
case helpers.ClientActionCreateRoom:
return clientActionCreateRoom(action.P, user, *connID, clientMux)
case helpers.ClientActionDeleteRoom:
return clientActionDeleteRoom(action.P, user, clientMux)
case helpers.ClientActionRoomInvite:
return clientActionRoomInvite(action.P, user, *connID, clientMux)
case helpers.ClientActionRevokeInvite:
return clientActionRevokeInvite(action.P, user, *connID, clientMux)
// Friending
case helpers.ClientActionFriendRequest:
return clientActionFriendRequest(action.P, user, clientMux)
case helpers.ClientActionAcceptFriend:
return clientActionAcceptFriend(action.P, user, clientMux)
case helpers.ClientActionDeclineFriend:
return clientActionDeclineFriend(action.P, user, clientMux)
case helpers.ClientActionRemoveFriend:
return clientActionRemoveFriend(action.P, user, clientMux)
// Database
case helpers.ClientActionSignup:
return clientActionSignup(action.P, user, clientMux)
case helpers.ClientActionDeleteAccount:
return clientActionDeleteAccount(action.P, user, clientMux)
case helpers.ClientActionChangePassword:
return clientActionChangePassword(action.P, user, clientMux)
case helpers.ClientActionChangeAccountInfo:
return clientActionChangeAccountInfo(action.P, user, clientMux)
// Invalid client action
default:
return nil, true, helpers.NewError(errorInvalidAction, helpers.ErrorGopherInvalidAction)
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// CUSTOM CLIENT ACTIONS /////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientCustomAction(params interface{}, user **core.User, conn *websocket.Conn, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
var ok bool
var pMap map[string]interface{}
var action string
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if action, ok = pMap["a"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatAction, helpers.ErrorGopherIncorrectCustomAction)
}
(*clientMux).Lock()
userRef := *user
(*clientMux).Unlock()
actions.HandleCustomClientAction(action, pMap["d"], userRef, conn, connID)
return nil, false, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// CHANGE USER STATUS ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionChangeStatus(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorLoggedIn, helpers.ErrorGopherNotLoggedIn)
}
userRef := *user
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var statusF float64
if statusF, ok = params.(float64); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
status := int(statusF)
//
userRef.SetStatus(status)
//
return status, true, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// ACCOUNT/DATABASE ACTIONS //////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionSignup(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user != nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorLoggedIn, helpers.ErrorGopherLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var customCols map[string]interface{}
var userName string
var pass string
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if pMap["c"] != nil {
if customCols, ok = pMap["c"].(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatCols, helpers.ErrorGopherColumnsFormat)
}
}
if userName, ok = pMap["n"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatName, helpers.ErrorGopherNameFormat)
}
if pass, ok = pMap["p"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatPass, helpers.ErrorGopherPasswordFormat)
}
// Sign client up
signupErr := database.SignUpClient(userName, pass, customCols)
if signupErr.ID != 0 {
return nil, true, signupErr
}
//
return nil, true, helpers.NoError()
}
func clientActionDeleteAccount(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user != nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorLoggedIn, helpers.ErrorGopherLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var customCols map[string]interface{}
var userName string
var pass string
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if pMap["c"] != nil {
if customCols, ok = pMap["c"].(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatCols, helpers.ErrorGopherColumnsFormat)
}
}
if userName, ok = pMap["n"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatName, helpers.ErrorGopherNameFormat)
}
if pass, ok = pMap["p"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatPass, helpers.ErrorGopherPasswordFormat)
}
// Check if online
_, err := core.GetUser(userName)
if err == nil {
return nil, true, helpers.NewError(err.Error(), helpers.ErrorGopherLoggedIn)
}
// Delete account
deleteErr := database.DeleteAccount(userName, pass, customCols)
if deleteErr.ID != 0 {
return nil, true, deleteErr
}
//
return nil, true, helpers.NoError()
}
func clientActionChangePassword(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
userRef := *user
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var customCols map[string]interface{}
var pass string
var newPass string
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if pMap["c"] != nil {
if customCols, ok = pMap["c"].(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherColumnsFormat)
}
}
if pass, ok = pMap["p"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatPass, helpers.ErrorGopherPasswordFormat)
}
if newPass, ok = pMap["n"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatNewPass, helpers.ErrorGopherNewPasswordFormat)
}
// Change password
changeErr := database.ChangePassword(userRef.Name(), pass, newPass, customCols)
if changeErr.ID != 0 {
return nil, true, changeErr
}
//
return nil, true, helpers.NoError()
}
func clientActionChangeAccountInfo(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
userRef := *user
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var customCols map[string]interface{}
var pass string
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if pMap["c"] != nil {
if customCols, ok = pMap["c"].(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatCols, helpers.ErrorGopherColumnsFormat)
}
}
if pass, ok = pMap["p"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatPass, helpers.ErrorGopherPasswordFormat)
}
// Change account info
changeErr := database.ChangeAccountInfo(userRef.Name(), pass, customCols)
if changeErr.ID != 0 {
return nil, true, changeErr
}
//
return nil, true, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// LOGIN+LOGOUT ACTIONS //////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionLogin(params interface{}, user **core.User, deviceTag *string, devicePass *string, deviceUserID *int, conn *websocket.Conn,
connID *string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user != nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorLoggedIn, helpers.ErrorGopherLoggedIn)
}
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var name string
var pass string
var remMe bool = false
var guest bool = false
var customCols map[string]interface{}
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if name, ok = pMap["n"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatName, helpers.ErrorGopherNameFormat)
}
if settings.EnableSqlFeatures {
if pass, ok = pMap["p"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatPass, helpers.ErrorGopherPasswordFormat)
}
if settings.RememberMe {
if remMe, ok = pMap["r"].(bool); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatRemember, helpers.ErrorGopherRememberFormat)
}
}
}
if pMap["g"] != nil {
if guest, ok = pMap["g"].(bool); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatGuest, helpers.ErrorGopherGuestFormat)
}
}
if pMap["c"] != nil {
if customCols, ok = pMap["c"].(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatCols, helpers.ErrorGopherColumnsFormat)
}
}
// Log socket in
var dbIndex int
var dPass string
var cID string
var err helpers.GopherError
if dbIndex, dPass, cID, err = loginClient(settings, guest, name, pass, *deviceTag, remMe, customCols, user,
conn, clientMux); err.ID != 0 {
return nil, false, err
}
// Update socket
*devicePass = dPass
*deviceUserID = dbIndex
*connID = cID
//
return nil, false, helpers.NoError()
}
func loginClient(s *ServerSettings, guest bool, name string, pass string, deviceTag string, remMe bool,
customCols map[string]interface{}, user **core.User, conn *websocket.Conn, clientMux *sync.Mutex) (int, string, string, helpers.GopherError) {
var dbIndex int
var dPass string
var cID string
var err helpers.GopherError
if s.EnableSqlFeatures && !guest {
var uName string
uName, dbIndex, dPass, err = database.LoginClient(name, pass, deviceTag, remMe, customCols)
if err.ID != 0 {
return 0, "", "", err
}
cID, err = core.Login(uName, dbIndex, dPass, guest, remMe, conn, user, clientMux)
} else {
cID, err = core.Login(name, -1, "", guest, false, conn, user, clientMux)
}
return dbIndex, dPass, cID, err
}
func clientActionLogout(user **core.User, deviceTag *string, devicePass *string, deviceUserID *int, connID *string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
}
userRef := *user
(*clientMux).Unlock()
// Log user out
userRef.Logout(*connID)
// Remove any auto-logins for this device tag
if (*settings).EnableSqlFeatures && (*settings).RememberMe {
database.RemoveAutoLog(*deviceUserID, *deviceTag)
}
// Update socket
*devicePass = ""
*deviceUserID = 0
*connID = ""
//
return nil, false, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// ROOM ACTIONS //////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionJoinRoom(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
}
userRef := *user
(*clientMux).Unlock()
// Get room name from params
var ok bool
var roomName string
if roomName, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
// Get room
room, roomErr := core.GetRoom(roomName)
if roomErr != nil {
return nil, true, helpers.NewError(roomErr.Error(), helpers.ErrorGopherJoin)
}
// Make user join the room
joinErr := userRef.Join(room, connID)
if joinErr != nil {
return nil, true, helpers.NewError(joinErr.Error(), helpers.ErrorGopherJoin)
}
//
return nil, false, helpers.NoError()
}
func clientActionLeaveRoom(user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
}
userRef := *user
(*clientMux).Unlock()
// Make user leave room
leaveErr := userRef.Leave(connID)
if leaveErr != nil {
return nil, true, helpers.NewError(leaveErr.Error(), helpers.ErrorGopherLeave)
}
//
return nil, false, helpers.NoError()
}
func clientActionCreateRoom(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).UserRoomControl {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorRoomControl, helpers.ErrorGopherRoomControl)
}
userRef := *user
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var roomName string
var roomType string
var private bool
var maxUsersF float64
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
if roomName, ok = pMap["n"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatRoomName, helpers.ErrorGopherRoomNameFormat)
}
if roomType, ok = pMap["t"].(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatRoomType, helpers.ErrorGopherRoomTypeFormat)
}
if private, ok = pMap["t"].(bool); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatPrivateRoom, helpers.ErrorGopherPrivateFormat)
}
if maxUsersF, ok = pMap["m"].(float64); !ok {
return nil, true, helpers.NewError(errorIncorrectFormatMaxRoomUsers, helpers.ErrorGopherMaxRoomFormat)
}
maxUsers := int(maxUsersF)
// Verify type
if rType, ok := core.GetRoomTypes()[roomType]; !ok {
return nil, true, helpers.NewError(errorRoomType, helpers.ErrorGopherMaxRoomFormat)
} else if rType.ServerOnly() {
return nil, true, helpers.NewError(errorServerRoom, helpers.ErrorGopherServerRoom)
}
// Make the room
room, roomErr := core.NewRoom(roomName, roomType, private, maxUsers, userRef.Name())
if roomErr != nil {
return nil, true, helpers.NewError(roomErr.Error(), helpers.ErrorGopherCreateRoom)
}
// Add user to the new room
joinErr := userRef.Join(room, connID)
if joinErr != nil {
return nil, true, helpers.NewError(joinErr.Error(), helpers.ErrorGopherJoin)
}
//
return roomName, true, helpers.NoError()
}
func clientActionDeleteRoom(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).UserRoomControl {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorRoomControl, helpers.ErrorGopherRoomControl)
}
userRef := *user
(*clientMux).Unlock()
// Get room name from params
var ok bool
var roomName string
if roomName, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
// Get room
room, roomErr := core.GetRoom(roomName)
if roomErr != nil {
return nil, true, helpers.NewError(roomErr.Error(), helpers.ErrorGopherDeleteRoom)
} else if room.Owner() != userRef.Name() {
return nil, true, helpers.NewError(errorNotOwner, helpers.ErrorGopherNotOwner)
}
//
rType := core.GetRoomTypes()[room.Type()]
if rType.ServerOnly() {
return nil, true, helpers.NewError(errorServerRoom, helpers.ErrorGopherServerRoom)
}
// Delete the room
deleteErr := room.Delete()
if deleteErr != nil {
return nil, true, helpers.NewError(deleteErr.Error(), helpers.ErrorGopherDeleteRoom)
}
return roomName, true, helpers.NoError()
}
func clientActionRoomInvite(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).UserRoomControl {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorRoomControl, helpers.ErrorGopherRoomControl)
}
userRef := *user
(*clientMux).Unlock()
// Get name from params
var ok bool
var name string
if name, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
// Get invited user
invUser, invUserErr := core.GetUser(name)
if invUserErr != nil {
return nil, true, helpers.NewError(invUserErr.Error(), helpers.ErrorGopherInvite)
}
// Invite user
invUserErr = userRef.Invite(invUser, connID)
if invUserErr != nil {
return nil, true, helpers.NewError(invUserErr.Error(), helpers.ErrorGopherInvite)
}
//
return nil, true, helpers.NoError()
}
func clientActionRevokeInvite(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).UserRoomControl {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorRoomControl, helpers.ErrorGopherRoomControl)
}
userRef := *user
(*clientMux).Unlock()
// Get name from params
var ok bool
var name string
if name, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
// Revoke invite
revokeErr := userRef.RevokeInvite(name, connID)
if revokeErr != nil {
return nil, true, helpers.NewError(revokeErr.Error(), helpers.ErrorGopherRevokeInvite)
}
//
return nil, true, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// CHAT+VOICE ACTIONS ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionVoiceStream(params interface{}, user **core.User, conn *websocket.Conn, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, false, helpers.NoError()
}
userRef := *user
(*clientMux).Unlock()
// Get current room
currRoom := userRef.RoomIn(connID)
if currRoom == nil || currRoom.Name() == "" {
return nil, false, helpers.NoError()
}
// Check for voice chat
rType := core.GetRoomTypes()[currRoom.Type()]
if !rType.VoiceChatEnabled() {
return nil, false, helpers.NoError()
}
// Send voice stream
currRoom.VoiceStream(userRef.Name(), conn, params)
//
return nil, false, helpers.NoError()
}
func clientActionChatMessage(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, false, helpers.NoError()
}
userRef := *user
(*clientMux).Unlock()
// Get current room
currRoom := userRef.RoomIn(connID)
if currRoom == nil || currRoom.Name() == "" {
return nil, false, helpers.NoError()
}
// Send chat message
currRoom.ChatMessage(userRef.Name(), params)
//
return nil, false, helpers.NoError()
}
func clientActionPrivateMessage(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, false, helpers.NoError()
}
userRef := *user
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var userName string
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, false, helpers.NoError()
}
if userName, ok = pMap["u"].(string); !ok {
return nil, false, helpers.NoError()
}
// Send chat message
userRef.PrivateMessage(userName, pMap["m"])
//
return nil, false, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// USER VARIABLES ////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionSetVariable(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, false, helpers.NoError()
}
userRef := *user
(*clientMux).Unlock()
// Get param map and extract values
var ok bool
var pMap map[string]interface{}
var varKey string
var varVal interface{}
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, false, helpers.NoError()
}
if varKey, ok = pMap["k"].(string); !ok {
return nil, false, helpers.NoError()
}
varVal = pMap["v"]
// Set the variable
userRef.SetVariable(varKey, varVal, connID)
//
return nil, false, helpers.NoError()
}
func clientActionSetVariables(params interface{}, user **core.User, connID string, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, false, helpers.NoError()
}
userRef := *user
(*clientMux).Unlock()
// Get variables map from params
var ok bool
var pMap map[string]interface{}
if pMap, ok = params.(map[string]interface{}); !ok {
return nil, false, helpers.NoError()
}
//SET THE VARIABLES
userRef.SetVariables(pMap, connID)
//
return nil, false, helpers.NoError()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// FRIENDING ACTIONS /////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
func clientActionFriendRequest(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
userRef := *user
(*clientMux).Unlock()
// Get name from params
var ok bool
var friendName string
if friendName, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
requestErr := userRef.FriendRequest(friendName)
if requestErr != nil {
return nil, true, helpers.NewError(requestErr.Error(), helpers.ErrorGopherFriendRequest)
}
//
return nil, false, helpers.NoError()
}
func clientActionAcceptFriend(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
userRef := *user
(*clientMux).Unlock()
// Get name from params
var ok bool
var friendName string
if friendName, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
acceptErr := userRef.AcceptFriendRequest(friendName)
if acceptErr != nil {
return nil, true, helpers.NewError(acceptErr.Error(), helpers.ErrorGopherFriendAccept)
}
//
return nil, false, helpers.NoError()
}
func clientActionDeclineFriend(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
userRef := *user
(*clientMux).Unlock()
// Get name from params
var ok bool
var friendName string
if friendName, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
declineErr := userRef.DeclineFriendRequest(friendName)
if declineErr != nil {
return nil, true, helpers.NewError(declineErr.Error(), helpers.ErrorGopherFriendDecline)
}
//
return nil, false, helpers.NoError()
}
func clientActionRemoveFriend(params interface{}, user **core.User, clientMux *sync.Mutex) (interface{}, bool, helpers.GopherError) {
(*clientMux).Lock()
if *user == nil {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorNotLoggedIn, helpers.ErrorGopherNotLoggedIn)
} else if !(*settings).EnableSqlFeatures {
(*clientMux).Unlock()
return nil, true, helpers.NewError(errorFeatureDisabled, helpers.ErrorGopherFeatureDisabled)
}
userRef := *user
(*clientMux).Unlock()
// Get name from params
var ok bool
var friendName string
if friendName, ok = params.(string); !ok {
return nil, true, helpers.NewError(errorIncorrectFormat, helpers.ErrorGopherIncorrectFormat)
}
removeErr := userRef.RemoveFriend(friendName)
if removeErr != nil {
return nil, true, helpers.NewError(removeErr.Error(), helpers.ErrorGopherFriendRemove)
}
//
return nil, false, helpers.NoError()
}