-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
339 lines (291 loc) · 9.73 KB
/
script.js
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/** Horaroのuser名 */
const HORARO_USER = "rtaij";
/** 1ページに表示するゲーム数の上限 */
let LIST_PAGENATION_NUM = 11;
/** eventId未指定時のリダイレクト */
const REDIRECT_QUERY = "?eventId=rtaijs2024";
const loadImage = (src) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = (e) => reject(e);
img.src = src;
});
}
/** クエリパラメータ取得 */
const parseParam = () => {
const queries = location.search.replace("?", "").split("&");
let eventId = "";
for (const q of queries) {
const [key, value] = q.split("=");
if (key === "eventId") {
eventId = value;
}
if (key === "max" && value.match(/\d+/)) {
LIST_PAGENATION_NUM = Number(value);
}
}
if (!eventId) {
alert("eventIdをクエリパラメータに指定してください");
// window.location.reload = `${window.location}`;
// throw new Error("query param Error");
window.location.search = REDIRECT_QUERY;
}
return eventId;
}
const getHoraro = async (user, eventId) => {
// データ取得
const url = `https://rtain.jp/api/ajax/index.php?url=https://horaro.org/-/api/v1/events/${user}/schedules/${eventId}`;
const res = await (await fetch(url)).json();
// const res = await (await fetch("./test/horaro.json")).json();
// 必要なデータを抽出
const tmplist = res.data.items.map(item => {
let gameName;
let category;
let gameConsole;
let runType;
let est;
let runners;
if (item.data.length === 6) {
// w2023以前
[gameName, category, gameConsole, runType, est, runners] = item.data;
} else {
// s2024
[gameName, category, runType, gameConsole, runners] = item.data;
}
/** @example "2023-08-10T12:00:00+09:00" */
const scheduled = item.scheduled;
console.log(runners);
return {
gameName: gameName.replace(/\\/g, ""),
category: category.replace(/\\/g, ""),
runners: runners.replace(/\\/g, ""),
scheduled,
}
});
// 日毎に分類
let list = [];
/** @type {number[]} 日のリスト。日付更新用トリガーとして記録 */
const dateList = [];
let index = -1;
for (const item of tmplist) {
const day = new Date(item.scheduled).getDate();
if (!dateList.includes(day)) {
dateList.push(day);
index++;
list.push([item]);
} else {
list[index].push(item);
}
}
return list;
}
/**
* Trackerからゲーム情報を取得
* @param {string} eventId 8とか
*/
const getTracker = async (eventId) => {
const url = `https://tracker.rtain.jp/search/?type=run&event=${eventId}`;
// const res = await (await fetch(url)).json();
const res = await (await fetch("./test/tracker.json")).json();
// 必要なデータを抽出
const tmplist = res.map(item => {
const { name, category, deprecated_runners } = item.fields;
/** @example "2023-08-10T12:00:00+09:00" */
const starttime = item.fields.starttime;
return {
gameName: name,
category: category,
runners: deprecated_runners,
scheduled: starttime
}
});
/** @type {object[][]} ゲームリスト。日付ごとの2次元配列 */
let list = [];
/** @type {Day[]} 日のリスト。日付更新用トリガーとして記録 */
const dateList = [];
let index = -1;
for (const item of tmplist) {
const day = new Date(item.scheduled).getDate();
if (!dateList.includes(day)) {
dateList.push(day);
index++;
list.push([item]);
} else {
list[index].push(item);
}
}
return list;
}
/** @type {{gameName:string; category:string;runners:string;scheduled:string;}[][]} */
var gameList = [];
const handleChangeDay = (day) => {
replaceSelectPage();
draw();
}
const handleChangePage = (page) => {
draw();
}
/** x日目のプルダウンを生成 */
const insertSelectDays = () => {
console.log("[insertSelectDays]");
const days = window.gameList.length;
const dom = document.getElementById("day");
let str = "";
for (let i = 0; i < days; i++) {
str += `<option value="${i}">${i + 1}日目</option>`
}
dom.insertAdjacentHTML("afterbegin", str);
}
/** pageのリスト再生成 */
const replaceSelectPage = () => {
console.log("[replaceSelectPage]");
const day = document.getElementById("day").value;
console.log(`day=${day}`);
const gameNum = gameList[day].length;
console.log(`gameNum=${gameNum}`);
// option要素を全削除
const dom = document.getElementById("page");
while (dom.firstChild) {
dom.removeChild(dom.firstChild);
}
// 追加
let str = "";
const pageNum = Math.ceil(gameNum / LIST_PAGENATION_NUM);
console.log(`pageNum=${pageNum}`);
for (let i = 0; i < pageNum; i++) {
str += `<option value="${i}">${i + 1}</option>`;
}
dom.insertAdjacentHTML("afterbegin", str);
}
const drawString = (ctx, font, textAlign, textBaseline, color, text, x, y) => {
ctx.font = font;
ctx.textAlign = textAlign;
ctx.textBaseline = textBaseline;
ctx.fillStyle = color;
// ctx.strokeStyle = "#FFFFFF#";
ctx.fillText(text, x, y);
}
/**
* 日付整形
* @param {string} str 日時の文字列
* @returns "8/10(木)" みたいな形式
*/
const toDateStr = (str) => {
const d = new Date(str);
const month = d.getMonth() + 1;
const day = d.getDate();
const date = ["日", "月", "火", "水", "木", "金", "土"][d.getDay()]
return `${month}/${day}(${date})`;
}
/**
*
* @param {string} text テキスト
* @param {number} maxFontsize 最大のフォントサイズ
* @param {number} minFontsize 最小のフォントサイズ
* @param {number} maxWidth 最大幅
* @returns {number}
*/
const calcFontSize = (text, fontFamily, maxFontsize, minFontsize, maxWidth) => {
/** @type HTMLElement */
const dom = document.getElementById("calcFont");
while (dom.firstChild) {
dom.removeChild(dom.firstChild);
}
dom.innerHTML = text;
dom.style.width = "fit-content";
dom.style.fontFamily = fontFamily;
dom.style.fontSize = `${maxFontsize}px`;
let fontSize = maxFontsize;
let width = dom.clientWidth;
// console.log(`maxWidth=${maxWidth} width=${width} fontSize=${fontSize} ${text}`);
while (width > maxWidth && minFontsize < fontSize) {
fontSize--;
dom.style.fontSize = `${fontSize}px`;
width = dom.clientWidth;
// console.log(`maxWidth=${maxWidth} width=${width} fontSize=${fontSize}`);
}
return fontSize;
}
/** canvas描画 */
const draw = async () => {
/** @type {HTMLCanvasElement} */
const canvas = document.getElementById("gamelist");
const dayIndex = Number(document.getElementById("day").value);
const pageIndex = Number(document.getElementById("page").value);
const targetGames = gameList[dayIndex].filter((v, i) => {
const minNum = pageIndex * LIST_PAGENATION_NUM;
const maxNum = (pageIndex + 1) * LIST_PAGENATION_NUM;
return minNum <= i && i < maxNum;
});
const targetGameLen = targetGames.length;
const isMultiPage = gameList[dayIndex].length > LIST_PAGENATION_NUM;
const date = toDateStr(targetGames[0].scheduled)
const HEADER_HEIGHT = 250;
const FOOTER_HEIGHT = 50 - 10;
const WIDTH = 800;
const height = HEADER_HEIGHT + targetGameLen * (80 + 10) + FOOTER_HEIGHT;
// canvasのサイズを設定
canvas.width = WIDTH;
canvas.height = height;
const ctx = canvas.getContext("2d");
const now = new Date().getTime();
// canvasの背景
const backgroundImg = await loadImage("./img/gamelist_background.png?" + now);
const pattern = ctx.createPattern(backgroundImg, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, WIDTH, height);
// ヘッダー
const hedaerImage = await loadImage("./img/gamelist_head.png?" + now);
ctx.drawImage(hedaerImage, 0, 0);
// 日付表示
const TEXT_COLOR = "rgb(37,48,58)";
// x日目
if (!isMultiPage) {
drawString(ctx, `100px ab-kokoro-no3`, "center", "bottom", TEXT_COLOR, Number(dayIndex) + 1, 600, 45 + 94); // ほんとは85だけど下が揃わないので
drawString(ctx, `40px ab-kokoro-no3`, "left", "bottom", TEXT_COLOR, "日目", 640, 45 + 85);
} else {
drawString(ctx, `100px ab-kokoro-no3`, "center", "bottom", TEXT_COLOR, Number(dayIndex) + 1, 580, 45 + 94); // ほんとは85だけど下が揃わないので
drawString(ctx, `40px ab-kokoro-no3`, "left", "bottom", TEXT_COLOR, `日目-${pageIndex + 1}`, 620, 45 + 85);
}
// 日付
drawString(ctx, `30px ab-kokoro-no3`, "center", "bottom", TEXT_COLOR, date, 650, 150 + 35);
// ゲームリスト
for (let i = 0; i < targetGames.length; i++) {
const game = targetGames[i];
const category_runner = `${game.category} / Runner: ${game.runners}`;
const y = 250 + i * 90;
const center_x = 400;
// 背景色
ctx.beginPath()
ctx.fillStyle = 'rgba(255,255,255,0.8)';
ctx.fillRect(50, y, 700, 80);
ctx.closePath();
console.log(`----------------------\n ${game.gameName}`);
// ゲーム情報
const title_y = y + 30;
const fontFamily = "MPLUS1p-Bold";
let text = game.gameName;
let size = calcFontSize(text, fontFamily, 30, 10, 700);
drawString(ctx, `${size}px ${fontFamily}`, "center", "middle", TEXT_COLOR, text, center_x, title_y);
// カテゴリ、走者
const runners_y = y + 80 - 18;
let len = category_runner.length;
text = category_runner;
size = calcFontSize(text, fontFamily, 18, 8, 695);
console.log(`category size=${size}`);
drawString(ctx, `${size}px ${fontFamily}`, "center", "middle", TEXT_COLOR, category_runner, center_x, runners_y);
}
}
const init = async () => {
console.log("init");
const eventId = parseParam();
gameList = await getHoraro(HORARO_USER, eventId);
// gameList = await getTracker(eventId);
console.log(gameList);
insertSelectDays();
replaceSelectPage();
draw();
draw();
}
init();