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

main
ts 130 lines 4.22 KB
Raw
1 import * as gamesConfiguration from "@distilled.cloud/gcp/gamesConfiguration_v1configuration";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import * as Effect from "effect/Effect";
4
5 export const DEFAULT_LOCALE = "en-US";
6 export const DEFAULT_ACHIEVEMENT_TYPE = "STANDARD";
7 export const DEFAULT_INITIAL_STATE = "REVEALED";
8 export const DEFAULT_POINT_VALUE = 5;
9 export const DEFAULT_STEPS_TO_UNLOCK = 10;
10 export const DEFAULT_SCORE_ORDER = "LARGER_IS_BETTER";
11 export const MAX_ACHIEVEMENT_NAME_LENGTH = 100;
12 export const MAX_ACHIEVEMENT_DESCRIPTION_LENGTH = 500;
13 export const MAX_LEADERBOARD_NAME_LENGTH = 100;
14 export const sameText = (left: string | undefined, right: string | undefined) =>
15 (left ?? "") === (right ?? "");
16
17 export const sameNumber = (left: number | undefined, right: number | undefined) =>
18 (left ?? 0) === (right ?? 0);
19
20 export const jsonEqual = (left: unknown, right: unknown) =>
21 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
22
23 export const catchMissing = <A, E extends { readonly _tag: string }, R>(
24 effect: Effect.Effect<A, E, R>,
25 ) =>
26 effect.pipe(
27 Effect.catchIf(
28 (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound",
29 () => Effect.undefined,
30 ),
31 );
32
33 export const ignoreMissing = <E extends { readonly _tag: string }, R>(
34 effect: Effect.Effect<unknown, E, R>,
35 ) =>
36 effect.pipe(
37 Effect.catchIf(
38 (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound",
39 () => Effect.void,
40 ),
41 );
42
43 export const toDisplayName = (
44 id: string,
45 requested: string | undefined,
46 existing: string | undefined,
47 maxLength = MAX_ACHIEVEMENT_NAME_LENGTH,
48 ) =>
49 Effect.gen(function* () {
50 if (requested !== undefined && requested.length > 0) {
51 return requested.slice(0, maxLength);
52 }
53 if (existing !== undefined && existing.length > 0) {
54 return existing.slice(0, maxLength);
55 }
56 const generated = yield* createPhysicalName({
57 id,
58 maxLength: Math.min(40, maxLength),
59 lowercase: true,
60 });
61 return generated.slice(0, maxLength);
62 });
63
64 export const translationsOf = (
65 bundle: gamesConfiguration.LocalizedStringBundle | undefined,
66 ): gamesConfiguration.LocalizedString[] =>
67 (bundle?.translations ?? []).map((entry) => ({
68 locale: entry.locale,
69 value: entry.value,
70 }));
71
72 export const translationValue = (
73 bundle: gamesConfiguration.LocalizedStringBundle | undefined,
74 locale = DEFAULT_LOCALE,
75 ): string | undefined => {
76 const translations = translationsOf(bundle);
77 const match =
78 translations.find((entry) => (entry.locale ?? locale) === locale) ?? translations[0];
79 return match?.value;
80 };
81
82 export const withTranslation = (
83 bundle: gamesConfiguration.LocalizedStringBundle | undefined,
84 locale: string,
85 value: string,
86 ): gamesConfiguration.LocalizedStringBundle => {
87 const translations = translationsOf(bundle);
88 const idx = translations.findIndex((entry) => (entry.locale ?? locale) === locale);
89 const next = { locale, value };
90 if (idx >= 0) {
91 translations[idx] = { ...translations[idx], ...next };
92 } else {
93 translations.unshift(next);
94 }
95 return { translations };
96 };
97
98 export const sameBundle = (
99 left: gamesConfiguration.LocalizedStringBundle | undefined,
100 right: gamesConfiguration.LocalizedStringBundle | undefined,
101 ) =>
102 jsonEqual(
103 translationsOf(left)
104 .map((entry) => ({
105 locale: entry.locale ?? DEFAULT_LOCALE,
106 value: entry.value ?? "",
107 }))
108 .sort((a, b) => a.locale.localeCompare(b.locale)),
109 translationsOf(right)
110 .map((entry) => ({
111 locale: entry.locale ?? DEFAULT_LOCALE,
112 value: entry.value ?? "",
113 }))
114 .sort((a, b) => a.locale.localeCompare(b.locale)),
115 );
116
117 export const defaultScoreFormat = (): gamesConfiguration.GamesNumberFormatConfiguration => ({
118 numberFormatType: "NUMERIC",
119 numDecimalPlaces: 0,
120 });
121
122 export const getAchievement = (achievementId: string) =>
123 achievementId.length === 0
124 ? Effect.undefined
125 : catchMissing(gamesConfiguration.getAchievementConfigurations({ achievementId }));
126
127 export const getLeaderboard = (leaderboardId: string) =>
128 leaderboardId.length === 0
129 ? Effect.undefined
130 : catchMissing(gamesConfiguration.getLeaderboardConfigurations({ leaderboardId }));