Compare commits
No commits in common. "9e34b7a1d94a8db6a3a8c27f26267c5b96fdc13e" and "b616b5e324de46d11216817304da9950b41d7b77" have entirely different histories.
9e34b7a1d9
...
b616b5e324
|
@ -118,6 +118,17 @@
|
|||
}
|
||||
}
|
||||
|
||||
newGame() {
|
||||
let settings = this.getSettingsObjectFromForm();
|
||||
if (settings === null) return;
|
||||
|
||||
let game;
|
||||
do {
|
||||
game = Bagels.generateRandom(settings);
|
||||
} while (game === null && window.confirm('Unable to generate puzzle with the given difficulty settings. Try again?'));
|
||||
if (game !== null) this.initialize(game);
|
||||
}
|
||||
|
||||
get defaultSettings() {
|
||||
let default_settings = {
|
||||
'min_word_length': 4,
|
||||
|
@ -176,11 +187,15 @@
|
|||
ui.saveSettings();
|
||||
});
|
||||
|
||||
this.nextGame(game);
|
||||
}
|
||||
|
||||
get gameClass() {
|
||||
return Bagels;
|
||||
if (game === null) {
|
||||
let fragment = this.getRecentGameFragment();
|
||||
game = Bagels.fromFragment(fragment);
|
||||
}
|
||||
if (game === null) {
|
||||
this.newGame();
|
||||
} else {
|
||||
this.initialize(game);
|
||||
}
|
||||
}
|
||||
|
||||
initialize(game) {
|
||||
|
@ -322,7 +337,7 @@
|
|||
</label>
|
||||
</div>
|
||||
<div id="endgame">
|
||||
<button id="newgame">Next Game</button>
|
||||
<button id="newgame">New Game</button>
|
||||
</div>
|
||||
<div id="available_letters_display">
|
||||
<button id="shuffle"></button>
|
||||
|
|
|
@ -172,7 +172,7 @@ class Crossword {
|
|||
}
|
||||
}
|
||||
|
||||
get fragment() {
|
||||
getFragment() {
|
||||
let sortedLetters = this.availableLetters.sort();
|
||||
return this.minGuessLength + '|' + sortedLetters.join('') + '|'
|
||||
+ this.clues.map(clue => clue.toFragment(sortedLetters)).join(';');
|
||||
|
|
|
@ -15,22 +15,16 @@
|
|||
<script src="../entryui.js"></script>
|
||||
<script >
|
||||
class CrosswordUI extends AnagramEntryUI {
|
||||
submitWord(word) {
|
||||
submitWord() {
|
||||
if (this.won) return;
|
||||
let guess;
|
||||
if (word) {
|
||||
guess = word;
|
||||
} else {
|
||||
let seenSpace = false;
|
||||
for (let idx in this.letters) {
|
||||
if (this.letters[idx] == '') seenSpace = true;
|
||||
else if (seenSpace) return;
|
||||
}
|
||||
guess = this.letters.join('');
|
||||
let seenSpace = false;
|
||||
for (let idx in this.letters) {
|
||||
if (this.letters[idx] == '') seenSpace = true;
|
||||
else if (seenSpace) return;
|
||||
}
|
||||
let guess = this.letters.join('');
|
||||
let results = this.game.makeGuess(guess);
|
||||
if (results === null) return;
|
||||
this.saveGuess(guess);
|
||||
|
||||
this.updateCrossword(results);
|
||||
|
||||
|
@ -39,7 +33,7 @@
|
|||
|
||||
for (let i = 0; i < this.game.numLetters; i++) {
|
||||
let letter = document.createElement('td');
|
||||
letter.innerText = guess[i] || '';
|
||||
letter.innerText = this.letters[i];
|
||||
resultDisplay.appendChild(letter);
|
||||
}
|
||||
let link = document.createElement('a');
|
||||
|
@ -128,6 +122,17 @@
|
|||
return this.mayEnterLetterAt(letter, this.focusedTextbox);
|
||||
}
|
||||
|
||||
newGame() {
|
||||
let settings = this.getSettingsObjectFromForm();
|
||||
if (settings === null) return;
|
||||
|
||||
let game;
|
||||
do {
|
||||
game = Crossword.generateRandom(settings);
|
||||
} while (game === null && window.confirm('Unable to generate puzzle with the given difficulty settings. Try again?'));
|
||||
if (game !== null) this.initialize(game);
|
||||
}
|
||||
|
||||
get defaultSettings() {
|
||||
let default_settings = {
|
||||
'min_word_length': 4,
|
||||
|
@ -191,11 +196,11 @@
|
|||
ui.saveSettings();
|
||||
});
|
||||
|
||||
this.nextGame(game);
|
||||
}
|
||||
|
||||
get gameClass() {
|
||||
return Crossword;
|
||||
if (game === null) {
|
||||
this.newGame();
|
||||
} else {
|
||||
this.initialize(game);
|
||||
}
|
||||
}
|
||||
|
||||
initialize(game) {
|
||||
|
@ -203,7 +208,7 @@
|
|||
this.game = game;
|
||||
let ui = this;
|
||||
|
||||
scrambleString(game.fragment, ['', 'A', 'B']).then(fragment => {
|
||||
scrambleString(game.getFragment(), ['', 'A', 'B']).then(fragment => {
|
||||
document.getElementById('permalink').href = '#' + fragment;
|
||||
document.getElementById('permalink_input').value
|
||||
= window.location.href.split('#')[0] + '#' + fragment;
|
||||
|
@ -281,8 +286,6 @@
|
|||
document.getElementById('bonus_words_found').innerText = 0;
|
||||
document.getElementById('clues_total').innerText = game.clues.length;
|
||||
document.getElementById('top_words_total').innerText = game.allTopWords.size - game.clues.length;
|
||||
|
||||
this.doPostInitialize();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -320,9 +323,7 @@
|
|||
<a href="#" target="_blank" id="permalink">Permalink</a>
|
||||
to current puzzle:
|
||||
<input contenteditable id="permalink_input" />
|
||||
<button id="copy_permalink">Copy</button><br />
|
||||
<select id="available_games"></select>
|
||||
<button id="switch_game">Switch Game</button>
|
||||
<button id="copy_permalink">Copy</button>
|
||||
<h2>Difficulty</h2>
|
||||
<form id="difficulty" onsubmit="return false;">
|
||||
Word length:
|
||||
|
@ -385,7 +386,7 @@
|
|||
</label>
|
||||
</div>
|
||||
<div id="endgame">
|
||||
<button id="newgame">Next Game</button>
|
||||
<button id="newgame">New Game</button>
|
||||
</div>
|
||||
<div id="available_letters_display">
|
||||
<button id="shuffle"></button>
|
||||
|
|
|
@ -19,14 +19,7 @@ class AnagramEntryUI {
|
|||
document.getElementById('clear_nonlocked')
|
||||
.addEventListener('click', _ => ui.clearUnlocked());
|
||||
document.getElementById('newgame')
|
||||
.addEventListener('click', _ => ui.nextGame());
|
||||
document.getElementById('switch_game')
|
||||
.addEventListener('click', _ => {
|
||||
let fragment = document.getElementById('available_games').value;
|
||||
if (!ui.game || fragment != ui.game.fragment) {
|
||||
ui.nextGame(ui.gameClass.fromFragment(fragment));
|
||||
}
|
||||
});
|
||||
.addEventListener('click', _ => ui.newGame());
|
||||
document.addEventListener('keyup', event => {
|
||||
if (!ui.game || document.activeElement.nodeName.toUpperCase() === 'INPUT') {
|
||||
return;
|
||||
|
@ -83,7 +76,9 @@ class AnagramEntryUI {
|
|||
document.getElementById('newgame_button')
|
||||
.addEventListener('click', _ => {
|
||||
if (this.getSettingsObjectFromForm() === null) return;
|
||||
ui.newGame();
|
||||
if (ui.won || window.confirm('Lose all progress on this puzzle and generate a new one?')) {
|
||||
ui.newGame();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -283,8 +278,21 @@ class AnagramEntryUI {
|
|||
|
||||
setSettingsForm(obj) {
|
||||
let els = document.forms['difficulty'].elements;
|
||||
let gameFragment = this.game ? this.game.fragment : null;
|
||||
for (let key in obj) {
|
||||
if (key === 'auto_submit') continue;
|
||||
if (key === 'saved_games') {
|
||||
let saves = obj[key];
|
||||
let game_select = document.getElementById('available_games');
|
||||
clearElement(game_select);
|
||||
for (let fragment in saves) {
|
||||
let option = document.createElement('option');
|
||||
option.value = fragment;
|
||||
option.selected = fragment == gameFragment;
|
||||
option.innerText = (option.selected ? '[*] ' : '') + saves[fragment].last_played + " - " + saves[fragment].guesses.slice(-1)[0];
|
||||
game_select.appendChild(option);
|
||||
}
|
||||
}
|
||||
els[key].value = obj[key];
|
||||
}
|
||||
}
|
||||
|
@ -302,30 +310,6 @@ class AnagramEntryUI {
|
|||
}
|
||||
return res;
|
||||
}
|
||||
updateSavedGamesMenu(settings) {
|
||||
let saves = settings['saved_games'];
|
||||
let gameSelect = document.getElementById('available_games');
|
||||
let gameFragment = this.game ? this.game.fragment : null;
|
||||
clearElement(gameSelect);
|
||||
for (let fragment in saves) {
|
||||
let option = document.createElement('option');
|
||||
option.value = fragment;
|
||||
option.selected = fragment == gameFragment;
|
||||
option.innerText = (option.selected ? '[*] ' : '') + new Date(saves[fragment].last_played) + " - [" + saves[fragment].guesses.length + "] " + (saves[fragment].guesses.slice(-1)[0] || "(no guesses yet)");
|
||||
gameSelect.appendChild(option);
|
||||
}
|
||||
}
|
||||
|
||||
newGame() {
|
||||
let settings = this.getSettingsObjectFromForm();
|
||||
if (settings === null) return;
|
||||
|
||||
let game;
|
||||
do {
|
||||
game = this.gameClass.generateRandom(settings);
|
||||
} while (game === null && window.confirm('Unable to generate puzzle with the given difficulty settings. Try again?'));
|
||||
if (game !== null) this.initialize(game);
|
||||
}
|
||||
|
||||
doPostInitialize() {
|
||||
let fragment = this.game.fragment;
|
||||
|
@ -339,15 +323,14 @@ class AnagramEntryUI {
|
|||
currentSettings.saved_games[fragment].guesses.forEach(guess => {
|
||||
this.submitWord(guess);
|
||||
});
|
||||
currentSettings.saved_games[fragment].last_played = new Date().toJSON();
|
||||
currentSettings.saved_games[fragment].last_played = Date.now();
|
||||
} else {
|
||||
currentSettings.saved_games[fragment] = {
|
||||
'guesses': [],
|
||||
'last_played': new Date().toJSON()
|
||||
'last_played': Date.now()
|
||||
};
|
||||
}
|
||||
this.saveSettingsJson(currentSettings);
|
||||
this.updateSavedGamesMenu(currentSettings);
|
||||
}
|
||||
getRecentGameFragment() {
|
||||
let currentSettings = this.loadSettingsJson();
|
||||
|
@ -357,10 +340,8 @@ class AnagramEntryUI {
|
|||
if (!saves) return null;
|
||||
|
||||
let recentDate =
|
||||
Object.keys(saves)
|
||||
.map(fragment => saves[fragment].last_played)
|
||||
.sort()
|
||||
.slice(-1)[0];
|
||||
Math.max(...Object.keys(saves)
|
||||
.map(fragment => saves[fragment].last_played));
|
||||
for (let fragment in saves) {
|
||||
if (saves[fragment].last_played == recentDate) {
|
||||
return fragment;
|
||||
|
@ -368,36 +349,12 @@ class AnagramEntryUI {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
nextGame(game) {
|
||||
if (this.won) {
|
||||
this.finishGame();
|
||||
}
|
||||
if (!game) {
|
||||
let fragment = this.getRecentGameFragment();
|
||||
game = this.gameClass.fromFragment(fragment);
|
||||
}
|
||||
if (game === null) {
|
||||
this.newGame();
|
||||
} else {
|
||||
this.initialize(game);
|
||||
}
|
||||
}
|
||||
finishGame() {
|
||||
if (this.won) {
|
||||
let fragment = this.game.fragment;
|
||||
let currentSettings = this.loadSettingsJson();
|
||||
delete currentSettings.saved_games[fragment];
|
||||
this.saveSettingsJson(currentSettings);
|
||||
this.updateSavedGamesMenu(currentSettings);
|
||||
}
|
||||
}
|
||||
saveGuess(guess) {
|
||||
let fragment = this.game.fragment;
|
||||
let currentSettings = this.loadSettingsJson();
|
||||
currentSettings.saved_games[fragment].guesses.push(guess);
|
||||
currentSettings.saved_games[fragment].last_played = new Date().toJSON();
|
||||
currentSettings.saved_games[fragment].last_played = Date.now();
|
||||
this.saveSettingsJson(currentSettings);
|
||||
this.updateSavedGamesMenu(currentSettings);
|
||||
}
|
||||
loadSettingsJson() {
|
||||
let json = window.localStorage.getItem(this.settingsKey);
|
||||
|
@ -423,9 +380,6 @@ class AnagramEntryUI {
|
|||
if ('auto_submit' in obj) {
|
||||
document.getElementById('auto_submit').checked = obj.auto_submit;
|
||||
}
|
||||
if ('saved_games' in obj) {
|
||||
this.updateSavedGamesMenu(obj);
|
||||
}
|
||||
this.setSettingsForm(settings);
|
||||
} else {
|
||||
this.setSettingsForm(default_settings);
|
||||
|
|
Loading…
Reference in New Issue
Block a user