Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
1722 lines (1486 loc) · 56.6 KB
/
Copy pathgame.js
File metadata and controls
1722 lines (1486 loc) · 56.6 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The Git-Goat's Gazette
* A WCAG 2.2 AA compliant GitHub learning game with 18+ stages
*
* Configuration
*/
// ⚙️ CONFIGURATION: Update these values to change the target repository
// These can be changed to point to different repos without modifying the game logic
const REPO_OWNER = 'mgifford'; // GitHub username who owns the repo
const REPO_NAME = 'learn-github-game'; // Repository name (no spaces, lowercase preferred)
// Seussian comment templates for user guidance
const SEUSSIAN_TEMPLATES = {
comment: [
"I do! I do! I like this issue!",
"This would be splendid! Can we make it so?",
"I shall help you with this!",
"What a wonderful idea!",
"Say! I like this! Yes, I do!"
],
closing: [
"This is done! Hooray!",
"The anchor is now loose. Problem solved!",
"I've fixed it, and it's splendid!",
"All done here, thank you!"
]
};
// API configuration
const GITHUB_API_BASE = 'https://api.github.com';
const API_HEADERS = {
'Accept': 'application/vnd.github.v3+json'
};
// Timing controls
const PENDING_RECHECK_INTERVAL_MS = 45000; // Re-validate pending steps every 45s
const CONNECTION_CHECK_INTERVAL_MS = 60000; // Connectivity ping interval
// Game state
let gameState = {
username: null,
userProfile: null, // Cache user profile data (name, bio, followers, etc.)
currentLevel: 0,
completedLevels: [],
pendingChecks: {},
skippedStages: [], // Track which stages user skipped
prNumber: null, // Store PR number for levels 16-17
};
/**
* Initialize the game
*/
document.addEventListener('DOMContentLoaded', () => {
restoreGameState();
// Check URL hash for level navigation
const hash = window.location.hash.slice(1);
if (hash && isValidLevel(hash)) {
gameState.currentLevel = getLevelNumberFromHash(hash);
}
updateUI();
updateURLHash();
// Event listeners for level validation
const loginForm = document.querySelector('#level-0 form');
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
handleLoginClick();
});
document.getElementById('login-button').addEventListener('click', handleLoginClick);
document.getElementById('check-level-1').addEventListener('click', checkLevel1);
document.getElementById('check-level-2').addEventListener('click', checkLevel2);
document.getElementById('check-level-3').addEventListener('click', checkLevel3);
document.getElementById('check-level-4').addEventListener('click', checkLevel4);
document.getElementById('check-level-5').addEventListener('click', checkLevel5);
document.getElementById('check-level-6').addEventListener('click', checkLevel6);
document.getElementById('check-level-7').addEventListener('click', checkLevel7);
document.getElementById('check-level-8').addEventListener('click', checkLevel8);
document.getElementById('check-level-9').addEventListener('click', checkLevel9);
document.getElementById('check-level-10').addEventListener('click', checkLevel10);
document.getElementById('check-level-11').addEventListener('click', checkLevel11);
document.getElementById('check-level-12').addEventListener('click', checkLevel12);
document.getElementById('check-level-13').addEventListener('click', checkLevel13);
document.getElementById('skip-level-13').addEventListener('click', skipLevel13);
document.getElementById('check-level-14').addEventListener('click', checkLevel14);
document.getElementById('check-level-15').addEventListener('click', checkLevel15);
document.getElementById('check-level-16').addEventListener('click', checkLevel16);
document.getElementById('check-level-17').addEventListener('click', checkLevel17);
// Navigation buttons
document.getElementById('prev-button').addEventListener('click', goToPreviousLevel);
document.getElementById('next-button').addEventListener('click', goToNextLevel);
// Listen for hash changes (browser back/forward)
window.addEventListener('hashchange', () => {
const hash = window.location.hash.slice(1);
if (hash && isValidLevel(hash)) {
gameState.currentLevel = getLevelNumberFromHash(hash);
updateUI();
}
});
// Pending validations (auto re-check for delayed API indexing)
startPendingCheckPoller();
});
/**
* Update the entire UI based on game state
*/
function updateUI() {
// Hide all sections
document.querySelectorAll('.level-card, .victory-card').forEach(section => {
section.setAttribute('hidden', '');
});
// Show current section
const currentSection = document.getElementById(`level-${gameState.currentLevel}`);
if (currentSection) {
currentSection.removeAttribute('hidden');
// Move focus to the section for accessibility
const heading = currentSection.querySelector('h2');
if (heading) {
setTimeout(() => heading.focus(), 100);
}
} else if (gameState.currentLevel >= 18) {
// Victory screen
const victorySection = document.getElementById('victory');
victorySection.removeAttribute('hidden');
const victoryHeading = victorySection.querySelector('h2');
if (victoryHeading) {
setTimeout(() => victoryHeading.focus(), 100);
}
// Set the profile link
document.getElementById('profile-link').href = `https://github.com/${gameState.username}`;
}
// Update user status with personalized greeting
if (gameState.username) {
const profile = gameState.userProfile;
const greeting = profile && profile.name
? `Welcome, ${profile.name}! You have ${profile.followers} followers and ${profile.publicRepos} repositories. Splendid! You're on Level ${gameState.currentLevel}.`
: `Welcome, ${gameState.username}! You're on Level ${gameState.currentLevel}.`;
document.getElementById('user-status').textContent = greeting;
}
// Update navigation UI
updateNavigationUI();
// Update train position based on current level
const train = document.querySelector('.seuss-train');
if (train) {
train.setAttribute('data-progress', gameState.currentLevel);
}
// Update URL hash to reflect current level
updateURLHash();
}
/**
* Update all repository links
*/
/**
* Update the URL hash to reflect current level
*/
function updateURLHash() {
const hash = getLevelHashName(gameState.currentLevel);
window.history.pushState(null, null, `#${hash}`);
}
/**
* Get hash name for a level number
*/
function getLevelHashName(levelNumber) {
if (levelNumber >= 18) {
return 'victory';
}
if (levelNumber >= 0 && levelNumber <= 17) {
return `level${levelNumber}`;
}
return 'level0';
}
/**
* Get level number from hash string
*/
function getLevelNumberFromHash(hash) {
const value = hash.toLowerCase();
if (value === 'victory') return 18;
if (value.startsWith('level')) {
const num = parseInt(value.replace('level', ''), 10);
if (!Number.isNaN(num) && num >= 0 && num <= 17) {
return num;
}
}
return 0;
}
/**
* Check if hash is a valid level
*/
function isValidLevel(hash) {
const value = hash.toLowerCase();
if (value === 'victory') return true;
if (value.startsWith('level')) {
const num = parseInt(value.replace('level', ''), 10);
return !Number.isNaN(num) && num >= 0 && num <= 17;
}
return false;
}
/**
* Handle login (Level 0)
*/
async function handleLoginClick() {
const input = document.getElementById('github-username');
const username = input.value.trim();
const errorMsg = document.getElementById('level-0-error');
if (!username) {
errorMsg.textContent = 'Please enter a GitHub username.';
errorMsg.classList.add('show');
return;
}
errorMsg.classList.remove('show');
// Check if user exists
try {
const response = await fetch(
`${GITHUB_API_BASE}/users/${username}`,
{ headers: API_HEADERS }
);
if (response.ok) {
const profile = await response.json();
gameState.username = username;
gameState.userProfile = {
name: profile.name || profile.login,
bio: profile.bio,
location: profile.location,
followers: profile.followers,
publicRepos: profile.public_repos,
avatarUrl: profile.avatar_url
};
gameState.currentLevel = 1;
saveGameState();
updateUI();
} else if (response.status === 404) {
errorMsg.textContent = `User "${username}" not found. Sign up at github.com/signup.`;
errorMsg.classList.add('show');
} else {
errorMsg.textContent = 'Error checking username. Please try again.';
errorMsg.classList.add('show');
}
} catch (e) {
errorMsg.textContent = 'Network error. Please check your connection.';
errorMsg.classList.add('show');
console.error('Login error:', e);
}
}
/**
* Level 1: Check if user commented on Issue #1
*/
async function checkLevel1(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-1-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const url = `${GITHUB_API_BASE}/repos/${REPO_OWNER}/${REPO_NAME}/issues/1/comments`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const comments = await response.json();
const userComment = comments.some(c => c.user.login === gameState.username);
if (userComment) {
markLevelCompleted(1);
gameState.currentLevel = Math.max(gameState.currentLevel, 2);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
// If this is a background recheck, pull the player back if needed
if (recheck) {
markLevelPending(1);
if (gameState.currentLevel > 1) {
gameState.currentLevel = 1;
saveGameState();
updateUI();
}
return false;
}
// First-attempt failure: save as pending and allow progress
const issueUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues/1`;
markLevelPending(1);
gameState.currentLevel = Math.max(gameState.currentLevel, 2);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong> because GitHub may delay search.<br>We'll re-check automatically and bring you back if needed.<br><br>Verify at: <a href="${issueUrl}" target="_blank" rel="noopener">${issueUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking comments. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 1 error:', e);
return false;
}
}
/**
* Level 2: Check if user created an issue with "Anchor" or "Tight" in the title
*/
async function checkLevel2(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-2-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
const foundIssue = result.items && result.items.length > 0;
if (foundIssue) {
markLevelCompleted(2);
gameState.currentLevel = Math.max(gameState.currentLevel, 3);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
if (recheck) {
markLevelPending(2);
if (gameState.currentLevel > 2) {
gameState.currentLevel = 2;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(2);
gameState.currentLevel = Math.max(gameState.currentLevel, 3);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong> because GitHub search may lag 30–60s.<br>We'll re-check automatically and return you to this step if it still isn't found.<br><br>Make sure the issue is <strong>closed</strong> and includes your follow-up comment.<br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking issues. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 2 error:', e);
return false;
}
}
/**
* Level 3: Edit issue title
*/
async function checkLevel3(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-3-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
// Find the user's issue
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
if (result.items && result.items.length > 0) {
const issue = result.items[0];
const issueNumber = issue.number;
// Check if the issue has been edited (updated_at > created_at)
const edited = await issueHasBeenEdited(issueNumber);
if (edited) {
markLevelCompleted(3);
gameState.currentLevel = Math.max(gameState.currentLevel, 4);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
}
if (recheck) {
markLevelPending(3);
if (gameState.currentLevel > 3) {
gameState.currentLevel = 3;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(3);
gameState.currentLevel = Math.max(gameState.currentLevel, 4);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>. Make sure you've edited the issue title.<br><br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking issue edits. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 3 error:', e);
return false;
}
}
/**
* Level 4: Edit issue body/description
*/
async function checkLevel4(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-4-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
if (result.items && result.items.length > 0) {
const issue = result.items[0];
const issueNumber = issue.number;
// Check if the issue has been edited
const edited = await issueHasBeenEdited(issueNumber);
if (edited) {
markLevelCompleted(4);
gameState.currentLevel = Math.max(gameState.currentLevel, 5);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
}
if (recheck) {
markLevelPending(4);
if (gameState.currentLevel > 4) {
gameState.currentLevel = 4;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(4);
gameState.currentLevel = Math.max(gameState.currentLevel, 5);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>. Edit the issue description/body text.<br><br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking issue. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 4 error:', e);
return false;
}
}
/**
* Level 5: Edit a comment
*/
async function checkLevel5(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-5-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
if (result.items && result.items.length > 0) {
const issue = result.items[0];
const issueNumber = issue.number;
// Fetch comments on the issue
const commentsUrl = `${GITHUB_API_BASE}/repos/${REPO_OWNER}/${REPO_NAME}/issues/${issueNumber}/comments`;
const commentsResponse = await fetch(commentsUrl, { headers: API_HEADERS });
if (commentsResponse.ok) {
const comments = await commentsResponse.json();
// Check if any user comment has been edited (updated_at > created_at)
const editedComment = comments.find(c =>
c.user.login === gameState.username &&
new Date(c.updated_at).getTime() > new Date(c.created_at).getTime() + 1000
);
if (editedComment) {
markLevelCompleted(5);
gameState.currentLevel = Math.max(gameState.currentLevel, 6);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
}
}
if (recheck) {
markLevelPending(5);
if (gameState.currentLevel > 5) {
gameState.currentLevel = 5;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(5);
gameState.currentLevel = Math.max(gameState.currentLevel, 6);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>. Edit one of your own comments on the issue.<br><br>Look for the ... menu on your comment and click "Edit".<br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking comments. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 5 error:', e);
return false;
}
}
/**
* Level 6: Reopen and close cycle
*/
async function checkLevel6(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-6-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
if (result.items && result.items.length > 0) {
const issue = result.items[0];
const issueNumber = issue.number;
// Fetch comments to check for activity after closure
const commentsUrl = `${GITHUB_API_BASE}/repos/${REPO_OWNER}/${REPO_NAME}/issues/${issueNumber}/comments`;
const commentsResponse = await fetch(commentsUrl, { headers: API_HEADERS });
if (commentsResponse.ok) {
const comments = await commentsResponse.json();
// Check if there are comments from user that were posted after multiple other interactions
const userCommentCount = comments.filter(c => c.user.login === gameState.username).length;
if (userCommentCount >= 2) { // At least opening + closing comment
markLevelCompleted(6);
gameState.currentLevel = Math.max(gameState.currentLevel, 7);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
}
}
if (recheck) {
markLevelPending(6);
if (gameState.currentLevel > 6) {
gameState.currentLevel = 6;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(6);
gameState.currentLevel = Math.max(gameState.currentLevel, 7);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>. Reopen the issue, add a clarification comment, then close it again.<br><br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking issue state. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 6 error:', e);
return false;
}
}
/**
* Level 7: Add a label
*/
async function checkLevel7(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-7-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
if (result.items && result.items.length > 0) {
const issue = result.items[0];
// Check if issue has at least one label
if (issue.labels && issue.labels.length > 0) {
markLevelCompleted(7);
gameState.currentLevel = Math.max(gameState.currentLevel, 8);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
}
if (recheck) {
markLevelPending(7);
if (gameState.currentLevel > 7) {
gameState.currentLevel = 7;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(7);
gameState.currentLevel = Math.max(gameState.currentLevel, 8);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>. Add a label to your issue from the Labels section.<br><br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking labels. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 7 error:', e);
return false;
}
}
/**
* Level 8: Self-assign the issue
*/
async function checkLevel8(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-8-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:issue state:closed author:${gameState.username} "Anchor" OR "Tight"`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
if (result.items && result.items.length > 0) {
const issue = result.items[0];
// Check if assigned to the user
const assigned = await issueIsAssignedToUser(issue.number, gameState.username);
if (assigned) {
markLevelCompleted(8);
gameState.currentLevel = Math.max(gameState.currentLevel, 9);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
}
if (recheck) {
markLevelPending(8);
if (gameState.currentLevel > 8) {
gameState.currentLevel = 8;
saveGameState();
updateUI();
}
return false;
}
const issuesUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/issues`;
markLevelPending(8);
gameState.currentLevel = Math.max(gameState.currentLevel, 9);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>. Assign the issue to yourself from the Assignees section.<br><br>Verify at: <a href="${issuesUrl}" target="_blank" rel="noopener">${issuesUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking assignment. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 8 error:', e);
return false;
}
}
/**
* Level 9: Check if LICENSE file exists in repo (renamed from Level 3)
*/
async function checkLevel9() {
const errorMsg = document.getElementById('level-9-error');
errorMsg.classList.remove('show');
try {
const url = `${GITHUB_API_BASE}/repos/${REPO_OWNER}/${REPO_NAME}/contents/LICENSE`;
const response = await fetch(url, { headers: API_HEADERS });
if (response.ok) {
markLevelCompleted(9);
gameState.currentLevel = 10;
saveGameState();
updateUI();
} else if (response.status === 404) {
errorMsg.textContent = 'LICENSE file not found. Create it in your repository and try again.';
errorMsg.classList.add('show');
} else {
throw new Error(`GitHub API error: ${response.status}`);
}
} catch (e) {
errorMsg.textContent = 'Error checking LICENSE file. Please try again.';
errorMsg.classList.add('show');
console.error('Level 9 error:', e);
}
}
/**
* Level 10: Check if user has an open Pull Request (renamed from Level 4)
*/
async function checkLevel10(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-10-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
const query = `repo:${REPO_OWNER}/${REPO_NAME} is:pr author:${gameState.username} is:open`;
const url = `${GITHUB_API_BASE}/search/issues?q=${encodeURIComponent(query)}`;
const response = await fetch(url, { headers: API_HEADERS });
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const result = await response.json();
const foundPR = result.items && result.items.length > 0;
if (foundPR) {
markLevelCompleted(10);
gameState.currentLevel = 5; // Victory!
saveGameState();
if (!recheck) {
updateUI();
}
return true;
}
if (recheck) {
markLevelPending(10);
if (gameState.currentLevel > 10) {
gameState.currentLevel = 10;
saveGameState();
updateUI();
}
return false;
}
const prsUrl = `https://github.com/${REPO_OWNER}/${REPO_NAME}/pulls`;
markLevelPending(10);
gameState.currentLevel = Math.max(gameState.currentLevel, 5);
saveGameState();
errorMsg.innerHTML = `Saved as <strong>pending</strong>—PR search can take 30–60s.<br>We'll re-check automatically and bring you back if your PR isn't found.<br><br>Verify at: <a href="${prsUrl}" target="_blank" rel="noopener">${prsUrl}</a>`;
errorMsg.classList.add('show');
updateUI();
return false;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking Pull Requests. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 10 error:', e);
return false;
}
}
/**
* Level 11: Fork the repository
* Checks if user has forked the git-goat-gazette repo
*/
async function checkLevel11(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-11-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
// Check if fork exists
const forkUrl = `https://api.github.com/repos/${gameState.username}/git-goat-gazette`;
const response = await fetch(forkUrl, { headers: API_HEADERS });
if (response.status === 404) {
if (!recheck) {
errorMsg.textContent = 'Fork not found yet. Click the Fork button on CivicActions/git-goat-gazette and try again!';
errorMsg.classList.add('show');
}
return false;
}
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const fork = await response.json();
// Verify it's actually a fork
if (!fork.fork) {
if (!recheck) {
errorMsg.textContent = 'Found your repository, but it doesn\'t appear to be a fork. Make sure to use the Fork button!';
errorMsg.classList.add('show');
}
return false;
}
// Success!
gameState.currentLevel = 12;
gameState.completedLevels.push(11);
saveGameState();
if (!recheck) {
updateUI();
}
return true;
} catch (e) {
if (!recheck) {
errorMsg.textContent = 'Error checking fork. Please try again.';
errorMsg.classList.add('show');
}
console.error('Level 11 error:', e);
return false;
}
}
/**
* Level 12: Web edit a file
* Checks if user has made a commit in their fork (web edit)
*/
async function checkLevel12(options = {}) {
const { recheck = false } = options;
const errorMsg = document.getElementById('level-12-error');
if (!recheck) {
errorMsg.classList.remove('show');
}
try {
// Get commits from the fork
const commitsUrl = `https://api.github.com/repos/${gameState.username}/git-goat-gazette/commits`;
const response = await fetch(commitsUrl, { headers: API_HEADERS });
if (response.status === 404) {
if (!recheck) {
errorMsg.textContent = 'Fork not found. Have you completed Level 11?';
errorMsg.classList.add('show');
}
return false;
}
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const commits = await response.json();
// Check if there's at least one commit by the user
const userCommit = commits.find(commit =>
commit.commit.author.name.toLowerCase().includes(gameState.username.toLowerCase()) ||
(commit.author && commit.author.login === gameState.username)
);
if (!userCommit) {
if (!recheck) {
errorMsg.textContent = 'No commits found yet. Edit README.md in your fork using the pencil icon and try again!';
errorMsg.classList.add('show');
}
return false;
}
// Success!
gameState.currentLevel = 13;
gameState.completedLevels.push(12);