-
Notifications
You must be signed in to change notification settings - Fork 1
/
liveblocks.config.ts
110 lines (99 loc) · 3.25 KB
/
liveblocks.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { LiveList, LiveMap, LiveObject, createClient } from "@liveblocks/client";
import { createRoomContext } from "@liveblocks/react";
import { Datum, CardFromLiveList } from "./types";
type Presence = {
lastPlayedCard: string | null,
// ...
};
export type LiveCard = LiveObject<{
id: string,
name: string;
card_faces?: LiveList<LiveCard>
image_uris: LiveObject<{
normal: string;
large: string;
}>;
produced_mana?: string[];
}>;
export type GameDataLive = LiveObject<GameData>
export type GameData = {
deck: string[];
allCards: LiveMap<string, LiveCard>;
related: LiveList<LiveCard>;
graveyard: string[];
exile: string[];
hand: string[];
battlefield: string[][];
engaged: LiveList<string>;
tokens: LiveMap<string, [number, number]>;
life: number;
selected: string[];
swapped: string[];
}
type Storage = {
playerOne: GameDataLive | null;
playerTwo: GameDataLive | null
};
const PUBLIC_API_KEY = process.env.NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_KEY;
// ============================================================================
if (!PUBLIC_API_KEY) {
throw new Error(`You must add your Liveblocks public key to .env.local`);
}
const client = createClient({
publicApiKey: PUBLIC_API_KEY,
});
export function datumToLiveCard(d: Datum) {
return new LiveObject({
id: d.id,
name: d.name,
image_uris: new LiveObject({
normal: d.image_uris?.normal,
large: d.image_uris?.large,
}),
produced_mana: (d as Datum).produced_mana,
card_faces: new LiveList(
d.card_faces?.map((cf) => {
return new LiveObject({
name: cf.name,
id: cf.id,
image_uris: new LiveObject({
normal: cf.image_uris?.normal,
large: cf.image_uris?.large,
}),
produced_mana: cf.produced_mana,
});
})
),
});
}
export function dataToLiveList(data?: Datum[] | CardFromLiveList) {
return new LiveList(
data?.map((d) => {
return new LiveObject({
id: d.id,
name: d.name,
image_uris: new LiveObject({
normal: d.image_uris?.normal,
large: d.image_uris?.large,
}),
produced_mana: (d as Datum).produced_mana,
card_faces: new LiveList(
d.card_faces?.map((cf) => {
return new LiveObject({
name: cf.name,
id: cf.id,
image_uris: new LiveObject({
normal: cf.image_uris?.normal,
large: cf.image_uris?.large,
}),
produced_mana: cf.produced_mana,
});
})
),
});
})
);
}
export const {
suspense: { RoomProvider, useStorage, useMutation, useUndo, useRoom, useCanRedo, useCanUndo, useRedo, useBatch, useMyPresence, useOthers, useHistory },
} = createRoomContext<Presence, Storage>(client);