|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Orleans; |
| 5 | +using Orleans.Runtime; |
| 6 | +using Turbo.Primitives.Action; |
| 7 | +using Turbo.Primitives.Networking; |
| 8 | +using Turbo.Primitives.Orleans; |
| 9 | +using Turbo.Primitives.Orleans.Snapshots.Room; |
| 10 | +using Turbo.Primitives.Players; |
| 11 | +using Turbo.Primitives.Rooms; |
| 12 | +using Turbo.Primitives.Rooms.Snapshots; |
| 13 | + |
| 14 | +namespace Turbo.Players.Grains; |
| 15 | + |
| 16 | +internal sealed partial class PlayerPresenceGrain |
| 17 | +{ |
| 18 | + public Task<RoomPointerSnapshot> GetActiveRoomAsync() => |
| 19 | + Task.FromResult( |
| 20 | + new RoomPointerSnapshot |
| 21 | + { |
| 22 | + RoomId = _state.ActiveRoomId, |
| 23 | + ActiveSinceUtc = _state.ActiveRoomSinceUtc, |
| 24 | + } |
| 25 | + ); |
| 26 | + |
| 27 | + public Task<RoomPendingSnapshot> GetPendingRoomAsync() => |
| 28 | + Task.FromResult( |
| 29 | + new RoomPendingSnapshot |
| 30 | + { |
| 31 | + RoomId = _state.PendingRoomId, |
| 32 | + Approved = _state.PendingRoomApproved, |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + public async Task SetActiveRoomAsync(RoomId roomId, CancellationToken ct) |
| 37 | + { |
| 38 | + if (roomId <= 0) |
| 39 | + return; |
| 40 | + |
| 41 | + await ClearActiveRoomAsync(ct); |
| 42 | + |
| 43 | + var next = roomId; |
| 44 | + |
| 45 | + _state.ActiveRoomId = next; |
| 46 | + _state.PendingRoomId = -1; |
| 47 | + _state.PendingRoomApproved = false; |
| 48 | + _state.ActiveRoomSinceUtc = DateTime.UtcNow; |
| 49 | + |
| 50 | + await _grainFactory |
| 51 | + .GetRoomDirectoryGrain() |
| 52 | + .AddPlayerToRoomAsync((int)this.GetPrimaryKeyLong(), next, ct); |
| 53 | + |
| 54 | + var provider = this.GetStreamProvider(OrleansStreamProviders.ROOM_STREAM_PROVIDER); |
| 55 | + var streamId = StreamId.Create(OrleansStreamNames.ROOM_STREAM, roomId.Value); |
| 56 | + var stream = provider.GetStream<RoomOutbound>(streamId); |
| 57 | + |
| 58 | + _roomOutboundSub = await stream.SubscribeAsync(this); |
| 59 | + |
| 60 | + var room = _grainFactory.GetRoomGrain(roomId); |
| 61 | + |
| 62 | + var playerSnapshot = await _grainFactory |
| 63 | + .GetPlayerGrain((PlayerId)this.GetPrimaryKeyLong()) |
| 64 | + .GetSummaryAsync(ct); |
| 65 | + |
| 66 | + var ctx = new ActionContext |
| 67 | + { |
| 68 | + Origin = ActionOrigin.Player, |
| 69 | + SessionKey = SessionKey.Invalid, |
| 70 | + PlayerId = (PlayerId)this.GetPrimaryKeyLong(), |
| 71 | + RoomId = roomId, |
| 72 | + }; |
| 73 | + |
| 74 | + await room.CreateAvatarFromPlayerAsync(ctx, playerSnapshot, ct); |
| 75 | + } |
| 76 | + |
| 77 | + public async Task ClearActiveRoomAsync(CancellationToken ct) |
| 78 | + { |
| 79 | + if (_state.ActiveRoomId <= 0) |
| 80 | + return; |
| 81 | + |
| 82 | + var prev = _state.ActiveRoomId; |
| 83 | + |
| 84 | + _state.ActiveRoomId = -1; |
| 85 | + _state.ActiveRoomSinceUtc = DateTime.UtcNow; |
| 86 | + |
| 87 | + var ctx = new ActionContext |
| 88 | + { |
| 89 | + Origin = ActionOrigin.Player, |
| 90 | + SessionKey = SessionKey.Invalid, |
| 91 | + PlayerId = PlayerId.Parse((int)this.GetPrimaryKeyLong()), |
| 92 | + RoomId = prev, |
| 93 | + }; |
| 94 | + |
| 95 | + var roomGrain = _grainFactory.GetRoomGrain(prev); |
| 96 | + |
| 97 | + await roomGrain.RemoveAvatarFromPlayerAsync(ctx, ctx.PlayerId, ct); |
| 98 | + |
| 99 | + await _grainFactory |
| 100 | + .GetRoomDirectoryGrain() |
| 101 | + .RemovePlayerFromRoomAsync((PlayerId)this.GetPrimaryKeyLong(), prev, ct); |
| 102 | + |
| 103 | + if (_roomOutboundSub is not null) |
| 104 | + { |
| 105 | + await _roomOutboundSub.UnsubscribeAsync(); |
| 106 | + |
| 107 | + _roomOutboundSub = null; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public async Task SetPendingRoomAsync(RoomId roomId, bool approved) |
| 112 | + { |
| 113 | + _state.PendingRoomId = roomId; |
| 114 | + _state.PendingRoomApproved = approved; |
| 115 | + } |
| 116 | +} |
0 commit comments