The best way to control Claude Code, Codex and more from any computer or phone

main
ts 359 lines 11.1 KB
Raw
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 DEFAULT_ACHIEVEMENT_TYPE,
12 DEFAULT_INITIAL_STATE,
13 DEFAULT_LOCALE,
14 DEFAULT_POINT_VALUE,
15 DEFAULT_STEPS_TO_UNLOCK,
16 getAchievement,
17 ignoreMissing,
18 sameBundle,
19 sameNumber,
20 sameText,
21 toDisplayName,
22 withTranslation,
23 translationValue,
24 } from "./internal.ts";
25
26 export type AchievementConfigurationProps = {
27 /**
28 * Play Games application id from the Google Play Console. Immutable —
29 * changing it replaces the achievement.
30 */
31 applicationId: string;
32 /**
33 * Server-assigned achievement id. Immutable — changing it replaces the
34 * achievement.
35 */
36 achievementId?: string;
37 /**
38 * Achievement type. Immutable after create.
39 * @default "STANDARD"
40 */
41 achievementType?: gamesConfiguration.AchievementConfigurationAchievementTypeEnum | (string & {});
42 /**
43 * Initial visibility of the achievement.
44 * @default "REVEALED"
45 */
46 initialState?: gamesConfiguration.AchievementConfigurationInitialStateEnum | (string & {});
47 /**
48 * Steps required to unlock. Required for `INCREMENTAL` achievements.
49 * @default 10
50 */
51 stepsToUnlock?: number;
52 /**
53 * Default-locale display name. If omitted, a unique name is generated.
54 */
55 name?: string;
56 /**
57 * Default-locale description.
58 * @default the name
59 */
60 description?: string;
61 /**
62 * Locale used for `name` and `description` translations.
63 * @default "en-US"
64 */
65 locale?: string;
66 /**
67 * Point value awarded when the achievement unlocks.
68 * @default 5
69 */
70 pointValue?: number;
71 /**
72 * Full draft detail. `name`, `description`, `locale`, and `pointValue`
73 * overlay the default-locale fields when set.
74 */
75 draft?: gamesConfiguration.AchievementConfigurationDetail;
76 };
77
78 export type AchievementConfiguration = Resource<
79 "GCP.GamesConfiguration.AchievementConfiguration",
80 AchievementConfigurationProps,
81 {
82 /** Server-assigned achievement id. */
83 achievementId: string;
84 /** Play Games application id. */
85 applicationId: string;
86 /** Project id used when the achievement was reconciled. */
87 project: string;
88 /** Achievement type. */
89 achievementType: string | undefined;
90 /** Initial visibility. */
91 initialState: string | undefined;
92 /** Steps to unlock, for incremental achievements. */
93 stepsToUnlock: number | undefined;
94 /** Default-locale name. */
95 name: string | undefined;
96 /** Default-locale description. */
97 description: string | undefined;
98 /** Default locale used for name and description. */
99 locale: string | undefined;
100 /** Point value. */
101 pointValue: number | undefined;
102 /** Opaque update token. */
103 token: string | undefined;
104 /** Draft icon URL. Writes to this field are ignored. */
105 iconUrl: string | undefined;
106 /** Draft sort rank. Writes to this field are ignored. */
107 sortRank: number | undefined;
108 },
109 never,
110 Providers
111 >;
112
113 /**
114 * A Play Games Services achievement configuration.
115 *
116 * Achievement configurations have no labels field and a server-assigned
117 * id, so only a recorded achievement can be read back. `applicationId` and `achievementId` are identity — changing either
118 * replaces the configuration. `achievementType` is immutable after
119 * create. Name, description, initial state, steps, and point value
120 * update in place.
121 *
122 * ### Creating an Achievement
123 * **Example:** Standard achievement
124 * ```typescript
125 * const achievement = yield* GCP.GamesConfiguration.AchievementConfiguration(
126 * "FirstWin",
127 * {
128 * applicationId: "123456789012",
129 * name: "First Win",
130 * description: "Win your first match",
131 * },
132 * );
133 * ```
134 *
135 * **Example:** Incremental achievement
136 * ```typescript
137 * const achievement = yield* GCP.GamesConfiguration.AchievementConfiguration(
138 * "Centurion",
139 * {
140 * applicationId: "123456789012",
141 * achievementType: "INCREMENTAL",
142 * stepsToUnlock: 100,
143 * name: "Centurion",
144 * description: "Win 100 matches",
145 * pointValue: 20,
146 * },
147 * );
148 * ```
149 *
150 * ### Updating an Achievement
151 * **Example:** Change the description
152 * ```typescript
153 * // Same logical id as before; only the changed props differ.
154 * const achievement = yield* GCP.GamesConfiguration.AchievementConfiguration(
155 * "FirstWin",
156 * {
157 * applicationId: "123456789012",
158 * name: "First Win",
159 * description: "Win a match",
160 * },
161 * );
162 * ```
163 *
164 * @resource
165 * @category GamesConfiguration
166 */
167 export const AchievementConfiguration = Resource<AchievementConfiguration>(
168 "GCP.GamesConfiguration.AchievementConfiguration",
169 );
170
171 export class AchievementConfigurationNotResolved extends Data.TaggedError(
172 "GCP.GamesConfiguration.AchievementConfigurationNotResolved",
173 )<{
174 applicationId: string;
175 achievementId: string;
176 }> {}
177
178 const localeOf = (
179 news: AchievementConfigurationProps,
180 current?: gamesConfiguration.AchievementConfiguration,
181 ) =>
182 news.locale ??
183 current?.draft?.name?.translations?.[0]?.locale ??
184 current?.draft?.description?.translations?.[0]?.locale ??
185 DEFAULT_LOCALE;
186
187 const toAttrs = (
188 achievement: gamesConfiguration.AchievementConfiguration,
189 applicationId: string,
190 project: string,
191 locale = DEFAULT_LOCALE,
192 ) => ({
193 achievementId: achievement.id ?? "",
194 applicationId,
195 project,
196 achievementType: achievement.achievementType,
197 initialState: achievement.initialState,
198 stepsToUnlock: achievement.stepsToUnlock,
199 name: translationValue(achievement.draft?.name, locale),
200 description: translationValue(achievement.draft?.description, locale),
201 locale,
202 pointValue: achievement.draft?.pointValue,
203 token: achievement.token,
204 iconUrl: achievement.draft?.iconUrl,
205 sortRank: achievement.draft?.sortRank,
206 });
207
208 const desiredBody = (input: {
209 locale: string;
210 name: string;
211 description: string | undefined;
212 news: AchievementConfigurationProps;
213 current?: gamesConfiguration.AchievementConfiguration;
214 }): gamesConfiguration.AchievementConfiguration => {
215 const achievementType =
216 input.news.achievementType ?? input.current?.achievementType ?? DEFAULT_ACHIEVEMENT_TYPE;
217 const draft = input.news.draft ?? {};
218 const name = withTranslation(draft.name, input.locale, input.name);
219 const description =
220 input.description !== undefined
221 ? withTranslation(draft.description, input.locale, input.description)
222 : (draft.description ?? withTranslation(undefined, input.locale, input.name));
223 return {
224 achievementType,
225 initialState: input.news.initialState ?? input.current?.initialState ?? DEFAULT_INITIAL_STATE,
226 stepsToUnlock:
227 achievementType === "INCREMENTAL"
228 ? (input.news.stepsToUnlock ?? input.current?.stepsToUnlock ?? DEFAULT_STEPS_TO_UNLOCK)
229 : undefined,
230 token: input.current?.token,
231 id: input.current?.id,
232 draft: {
233 ...draft,
234 name,
235 description,
236 pointValue:
237 input.news.pointValue ??
238 draft.pointValue ??
239 input.current?.draft?.pointValue ??
240 DEFAULT_POINT_VALUE,
241 },
242 };
243 };
244
245 const needsSync = (
246 current: gamesConfiguration.AchievementConfiguration,
247 desired: gamesConfiguration.AchievementConfiguration,
248 ) =>
249 !sameText(current.achievementType, desired.achievementType) ||
250 !sameText(current.initialState, desired.initialState) ||
251 !sameNumber(current.stepsToUnlock, desired.stepsToUnlock) ||
252 !sameBundle(current.draft?.name, desired.draft?.name) ||
253 !sameBundle(current.draft?.description, desired.draft?.description) ||
254 !sameNumber(current.draft?.pointValue, desired.draft?.pointValue);
255
256 export const AchievementConfigurationProvider = () =>
257 Provider.succeed(AchievementConfiguration, {
258 stables: ["achievementId", "applicationId", "project"],
259
260 diff: Effect.fn(function* ({ news, olds, output }) {
261 if (!isResolved(news)) return undefined;
262 const previousApp = olds?.applicationId ?? output?.applicationId;
263 if (previousApp !== undefined && news.applicationId !== previousApp) {
264 return { action: "replace" as const, deleteFirst: false };
265 }
266 const previousId = olds?.achievementId ?? output?.achievementId;
267 if (
268 previousId !== undefined &&
269 news.achievementId !== undefined &&
270 news.achievementId !== previousId
271 ) {
272 return { action: "replace" as const, deleteFirst: false };
273 }
274 const previousType = olds?.achievementType ?? output?.achievementType;
275 if (
276 previousType !== undefined &&
277 news.achievementType !== undefined &&
278 news.achievementType !== previousType
279 ) {
280 return { action: "replace" as const, deleteFirst: false };
281 }
282 return undefined;
283 }),
284
285 read: Effect.fn(function* ({ olds, output }) {
286 const env = yield* GcpEnvironment.current;
287 // Server-assigned id: only a recorded achievement can be observed.
288 const achievementId = olds?.achievementId ?? output?.achievementId ?? "";
289 const existing = yield* getAchievement(achievementId);
290 if (existing === undefined) return undefined;
291 const attrs = toAttrs(
292 existing,
293 olds?.applicationId ?? output?.applicationId ?? "",
294 env.project,
295 olds?.locale ?? output?.locale,
296 );
297 return output !== undefined ? attrs : Unowned(attrs);
298 }),
299
300 reconcile: Effect.fn(function* ({ id, news, output }) {
301 const env = yield* GcpEnvironment.current;
302 const applicationId = news.applicationId;
303 const name = yield* toDisplayName(
304 id,
305 news.name ?? translationValue(news.draft?.name, news.locale),
306 output?.name,
307 );
308
309 let current = yield* getAchievement(news.achievementId ?? output?.achievementId ?? "");
310
311 const locale = localeOf(news, current);
312 const desired = desiredBody({
313 locale,
314 name,
315 description: news.description,
316 news,
317 current,
318 });
319
320 if (current === undefined) {
321 current = yield* gamesConfiguration.insertAchievementConfigurations({
322 applicationId,
323 body: desired,
324 });
325 }
326
327 if (current === undefined) {
328 return yield* new AchievementConfigurationNotResolved({
329 applicationId,
330 achievementId: news.achievementId ?? output?.achievementId ?? "",
331 });
332 }
333
334 const synced = desiredBody({
335 locale,
336 name,
337 description: news.description,
338 news,
339 current,
340 });
341 if (needsSync(current, synced)) {
342 current = yield* gamesConfiguration.updateAchievementConfigurations({
343 achievementId: current.id ?? news.achievementId ?? "",
344 body: synced,
345 });
346 }
347
348 return toAttrs(current, applicationId, env.project, locale);
349 }),
350
351 delete: Effect.fn(function* ({ output }) {
352 if (output.achievementId.length === 0) return;
353 yield* ignoreMissing(
354 gamesConfiguration.deleteAchievementConfigurations({
355 achievementId: output.achievementId,
356 }),
357 );
358 }),
359 });