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 ;
1614using Turbo . Primitives . Rooms . Enums ;
1715
1816namespace Turbo . Players . Grains ;
1917
20- internal sealed class PlayerGrain (
21- [ PersistentState ( OrleansStateNames . PLAYER_STATE , OrleansStorageNames . PLAYER_STORE ) ]
22- IPersistentState < PlayerState > state ,
23- IDbContextFactory < TurboDbContext > dbCtxFactory ,
24- IGrainFactory grainFactory
25- ) : Grain , IPlayerGrain
18+ internal sealed class PlayerGrain : Grain , IPlayerGrain
2619{
27- private readonly IDbContextFactory < TurboDbContext > _dbCtxFactory = dbCtxFactory ;
28- 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+ }
2932
3033 public override async Task OnActivateAsync ( CancellationToken ct )
3134 {
@@ -39,68 +42,52 @@ public override async Task OnDeactivateAsync(DeactivationReason reason, Cancella
3942
4043 public async Task SetFigureAsync ( string figure , AvatarGenderType gender , CancellationToken ct )
4144 {
42- state . State . Figure = figure ;
43- state . State . Gender = gender ;
44- state . State . LastUpdated = DateTime . UtcNow ;
45-
46- await state . WriteStateAsync ( ct ) ;
45+ _state . Figure = figure ;
46+ _state . Gender = gender ;
4747
48- var summary = await GetSummaryAsync ( ct ) ;
48+ await WriteToDatabaseAsync ( ct ) ;
4949
5050 var playerPresence = _grainFactory . GetPlayerPresenceGrain ( ( int ) this . GetPrimaryKeyLong ( ) ) ;
5151
52- await playerPresence . OnFigureUpdatedAsync ( summary , ct ) ;
52+ await playerPresence . OnFigureUpdatedAsync ( await GetSummaryAsync ( ct ) , ct ) ;
5353
5454 await WriteToDatabaseAsync ( ct ) ;
5555 }
5656
5757 public async Task SetMottoAsync ( string text , CancellationToken ct )
5858 {
59- state . State . Motto = text ;
60- state . State . LastUpdated = DateTime . UtcNow ;
59+ _state . Motto = text ;
6160
62- await state . WriteStateAsync ( ct ) ;
63-
64- var summary = await GetSummaryAsync ( ct ) ;
61+ await WriteToDatabaseAsync ( ct ) ;
6562
6663 var playerPresence = _grainFactory . GetPlayerPresenceGrain ( ( int ) this . GetPrimaryKeyLong ( ) ) ;
6764
68- await playerPresence . OnPlayerUpdatedAsync ( summary , ct ) ;
65+ await playerPresence . OnPlayerUpdatedAsync ( await GetSummaryAsync ( ct ) , ct ) ;
6966
7067 await WriteToDatabaseAsync ( ct ) ;
7168 }
7269
7370 private async Task HydrateAsync ( CancellationToken ct )
7471 {
75- if ( state . State . IsLoaded )
76- return ;
77-
7872 await using var dbCtx = await _dbCtxFactory . CreateDbContextAsync ( ct ) ;
7973
8074 var entity =
8175 await dbCtx
8276 . Players . AsNoTracking ( )
83- . SingleOrDefaultAsync ( x => x . Id == ( int ) this . GetPrimaryKeyLong ( ) , ct )
77+ . SingleOrDefaultAsync ( x => x . Id == ( int ) _state . PlayerId , ct )
8478 ?? throw new TurboException ( TurboErrorCodeEnum . PlayerNotFound ) ;
8579
86- state . State . Name = entity . Name ?? string . Empty ;
87- state . State . Motto = entity . Motto ?? string . Empty ;
88- state . State . Figure = entity . Figure ?? string . Empty ;
89- state . State . Gender = entity . Gender ;
90- state . State . AchievementScore = 0 ;
91- state . State . CreatedAt = entity . CreatedAt ;
92- state . State . LastUpdated = DateTime . UtcNow ;
93- 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 ;
9487
9588 await _grainFactory
9689 . GetPlayerDirectoryGrain ( )
97- . SetPlayerNameAsync (
98- PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) ,
99- state . State . Name ,
100- ct
101- ) ;
102-
103- await state . WriteStateAsync ( ct ) ;
90+ . SetPlayerNameAsync ( PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) , _state . Name , ct ) ;
10491 }
10592
10693 private async Task WriteToDatabaseAsync ( CancellationToken ct )
@@ -110,7 +97,7 @@ private async Task WriteToDatabaseAsync(CancellationToken ct)
11097 var snapshot = await GetSummaryAsync ( ct ) ;
11198
11299 await dbCtx
113- . Players . Where ( p => p . Id == ( int ) this . GetPrimaryKeyLong ( ) )
100+ . Players . Where ( x => x . Id == ( int ) _state . PlayerId )
114101 . ExecuteUpdateAsync (
115102 up =>
116103 up . SetProperty ( p => p . Name , snapshot . Name )
@@ -119,34 +106,35 @@ await dbCtx
119106 . SetProperty ( p => p . Gender , snapshot . Gender ) ,
120107 ct
121108 ) ;
109+
110+ _state . LastUpdated = DateTime . Now ;
122111 }
123112
124113 public Task < PlayerSummarySnapshot > GetSummaryAsync ( CancellationToken ct ) =>
125114 Task . FromResult (
126115 new PlayerSummarySnapshot
127116 {
128- PlayerId = PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) ,
129- Name = state . State . Name ,
130- Motto = state . State . Motto ,
131- Figure = state . State . Figure ,
132- Gender = state . State . Gender ,
133- AchievementScore = state . State . AchievementScore ,
134- 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 ,
135124 }
136125 ) ;
137126
138127 public Task < PlayerExtendedProfileSnapshot > GetExtendedProfileSnapshotAsync ( CancellationToken ct )
139128 {
140- var s = state . State ;
141129 return Task . FromResult (
142130 new PlayerExtendedProfileSnapshot
143131 {
144- UserId = PlayerId . Parse ( ( int ) this . GetPrimaryKeyLong ( ) ) ,
145- UserName = s . Name ,
146- Figure = s . Figure ,
147- Motto = s . Motto ,
148- CreationDate = s . CreatedAt . ToString ( "yyyy-MM-dd" ) ,
149- AchievementScore = s . AchievementScore ,
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 ,
150138 FriendCount = 0 ,
151139 IsFriend = false ,
152140 IsFriendRequestSent = false ,
0 commit comments