forked from code4fukui/comical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
215 lines (202 loc) · 5.65 KB
/
index.html
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
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>高専キャンパスBINGO</title>
</head><body>
<h1>高専キャンパスBINGO</h1>
<main>
<div id=divbingo></div>
<h2>キャンパスに行ったらオープン!</h2>
<div id=divmember></div>
</main>
<hr>
<button id=btnreset>BINGOカードをリセットする</button>
<hr>
DATA: <a href=https://codeforkosen.github.io/kosen-opendata/data/kosen_campus.csv>高専キャンパスオープンデータ(CSV)</a>, <a href=https://codeforkosen.github.io/kosen-opendata/data/kosen.csv>高専一覧</a> (<a href=https://github.com/codeforkosen/kosen-opendata>高専オープンデータ</a>)<br>
IMAGE: <a href=https://kosenjin.org/>高専人会</a><br>
<a href=https://github.com/codeforkosen/kosen-bingo/>src on GitHub</a><br>
<script type="module">
import { CSV } from "https://code4fukui.github.io/CSV/CSV.js";
import { rnd } from "https://js.sabae.cc/rnd.js";
import { shuffle } from "https://js.sabae.cc/shuffle.js";
import isMobile from "https://code4fukui.github.io/isMobile/isMobile.js";
const storageName = "kosen-bingo";
const url = "https://codeforkosen.github.io/kosen-opendata/data/kosen_campus.csv";
const members = await CSV.fetchJSON(url);
console.log(members);
//members.length = 5; // for test
const url2 = "https://codeforkosen.github.io/kosen-opendata/data/kosen.csv";
const kosens = await CSV.fetchJSON(url2);
console.log(kosens);
for (const m of members) {
const kosen0 = kosens.find(i => {
return i.name_ja.indexOf(m.shortname2) >= 0;
});
const kosen = kosen0 ?? kosens.find(i => {
const name = m.shortname.substring(0, m.shortname.indexOf("("));
return i.name_ja.indexOf(name) >= 0;
});
if (!kosen) {
console.log("not found", m);
} else {
m.img = kosen.img_emblem;
}
}
const cr = (tag, parent) => {
const c = document.createElement(tag);
if (parent) parent.appendChild(c);
return c;
};
const btns = [];
for (let i = 0; i < members.length; i++) {
const m = members[i];
const div = cr("div", divmember);
const a = cr("a", div);
a.setAttribute("href", m.URL);
a.setAttribute("target", "_blank");
const img = new Image();
img.src = m.img;
a.appendChild(img);
const btn = cr("button", div);
if (isMobile(window.navigator).any) {
btn.innerHTML = m.name + "に<br>行った!";
} else {
btn.textContent = m.name + "に行った!";
}
btn.targetName = m.name;
btns.push(btn);
}
const supporters = [
"./img/kosenjinkai.png",
];
const makeBingoCard = () => {
const wbingo = 5;
const bingo = [];
if (members.length <= wbingo * wbingo - 1) {
for (const m of members) {
bingo.push({ open: false, name: m.name, img: m.img });
}
const members2 = members.map(i => i);
shuffle(members2);
for (let i = 0; i < wbingo * wbingo - 1 - members.length; i++) {
const m = members2[i % members2.length];
bingo.push({ open: false, name: m.name, img: m.img });
}
console.log(bingo.length);
} else {
const members2 = members.map(i => i);
shuffle(members2);
for (let i = 0; i < wbingo * wbingo - 1; i++) {
const m = members2[i];
bingo.push({ open: false, name: m.name, img: m.img });
}
}
shuffle(bingo);
bingo.splice(12, 0, { open: true, name: "Code for KOSEN", img: "./img/codeforkosen.png" });
console.log(bingo);
return bingo;
};
const showBingoCard = (bingo) => {
divbingo.innerHTML = "";
for (let i = 0; i < 5; i++) {
const div = cr("div", divbingo);
div.textContent = "BINGO"[i];
div.className = "bingo";
}
for (let i = 0; i < bingo.length; i++) {
const b = bingo[i];
const div = cr("div", divbingo);
const img = new Image();
img.src = b.open ? b.img : supporters[i % supporters.length];
img.style.filter = b.open ? "" : "grayscale(1)";
div.appendChild(img);
div.onclick = () => {
if (b.open) {
alert(b.name);
}
};
}
};
const loadBingoCard = () => {
const s = localStorage.getItem(storageName);
if (!s) return null;
return JSON.parse(s);
};
const saveBingoCard = (bingo) => {
localStorage.setItem(storageName, JSON.stringify(bingo));
};
const resetBingoCard = () => {
localStorage.removeItem(storageName);
};
const bingo = loadBingoCard() || makeBingoCard();
//const bingo = makeBingoCard();
showBingoCard(bingo);
saveBingoCard(bingo);
btns.forEach(btn => {
btn.onclick = () => {
const b = bingo.find(b => b.name == btn.targetName);
if (!b) {
alert("残念!" + btn.targetName + "は、カードにはなかった・・・");
} else {
if (b.open) {
alert(b.name + "に再度行った!");
} else {
alert(b.name + "に行った!");
const kosens = bingo.filter(b => b.name == btn.targetName);
kosens.forEach(b => b.open = true);
saveBingoCard(bingo);
showBingoCard(bingo);
}
}
scrollTo({
top: divbingo.offsetTop,
behavior: "smooth"
});
}
});
btnreset.onclick = () => {
if (confirm("BINGOカード、リセットしちゃう?")) {
resetBingoCard();
location.reload();
}
};
</script>
<style>
body {
font-family: sans-serif;
text-align: center;
color: #0e1d2b;
}
#divmember > div {
display: inline-block;
margin: .4vw;
}
#divmember img {
width: 30vw;
display: block;
}
#divmember button {
display: inline-block;
margin: .3em 0 .8em 0;
}
#divbingo {
width: 80vw;
display: inline-block;
}
#divbingo .bingo {
width: 15vw;
font-size: 4vw;
}
#divbingo > div {
display: inline-block;
margin: .4vw;
}
#divbingo img {
width: 15vw;
}
#btnreset {
font-size: 80%;
}
a {
color: gray !important;
}
</style>
</body></html>