Stop caching Shardok game state locally (#7015)

This commit is contained in:
2026-06-10 07:47:32 -07:00
committed by GitHub
parent f1429b9560
commit 05de5fd007
2 changed files with 8 additions and 117 deletions
@@ -295,32 +295,9 @@ namespace eagle {
}
_lastAppliedUnfilteredCount = cachedState.UnfilteredResultCount;
var restoredShardokCount = RestoreCachedShardokStates(cachedState.ShardokGames);
_connectionLogger.LogLine(
$"[STATE_CACHE] Restored game {GameId} at count " +
$"{cachedState.UnfilteredResultCount} with " +
$"{restoredShardokCount}/{cachedState.ShardokGames.Count} cached Shardok game(s)");
}
private int RestoreCachedShardokStates(
List<EagleGameStateDiskCache.CachedShardokGameState> cachedShardokStates) {
int restoredCount = 0;
foreach (var cachedShardokState in cachedShardokStates) {
var model = MakeGameModel(cachedShardokState.ShardokGameId);
if (model == null) { continue; }
var updatesOk = model.HandleUpdates(
cachedShardokState.ActionResultViews,
cachedShardokState.ResultCount);
if (!updatesOk) { continue; }
_currentModel.ShardokGameModels[cachedShardokState.ShardokGameId] = model;
_shardokResultCounts[cachedShardokState.ShardokGameId] =
cachedShardokState.ResultCount;
restoredCount++;
}
return restoredCount;
$"{cachedState.UnfilteredResultCount}; Shardok state will refresh from server");
}
private ShardokGameModel MakeGameModel(ShardokGameId shardokGameId) {
@@ -766,16 +743,13 @@ namespace eagle {
private void SaveCurrentStateToCache(int unfilteredResultCount) {
if (unfilteredResultCount <= 0 || _currentModel.GsView == null) { return; }
var cachedShardokStates = CachedShardokGameStates();
EagleGameStateDiskCache.Save(
GameId,
_currentModel.PlayerId,
unfilteredResultCount,
_currentModel.GsView,
cachedShardokStates);
_currentModel.GsView);
_connectionLogger.LogLine(
$"[STATE_CACHE] Saved game={GameId} count={unfilteredResultCount} " +
$"shardokGames={cachedShardokStates.Count}");
$"[STATE_CACHE] Saved game={GameId} count={unfilteredResultCount}");
}
private void SaveCurrentStateToCache() {
@@ -784,17 +758,6 @@ namespace eagle {
SaveCurrentStateToCache(count);
}
private List<EagleGameStateDiskCache.CachedShardokGameState> CachedShardokGameStates() {
return _currentModel.ShardokGameModels
.Where(kv => !_shardokNeedsResync.GetValueOrDefault(kv.Key, false))
.Select(kv => new EagleGameStateDiskCache.CachedShardokGameState {
ShardokGameId = kv.Key,
ResultCount = kv.Value.History.Count,
ActionResultViews = kv.Value.History.ToList()
})
.ToList();
}
private static HashSet<FactionId> InvitationAcceptedFactionIds(
IEnumerable<ActionResultView> results) {
var invitedFactionIds = new HashSet<FactionId>();
@@ -1,22 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Google.Protobuf;
using Net.Eagle0.Eagle.Views;
using ShardokActionResultView = Net.Eagle0.Shardok.Api.ActionResultView;
using UnityEngine;
namespace eagle {
public static class EagleGameStateDiskCache {
private const int CurrentSchemaVersion = 2;
[Serializable]
private class ShardokCacheEntry {
public string shardokGameId;
public int resultCount;
public List<string> actionResultViewsBase64 = new();
}
private const int CurrentSchemaVersion = 3;
[Serializable]
private class CacheFile {
@@ -26,20 +16,12 @@ namespace eagle {
public int playerId;
public int unfilteredResultCount;
public string gameStateViewBase64;
public List<ShardokCacheEntry> shardokGames = new();
public string savedAtUtc;
}
public class CachedShardokGameState {
public string ShardokGameId { get; set; }
public List<ShardokActionResultView> ActionResultViews { get; set; }
public int ResultCount { get; set; }
}
public class CachedGameState {
public GameStateView GameStateView { get; set; }
public int UnfilteredResultCount { get; set; }
public List<CachedShardokGameState> ShardokGames { get; set; } = new();
}
private static string _cacheDirectory;
@@ -101,13 +83,11 @@ namespace eagle {
return null;
}
LastLoadDiagnostic =
$"loaded {path} count={cache.unfilteredResultCount} shardokGames={cache.shardokGames?.Count ?? 0}";
LastLoadDiagnostic = $"loaded {path} count={cache.unfilteredResultCount}";
return new CachedGameState {
GameStateView = gameStateView,
UnfilteredResultCount = cache.unfilteredResultCount,
ShardokGames = LoadShardokGames(cache.shardokGames)
UnfilteredResultCount = cache.unfilteredResultCount
};
} catch (Exception e) {
LastLoadDiagnostic = $"exception loading {path}: {e}";
@@ -117,12 +97,8 @@ namespace eagle {
}
}
public static void Save(
long gameId,
int? playerId,
int unfilteredResultCount,
GameStateView gameStateView,
IEnumerable<CachedShardokGameState> shardokGames) {
public static void
Save(long gameId, int? playerId, int unfilteredResultCount, GameStateView gameStateView) {
if (unfilteredResultCount <= 0 || gameStateView == null) { return; }
EnsureCacheDirectoryInitialized();
@@ -136,7 +112,6 @@ namespace eagle {
playerId = playerId ?? 0,
unfilteredResultCount = unfilteredResultCount,
gameStateViewBase64 = Convert.ToBase64String(gameStateView.ToByteArray()),
shardokGames = SaveShardokGames(shardokGames),
savedAtUtc = DateTime.UtcNow.ToString("O")
};
@@ -146,53 +121,6 @@ namespace eagle {
}
}
private static List<CachedShardokGameState> LoadShardokGames(
List<ShardokCacheEntry> cacheEntries) {
var result = new List<CachedShardokGameState>();
if (cacheEntries == null) { return result; }
foreach (var cacheEntry in cacheEntries) {
if (String.IsNullOrEmpty(cacheEntry.shardokGameId) || cacheEntry.resultCount < 0 ||
cacheEntry.actionResultViewsBase64 == null ||
cacheEntry.resultCount != cacheEntry.actionResultViewsBase64.Count) {
continue;
}
var actionResultViews =
cacheEntry.actionResultViewsBase64
.Select(encoded => ShardokActionResultView.Parser.ParseFrom(
Convert.FromBase64String(encoded)))
.ToList();
result.Add(new CachedShardokGameState {
ShardokGameId = cacheEntry.shardokGameId,
ResultCount = cacheEntry.resultCount,
ActionResultViews = actionResultViews
});
}
return result;
}
private static List<ShardokCacheEntry> SaveShardokGames(
IEnumerable<CachedShardokGameState> shardokGames) {
if (shardokGames == null) { return new List<ShardokCacheEntry>(); }
return shardokGames
.Where(game => !String.IsNullOrEmpty(game.ShardokGameId) &&
game.ActionResultViews != null &&
game.ResultCount == game.ActionResultViews.Count)
.Select(game => new ShardokCacheEntry {
shardokGameId = game.ShardokGameId,
resultCount = game.ResultCount,
actionResultViewsBase64 =
game.ActionResultViews
.Select(view => Convert.ToBase64String(view.ToByteArray()))
.ToList()
})
.ToList();
}
private static void EnsureCacheDirectoryInitialized() {
if (_cacheDirectory != null) { return; }