Files
entity/src/game_progress.zi
T
waozi 02a1dfbba9 Build game-progress requests and level marks in Entity
Games asked entity.records for their progress with hand-written JSON and
converted completion marks themselves. GameProgressGetRequest,
GameProgressPutRequest and GameProgressReply carry the record, and
GameProgressSetLevels and GameProgressMarkLevels convert a game's completed
levels.
2026-10-29 11:29:15 -03:00

131 lines
5.6 KiB
Ziran

// Account progress contains earned unlocks and completion marks only.
// A device's unfinished board and player position never enter this model.
#import "std/json_scan"
#import "std/c_string"
#import "std/text_buffer"
#import "std/vec"
#import, file "model.zi";
#import, file "game_progress_rules.zi";
GAME_PROGRESS_KIND :: "game-progress"
GAME_PROGRESS_SCHEMA :: "entity.game-progress.v1"
GAME_PROGRESS_LIMIT :: 4096
GameProgressRecord :: struct {
game: [97]u8
unlocked: s32
completed: [GAME_PROGRESS_LIMIT + 1]u8
updated_at: s64
}
GameProgressGameValid :: (game: string) -> bool {
if game.count < 1 || game.count > 96 { return false }
for byte: game {
if !((byte >= #char "a" && byte <= #char "z") || (byte >= #char "A" && byte <= #char "Z") ||
(byte >= #char "0" && byte <= #char "9") || byte == #char "." || byte == #char "-" || byte == #char "_") { return false }
}
return true
}
GameProgressRead :: (text: string, output: *GameProgressRecord) -> bool {
if Space(text, 0) != 0 || text.count < 2 || text[0] != #char "{" ||
ValueEnd(text, 0, 0) != text.count { return false }
value: GameProgressRecord
game_count := ReadStringText(text, FieldAt(text, 0, "game"), value.game[:])
completed_count := ReadStringText(text, FieldAt(text, 0, "completed"), value.completed[:])
// Old documents may contain state; ignore it and never serialize it.
if game_count <= 0 || completed_count < 0 { return false }
if !GameProgressGameValid(TextView(value.game[:game_count])) { return false }
unlocked := ReadNumber(text, FieldAt(text, 0, "unlocked"))
if !unlocked.valid || unlocked.value < 1.0 || unlocked.value > cast(float64)GAME_PROGRESS_LIMIT ||
unlocked.value != cast(float64)cast(s32)unlocked.value { return false }
value.unlocked = cast(s32)unlocked.value
for mark: value.completed[:completed_count] {
if mark != #char "0" && mark != #char "1" { return false }
}
stamp_at := FieldAt(text, 0, "updated_at")
if stamp_at >= 0 {
stamp := ReadNumber(text, stamp_at)
if !stamp.valid || stamp.value < 0.0 || stamp.value > 9007199254740991.0 ||
stamp.value != cast(float64)cast(s64)stamp.value { return false }
value.updated_at = cast(s64)stamp.value
}
output.* = value
return true
}
GameProgressWrite :: (value: *GameProgressRecord, output: *Vec(u8)) {
BuilderPrint(output, "{\"game\":")
EntityQuote(output, TextUntilNul(value.game[:]))
BuilderPrint(output, ",\"unlocked\":%,\"completed\":", value.unlocked)
EntityQuote(output, TextUntilNul(value.completed[:]))
BuilderPrint(output, ",\"updated_at\":%}", value.updated_at)
}
// The entity.records request that reads a game's account progress.
GameProgressGetRequest :: (game: string, output: *Vec(u8)) {
BuilderPrint(output, "{\"operation\":\"get\",\"kind\":\"%\",\"game\":", GAME_PROGRESS_KIND)
EntityQuote(output, game)
BuilderPrint(output, "}")
}
// The entity.records request that stores a game's account progress.
GameProgressPutRequest :: (value: *GameProgressRecord, output: *Vec(u8)) {
BuilderPrint(output, "{\"operation\":\"put\",\"kind\":\"%\",\"schema\":\"%\",\"document\":",
GAME_PROGRESS_KIND, GAME_PROGRESS_SCHEMA)
GameProgressWrite(value, output)
BuilderPrint(output, "}")
}
// The progress an entity.records reply holds, when it holds a valid record.
GameProgressReply :: (reply: string, output: *GameProgressRecord) -> bool {
at := FieldAt(reply, 0, "document")
end := ValueEnd(reply, at, 0)
return at >= 0 && end > at && GameProgressRead(reply[at:end], output)
}
// Marks each completed level and ends the marks after the last of them.
GameProgressSetLevels :: (value: *GameProgressRecord, completed: []bool) {
count: s32 = 0
for done, index: completed {
if index >= GAME_PROGRESS_LIMIT { break }
value.completed[index] = ifx done then #char "1" else #char "0"
if done { count = cast(s32)index + 1 }
}
value.completed[count] = 0
}
// Adds the record's completed levels to `completed`; it never clears one.
GameProgressMarkLevels :: (value: *GameProgressRecord, completed: []bool) {
marks := TextUntilNul(value.completed[:])
for index: cast(s64)0..marks.count - 1 {
if index < completed.count && marks[index] == #char "1" { completed[index] = true }
}
}
// Even an older reply can carry earned progress this device has not seen.
GameProgressMerge :: (current: *GameProgressRecord, incoming: *GameProgressRecord) -> bool {
if TextUntilNul(current.game[:]) != TextUntilNul(incoming.game[:]) { return false }
merged := current.*
merged.unlocked = GameProgressUnlocked(current.unlocked, incoming.unlocked)
if current.updated_at > merged.updated_at { merged.updated_at = current.updated_at }
if incoming.updated_at > merged.updated_at { merged.updated_at = incoming.updated_at }
{
left := TextUntilNul(current.completed[:])
right := TextUntilNul(incoming.completed[:])
count := ifx left.count > right.count then left.count else right.count
for index: cast(s32)0..cast(s32)count - 1 {
merged.completed[index] = ifx GameProgressCompleted(index < left.count && left[index] == #char "1",
index < right.count && right[index] == #char "1") then #char "1" else #char "0"
}
merged.completed[count] = 0
}
current.* = merged
return true
}
GameProgressSame :: (left: *GameProgressRecord, right: *GameProgressRecord) -> bool {
return TextUntilNul(left.game[:]) == TextUntilNul(right.game[:]) && left.unlocked == right.unlocked &&
TextUntilNul(left.completed[:]) == TextUntilNul(right.completed[:])
}