-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
290 lines (269 loc) · 7.91 KB
/
script.js
File metadata and controls
290 lines (269 loc) · 7.91 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const game = {
slug: "trouble-maker",
host: "bobanya",
createdAt: 1588553102804,
updatedAt: 1588553102804,
players: ["bobanya", "genji", "millie", "jilly", "rosa", "heinrich"],
settings: {
rotations: 2,
turnDurationSeconds: 90,
teamsCount: 2,
autoStart: false,
},
games: [
{
startTime: 1588553102804,
endTime: null,
teams: [
{
teamName: "Booty Shakers",
players: ["jilly", "rosa", "heinrich"],
playerIndex: 0,
score: 3,
},
{
teamName: "Silver Tiger Claw",
players: ["bobanya", "genji", "millie"],
playerIndex: 2,
score: 1,
},
],
totalTurns: 12,
turns: [
{
startTime: 1588553102804,
endTime: 1588554102804,
teamIndex: 0,
player: "jilly",
author: "bobanya",
prompt: "car jacking",
success: true,
},
{
startTime: 1588555102804,
endTime: 1588556102804,
teamIndex: 1,
player: "bobanya",
author: "jilly",
prompt: "aquaphor",
success: true,
},
{
startTime: 1588557102804,
endTime: 1588558102804,
teamIndex: 0,
player: "rosa",
author: "genji",
prompt: "snail eating a cucumber",
success: true,
},
{
startTime: 1588558102804,
endTime: 1588559102804,
teamIndex: 1,
player: "genji",
author: "rosa",
prompt: "five star wings",
success: false,
},
{
startTime: 1588558102804,
endTime: 1588559102804,
teamIndex: 0,
player: "heinrich",
author: "millie",
prompt: "i'm a lil bitch",
success: true,
},
],
},
],
prompts: [
{
author: "bobanya",
prompt: "lip light",
},
{
author: "genji",
prompt: "star wards: the last jedi",
},
{
author: "millie",
prompt: "plush baby legs",
},
{
author: "jilly",
prompt: "smoking is cool kids",
},
{
author: "rosa",
prompt: "smoothie hoochie",
},
{
author: "heinrich",
prompt: "gloop gloop gloop",
},
{
author: "heinrich",
prompt: "mousie go bye bye",
},
],
}
window.onload = () => {
defineCustomElements();
const colors = generateColors(game.games[0].teams.length);
const main = document.querySelector('main');
const teams = game.games[0].teams;
const players = game.players;
const host = 'jilly';
const actorUp = 'rosa';
const onDeck = 'millie';
main.appendChild(h('team-box', {
players: teams[0].players,
teamColor: colors[0],
score: teams[0].score,
host: host,
actorUp: actorUp,
onDeck: onDeck,
myTeam: true,
}));
main.appendChild(h('team-box', {
players: teams[1].players,
teamColor: colors[1],
score: teams[1].score,
host: host,
actorUp: actorUp,
onDeck: onDeck,
myTeam: false,
}));
}
/* Paul Frazee's handy templating function: */
function h(tag, attrs, ...children) {
const el = document.createElement(tag);
for (let k in attrs) {
if (k === 'className') el.className = attrs[k];
else el.setAttribute(k, attrs[k]);
}
for (let child of children) el.appendChild(child);
return el;
}
/* create a text node */
function t(text) {
return document.createTextNode(text);
}
/* create a template instance from a template ID */
function c(templateId, ...children) {
const el = document.importNode(document.getElementById(templateId).content, true);
const slot = el.querySelector('slot')
for (let child of children) slot.appendChild(child);
return el;
}
/* generate a set of n equidistant colors */
function generateColors(
n,
startingHue = Math.floor(Math.random() * 360)
) {
return new Array(n)
.fill('')
.map((_, index) => {
var hue = (startingHue + (Math.floor(360 / n) * index)) % 360;
return `hsl(${hue}, 100%, 75%)`
});
}
/* get array from attribute object */
function arrayFromAttribute(attrObj) {
return attrObj.value.split(',');
}
/* get boolean from attribute object */
function booleanFromAttribute(attrObj) {
return attrObj.value === "true";
}
function applyStyleToSelector(root, property, value, selector) {
const elements = root.querySelectorAll(selector);
elements.forEach((element) => {
element.style[property] = value;
});
return elements;
}
function hideElement(root, selector) {
applyStyleToSelector(root, 'display', 'none', selector);
}
function setupCustomElement(context) {
context.attachShadow({ mode: 'open' });
const template = document.getElementById(context.localName);
const el = template.content.cloneNode(true);
context.shadowRoot.appendChild(el);
}
function defineCustomElements() {
/* More Verbose Class Syntax:
* class PlayerListItem extends HTMLElement { ... }
* customElements.define('player-list-item', PlayerListItem) */
/* PlayerListItem */
customElements.define('player-list-item', class extends HTMLElement {
connectedCallback() {
const teamColor = this.attributes.teamColor.value;
const player = this.attributes.player.value;
const host = this.attributes.host.value;
const actorUp = this.attributes.actorUp.value;
const onDeck = this.attributes.onDeck.value;
/* this.localName is 'player-list' in this case */
const el = c(this.localName, t(player));
applyStyleToSelector(el, 'background', teamColor, '.team-box__badge--status-on-deck');
applyStyleToSelector(el, 'color', teamColor, '.team-box__player-name');
player !== host && hideElement(el, '.team-box__badge--host');
player !== actorUp && hideElement(el, '.team-box__badge--status-actor-up');
player !== onDeck && hideElement(el, '.team-box__badge--status-on-deck');
this.appendChild(el);
}
});
/* PlayerList */
customElements.define('player-list', class extends HTMLElement {
constructor() {
super();
setupCustomElement(this);
}
connectedCallback() {
const teamColor = this.attributes.teamColor.value;
/* `players` is a comma-separated list,
* so it needs to be converted back to array */
const players = arrayFromAttribute(this.attributes.players);
const host = this.attributes.host.value;
const actorUp = this.attributes.actorUp.value;
const onDeck = this.attributes.onDeck.value;
const items = players.map((player) => h('player-list-item', { teamColor, player, host, actorUp, onDeck }));
const slot = this.shadowRoot.querySelector('slot');
items.forEach((item) => {
slot.appendChild(item);
});
}
});
/* TeamScore */
customElements.define('team-score', class extends HTMLElement {
connectedCallback() {
const score = this.attributes.score.value;
const el = c(this.localName, t(score));
this.appendChild(el);
}
});
/* TeamBox */
customElements.define('team-box', class extends HTMLElement {
constructor() {
super();
setupCustomElement(this);
}
connectedCallback() {
const players = arrayFromAttribute(this.attributes.players);
const teamColor = this.attributes.teamColor.value;
const host = this.attributes.host.value;
const actorUp = this.attributes.actorUp.value;
const onDeck = this.attributes.onDeck.value;
const myTeam = booleanFromAttribute(this.attributes.myTeam);
!myTeam && hideElement(this.shadowRoot, '.team-box__rename-button');
const score = this.attributes.score.value;
applyStyleToSelector(this.shadowRoot, 'background', teamColor, '.team-box__body, .team-box__team-label');
const slot = this.shadowRoot.querySelector('slot');
slot.appendChild(h('player-list', { players, teamColor, host, actorUp, onDeck }));
slot.appendChild(h('team-score', { score }));
}
});
}