forked from ppy/osu-server-spectator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiplayerHub.cs
937 lines (731 loc) · 38.7 KB
/
MultiplayerHub.cs
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
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using osu.Framework.Logging;
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Multiplayer.Countdown;
using osu.Game.Online.Rooms;
using osu.Server.Spectator.Database;
using osu.Server.Spectator.Database.Models;
using osu.Server.Spectator.Entities;
using osu.Server.Spectator.Extensions;
namespace osu.Server.Spectator.Hubs.Multiplayer
{
public class MultiplayerHub : StatefulUserHub<IMultiplayerClient, MultiplayerClientState>, IMultiplayerServer
{
protected readonly EntityStore<ServerMultiplayerRoom> Rooms;
protected readonly MultiplayerHubContext HubContext;
private readonly IDatabaseFactory databaseFactory;
public MultiplayerHub(IDistributedCache cache, EntityStore<ServerMultiplayerRoom> rooms, EntityStore<MultiplayerClientState> users, IDatabaseFactory databaseFactory,
IHubContext<MultiplayerHub> hubContext)
: base(cache, users)
{
Rooms = rooms;
this.databaseFactory = databaseFactory;
HubContext = new MultiplayerHubContext(hubContext, rooms, users);
}
public Task<MultiplayerRoom> JoinRoom(long roomId) => JoinRoomWithPassword(roomId, string.Empty);
public async Task<MultiplayerRoom> JoinRoomWithPassword(long roomId, string password)
{
Log($"Attempting to join room {roomId}");
bool isRestricted;
using (var db = databaseFactory.GetInstance())
isRestricted = await db.IsUserRestrictedAsync(Context.GetUserId());
if (isRestricted)
throw new InvalidStateException("Can't join a room when restricted.");
using (var userUsage = await GetOrCreateLocalUserState())
{
if (userUsage.Item != null)
{
// if the user already has a state, it means they are already in a room and can't join another without first leaving.
throw new InvalidStateException("Can't join a room when already in another room.");
}
// add the user to the room.
var roomUser = new MultiplayerRoomUser(Context.GetUserId());
// track whether this join necessitated starting the process of fetching the room and adding it to the room store.
bool newRoomFetchStarted = false;
ServerMultiplayerRoom? room = null;
using (var roomUsage = await Rooms.GetForUse(roomId, true))
{
try
{
if (roomUsage.Item == null)
{
newRoomFetchStarted = true;
// the requested room is not yet tracked by this server.
room = await retrieveRoom(roomId);
if (!string.IsNullOrEmpty(room.Settings.Password))
{
if (room.Settings.Password != password)
throw new InvalidPasswordException();
}
// the above call will only succeed if this user is the host.
room.Host = roomUser;
// mark the room active - and wait for confirmation of this operation from the database - before adding the user to the room.
await markRoomActive(room);
roomUsage.Item = room;
}
else
{
room = roomUsage.Item;
// this is a sanity check to keep *rooms* in a good state.
// in theory the connection clean-up code should handle this correctly.
if (room.Users.Any(u => u.UserID == roomUser.UserID))
throw new InvalidOperationException($"User {roomUser.UserID} attempted to join room {room.RoomID} they are already present in.");
}
userUsage.Item = new MultiplayerClientState(Context.ConnectionId, Context.GetUserId(), roomId);
// because match type implementations may send subsequent information via Users collection hooks,
// inform clients before adding user to the room.
await Clients.Group(GetGroupId(roomId)).UserJoined(roomUser);
room.AddUser(roomUser);
room.UpdateForRetrieval();
await addDatabaseUser(room, roomUser);
await Groups.AddToGroupAsync(Context.ConnectionId, GetGroupId(roomId));
Log(room, "User joined");
}
catch
{
try
{
if (userUsage.Item != null)
{
// the user was joined to the room, so we can run the standard leaveRoom method.
// this will handle closing the room if this was the only user.
await leaveRoom(userUsage.Item, roomUsage, false);
}
else if (newRoomFetchStarted)
{
if (room != null)
{
// the room was retrieved and associated to the usage, but something failed before the user (host) could join.
// for now, let's mark the room as ended if this happens.
await endDatabaseMatch(room);
}
roomUsage.Destroy();
}
}
finally
{
// no matter how we end up cleaning up the room, ensure the user's context is destroyed.
userUsage.Destroy();
}
throw;
}
}
var settings = new JsonSerializerSettings
{
// explicitly use Auto here as we are not interested in the top level type being conveyed to the user.
TypeNameHandling = TypeNameHandling.Auto,
};
return JsonConvert.DeserializeObject<MultiplayerRoom>(JsonConvert.SerializeObject(room, settings), settings)
?? throw new InvalidOperationException();
}
}
/// <summary>
/// Attempt to retrieve and construct a room from the database backend, based on a room ID specification.
/// This will check the database backing to ensure things are in a consistent state.
/// It should only be called by the room's host, before any other user has joined (and will throw if not).
/// </summary>
/// <param name="roomId">The proposed room ID.</param>
/// <exception cref="InvalidOperationException">If anything is wrong with this request.</exception>
private async Task<ServerMultiplayerRoom> retrieveRoom(long roomId)
{
Log($"Retrieving room {roomId} from database");
using (var db = databaseFactory.GetInstance())
{
// TODO: this call should be transactional, and mark the room as managed by this server instance.
// This will allow for other instances to know not to reinitialise the room if the host arrives there.
// Alternatively, we can move lobby retrieval away from osu-web and not require this in the first place.
// Needs further discussion and consideration either way.
var databaseRoom = await db.GetRoomAsync(roomId);
if (databaseRoom == null)
throw new InvalidOperationException("Specified match does not exist.");
if (databaseRoom.ends_at != null && databaseRoom.ends_at < DateTimeOffset.Now)
throw new InvalidStateException("Match has already ended.");
if (databaseRoom.user_id != Context.GetUserId())
throw new InvalidOperationException("Non-host is attempting to join match before host");
var room = new ServerMultiplayerRoom(roomId, HubContext)
{
Settings = new MultiplayerRoomSettings
{
Name = databaseRoom.name,
Password = databaseRoom.password,
MatchType = databaseRoom.type.ToMatchType(),
QueueMode = databaseRoom.queue_mode.ToQueueMode(),
AutoStartDuration = TimeSpan.FromSeconds(databaseRoom.auto_start_duration),
AutoSkip = databaseRoom.auto_skip
}
};
await room.Initialise(databaseFactory);
return room;
}
}
/// <summary>
/// Marks a room active at the database, implying the host has joined and this server is now in control of the room's lifetime.
/// </summary>
private async Task markRoomActive(ServerMultiplayerRoom room)
{
Log(room, "Host marking room active");
using (var db = databaseFactory.GetInstance())
await db.MarkRoomActiveAsync(room);
}
public async Task LeaveRoom()
{
Log("Requesting to leave room");
using (var userUsage = await GetOrCreateLocalUserState())
{
if (userUsage.Item == null)
return;
try
{
await leaveRoom(userUsage.Item, false);
}
finally
{
userUsage.Destroy();
}
}
}
public async Task InvitePlayer(int userId)
{
using (var db = databaseFactory.GetInstance())
{
bool isRestricted = await db.IsUserRestrictedAsync(userId);
if (isRestricted)
throw new InvalidStateException("Can't invite a restricted user to a room.");
var relation = await db.GetUserRelation(Context.GetUserId(), userId);
// The local user has the player they are trying to invite blocked.
if (relation?.foe == true)
throw new UserBlockedException();
var inverseRelation = await db.GetUserRelation(userId, Context.GetUserId());
// The player being invited has the local user blocked.
if (inverseRelation?.foe == true)
throw new UserBlockedException();
// The player being invited disallows unsolicited PMs and the local user is not their friend.
if (inverseRelation?.friend != true && !await db.GetUserAllowsPMs(userId))
throw new UserBlocksPMsException();
}
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var user = userUsage.Item;
var room = roomUsage.Item;
if (user == null)
throw new InvalidStateException("Local user was not found in the expected room");
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
await Clients.User(userId.ToString()).Invited(user.UserId, room.RoomID, room.Settings.Password);
}
}
public async Task TransferHost(int userId)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
Log(room, $"Transferring host from {room.Host?.UserID} to {userId}");
ensureIsHost(room);
var newHost = room.Users.FirstOrDefault(u => u.UserID == userId);
if (newHost == null)
throw new Exception("Target user is not in the current room");
await setNewHost(room, newHost);
}
}
public async Task KickUser(int userId)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
Log(room, $"Kicking user {userId}");
if (userId == userUsage.Item?.UserId)
throw new InvalidStateException("Can't kick self");
ensureIsHost(room);
var kickTarget = room.Users.FirstOrDefault(u => u.UserID == userId);
if (kickTarget == null)
throw new InvalidOperationException("Target user is not in the current room");
using (var targetUserUsage = await GetStateFromUser(kickTarget.UserID))
{
if (targetUserUsage.Item == null)
throw new InvalidOperationException();
try
{
await leaveRoom(targetUserUsage.Item, roomUsage, true);
}
finally
{
targetUserUsage.Destroy();
}
}
}
}
public async Task ChangeState(MultiplayerUserState newState)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidStateException("Local user was not found in the expected room");
if (user.State == newState)
return;
// There's a potential that a client attempts to change state while a message from the server is in transit. Silently block these changes rather than informing the client.
switch (newState)
{
// If a client triggered `Idle` (ie. un-readying) before they received the `WaitingForLoad` message from the match starting.
case MultiplayerUserState.Idle:
if (isGameplayState(user.State))
return;
break;
// If a client a triggered gameplay state before they received the `Idle` message from their gameplay being aborted.
case MultiplayerUserState.Loaded:
case MultiplayerUserState.ReadyForGameplay:
if (!isGameplayState(user.State))
return;
break;
}
Log(room, $"User changing state from {user.State} to {newState}");
ensureValidStateSwitch(room, user.State, newState);
await HubContext.ChangeAndBroadcastUserState(room, user, newState);
// Signal newly-spectating users to load gameplay if currently in the middle of play.
if (newState == MultiplayerUserState.Spectating
&& (room.State == MultiplayerRoomState.WaitingForLoad || room.State == MultiplayerRoomState.Playing))
{
await Clients.Caller.LoadRequested();
}
await updateRoomStateIfRequired(room);
}
}
public async Task ChangeBeatmapAvailability(BeatmapAvailability newBeatmapAvailability)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
await HubContext.ChangeAndBroadcastUserBeatmapAvailability(room, user, newBeatmapAvailability);
}
}
public async Task ChangeUserMods(IEnumerable<APIMod> newMods)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
await HubContext.ChangeUserMods(newMods, room, user);
}
}
public async Task SendMatchRequest(MatchUserRequest request)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
switch (request)
{
case StartMatchCountdownRequest startMatchCountdownRequest:
ensureIsHost(room);
if (room.State != MultiplayerRoomState.Open)
throw new InvalidStateException("Cannot start a countdown during ongoing play.");
if (room.Settings.AutoStartEnabled)
throw new InvalidStateException("Cannot start manual countdown if auto-start is enabled.");
await room.StartCountdown(new MatchStartCountdown { TimeRemaining = startMatchCountdownRequest.Duration }, HubContext.StartMatch);
break;
case StopCountdownRequest stopCountdownRequest:
ensureIsHost(room);
MultiplayerCountdown? countdown = room.FindCountdownById(stopCountdownRequest.ID);
if (countdown == null)
break;
switch (countdown)
{
case MatchStartCountdown when room.Settings.AutoStartEnabled:
case ForceGameplayStartCountdown:
case ServerShuttingDownCountdown:
throw new InvalidStateException("Cannot stop the requested countdown.");
}
await room.StopCountdown(countdown);
break;
default:
room.MatchTypeImplementation.HandleUserRequest(user, request);
break;
}
}
}
public async Task StartMatch()
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
ensureIsHost(room);
if (room.Host != null && room.Host.State != MultiplayerUserState.Spectating && room.Host.State != MultiplayerUserState.Ready)
throw new InvalidStateException("Can't start match when the host is not ready.");
if (room.Users.All(u => u.State != MultiplayerUserState.Ready))
throw new InvalidStateException("Can't start match when no users are ready.");
await HubContext.StartMatch(room);
}
}
public async Task AbortMatch()
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
ensureIsHost(room);
if (room.State != MultiplayerRoomState.WaitingForLoad && room.State != MultiplayerRoomState.Playing)
throw new InvalidStateException("Cannot abort a match that hasn't started.");
foreach (var user in room.Users)
await HubContext.ChangeAndBroadcastUserState(room, user, MultiplayerUserState.Idle);
await Clients.Group(GetGroupId(room.RoomID)).GameplayAborted(GameplayAbortReason.HostAbortedTheMatch);
await updateRoomStateIfRequired(room);
}
}
public async Task AbortGameplay()
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
if (!isGameplayState(user.State))
throw new InvalidStateException("Cannot abort gameplay while not in a gameplay state");
await HubContext.ChangeAndBroadcastUserState(room, user, MultiplayerUserState.Idle);
await updateRoomStateIfRequired(room);
}
}
public async Task AddPlaylistItem(MultiplayerPlaylistItem item)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
Log(room, $"Adding playlist item for beatmap {item.BeatmapID}");
await room.Queue.AddItem(item, user);
Log(room, $"Item ID {item.ID} added at slot {room.Queue.UpcomingItems.TakeWhile(i => i != item).Count() + 1} (of {room.Playlist.Count})");
await updateRoomStateIfRequired(room);
}
}
public async Task EditPlaylistItem(MultiplayerPlaylistItem item)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
Log(room, $"Editing playlist item {item.ID} for beatmap {item.BeatmapID}");
await room.Queue.EditItem(item, user);
}
}
public async Task RemovePlaylistItem(long playlistItemId)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
var user = room.Users.FirstOrDefault(u => u.UserID == Context.GetUserId());
if (user == null)
throw new InvalidOperationException("Local user was not found in the expected room");
Log(room, $"Removing playlist item {playlistItemId}");
await room.Queue.RemoveItem(playlistItemId, user);
await updateRoomStateIfRequired(room);
}
}
public async Task ChangeSettings(MultiplayerRoomSettings settings)
{
using (var userUsage = await GetOrCreateLocalUserState())
using (var roomUsage = await getLocalUserRoom(userUsage.Item))
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
if (room.State != MultiplayerRoomState.Open)
throw new InvalidStateException("Attempted to change settings while game is active");
ensureIsHost(room);
Log(room, "Settings updating");
// Server is authoritative over the playlist item ID.
// Todo: This needs to change for tournament mode.
settings.PlaylistItemId = room.Settings.PlaylistItemId;
if (room.Settings.Equals(settings))
return;
var previousSettings = room.Settings;
if (settings.MatchType == MatchType.Playlists)
throw new InvalidStateException("Invalid match type selected");
try
{
room.Settings = settings;
await updateDatabaseSettings(room);
}
catch
{
// rollback settings if an error occurred when updating the database.
room.Settings = previousSettings;
throw;
}
if (previousSettings.MatchType != settings.MatchType)
{
room.ChangeMatchType(settings.MatchType);
Log(room, $"Switching room ruleset to {room.MatchTypeImplementation}");
}
if (previousSettings.QueueMode != settings.QueueMode)
{
await room.Queue.UpdateFromQueueModeChange();
Log(room, $"Switching queue mode to {settings.QueueMode}");
}
await HubContext.NotifySettingsChanged(room, false);
await updateRoomStateIfRequired(room);
}
}
/// <summary>
/// Get the group ID to be used for multiplayer messaging.
/// </summary>
/// <param name="roomId">The databased room ID.</param>
public static string GetGroupId(long roomId) => $"room:{roomId}";
private async Task updateDatabaseSettings(MultiplayerRoom room)
{
var playlistItem = room.Playlist.FirstOrDefault(item => item.ID == room.Settings.PlaylistItemId);
if (playlistItem == null)
throw new InvalidStateException("Attempted to select a playlist item not contained by the room.");
using (var db = databaseFactory.GetInstance())
await db.UpdateRoomSettingsAsync(room);
}
private async Task updateDatabaseHost(MultiplayerRoom room)
{
using (var db = databaseFactory.GetInstance())
await db.UpdateRoomHostAsync(room);
}
private async Task endDatabaseMatch(MultiplayerRoom room)
{
using (var db = databaseFactory.GetInstance())
await db.EndMatchAsync(room);
}
private async Task addDatabaseUser(MultiplayerRoom room, MultiplayerRoomUser user)
{
using (var db = databaseFactory.GetInstance())
await db.AddRoomParticipantAsync(room, user);
}
private async Task removeDatabaseUser(MultiplayerRoom room, MultiplayerRoomUser user)
{
using (var db = databaseFactory.GetInstance())
await db.RemoveRoomParticipantAsync(room, user);
}
protected override async Task CleanUpState(MultiplayerClientState state)
{
await leaveRoom(state, true);
await base.CleanUpState(state);
}
private async Task setNewHost(MultiplayerRoom room, MultiplayerRoomUser newHost)
{
room.Host = newHost;
await Clients.Group(GetGroupId(room.RoomID)).HostChanged(newHost.UserID);
await updateDatabaseHost(room);
}
/// <summary>
/// Should be called when user states change, to check whether the new overall room state can trigger a room-level state change.
/// </summary>
private async Task updateRoomStateIfRequired(ServerMultiplayerRoom room)
{
//check whether a room state change is required.
switch (room.State)
{
case MultiplayerRoomState.Open:
if (room.Settings.AutoStartEnabled)
{
bool shouldHaveCountdown = !room.Queue.CurrentItem.Expired && room.Users.Any(u => u.State == MultiplayerUserState.Ready);
if (shouldHaveCountdown && !room.ActiveCountdowns.Any(c => c is MatchStartCountdown))
await room.StartCountdown(new MatchStartCountdown { TimeRemaining = room.Settings.AutoStartDuration }, HubContext.StartMatch);
}
break;
case MultiplayerRoomState.WaitingForLoad:
int countGameplayUsers = room.Users.Count(u => isGameplayState(u.State));
int countReadyUsers = room.Users.Count(u => u.State == MultiplayerUserState.ReadyForGameplay);
// Attempt to start gameplay when no more users need to change states. If all users have aborted, this will abort the match.
if (countReadyUsers == countGameplayUsers)
await HubContext.StartOrStopGameplay(room);
break;
case MultiplayerRoomState.Playing:
if (room.Users.All(u => u.State != MultiplayerUserState.Playing))
{
foreach (var u in room.Users.Where(u => u.State == MultiplayerUserState.FinishedPlay))
await HubContext.ChangeAndBroadcastUserState(room, u, MultiplayerUserState.Results);
await HubContext.ChangeRoomState(room, MultiplayerRoomState.Open);
await Clients.Group(GetGroupId(room.RoomID)).ResultsReady();
await room.Queue.FinishCurrentItem();
}
break;
}
}
/// <summary>
/// Given a room and a state transition, throw if there's an issue with the sequence of events.
/// </summary>
/// <param name="room">The room.</param>
/// <param name="oldState">The old state.</param>
/// <param name="newState">The new state.</param>
private void ensureValidStateSwitch(ServerMultiplayerRoom room, MultiplayerUserState oldState, MultiplayerUserState newState)
{
switch (newState)
{
case MultiplayerUserState.Idle:
if (isGameplayState(oldState))
throw new InvalidStateException("Cannot return to idle without aborting gameplay.");
// any non-gameplay state can return to idle.
break;
case MultiplayerUserState.Ready:
if (oldState != MultiplayerUserState.Idle)
throw new InvalidStateChangeException(oldState, newState);
if (room.Queue.CurrentItem.Expired)
throw new InvalidStateException("Cannot ready up while all items have been played.");
break;
case MultiplayerUserState.WaitingForLoad:
// state is managed by the server.
throw new InvalidStateChangeException(oldState, newState);
case MultiplayerUserState.Loaded:
if (oldState != MultiplayerUserState.WaitingForLoad)
throw new InvalidStateChangeException(oldState, newState);
break;
case MultiplayerUserState.ReadyForGameplay:
if (oldState != MultiplayerUserState.Loaded)
throw new InvalidStateChangeException(oldState, newState);
break;
case MultiplayerUserState.Playing:
// state is managed by the server.
throw new InvalidStateChangeException(oldState, newState);
case MultiplayerUserState.FinishedPlay:
if (oldState != MultiplayerUserState.Playing)
throw new InvalidStateChangeException(oldState, newState);
break;
case MultiplayerUserState.Results:
// state is managed by the server.
throw new InvalidStateChangeException(oldState, newState);
case MultiplayerUserState.Spectating:
if (oldState != MultiplayerUserState.Idle && oldState != MultiplayerUserState.Ready)
throw new InvalidStateChangeException(oldState, newState);
break;
default:
throw new ArgumentOutOfRangeException(nameof(newState), newState, null);
}
}
private static bool isGameplayState(MultiplayerUserState state)
{
switch (state)
{
default:
return false;
case MultiplayerUserState.WaitingForLoad:
case MultiplayerUserState.Loaded:
case MultiplayerUserState.ReadyForGameplay:
case MultiplayerUserState.Playing:
return true;
}
}
/// <summary>
/// Ensure the local user is the host of the room, and throw if they are not.
/// </summary>
private void ensureIsHost(MultiplayerRoom room)
{
if (room.Host?.UserID != Context.GetUserId())
throw new NotHostException();
}
/// <summary>
/// Retrieve the <see cref="MultiplayerRoom"/> for the local context user.
/// </summary>
private async Task<ItemUsage<ServerMultiplayerRoom>> getLocalUserRoom(MultiplayerClientState? state)
{
if (state == null)
throw new NotJoinedRoomException();
long roomId = state.CurrentRoomID;
return await Rooms.GetForUse(roomId);
}
private async Task leaveRoom(MultiplayerClientState state, bool wasKick)
{
using (var roomUsage = await getLocalUserRoom(state))
await leaveRoom(state, roomUsage, wasKick);
}
private async Task leaveRoom(MultiplayerClientState state, ItemUsage<ServerMultiplayerRoom> roomUsage, bool wasKick)
{
var room = roomUsage.Item;
if (room == null)
throw new InvalidOperationException("Attempted to operate on a null room");
Log(room, wasKick ? "User kicked" : "User left");
await Groups.RemoveFromGroupAsync(state.ConnectionId, GetGroupId(room.RoomID));
var user = room.Users.FirstOrDefault(u => u.UserID == state.UserId);
if (user == null)
throw new InvalidStateException("User was not in the expected room.");
room.RemoveUser(user);
await removeDatabaseUser(room, user);
// handle closing the room if the only participant is the user which is leaving.
if (room.Users.Count == 0)
{
await endDatabaseMatch(room);
// only destroy the usage after the database operation succeeds.
Log(room, "Stopping tracking of room (all users left).");
roomUsage.Destroy();
return;
}
await updateRoomStateIfRequired(room);
// if this user was the host, we need to arbitrarily transfer host so the room can continue to exist.
if (room.Host?.Equals(user) == true)
{
// there *has* to still be at least one user in the room (see user check above).
var newHost = room.Users.First();
await setNewHost(room, newHost);
}
if (wasKick)
{
// the target user has already been removed from the group, so send the message to them separately.
await Clients.Client(state.ConnectionId).UserKicked(user);
await Clients.Group(GetGroupId(room.RoomID)).UserKicked(user);
}
else
await Clients.Group(GetGroupId(room.RoomID)).UserLeft(user);
}
internal Task<ItemUsage<ServerMultiplayerRoom>> GetRoom(long roomId) => Rooms.GetForUse(roomId);
protected void Log(ServerMultiplayerRoom room, string message, LogLevel logLevel = LogLevel.Verbose) => base.Log($"[room:{room.RoomID}] {message}", logLevel);
}
}