The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as gamesConfiguration from "@distilled.cloud/gcp/gamesConfiguration_v1configuration"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | defaultScoreFormat, |
| 12 | DEFAULT_LOCALE, |
| 13 | DEFAULT_SCORE_ORDER, |
| 14 | getLeaderboard, |
| 15 | ignoreMissing, |
| 16 | jsonEqual, |
| 17 | MAX_LEADERBOARD_NAME_LENGTH, |
| 18 | sameBundle, |
| 19 | sameText, |
| 20 | toDisplayName, |
| 21 | translationValue, |
| 22 | withTranslation, |
| 23 | } from "./internal.ts"; |
| 24 | |
| 25 | export type LeaderboardConfigurationProps = { |
| 26 | /** |
| 27 | * Play Games application id from the Google Play Console. Immutable — |
| 28 | * changing it replaces the leaderboard. |
| 29 | */ |
| 30 | applicationId: string; |
| 31 | /** |
| 32 | * Server-assigned leaderboard id. Immutable — changing it replaces the |
| 33 | * leaderboard. |
| 34 | */ |
| 35 | leaderboardId?: string; |
| 36 | /** |
| 37 | * Sort order for posted scores. |
| 38 | * @default "LARGER_IS_BETTER" |
| 39 | */ |
| 40 | scoreOrder?: gamesConfiguration.LeaderboardConfigurationScoreOrderEnum | (string & {}); |
| 41 | /** |
| 42 | * Minimum score that can be posted (int64 string). |
| 43 | */ |
| 44 | scoreMin?: string; |
| 45 | /** |
| 46 | * Maximum score that can be posted (int64 string). |
| 47 | */ |
| 48 | scoreMax?: string; |
| 49 | /** |
| 50 | * Default-locale display name. If omitted, a unique name is generated. |
| 51 | */ |
| 52 | name?: string; |
| 53 | /** |
| 54 | * Locale used for `name` translations. |
| 55 | * @default "en-US" |
| 56 | */ |
| 57 | locale?: string; |
| 58 | /** |
| 59 | * Score formatting. Defaults to a whole-number `NUMERIC` format. |
| 60 | */ |
| 61 | scoreFormat?: gamesConfiguration.GamesNumberFormatConfiguration; |
| 62 | /** |
| 63 | * Full draft detail. `name`, `locale`, and `scoreFormat` overlay the |
| 64 | * default-locale fields when set. |
| 65 | */ |
| 66 | draft?: gamesConfiguration.LeaderboardConfigurationDetail; |
| 67 | }; |
| 68 | |
| 69 | export type LeaderboardConfiguration = Resource< |
| 70 | "GCP.GamesConfiguration.LeaderboardConfiguration", |
| 71 | LeaderboardConfigurationProps, |
| 72 | { |
| 73 | /** Server-assigned leaderboard id. */ |
| 74 | leaderboardId: string; |
| 75 | /** Play Games application id. */ |
| 76 | applicationId: string; |
| 77 | /** Project id used when the leaderboard was reconciled. */ |
| 78 | project: string; |
| 79 | /** Sort order for posted scores. */ |
| 80 | scoreOrder: string | undefined; |
| 81 | /** Minimum postable score. */ |
| 82 | scoreMin: string | undefined; |
| 83 | /** Maximum postable score. */ |
| 84 | scoreMax: string | undefined; |
| 85 | /** Default-locale name with the Alchemy ownership prefix stripped. */ |
| 86 | name: string | undefined; |
| 87 | /** Default locale used for the name. */ |
| 88 | locale: string | undefined; |
| 89 | /** Score formatting. */ |
| 90 | scoreFormat: gamesConfiguration.GamesNumberFormatConfiguration | undefined; |
| 91 | /** Opaque update token. */ |
| 92 | token: string | undefined; |
| 93 | /** Draft icon URL. Writes to this field are ignored. */ |
| 94 | iconUrl: string | undefined; |
| 95 | /** Draft sort rank. Writes to this field are ignored. */ |
| 96 | sortRank: number | undefined; |
| 97 | }, |
| 98 | never, |
| 99 | Providers |
| 100 | >; |
| 101 | |
| 102 | /** |
| 103 | * A Play Games Services leaderboard configuration. |
| 104 | * |
| 105 | * Leaderboard configurations have no labels field and a server-assigned |
| 106 | * id, so only a recorded leaderboard can be read back. `applicationId` and `leaderboardId` are identity — changing either |
| 107 | * replaces the configuration. Score order, bounds, name, and format |
| 108 | * update in place. |
| 109 | * |
| 110 | * ### Creating a Leaderboard |
| 111 | * **Example:** High-score board |
| 112 | * ```typescript |
| 113 | * const board = yield* GCP.GamesConfiguration.LeaderboardConfiguration( |
| 114 | * "HighScore", |
| 115 | * { |
| 116 | * applicationId: "123456789012", |
| 117 | * name: "High Score", |
| 118 | * scoreOrder: "LARGER_IS_BETTER", |
| 119 | * }, |
| 120 | * ); |
| 121 | * ``` |
| 122 | * |
| 123 | * **Example:** Fastest-time board |
| 124 | * ```typescript |
| 125 | * const board = yield* GCP.GamesConfiguration.LeaderboardConfiguration( |
| 126 | * "BestTime", |
| 127 | * { |
| 128 | * applicationId: "123456789012", |
| 129 | * name: "Best Time", |
| 130 | * scoreOrder: "SMALLER_IS_BETTER", |
| 131 | * scoreFormat: { numberFormatType: "TIME_DURATION" }, |
| 132 | * }, |
| 133 | * ); |
| 134 | * ``` |
| 135 | * |
| 136 | * ### Updating a Leaderboard |
| 137 | * **Example:** Change the name |
| 138 | * ```typescript |
| 139 | * // Same logical id as before; only the changed props differ. |
| 140 | * const board = yield* GCP.GamesConfiguration.LeaderboardConfiguration( |
| 141 | * "HighScore", |
| 142 | * { |
| 143 | * applicationId: "123456789012", |
| 144 | * name: "All-time High Score", |
| 145 | * }, |
| 146 | * ); |
| 147 | * ``` |
| 148 | * |
| 149 | * @resource |
| 150 | * @category GamesConfiguration |
| 151 | */ |
| 152 | export const LeaderboardConfiguration = Resource<LeaderboardConfiguration>( |
| 153 | "GCP.GamesConfiguration.LeaderboardConfiguration", |
| 154 | ); |
| 155 | |
| 156 | export class LeaderboardConfigurationNotResolved extends Data.TaggedError( |
| 157 | "GCP.GamesConfiguration.LeaderboardConfigurationNotResolved", |
| 158 | )<{ |
| 159 | applicationId: string; |
| 160 | leaderboardId: string; |
| 161 | }> {} |
| 162 | |
| 163 | const localeOf = ( |
| 164 | news: LeaderboardConfigurationProps, |
| 165 | current?: gamesConfiguration.LeaderboardConfiguration, |
| 166 | ) => news.locale ?? current?.draft?.name?.translations?.[0]?.locale ?? DEFAULT_LOCALE; |
| 167 | |
| 168 | const toAttrs = ( |
| 169 | leaderboard: gamesConfiguration.LeaderboardConfiguration, |
| 170 | applicationId: string, |
| 171 | project: string, |
| 172 | locale = DEFAULT_LOCALE, |
| 173 | ) => ({ |
| 174 | leaderboardId: leaderboard.id ?? "", |
| 175 | applicationId, |
| 176 | project, |
| 177 | scoreOrder: leaderboard.scoreOrder, |
| 178 | scoreMin: leaderboard.scoreMin, |
| 179 | scoreMax: leaderboard.scoreMax, |
| 180 | name: translationValue(leaderboard.draft?.name, locale), |
| 181 | locale, |
| 182 | scoreFormat: leaderboard.draft?.scoreFormat, |
| 183 | token: leaderboard.token, |
| 184 | iconUrl: leaderboard.draft?.iconUrl, |
| 185 | sortRank: leaderboard.draft?.sortRank, |
| 186 | }); |
| 187 | |
| 188 | const desiredBody = (input: { |
| 189 | locale: string; |
| 190 | name: string; |
| 191 | news: LeaderboardConfigurationProps; |
| 192 | current?: gamesConfiguration.LeaderboardConfiguration; |
| 193 | }): gamesConfiguration.LeaderboardConfiguration => { |
| 194 | const draft = input.news.draft ?? {}; |
| 195 | const name = withTranslation(draft.name, input.locale, input.name); |
| 196 | return { |
| 197 | scoreOrder: input.news.scoreOrder ?? input.current?.scoreOrder ?? DEFAULT_SCORE_ORDER, |
| 198 | scoreMin: input.news.scoreMin ?? input.current?.scoreMin, |
| 199 | scoreMax: input.news.scoreMax ?? input.current?.scoreMax, |
| 200 | token: input.current?.token, |
| 201 | id: input.current?.id, |
| 202 | draft: { |
| 203 | ...draft, |
| 204 | name, |
| 205 | scoreFormat: |
| 206 | input.news.scoreFormat ?? |
| 207 | draft.scoreFormat ?? |
| 208 | input.current?.draft?.scoreFormat ?? |
| 209 | defaultScoreFormat(), |
| 210 | }, |
| 211 | }; |
| 212 | }; |
| 213 | |
| 214 | const needsSync = ( |
| 215 | current: gamesConfiguration.LeaderboardConfiguration, |
| 216 | desired: gamesConfiguration.LeaderboardConfiguration, |
| 217 | ) => |
| 218 | !sameText(current.scoreOrder, desired.scoreOrder) || |
| 219 | !sameText(current.scoreMin, desired.scoreMin) || |
| 220 | !sameText(current.scoreMax, desired.scoreMax) || |
| 221 | !sameBundle(current.draft?.name, desired.draft?.name) || |
| 222 | !jsonEqual(current.draft?.scoreFormat, desired.draft?.scoreFormat); |
| 223 | |
| 224 | export const LeaderboardConfigurationProvider = () => |
| 225 | Provider.succeed(LeaderboardConfiguration, { |
| 226 | stables: ["leaderboardId", "applicationId", "project"], |
| 227 | |
| 228 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 229 | if (!isResolved(news)) return undefined; |
| 230 | const previousApp = olds?.applicationId ?? output?.applicationId; |
| 231 | if (previousApp !== undefined && news.applicationId !== previousApp) { |
| 232 | return { action: "replace" as const, deleteFirst: false }; |
| 233 | } |
| 234 | const previousId = olds?.leaderboardId ?? output?.leaderboardId; |
| 235 | if ( |
| 236 | previousId !== undefined && |
| 237 | news.leaderboardId !== undefined && |
| 238 | news.leaderboardId !== previousId |
| 239 | ) { |
| 240 | return { action: "replace" as const, deleteFirst: false }; |
| 241 | } |
| 242 | return undefined; |
| 243 | }), |
| 244 | |
| 245 | read: Effect.fn(function* ({ olds, output }) { |
| 246 | const env = yield* GcpEnvironment.current; |
| 247 | // Server-assigned id: only a recorded leaderboard can be observed. |
| 248 | const leaderboardId = olds?.leaderboardId ?? output?.leaderboardId ?? ""; |
| 249 | const existing = yield* getLeaderboard(leaderboardId); |
| 250 | if (existing === undefined) return undefined; |
| 251 | const attrs = toAttrs( |
| 252 | existing, |
| 253 | olds?.applicationId ?? output?.applicationId ?? "", |
| 254 | env.project, |
| 255 | olds?.locale ?? output?.locale, |
| 256 | ); |
| 257 | return output !== undefined ? attrs : Unowned(attrs); |
| 258 | }), |
| 259 | |
| 260 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 261 | const env = yield* GcpEnvironment.current; |
| 262 | const applicationId = news.applicationId; |
| 263 | const name = yield* toDisplayName( |
| 264 | id, |
| 265 | news.name ?? translationValue(news.draft?.name, news.locale), |
| 266 | output?.name, |
| 267 | MAX_LEADERBOARD_NAME_LENGTH, |
| 268 | ); |
| 269 | |
| 270 | let current = yield* getLeaderboard(news.leaderboardId ?? output?.leaderboardId ?? ""); |
| 271 | |
| 272 | const locale = localeOf(news, current); |
| 273 | const desired = desiredBody({ |
| 274 | locale, |
| 275 | name, |
| 276 | news, |
| 277 | current, |
| 278 | }); |
| 279 | |
| 280 | if (current === undefined) { |
| 281 | current = yield* gamesConfiguration.insertLeaderboardConfigurations({ |
| 282 | applicationId, |
| 283 | body: desired, |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | if (current === undefined) { |
| 288 | return yield* new LeaderboardConfigurationNotResolved({ |
| 289 | applicationId, |
| 290 | leaderboardId: news.leaderboardId ?? output?.leaderboardId ?? "", |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | const synced = desiredBody({ |
| 295 | locale, |
| 296 | name, |
| 297 | news, |
| 298 | current, |
| 299 | }); |
| 300 | if (needsSync(current, synced)) { |
| 301 | current = yield* gamesConfiguration.updateLeaderboardConfigurations({ |
| 302 | leaderboardId: current.id ?? news.leaderboardId ?? "", |
| 303 | body: synced, |
| 304 | }); |
| 305 | } |
| 306 | |
| 307 | return toAttrs(current, applicationId, env.project, locale); |
| 308 | }), |
| 309 | |
| 310 | delete: Effect.fn(function* ({ output }) { |
| 311 | if (output.leaderboardId.length === 0) return; |
| 312 | yield* ignoreMissing( |
| 313 | gamesConfiguration.deleteLeaderboardConfigurations({ |
| 314 | leaderboardId: output.leaderboardId, |
| 315 | }), |
| 316 | ); |
| 317 | }), |
| 318 | }); |