Recover orphaned Postgres games in admin

This commit is contained in:
2026-07-15 13:20:34 -07:00
parent c49c4d26e1
commit 811761ca19
2 changed files with 82 additions and 10 deletions
@@ -712,9 +712,10 @@ type GamesPageData struct {
}
type GameDetailPageData struct {
Title string
Game GameInfo
BuildInfo BuildInfo
Title string
Game GameInfo
IsOrphaned bool
BuildInfo BuildInfo
}
type ErrorPageData struct {
@@ -850,6 +851,9 @@ func handleGameRoutes(w http.ResponseWriter, r *http.Request) {
} else if parts[1] == "raw-rewind" {
// /games/{id}/raw-rewind - raw rewind by truncating persisted files (htmx POST)
handleRawRewind(w, r, gameIDHex, gameID)
} else if parts[1] == "import" {
// /games/{id}/import - restore a persisted game to the running-games index (htmx POST)
handleGameImport(w, r, gameIDHex, gameID)
} else if parts[1] == "download" {
// /games/{id}/download - download game save as zip
handleGameDownload(w, r, gameIDHex, gameID)
@@ -887,16 +891,36 @@ func handleGameDetailPage(w http.ResponseWriter, r *http.Request, gameIDHex stri
}
if game == nil {
renderError(w, http.StatusNotFound, "Game not found",
fmt.Sprintf("Game %s does not exist. It may have been deleted.", gameIDHex),
"/games", "Back to games")
return
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
existsResp, existsErr := gameAdminClient.CheckGameExists(ctx, &gameadminpb.CheckGameExistsRequest{
GameId: gameID,
})
if existsErr != nil {
log.Printf("Failed to check persisted game %s: %v", gameIDHex, existsErr)
http.Error(w, fmt.Sprintf("Failed to check persisted game: %v", existsErr), http.StatusInternalServerError)
return
}
if !existsResp.ExistsInMemory && !existsResp.ExistsOnDisk {
renderError(w, http.StatusNotFound, "Game not found",
fmt.Sprintf("Game %s does not exist. It may have been deleted.", gameIDHex),
"/games", "Back to games")
return
}
game = &GameInfo{
GameID: gameIDHex,
GameIDInt: gameID,
RunStatus: "Persisted save (not indexed)",
}
}
data := GameDetailPageData{
Title: fmt.Sprintf("Game %s", gameIDHex),
Game: *game,
BuildInfo: getBuildInfo(),
Title: fmt.Sprintf("Game %s", gameIDHex),
Game: *game,
IsOrphaned: game.RunStatus == "Persisted save (not indexed)",
BuildInfo: getBuildInfo(),
}
w.Header().Set("Content-Type", "text/html")
@@ -1237,6 +1261,36 @@ func handleRawRewind(w http.ResponseWriter, r *http.Request, gameIDHex string, g
}
}
func handleGameImport(w http.ResponseWriter, r *http.Request, gameIDHex string, gameID int64) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
resp, err := gameAdminClient.ImportGame(ctx, &gameadminpb.ImportGameRequest{GameId: gameID})
if err != nil {
log.Printf("Failed to import persisted game %s: %v", gameIDHex, err)
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<div class="alert error">Import failed: %v</div>`, err)
return
}
if resp.Result != gameadminpb.ImportGameResponse_SUCCESS {
log.Printf("Failed to import persisted game %s: %s", gameIDHex, resp.ErrorMessage)
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<div class="alert error">Import failed: %s</div>`, resp.ErrorMessage)
return
}
log.Printf("Imported persisted game %s into the running-games index", gameIDHex)
w.Header().Set("HX-Redirect", "/games/"+gameIDHex)
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<div class="alert success">Game imported. Reloading...</div>`)
}
func handleGameDownload(w http.ResponseWriter, r *http.Request, gameIDHex string, gameID int64) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
@@ -11,9 +11,27 @@
{{.Game.RunStatus}}
</span>
<a href="/games/{{.Game.GameID}}/download" class="btn-secondary" style="margin-left: 1rem;">Download Save</a>
{{if .IsOrphaned}}
<button class="btn-primary"
style="margin-left: 0.5rem;"
hx-post="/games/{{.Game.GameID}}/import"
hx-target="#import-result"
hx-confirm="Import this persisted save into the running-games index? Rewind past any corrupt action first.">
Import and Continue
</button>
{{end}}
<button class="btn-danger" style="margin-left: 0.5rem;" onclick="showDeleteGameModal()">Delete Game</button>
</div>
{{if .IsOrphaned}}
<div class="alert warning">
This game still has persisted history but is missing from the running-games index.
Use the raw action history below to rewind past the corrupt action, then click <strong>Import and Continue</strong>.
The imported game initially has no assigned human players; assign your faction after import.
</div>
<div id="import-result"></div>
{{end}}
<div id="delete-result"></div>
<article>