forked from SimonStnn/pop-a-loon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst.ts
241 lines (211 loc) · 4.43 KB
/
const.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import * as Balloons from '@/balloons';
import { LogLevelNumbers } from '@/managers/log';
//
// * Config types
//
type _initialConfig = {
popVolume: number;
spawnRate: number;
fullScreenVideoSpawn: boolean;
} & RemoteConfig;
export const initalConfig: _initialConfig = {
// Local config
popVolume: 70,
spawnRate: 1,
fullScreenVideoSpawn: false,
// Remote config -> can be overriden by the remote
badge: {
color: '#26282b',
backgroundColor: '#7aa5eb',
},
spawnInterval: {
min: 1000,
max: 10 * 60000,
},
};
//
// * Remote types
//
export type User = {
id: string;
username?: string;
email?: string;
count: number;
updatedAt: string;
createdAt: string;
};
export type HistoryNode = {
date: Date;
pops: Record<BalloonName, number>;
};
export type RemoteConfig = {
badge: {
color: hexColor;
backgroundColor: hexColor;
};
spawnInterval: {
min: number;
max: number;
};
};
export type RemoteResponse = {
user: User;
count: {
id: string;
count: number;
updatedAt: string;
};
status: {
status: 'up';
version: string;
};
configuration: RemoteConfig;
leaderboard: {
user: User;
rank: number | null;
topUsers: User[];
};
statistics: {
totalPopped: number;
};
popHistory: {
history: HistoryNode[];
};
scores: {
scores: {
name: BalloonName;
count: number;
}[];
};
};
export type Endpoint =
| '/status'
| '/configuration'
| '/user/new'
| '/user/:id'
| '/user'
| '/user/count/increment'
| '/leaderboard'
| '/statistics'
| '/statistics/history'
| '/statistics/scores';
//
// * Storage types
//
type Config = Prettify<typeof initalConfig & RemoteConfig>;
export type SyncStorageStructure = {
config: Config;
token: string;
user: User;
snooze: number | null;
};
export type SyncStorageKey = keyof SyncStorageStructure;
export type LocalStorageStructure = {
loglevel?: LogLevelNumbers;
};
export type LocalStorageKey = keyof LocalStorageStructure;
//
// * Chrome message types
//
type UpdateCounterMessage = {
action: 'updateCounter';
balloonCount: number;
};
type IncrementCount = {
action: 'incrementCount';
type: BalloonName;
};
type SpawnBalloonMessage = {
action: 'spawnBalloon';
};
type SetLogLevelMessage = {
action: 'setLogLevel';
level: LogLevelNumbers;
};
export type Message =
| UpdateCounterMessage
| IncrementCount
| SpawnBalloonMessage
| SetLogLevelMessage;
//
// * Alarms
//
export type AlarmName = 'spawnBalloon' | 'restart';
//
// * Development
//
const dev_user: User = {
id: '6623b79f2fc0a498832e993d',
username: 'John',
email: '',
count: 0,
updatedAt: '2021-10-10T10:00:00Z',
createdAt: '2021-10-10T10:00:00Z',
};
export const devRemoteResponse: Record<Endpoint, any> = new Proxy(
{
'/status': { status: 'up', version: '1.0.0' },
'/configuration': {
...initalConfig,
spawnInterval: {
min: 500,
max: 0.01 * 60000,
},
},
'/user/count/increment': {},
'/user/new': { token: 'token', ...dev_user },
'/user/:id': dev_user,
'/user': dev_user,
'/leaderboard': {
user: dev_user,
rank: 1,
topUsers: Array.from({ length: 10 }, (_, i) => ({
...dev_user,
username: `User_${i}`,
count: (10 - i) * 10,
})),
},
'/statistics': { totalPopped: 100 },
'/statistics/history': {
history: Array.from({ length: 30 }, (_, i) => ({
date: new Date(2021, 10, i + 1),
pops: {
default: Math.floor(Math.random() * 100),
confetti: Math.floor(Math.random() * 100),
gold: Math.floor(Math.random() * 100),
},
})),
},
'/statistics/scores': {
scores: [
{ name: 'default', count: 10 },
{ name: 'confetti', count: 20 },
{ name: 'gold', count: 30 },
],
},
},
{
get: function (target, prop, receiver) {
if (prop === '/user/count/increment') {
return {
id: dev_user.id,
count: ++dev_user.count,
updatedAt: new Date().toISOString(),
};
}
return Reflect.get(target, prop, receiver);
},
}
);
//
// * Other
//
export const BalloonContainerId = 'balloon-container';
export type BalloonInstance = InstanceType<
(typeof Balloons)[keyof typeof Balloons]
>;
export type BalloonName = BalloonInstance['name'];
export type hexColor = string;
export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};