44using System . Threading . Tasks ;
55using Microsoft . EntityFrameworkCore ;
66using Orleans ;
7- using Orleans . Runtime ;
87using Turbo . Database . Context ;
98using Turbo . Logging ;
109using Turbo . Primitives ;
1110using Turbo . Primitives . Grains . Players ;
1211using Turbo . Primitives . Orleans ;
1312using Turbo . Primitives . Orleans . Snapshots . Players ;
14- using Turbo . Primitives . Orleans . States . Players ;
1513using Turbo . Primitives . Players ;
14+ using Turbo . Primitives . Rooms . Enums ;
1615
1716namespace Turbo . Players . Grains ;
1817
19- internal sealed class PlayerGrain (
20- [ PersistentState ( OrleansStateNames . PLAYER_STATE , OrleansStorageNames . PLAYER_STORE ) ]
21- IPersistentState < PlayerState > state ,
22- IDbContextFactory < TurboDbContext > dbCtxFactory ,
23- IGrainFactory grainFactory
24- ) : Grain , IPlayerGrain
18+ internal sealed class PlayerGrain : Grain , IPlayerGrain
2519{
26- private readonly IDbContextFactory < TurboDbContext > _dbCtxFactory = dbCtxFactory ;
27- private readonly IGrainFactory _grainFactory = grainFactory ;
20+ private readonly IDbContextFactory < TurboDbContext > _dbCtxFactory ;
21+ private readonly IGrainFactory _grainFactory ;
22+
23+ private readonly PlayerLiveState _state ;
24+
25+ public PlayerGrain ( IDbContextFactory < TurboDbContext > dbCtxFactory , IGrainFactory grainFactory )
26+ {
27+ _dbCtxFactory = dbCtxFactory ;
28+ _grainFactory = grainFactory ;
29+
30+ _state = new ( ) { PlayerId = PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) } ;
31+ }
2832
2933 public override async Task OnActivateAsync ( CancellationToken ct )
3034 {
@@ -36,36 +40,54 @@ public override async Task OnDeactivateAsync(DeactivationReason reason, Cancella
3640 await WriteToDatabaseAsync ( ct ) ;
3741 }
3842
39- private async Task HydrateAsync ( CancellationToken ct )
43+ public async Task SetFigureAsync ( string figure , AvatarGenderType gender , CancellationToken ct )
44+ {
45+ _state . Figure = figure ;
46+ _state . Gender = gender ;
47+
48+ await WriteToDatabaseAsync ( ct ) ;
49+
50+ var playerPresence = _grainFactory . GetPlayerPresenceGrain ( ( int ) this . GetPrimaryKeyLong ( ) ) ;
51+
52+ await playerPresence . OnFigureUpdatedAsync ( await GetSummaryAsync ( ct ) , ct ) ;
53+
54+ await WriteToDatabaseAsync ( ct ) ;
55+ }
56+
57+ public async Task SetMottoAsync ( string text , CancellationToken ct )
4058 {
41- if ( state . State . IsLoaded )
42- return ;
59+ _state . Motto = text ;
60+
61+ await WriteToDatabaseAsync ( ct ) ;
62+
63+ var playerPresence = _grainFactory . GetPlayerPresenceGrain ( ( int ) this . GetPrimaryKeyLong ( ) ) ;
64+
65+ await playerPresence . OnPlayerUpdatedAsync ( await GetSummaryAsync ( ct ) , ct ) ;
66+
67+ await WriteToDatabaseAsync ( ct ) ;
68+ }
4369
70+ private async Task HydrateAsync ( CancellationToken ct )
71+ {
4472 await using var dbCtx = await _dbCtxFactory . CreateDbContextAsync ( ct ) ;
4573
4674 var entity =
4775 await dbCtx
4876 . Players . AsNoTracking ( )
49- . SingleOrDefaultAsync ( x => x . Id == ( int ) this . GetPrimaryKeyLong ( ) , ct )
77+ . SingleOrDefaultAsync ( x => x . Id == ( int ) _state . PlayerId , ct )
5078 ?? throw new TurboException ( TurboErrorCodeEnum . PlayerNotFound ) ;
5179
52- state . State . Name = entity . Name ?? string . Empty ;
53- state . State . Motto = entity . Motto ?? string . Empty ;
54- state . State . Figure = entity . Figure ?? string . Empty ;
55- state . State . Gender = entity . Gender ;
56- state . State . CreatedAt = entity . CreatedAt ;
57- state . State . LastUpdated = DateTime . UtcNow ;
58- state . State . IsLoaded = true ;
80+ _state . Name = entity . Name ;
81+ _state . Motto = entity . Motto ?? string . Empty ;
82+ _state . Figure = entity . Figure ;
83+ _state . Gender = entity . Gender ;
84+ _state . AchievementScore = 0 ;
85+ _state . CreatedAt = entity . CreatedAt ;
86+ _state . LastUpdated = entity . UpdatedAt ;
5987
6088 await _grainFactory
6189 . GetPlayerDirectoryGrain ( )
62- . SetPlayerNameAsync (
63- PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) ,
64- state . State . Name ,
65- ct
66- ) ;
67-
68- await state . WriteStateAsync ( ct ) ;
90+ . SetPlayerNameAsync ( PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) , _state . Name , ct ) ;
6991 }
7092
7193 private async Task WriteToDatabaseAsync ( CancellationToken ct )
@@ -75,7 +97,7 @@ private async Task WriteToDatabaseAsync(CancellationToken ct)
7597 var snapshot = await GetSummaryAsync ( ct ) ;
7698
7799 await dbCtx
78- . Players . Where ( p => p . Id == ( int ) this . GetPrimaryKeyLong ( ) )
100+ . Players . Where ( x => x . Id == ( int ) _state . PlayerId )
79101 . ExecuteUpdateAsync (
80102 up =>
81103 up . SetProperty ( p => p . Name , snapshot . Name )
@@ -84,33 +106,35 @@ await dbCtx
84106 . SetProperty ( p => p . Gender , snapshot . Gender ) ,
85107 ct
86108 ) ;
109+
110+ _state . LastUpdated = DateTime . Now ;
87111 }
88112
89113 public Task < PlayerSummarySnapshot > GetSummaryAsync ( CancellationToken ct ) =>
90114 Task . FromResult (
91115 new PlayerSummarySnapshot
92116 {
93- PlayerId = PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) ,
94- Name = state . State . Name ,
95- Motto = state . State . Motto ,
96- Figure = state . State . Figure ,
97- Gender = state . State . Gender ,
98- CreatedAt = state . State . CreatedAt ,
117+ PlayerId = _state . PlayerId ,
118+ Name = _state . Name ,
119+ Motto = _state . Motto ,
120+ Figure = _state . Figure ,
121+ Gender = _state . Gender ,
122+ AchievementScore = _state . AchievementScore ,
123+ CreatedAt = _state . CreatedAt ,
99124 }
100125 ) ;
101126
102127 public Task < PlayerExtendedProfileSnapshot > GetExtendedProfileSnapshotAsync ( CancellationToken ct )
103128 {
104- var s = state . State ;
105129 return Task . FromResult (
106130 new PlayerExtendedProfileSnapshot
107131 {
108- UserId = PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) ,
109- UserName = s . Name ,
110- Figure = s . Figure ,
111- Motto = s . Motto ,
112- CreationDate = s . CreatedAt . ToString ( "yyyy-MM-dd" ) ,
113- AchievementScore = 0 ,
132+ UserId = _state . PlayerId ,
133+ UserName = _state . Name ,
134+ Figure = _state . Figure ,
135+ Motto = _state . Motto ,
136+ CreationDate = _state . CreatedAt . ToString ( "yyyy-MM-dd" ) ,
137+ AchievementScore = _state . AchievementScore ,
114138 FriendCount = 0 ,
115139 IsFriend = false ,
116140 IsFriendRequestSent = false ,
0 commit comments