Skip to content

Commit

Permalink
Attempt fix for failing daily word resets
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurorum committed Sep 24, 2024
1 parent 4b5f864 commit b8a783b
Showing 1 changed file with 36 additions and 49 deletions.
85 changes: 36 additions & 49 deletions dictum/scripts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let canType = false;
let hasLoaded = false;
let isDailyWord = true;
let selectedOption = 'any-list';
let selectedList;
Expand All @@ -22,11 +23,11 @@ window.onload = function () {

buildStats();

if ( localStorage.getItem( 'dailyWordLastTime' ) ) {
let dailyWordLastTime = localStorage.getItem( 'dailyWordLastTime' );

if ( dailyWordLastTime ) {
let currentDay = parseInt( timeDifference( Date.now(), unix ) );
let lastPlayedDay = parseInt(
timeDifference( parseInt( localStorage.getItem( 'dailyWordLastTime' ) ), unix )
);
let lastPlayedDay = parseInt( timeDifference( parseInt( dailyWordLastTime ), unix ) );

if ( currentDay !== lastPlayedDay ) {
localStorage.removeItem( 'dailyWord' );
Expand All @@ -40,6 +41,15 @@ window.onload = function () {
'dictum_streak_lost_inactivity'
);
}

collectData(
'currentDay: ' +
currentDay +
'\nlastPlayedDay: ' +
lastPlayedDay +
'\ndailyWordLastTime: ' +
dailyWordLastTime
);
}

if ( new URLSearchParams( window.location.search ).get( 'ref' ) ) {
Expand All @@ -50,6 +60,8 @@ window.onload = function () {
navigator.userAgent
);
}

hasLoaded = true;
};

window.onerror = function ( message, source, line, col, error ) {
Expand Down Expand Up @@ -127,13 +139,23 @@ function loadAllVocabFiles() {
}

function startGame( type ) {
if ( typeof anyList === 'undefined' || typeof validGuesses === 'undefined' ) {
if ( ! hasLoaded || typeof anyList === 'undefined' || typeof validGuesses === 'undefined' ) {
document.getElementById( 'loading-warning' ).style.display = 'block';
collectData( 'Started before initialisation', 'dictum_initialisation_error' );

if ( ! hasLoaded && typeof anyList !== 'undefined' ) {
collectData(
'Started before initialisation with onload incomplete',
'dictum_initialisation_onload_error'
);
}

checkVocabLoaded( type );
return;
}

selectedWord = findWord();

if ( type === 'streaks' ) {
isDailyWord = false;
document.body.classList.add( 'is-streaks' );
Expand All @@ -143,27 +165,28 @@ function startGame( type ) {
if ( type !== 'streaks' && localStorage.getItem( 'dailyWord' ) ) {
document.getElementById( 'game' ).innerHTML = localStorage.getItem( 'dailyWord' );
collectData( 'Continued Daily Word', 'dictum_continued_daily_word' );

if ( selectedWord !== localStorage.getItem( 'dailyWord' ) ) {
collectData( 'Mismatch between selectedWord and dailyWord' );
}
}

if ( type !== 'streaks' ) {
selectedList = asLevelList;
localStorage.setItem( 'dailyWordLastTime', Date.now() );
collectData( 'Started Daily Word', 'dictum_started_daily_word' );
document.body.classList.add( 'is-daily-word' );
}

document.body.classList.add( 'is-daily-word' );
document.body.classList.remove( 'is-displaying-modal' );
selectedWord = findWord();
canType = ! document.getElementById( 'game-board' ).classList.contains( 'is-completed' );
}

function checkVocabLoaded( type ) {
if ( typeof anyList !== 'undefined' && typeof validGuesses !== 'undefined' ) {
if ( typeof anyList !== 'undefined' && typeof validGuesses !== 'undefined' && hasLoaded ) {
startGame( type );
} else {
setTimeout( function () {
checkVocabLoaded( type );
}, 100 );
setTimeout( checkVocabLoaded, 100, type );
}
}

Expand Down Expand Up @@ -381,7 +404,6 @@ function removeLetter() {

function checkAnswer() {
let targetWord = selectedWord.word;
let wordSplit = targetWord.split( '' );

let gameBoardId = document.querySelectorAll( '.game-row.is-not-completed' )[ 0 ].id;
let fullSubmission = [];
Expand Down Expand Up @@ -835,54 +857,19 @@ function collectData( content, analyticsID ) {
}

function keyPress( e, manual ) {
let keyNo;
let keyNo = manual ? null : e.keyCode || e.which;

if ( ! manual ) {
if ( window.event ) {
keyNo = e.keyCode;
} else if ( e.which ) {
keyNo = e.which;
}

if ( keyNo === 8 ) {
return removeLetter();
}

if ( keyNo === 13 ) {
return checkAnswer();
}
}

let keyCode = manual ? e : String.fromCharCode( keyNo ).toLowerCase();

let validKeys = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
];
let validKeys = 'abcdefghijklmnopqrstuvwxyz';

if ( validKeys.includes( keyCode ) ) {
addLetter( keyCode );
Expand Down

0 comments on commit b8a783b

Please sign in to comment.