-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
292 lines (247 loc) · 10.4 KB
/
script.js
File metadata and controls
292 lines (247 loc) · 10.4 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
291
292
let selectedLang = 'cpp';
let selectedMode = '';
let currentMode = '';
let wordIdx = 0;
let charIdx = 0;
let isPlaying = false;
let startTime;
let timerInterval;
let countdownInterval; // 追加:宣言を忘れずに
let missCount = 0;
let totalTyped = 0;
let currentTargetWord = "";
const menuScreen = document.getElementById('menu-screen');
const gameScreen = document.getElementById('game-screen');
const wordDisplay = document.getElementById('word-display');
const infoDisplay = document.getElementById('game-info');
const timerDisplay = document.getElementById('timer-display');
const startBtn = document.getElementById('start-btn');
// --- 選択イベント ---
document.querySelectorAll('.lang-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelector('.lang-btn.active').classList.remove('active');
btn.classList.add('active');
selectedLang = btn.dataset.lang;
});
});
document.querySelectorAll('.mode-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.mode-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
selectedMode = btn.dataset.mode;
startBtn.disabled = false;
});
});
// --- ゲーム制御 ---
window.startGame = function() {
if (!selectedMode) return;
menuScreen.style.display = 'none';
gameScreen.style.display = 'block';
runCountdown();
};
function runCountdown() {
let count = 3;
isPlaying = false;
wordDisplay.innerHTML = `<div style="font-size: 4rem;">${count}</div>`;
timerDisplay.innerText = "READY...";
if (countdownInterval) clearInterval(countdownInterval); // 二重起動防止
countdownInterval = setInterval(() => {
count--;
if (count > 0) {
wordDisplay.innerHTML = `<div style="font-size: 4rem;">${count}</div>`;
} else if (count === 0) {
wordDisplay.innerHTML = `<div style="font-size: 4rem;">GO!</div>`;
} else {
clearInterval(countdownInterval);
countdownInterval = null; // リセット
initGameLogic();
}
}, 1000);
}
function initGameLogic() {
isPlaying = true;
currentMode = selectedMode;
wordIdx = 0;
charIdx = 0;
missCount = 0;
totalTyped = 0;
setNextWord();
startTime = performance.now();
timerInterval = setInterval(updateTimer, 10);
}
let lastWord = "";
function setNextWord() {
const pool = WORD_DATA[selectedLang];
let target;
do {
target = pool[Math.floor(Math.random() * pool.length)];
} while (target === lastWord && pool.length > 1);
lastWord = target;
currentTargetWord = target;
charIdx = 0;
// 進捗表示の更新
if (currentMode === 'time-trial') {
infoDisplay.innerText = `PROGRESS: ${wordIdx + 1} / 10`;
} else {
infoDisplay.innerText = `WORDS CLEARED: ${wordIdx}`;
}
// 画面表示の生成
wordDisplay.innerHTML = target.split('').map((c, i) =>
`<span class="${i === 0 ? 'current' : ''}">${c}</span>`
).join('');
}
function updateTimer() {
const elapsed = (performance.now() - startTime) / 1000;
if (currentMode === 'time-trial') {
timerDisplay.innerText = `TIME: ${elapsed.toFixed(2)}s`;
} else {
const remaining = Math.max(0, 30 - elapsed);
timerDisplay.innerText = `REMAINING: ${remaining.toFixed(2)}s`;
if (remaining <= 0) finishGame();
}
}
function abortGame() {
isPlaying = false;
clearInterval(timerInterval);
if (countdownInterval) clearInterval(countdownInterval);
gameScreen.style.display = 'none';
menuScreen.style.display = 'block';
wordIdx = 0;
charIdx = 0;
}
// --- キー入力(修正:二重リスナーを解消) ---
window.addEventListener('keydown', (e) => {
// 1. 強制終了 (Ctrl+C) の判定
// カウントダウン中、またはプレイ中のどちらでも中断できるようにします
if (isPlaying || countdownInterval) {
if (e.ctrlKey && e.key.toLowerCase() === 'c') {
e.preventDefault(); // ブラウザのコピー動作を防止
if (confirm('MISSION ABORTED. 中断しますか?')) {
abortGame();
}
return;
}
}
// 2. ゲーム中ではない時のメニュー操作
if (!isPlaying && !countdownInterval) {
if (e.key === '1') {
const btn = document.querySelector('[data-mode="time-trial"]');
if (btn) btn.click();
}
if (e.key === '2') {
const btn = document.querySelector('[data-mode="score-attack"]');
if (btn) btn.click();
}
if (e.key === 'Enter' && !startBtn.disabled) {
startGame();
}
return;
}
// 3. タイピング判定(ゲーム中のみ)
if (!isPlaying) return;
if (e.key.length !== 1) return; // 修飾キーなどは無視
const spans = wordDisplay.querySelectorAll('span');
const targetChar = currentTargetWord[charIdx];
if (e.key === targetChar) {
// 正解時:従来どおりの処理
totalTyped++;
spans[charIdx].classList.remove('miss');
spans[charIdx].classList.remove('current');
spans[charIdx].classList.add('cleared');
charIdx++;
if (charIdx < currentTargetWord.length) {
spans[charIdx].classList.add('current');
} else {
wordIdx++;
if (currentMode === 'time-trial' && wordIdx >= 10) {
finishGame();
} else {
setNextWord();
}
}
}
// 【追加】スペースがない場所でスペースを押した場合は「無視」する
else if (e.key === ' ' && targetChar !== ' ') {
return;
}
else if (!['Shift', 'Control', 'Alt', 'CapsLock', 'Tab'].includes(e.key)) {
// 不正解時:スペースが必要な場所で打たなかった場合などはここに来る
spans[charIdx].classList.add('miss');
missCount++;
}
});
//言語追加時はlangNamesも追加すること
const langNames = { 'cpp': 'C++', 'js': 'JavaScript', 'python': 'Python' };
const displayName = langNames[selectedLang] || selectedLang;
function finishGame() {
isPlaying = false;
clearInterval(timerInterval);
const endTime = performance.now();
const finalTime = (endTime - startTime) / 1000;
const kpm = finalTime > 0 ? ((totalTyped / finalTime) * 60).toFixed(1) : 0;
const accuracy = totalTyped + missCount > 0
? ((totalTyped / (totalTyped + missCount)) * 100).toFixed(1)
: 0;
// --- ハイスコアの処理 ---
const scoreKey = `best_${selectedLang}_${currentMode}`;
const savedBest = localStorage.getItem(scoreKey);
let isNewRecord = false;
if (currentMode === 'time-trial') {
if (!savedBest || finalTime < parseFloat(savedBest)) {
localStorage.setItem(scoreKey, finalTime);
isNewRecord = true;
}
} else {
if (!savedBest || wordIdx > parseInt(savedBest)) {
localStorage.setItem(scoreKey, wordIdx);
isNewRecord = true;
}
}
const bestScoreDisplay = isNewRecord ? "NEW RECORD!" : `BEST: ${savedBest || '-'}`;
// --- 【重要】表示名の設定と文面の構築 ---
const langNames = { 'cpp': 'C++', 'js': 'JavaScript', 'python': 'Python' };
const displayName = langNames[selectedLang] || selectedLang;
const scoreResult = currentMode === 'time-trial' ? `${finalTime.toFixed(2)}s` : `${wordIdx} WORDS`;
// TERMINAL_LOG スタイル(行頭のスペースを入れない)
const shareText = `[CODTEC_REPORT]
>> LANG: ${displayName} / MODE: ${currentMode}
>> RESULT: ${scoreResult}
>> KPM: ${kpm} / ACC: ${accuracy}%
>> ERR_LOG: ${missCount} cases detected.
https://ssmbar.com/codtec/`;
const xLink = `https://twitter.com/intent/tweet?text=${encodeURIComponent(shareText)}&hashtags=codtec`;
// --- フィードバック用URL ---
const feedbackUrl = "https://docs.google.com/forms/d/e/1FAIpQLSfiswxbXdiDrfv-ioF_M2Tyt3Vjkeo6WETMfemG2dpUdL4AQA/viewform";
const portalUrl = "https://ssmbar.com/"; // あなたのポータルサイトURL
wordDisplay.innerHTML = `
<div class="result-container" style="text-align: center;">
<div style="font-size: 1.2rem; color: #aaa; letter-spacing: 2px;">MISSION COMPLETE</div>
<div style="font-size: 2.5rem; color: ${currentMode === 'time-trial' ? '#ffd700' : '#00ffcc'}; margin-bottom: 5px;">${scoreResult}</div>
<div style="font-size: 1rem; color: #ff00ff; margin-bottom: 10px; font-weight: bold;">${bestScoreDisplay}</div>
<div style="font-size: 1.2rem; text-align: left; display: inline-block; border-top: 1px solid #444; padding: 15px 0;">
<p>SPEED: ${kpm} KPM</p>
<p>MISSES: ${missCount}</p>
<p>ACCURACY: ${accuracy}%</p>
</div>
<div style="margin-top: 15px; display: flex; justify-content: center; gap: 10px; flex-wrap: wrap;">
<a href="${xLink}" target="_blank" rel="noopener noreferrer" style="display: inline-block; background: #000; color: #fff; padding: 10px 20px; border-radius: 5px; text-decoration: none; font-weight: bold; font-size: 0.9rem; border: 1px solid #444;">
X でシェア
</a>
<a href="${feedbackUrl}" target="_blank" rel="noopener noreferrer" style="display: inline-block; background: #222; color: #00ffcc; padding: 10px 20px; border-radius: 5px; text-decoration: none; font-weight: bold; font-size: 0.9rem; border: 1px solid #00ffcc;">
報告を送信
</a>
<a href="${portalUrl}" style="display: inline-block; background: rgba(0, 69, 255, 0.2); color: #00d2ff; padding: 10px 20px; border-radius: 5px; text-decoration: none; font-weight: bold; font-size: 0.9rem; border: 1px solid #00d2ff;">
ポータルに戻る
</a>
</div>
<div style="margin-top: 25px; font-size: 1rem; color: #888; animation: blink 1s infinite;">Press Enter to Retry</div>
</div>
`;
const restartHandler = (e) => {
if (e.key === 'Enter') {
window.removeEventListener('keydown', restartHandler);
location.reload();
}
};
window.addEventListener('keydown', restartHandler);
}