-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.go
1712 lines (1482 loc) · 42.1 KB
/
parser.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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2021 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package parser
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"io"
"strconv"
"strings"
"time"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/model"
"github.com/sirupsen/logrus"
)
// Error types
var (
ErrUnknownType = errors.New("unknown type")
ErrUnexpectedType = errors.New("unexpected type")
ErrInvalidMessageLine = errors.New("invalid message line")
)
const (
newLine = '\n'
keyType = "type: "
)
// UnmarshalResponse converts response message to internal message format
func UnmarshalResponse(message []byte) (model.Message, error) {
reader := bufio.NewReader(bytes.NewReader(message))
msgType, err := getResponseIdentifier(reader)
if err != nil {
return nil, err
}
return unmarshalResponseContent(msgType, reader)
}
func getResponseIdentifier(reader *bufio.Reader) (messageType string, err error) {
msgType, err := reader.ReadString(newLine)
if err != nil && err != io.EOF {
return model.TypeUnknown, err
}
msgType = strings.TrimSuffix(msgType, string(newLine))
msgType = strings.TrimPrefix(msgType, keyType)
if msgType == "" {
return model.TypeHeartbeat, nil
}
switch msgType {
case model.TypeConnected,
model.TypeDisconnected,
model.TypeError, model.TypeShutdown,
model.TypeInfoResponse,
model.TypeCreateResponse,
model.TypeLeaveResponse,
model.TypeInviteResponse,
model.TypeJoinResponse,
model.TypeKickResponse,
model.TypeKickNotice,
model.TypeJoinNotif,
model.TypeInviteNotif,
model.TypeLeaveNotif,
model.TypePersonalChatResponse,
model.TypePersonalChatNotif,
model.TypePartyChatResponse,
model.TypePersonalChatHistoryResponse,
model.TypePartyChatNotif,
model.TypeNotificationMessage,
model.TypeGetOfflineNotificationRequest,
model.TypeFriendsPresenceResponse,
model.TypeSetUserStatusResponse,
model.TypeGetInvitatedNotif,
model.TypeUserStatusNotif,
model.TypeGetOfflineNotificationResponse,
model.TypeStartMatchmakingResponse,
model.TypeCancelMatchmakingResponse,
model.TypeMatchmakingNotif,
model.TypeSetReadyConsentResponse,
model.TypeSetReadyConsentNotif,
model.TypeRematchmakingNotif,
model.TypeRequestFriendsResponse,
model.TypeListIncomingFriendsResponse,
model.TypeListOutgoingFriendsResponse,
model.TypeAcceptFriendsResponse,
model.TypeRejectFriendsResponse,
model.TypeRejectFriendsNotif,
model.TypeCancelFriendsResponse,
model.TypeCancelFriendsNotif,
model.TypeUnfriendResponse,
model.TypeListOfFriendsResponse,
model.TypeGetFriendshipStatusResponse,
model.TypeRequestFriendsNotif,
model.TypeAcceptFriendsNotif,
model.TypeDSNotif,
model.TypePartyDataUpdateNotif,
model.TypeJoinDefaultChannelResponse,
model.TypeSendChannelChatResponse,
model.TypeChannelChatNotif,
model.TypeRejectNotif,
model.TypeRejectResponse,
model.TypeBlockPlayerResponse,
model.TypeUnblockPlayerResponse,
model.TypeBlockPlayerNotif,
model.TypeUnblockPlayerNotif,
model.TypeSetSessionAttributeResponse,
model.TypeSignalingP2PNotif,
model.TypePromoteLeaderResponse,
model.TypeGeneratePartyCodeResponse,
model.TypeGetPartyCodeResponse,
model.TypeDeletePartyCodeResponse,
model.TypeJoinViaPartyCodeResponse,
model.TypeUnfriendNotif,
model.TypeUserBannedNotification,
model.TypeUserUnbannedNotification:
default:
return model.TypeUnknown, ErrUnknownType
}
return msgType, nil
}
// nolint: gocyclo
func unmarshalResponseContent(messageType string, reader *bufio.Reader) (model.Message, error) {
switch messageType {
case model.TypeError:
return unmarshalError(reader)
case model.TypeConnected:
return unmarshalConnectedResponse(reader)
case model.TypeInfoResponse:
return unmarshalInfoResponse(reader)
case model.TypeCreateResponse:
return unmarshalCreateResponse(reader)
case model.TypeLeaveResponse:
return unmarshalLeaveResponse(reader)
case model.TypeInviteResponse:
return unmarshalInviteResponse(reader)
case model.TypeGetInvitatedNotif:
return unmarshalInvitation(reader)
case model.TypeJoinResponse:
return unmarshalJoinResponse(reader)
case model.TypeRejectResponse:
return unmarshalRejectResponse(reader)
case model.TypeRejectNotif:
return unmarshalRejectNotice(reader)
case model.TypeKickResponse:
return unmarshalKickResponse(reader)
case model.TypeKickNotice:
return unmarshalKickNotice(reader)
case model.TypeJoinNotif:
return unmarshalJoinNotice(reader)
case model.TypeInviteNotif:
return unmarshalInviteNotice(reader)
case model.TypeLeaveNotif:
return unmarshalLeaveNotice(reader)
case model.TypePersonalChatNotif:
return unmarshalReceived1to1Chat(reader)
case model.TypePersonalChatResponse:
return unmarshalPersonalChatResponse(reader)
case model.TypePartyChatNotif:
return unmarshalPartyChatNotif(reader)
case model.TypePartyChatResponse:
return unmarshalPartyChatResponse(reader)
case model.TypePersonalChatHistoryResponse:
return unmarshalPersonalChatHistoryResponse(reader)
case model.TypeNotificationMessage:
return unmarshalNotificationMessage(reader)
case model.TypeGetOfflineNotificationResponse:
return unmarshalNotificationResponse(reader)
case model.TypeSetUserStatusResponse:
return unmarshalSetUserStatusResponse(reader)
case model.TypeFriendsPresenceResponse:
return unmarshalFriendsPresenceResponse(reader)
case model.TypeUserStatusNotif:
return unmarshalNotifyFriendsResponse(reader)
case model.TypeStartMatchmakingResponse:
return unmarshalStartMatchmakingResponse(reader)
case model.TypeCancelMatchmakingResponse:
return unmarshalCancelMatchmakingResponse(reader)
case model.TypeMatchmakingNotif:
return unmarshalMatchmakingNotification(reader)
case model.TypeSetReadyConsentResponse:
return unmarshalSetReadyConsentResponse(reader)
case model.TypeSetReadyConsentNotif:
return unmarshalSetReadyConsentNotif(reader)
case model.TypeRematchmakingNotif:
return unmarshalRematchmakingNotif(reader)
case model.TypeRequestFriendsResponse:
return unmarshalRequestFriendsResponse(reader)
case model.TypeRequestFriendsNotif:
return unmarshalRequestFriendsNotif(reader)
case model.TypeListIncomingFriendsResponse:
return unmarshalListIncomingFriendsResponse(reader)
case model.TypeListOutgoingFriendsResponse:
return unmarshalListOutgoingFriendsResponse(reader)
case model.TypeAcceptFriendsResponse:
return unmarshalAcceptFriendsResponse(reader)
case model.TypeAcceptFriendsNotif:
return unmarshalAcceptFriendsNotif(reader)
case model.TypeRejectFriendsResponse:
return unmarshalRejectFriendsResponse(reader)
case model.TypeRejectFriendsNotif:
return unmarshalRejectFriendsNotif(reader)
case model.TypeCancelFriendsResponse:
return unmarshalCancelFriendsResponse(reader)
case model.TypeCancelFriendsNotif:
return unmarshalCancelFriendsNotif(reader)
case model.TypeUnfriendResponse:
return unmarshalUnfriendResponse(reader)
case model.TypeUnfriendNotif:
return unmarshalUnfriendNotif(reader)
case model.TypeListOfFriendsResponse:
return unmarshalListOfFriendsResponse(reader)
case model.TypeGetFriendshipStatusResponse:
return unmarshalGetFriendshipStatusResponse(reader)
case model.TypeDSNotif:
return unmarshalDSNotif(reader)
case model.TypePartyDataUpdateNotif:
return unmarshalPartyDataUpdateNotif(reader)
case model.TypeJoinDefaultChannelResponse:
return unmarshalJoinDefaultChannelResponse(reader)
case model.TypeChannelChatNotif:
return unmarshalChannelChatNotif(reader)
case model.TypeBlockPlayerResponse:
return unmarshalBlockPlayerResponse(reader)
case model.TypeBlockPlayerNotif:
return unmarshalBlockPlayerNotif(reader)
case model.TypeUnblockPlayerResponse:
return unmarshalUnblockPlayerResponse(reader)
case model.TypeUnblockPlayerNotif:
return unmarshalUnblockPlayerNotif(reader)
case model.TypeSetSessionAttributeResponse:
return unmarshalSetSessionAttributeResponse(reader)
case model.TypeGetSessionAttributeResponse:
return unmarshalGetSessionAttributeResponse(reader)
case model.TypeGetAllSessionAttributeResponse:
return unmarshalGetAllSessionAttributeResponse(reader)
case model.TypeSendChannelChatResponse:
return unmarshalSendChannelChatResponse(reader)
case model.TypeSignalingP2PNotif:
return unmarshalSignalingP2PResponse(reader)
case model.TypePromoteLeaderResponse:
return unmarshalPromoteLeaderResponse(reader)
case model.TypeGeneratePartyCodeResponse:
return unmarshalGeneratePartyCodeResponse(reader)
case model.TypeGetPartyCodeResponse:
return unmarshalGetPartyCodeResponse(reader)
case model.TypeDeletePartyCodeResponse:
return unmarshalDeletePartyCodeResponse(reader)
case model.TypeJoinViaPartyCodeResponse:
return unmarshalJoinViaPartyCodeResponse(reader)
case model.TypeUserBannedNotification:
return unmarshalUserBannedNotificationResponse(reader)
case model.TypeUserUnbannedNotification:
return unmarshalUserUnbannedNotificationResponse(reader)
case model.TypeSendPartyNotifResponse:
return unmarshalSendPartyNotifResponse(reader)
case model.TypePartyNotif:
return unmarshalPartyNotif(reader)
}
logrus.Debug("type not handled : ", messageType)
return nil, ErrUnexpectedType
}
func unmarshalError(reader *bufio.Reader) (*model.ErrorMessage, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.ErrorMessage{
BaseResponse: &model.BaseResponse{Code: responseCode},
Message: content["message"],
}
return response, nil
}
func unmarshalConnectedResponse(reader *bufio.Reader) (*model.Connected, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.Connected{
LobbySessionID: content["lobbySessionID"],
}
return response, nil
}
func unmarshalInfoResponse(reader *bufio.Reader) (*model.InfoResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.InfoResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
PartyID: content["partyID"],
LeaderID: content["leaderID"],
Members: content["members"],
Invitees: content["invitees"],
InvitationToken: content["invitationToken"],
}
return response, nil
}
func unmarshalCreateResponse(reader *bufio.Reader) (*model.CreateResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.CreateResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
PartyID: content["partyID"],
LeaderID: content["leaderID"],
Members: content["members"],
Invitees: content["invitees"],
InvitationToken: content["invitationToken"],
PartyCode: content["partyCode"],
}
return response, nil
}
func unmarshalLeaveResponse(reader *bufio.Reader) (*model.LeaveResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.LeaveResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
}
return response, nil
}
func unmarshalInviteResponse(reader *bufio.Reader) (*model.InviteResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.InviteResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
}
return response, nil
}
func unmarshalInvitation(reader *bufio.Reader) (*model.Invitation, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.Invitation{
From: content["from"],
PartyID: content["partyID"],
InvitationToken: content["invitationToken"],
}
return response, nil
}
func unmarshalJoinResponse(reader *bufio.Reader) (*model.JoinResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.JoinResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
PartyID: content["partyID"],
LeaderID: content["leaderID"],
Members: content["members"],
Invitees: content["invitees"],
InvitationToken: content["invitationToken"],
}
return response, nil
}
func unmarshalRejectResponse(reader *bufio.Reader) (*model.RejectResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.RejectResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
PartyID: content["partyID"],
}
return response, nil
}
func unmarshalRejectNotice(reader *bufio.Reader) (*model.RejectNotice, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.RejectNotice{
PartyID: content["partyID"],
LeaderID: content["leaderID"],
UserID: content["userID"],
}
return response, nil
}
func unmarshalKickResponse(reader *bufio.Reader) (*model.KickResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.KickResponse{
BaseResponse: &model.BaseResponse{Code: responseCode},
}
return response, nil
}
func unmarshalKickNotice(reader *bufio.Reader) (*model.KickNotice, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.KickNotice{
LeaderID: content["leaderID"],
UserID: content["userID"],
PartyID: content["partyID"],
}
return response, nil
}
func unmarshalJoinNotice(reader *bufio.Reader) (*model.JoinNotice, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.JoinNotice{
UserID: content["userID"],
}
return response, nil
}
func unmarshalInviteNotice(reader *bufio.Reader) (*model.InviteNotice, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.InviteNotice{
InviterID: content["inviterID"],
InviteeID: content["inviteeID"],
}
return response, nil
}
func unmarshalLeaveNotice(reader *bufio.Reader) (*model.LeaveNotice, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.LeaveNotice{
UserID: content["userID"],
LeaderID: content["leaderID"],
}
return response, nil
}
func unmarshalReceived1to1Chat(reader *bufio.Reader) (notif *model.PersonalChatNotif, err error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
parsedTime, err := time.Parse(time.RFC3339, content["receivedAt"])
if err != nil {
return nil, err
}
response := &model.PersonalChatNotif{
ChatMessage: &model.ChatMessage{
ID: content["id"],
From: content["from"],
To: content["to"],
Payload: content["payload"],
ReceivedAt: parsedTime.Unix(),
},
}
return response, nil
}
func unmarshalPersonalChatResponse(reader *bufio.Reader) (response *model.PersonalChatResponse, err error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response = &model.PersonalChatResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalPartyChatNotif(reader *bufio.Reader) (notif *model.PartyChatNotif, err error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
parsedTime, err := time.Parse(time.RFC3339, content["receivedAt"])
if err != nil {
return nil, err
}
response := &model.PartyChatNotif{
ChatMessage: &model.ChatMessage{
ID: content["id"],
From: content["from"],
To: content["to"],
Payload: content["payload"],
ReceivedAt: parsedTime.Unix(),
},
}
return response, nil
}
func unmarshalPartyChatResponse(reader *bufio.Reader) (notif *model.PartyChatResponse, err error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.PartyChatResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalPersonalChatHistoryResponse(reader *bufio.Reader) (notif *model.PersonalChatHistoryResponse, err error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.PersonalChatHistoryResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
FriendID: content["friendId"],
Chat: content["chat"],
}
return response, nil
}
func unmarshalNotificationMessage(reader *bufio.Reader) (*model.NotificationMessage, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
parsedTime, err := time.Parse(time.RFC3339, content["sentAt"])
if err != nil {
return nil, err
}
response := &model.NotificationMessage{
ID: content["id"],
From: content["from"],
To: content["to"],
Topic: content["topic"],
Payload: content["payload"],
SentAt: parsedTime.Unix(),
}
return response, nil
}
func unmarshalNotificationResponse(reader *bufio.Reader) (response *model.GetOfflineNotificationResponse, err error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response = &model.GetOfflineNotificationResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalFriendsPresenceResponse(reader *bufio.Reader) (*model.ListFriendsPresenceResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
var userIDs []string
userIDsPayload := strings.TrimPrefix(content["friendsId"], "[")
userIDsPayload = strings.TrimSuffix(userIDsPayload, "]")
for _, userID := range strings.Split(userIDsPayload, ",") {
if userID != "" {
userIDs = append(userIDs, userID)
}
}
var userAvailability []string
statusAvailability := strings.TrimPrefix(content["availability"], "[")
statusAvailability = strings.TrimSuffix(statusAvailability, "]")
for _, payload := range strings.Split(statusAvailability, ",") {
if payload != "" {
userAvailability = append(userAvailability, payload)
}
}
var userActivity []string
statusActivity := strings.TrimPrefix(content["activity"], "[")
statusActivity = strings.TrimSuffix(statusActivity, "]")
for _, payload := range strings.Split(statusActivity, ",") {
if payload != "" {
userActivity = append(userActivity, payload)
}
}
var userPlatform []string
statusPlatform := strings.TrimPrefix(content["platform"], "[")
statusPlatform = strings.TrimSuffix(statusPlatform, "]")
for _, payload := range strings.Split(statusPlatform, ",") {
if payload != "" {
userPlatform = append(userPlatform, payload)
}
}
var userLastSeenAt []string
statusLastSeenAt := strings.TrimPrefix(content["lastSeenAt"], "[")
statusLastSeenAt = strings.TrimSuffix(statusLastSeenAt, "]")
for _, payload := range strings.Split(statusLastSeenAt, ",") {
if payload != "" {
userLastSeenAt = append(userLastSeenAt, payload)
}
}
response := &model.ListFriendsPresenceResponse{
BaseResponse: &model.BaseResponse{Code: responseCode, MessageID: content["id"]},
UserID: userIDs,
Availability: userAvailability,
Activity: userActivity,
Platform: userPlatform,
LastSeenAt: userLastSeenAt,
}
return response, nil
}
func unmarshalSetUserStatusResponse(reader *bufio.Reader) (*model.SetUserStatusResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.SetUserStatusResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalNotifyFriendsResponse(reader *bufio.Reader) (*model.FriendsPresenceNotif, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.FriendsPresenceNotif{
UserID: content["userID"],
Availability: content["availability"],
Activity: content["activity"],
Platform: content["platform"],
}
return response, nil
}
func unmarshalStartMatchmakingResponse(reader *bufio.Reader) (*model.StartMatchmakingResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.StartMatchmakingResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
Message: content["message"],
}
return response, nil
}
func unmarshalCancelMatchmakingResponse(reader *bufio.Reader) (*model.CancelMatchmakingResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.CancelMatchmakingResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalMatchmakingNotification(reader *bufio.Reader) (*model.MatchmakingNotification, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
pm := content["partyMember"]
pm = strings.TrimSuffix(pm, "]")
pm = strings.TrimPrefix(pm, "[")
partyMember := strings.Split(pm, ",")
if pm == "" {
partyMember = []string{}
}
cpm := content["counterPartyMember"]
cpm = strings.TrimSuffix(cpm, "]")
cpm = strings.TrimPrefix(cpm, "[")
counterPartyMember := strings.Split(cpm, ",")
if cpm == "" {
counterPartyMember = []string{}
}
d, _ := strconv.Atoi(content["duration"])
response := &model.MatchmakingNotification{
MatchID: content["matchId"],
Status: content["status"],
PartyMemberUserID: partyMember,
CounterPartyMemberUserID: counterPartyMember,
ReadyDuration: d,
Message: content["message"],
}
return response, nil
}
func unmarshalSetReadyConsentResponse(reader *bufio.Reader) (*model.SetReadyConsentResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.SetReadyConsentResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalSetReadyConsentNotif(reader *bufio.Reader) (*model.SetReadyConsentNotif, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
response := &model.SetReadyConsentNotif{
MatchID: content["matchId"],
UserID: content["userId"],
}
return response, nil
}
func unmarshalRematchmakingNotif(reader *bufio.Reader) (*model.RematchmakingNotif, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
banDuration, err := strconv.Atoi(content["banDuration"])
if err != nil {
return nil, err
}
response := &model.RematchmakingNotif{
BanDuration: banDuration,
}
return response, nil
}
func unmarshalRequestFriendsResponse(reader *bufio.Reader) (*model.RequestFriendsResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.RequestFriendsResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalRequestFriendsNotif(reader *bufio.Reader) (*model.RequestFriendsNotif, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
return &model.RequestFriendsNotif{FriendID: content["friendId"]}, nil
}
func unmarshalListIncomingFriendsResponse(reader *bufio.Reader) (*model.ListIncomingFriendsResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
var userIDs []string
userIDsPayload := strings.TrimPrefix(content["friendsId"], "[")
userIDsPayload = strings.TrimSuffix(userIDsPayload, "]")
for _, userID := range strings.Split(userIDsPayload, ",") {
if userID != "" {
userIDs = append(userIDs, userID)
}
}
response := &model.ListIncomingFriendsResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
UserIds: userIDs,
}
return response, nil
}
func unmarshalListOutgoingFriendsResponse(reader *bufio.Reader) (*model.ListOutgoingFriendsResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
var userIDs []string
userIDsPayload := strings.TrimPrefix(content["friendsId"], "[")
userIDsPayload = strings.TrimSuffix(userIDsPayload, "]")
for _, userID := range strings.Split(userIDsPayload, ",") {
if userID != "" {
userIDs = append(userIDs, userID)
}
}
response := &model.ListOutgoingFriendsResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
FriendIds: userIDs,
}
return response, nil
}
func unmarshalAcceptFriendsResponse(reader *bufio.Reader) (*model.AcceptFriendsResponse, error) {
content, err := readAll(reader)
if err != nil {
return nil, err
}
responseCode, err := strconv.Atoi(content["code"])
if err != nil {
return nil, err
}
response := &model.AcceptFriendsResponse{
BaseResponse: &model.BaseResponse{
MessageID: content["id"],
Code: responseCode,
},
}
return response, nil
}
func unmarshalAcceptFriendsNotif(reader *bufio.Reader) (*model.AcceptFriendsNotif, error) {
content, err := readAll(reader)
if err != nil {