From 68312b82d11b2a745317dc041048b01c9e7d68c7 Mon Sep 17 00:00:00 2001 From: s2 Date: Mon, 4 Dec 2017 14:37:27 +0100 Subject: [PATCH] change demo url --- README.md | 3 +- app/js/config.js | 2 +- app/js/index.js | 140 ++++++++++++++++++++++++---------------------- createwordlist.js | 51 +++++++++-------- 4 files changed, 103 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 946f573..6f3920c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ Similar Words Game. ============ -A demo is on http://w.r0xy.it/ - +A demo is on https://similarwords.31337.it/ To create your own wordlist just edit ```createwordlist.js``` and run it with ```node```. diff --git a/app/js/config.js b/app/js/config.js index 6384875..d60e232 100644 --- a/app/js/config.js +++ b/app/js/config.js @@ -1,2 +1,2 @@ var sw = {}; -sw.apiPrefix = './'; +sw.apiPrefix = './'; diff --git a/app/js/index.js b/app/js/index.js index 861c36a..4d77117 100644 --- a/app/js/index.js +++ b/app/js/index.js @@ -1,13 +1,13 @@ -if (typeof(sw) == 'undefined') { +if (typeof (sw) == 'undefined') { var sw = {}; - sw.apiPrefix = '/'; + sw.apiPrefix = '/'; } (function($) { - - - //helper functions + + + // helper functions var shuffle = function(myArray) { var i = myArray.length, j, tempi, tempj; if (i === 0) @@ -19,57 +19,66 @@ if (typeof(sw) == 'undefined') { myArray[i] = tempj; myArray[j] = tempi; } - + return myArray; }; - + var getKey = function(obj) { var word = []; - for(var k in obj) word.push(k); + for ( var k in obj) + word.push(k); return word[0]; }; - + var selectWordsForGame = function() { - //select a random word from the list + // select a random word from the list var words = sw.words[Math.round(Math.random() * sw.words.length)]; - + var word = getKey(words); var similar = shuffle(words[word]); similar = similar.slice(0, 3); similar.push(word); similar = shuffle(similar); - - return {word: word, similar: similar}; + + return { + word: word, + similar: similar + }; }; - + var startGame = function(word) { - if(typeof(word) == 'undefined') { + if (typeof (word) == 'undefined') { word = selectWordsForGame(); } sw.currentWord = word; $("#main").empty().html('views/index.ejs', word); $('.currentword').animate({ - opacity : 0 + opacity: 0 }, 1000, 'linear'); }; - + var gameOver = function() { var result = 'AWESOME!'; if (sw.finaltime > 33) { result = 'You can do better!'; - }; + } if (sw.finaltime > 36) { result = 'meh...'; - }; + } if (sw.wrongWords > 0 && sw.finaltime <= 32) { - result = 'You are fast, but you made errors'; - }; - - $("#main").empty().html('views/gameover.ejs', {result: result, time: sw.finaltime, wrong: sw.wrongWords, correct: sw.correctWords}); + result = 'You are fast, but you made errors'; + } + + $("#main").empty().html('views/gameover.ejs', { + result: result, + time: sw.finaltime, + wrong: sw.wrongWords, + correct: sw.correctWords + }); }; - + var startNewGame = function() { var playforrounds = 20; sw.correctWords = 0; @@ -77,13 +86,13 @@ if (typeof(sw) == 'undefined') { sw.wrongWords = 0; sw.time = Math.round((new Date()).getTime() / 1000); sw.gameover = false; - + var updateBadges = function() { $('.num-of-words-wrong').html(sw.wrongWords + (sw.wrongWords == 1 ? ' word' : ' words')); $('.num-of-words').html(sw.correctWords + (sw.correctWords == 1 ? ' word' : ' words')); $('.time-words').html(Math.round((new Date()).getTime() / 1000) - sw.time); - - if(playforrounds >= sw.rounds) { + + if (playforrounds >= sw.rounds) { setTimeout(function() { updateBadges(); }, 100); @@ -93,57 +102,57 @@ if (typeof(sw) == 'undefined') { } }; updateBadges(); - + startGame(); }; - + var loadDictionary = function(dict) { var w = $.ajax({ url: sw.apiPrefix + 'words_' + dict + '.json', dataType: 'json' }); - - + + w.done(function(words) { - var start = $('.start'); + var start = $('.start'); start.removeClass('disabled'); start.html(' Start '); start.animate({ - opacity : 1 + opacity: 1 }, 1000, 'linear'); - + sw.words = words.words; - }); - + }); + w.fail(function(err) { console.log(err); }); - + return w; }; - - //events + + // events $(document).on('click', '.dict', function(ev) { var el = $(ev.currentTarget); var dict = el.attr('data-dict'); $('.start-btn').html('views/start-button.ejs', {}); loadDictionary(dict); - }); - + }); + $(document).on('click', '.start', function() { startNewGame(); }); $(document).on('click', '.word-button', function(ev) { var el = $(ev.currentTarget); - + if (el.hasClass('disabled')) { return; } $('.word-button').addClass('disabled'); - - var wrongWord = undefined; - if(sw.currentWord.word == el.html()) { + + var wrongWord; + if (sw.currentWord.word == el.html()) { sw.correctWords = sw.correctWords + 1; sw.rounds += 1; el.addClass('btn-success'); @@ -152,18 +161,18 @@ if (typeof(sw) == 'undefined') { sw.wrongWords = sw.wrongWords + 1; el.addClass('btn-danger'); } - + setTimeout(function() { - if(sw.gameover) { + if (sw.gameover) { gameOver(); } else { startGame(wrongWord); } }, 300); }); - - - //globals + + + // globals sw.words = []; sw.correctWords = 0; sw.wrongWords = 0; @@ -171,30 +180,29 @@ if (typeof(sw) == 'undefined') { sw.time = 0; sw.finaltime = 0; sw.gameover = false; - sw.currentWord; + sw.currentWord = ''; })(jQuery); -//utils +// utils (function() { window.Utils = {}; - - Utils.ellipsis = function(text, maxLength){ - if(typeof maxLength === 'undefined'){ - maxLength = 9000; //a large number - } - if (text.length <= maxLength) - return text; + Utils.ellipsis = function(text, maxLength) { + if (typeof maxLength === 'undefined') { + maxLength = 9000; // a large number + } - var xMaxFit = maxLength - 1; - var xTruncateAt = text.lastIndexOf(' ', xMaxFit); - if (xTruncateAt == -1 || xTruncateAt < maxLength / 2) - xTruncateAt = xMaxFit; + if (text.length <= maxLength) + return text; - return text.substr(0, xTruncateAt) + "…"; - }; + var xMaxFit = maxLength - 1; + var xTruncateAt = text.lastIndexOf(' ', xMaxFit); + if (xTruncateAt == -1 || xTruncateAt < maxLength / 2) + xTruncateAt = xMaxFit; + + return text.substr(0, xTruncateAt) + "…"; + }; })(); - diff --git a/createwordlist.js b/createwordlist.js index f559cff..6bd385d 100644 --- a/createwordlist.js +++ b/createwordlist.js @@ -1,8 +1,8 @@ fs = require('fs'); -//var dict = '/usr/share/dict/american-english'; +// var dict = '/usr/share/dict/american-english'; var dict = '/usr/share/dict/italian'; -//var dict = '/usr/share/dict/ngerman'; +// var dict = '/usr/share/dict/ngerman'; fs.readFile(dict, 'utf8', function(err, data) { if (err) { @@ -11,62 +11,65 @@ fs.readFile(dict, 'utf8', function(err, data) { var lines = data.split('\n'); console.log(lines.length + ' words in dictionary'); - + var word = ''; var words = []; - for(var i=0; i 6 && word.length < 10) { - //console.log('using ' + word); + // console.log('using ' + word); words.push(word); - }; + } + ; } console.log('using ' + words.length + ' words out of the dictionary'); - + var wildcardWord = function(word) { var words = []; - for(var i=1; i ' + words[j]); + // console.log(word + ' -> ' + words[j]); } } } - - //add to dictionary? - if(matches.length >= 4) { + + // add to dictionary? + if (matches.length >= 6) { console.log('using ' + word); var mw = {}; mw[word] = matches; matchingWordlist.push(mw); } - - if(w % 5000 == 0) { + + if (w % 5000 == 0) { console.log('intermediary results...'); console.log(matchingWordlist); } } - + console.log('final result'); - console.log(JSON.stringify(matchingWordlist)); + console.log(JSON.stringify({ + words: matchingWordlist + })); process.exit(0); });