Get available files and new games with Ajax

master
Guillaume Dott 2014-12-09 12:47:40 +01:00
parent 562451fa5d
commit 156b052b63
5 changed files with 30 additions and 29 deletions

View File

@ -1,14 +1,18 @@
window.Nikoli = Nikoli = {}
class Nikoli.Game
constructor: (@board, @name) ->
constructor: (@board, @name, @url) ->
@name = 'nikoli' unless @name?
@url = "/data/#{@name}" unless @url?
@board.classList.add @name
@grid = document.createElement 'div'
@grid.classList.add 'game-container'
@board.appendChild @grid
@getFiles()
buttons_div = document.createElement 'div'
buttons = {check: 'Check', reset: 'Reset', newgame: 'New game', help: '?'}
@ -23,6 +27,7 @@ class Nikoli.Game
@board.querySelector('.check').addEventListener('click', @check.bind(this))
@board.querySelector('.reset').addEventListener('click', @reset.bind(this))
@board.querySelector('.newgame').addEventListener('click', @newgame.bind(this))
check: ->
errors = @errors()
@ -32,6 +37,28 @@ class Nikoli.Game
else
alert errors.map((el) -> el.message).join()
getFiles: ->
xmlhttp = new XMLHttpRequest()
xmlhttp.open("GET", "#{@url}.json")
xmlhttp.addEventListener('load', (evt) =>
@setFiles JSON.parse(evt.target.responseText))
xmlhttp.send()
setFiles: (files) ->
@files = files
@file = @files[0]
@newgame() unless @game?
newgame: ->
xmlhttp = new XMLHttpRequest()
xmlhttp.open("GET", "#{@url}/#{@file}.json")
xmlhttp.addEventListener('load', (evt) =>
@generate JSON.parse(evt.target.responseText))
xmlhttp.send()
reset: ->
@generate()

View File

@ -1,6 +1,6 @@
class Nikoli.Hitori extends Nikoli.Game
constructor: (@board, @name = 'hitori') ->
super @board, @name
constructor: (@board, @name = 'hitori', @url = null) ->
super @board, @name, @url
errors: ->
solution = @toArray()

View File

@ -6,17 +6,5 @@ h1 Hitori
script type="text/javascript" src="/hitori.js"
javascript:
document.addEventListener("DOMContentLoaded", function() {
var game = [
[4,5,6,8,3,7,8,1],
[1,1,1,4,8,6,7,2],
[5,4,7,6,3,1,2,8],
[7,4,5,3,2,8,1,6],
[2,2,2,7,1,4,4,4],
[8,7,4,5,5,2,6,3],
[6,7,8,5,5,4,3,2],
[3,3,3,2,4,6,1,7]
];
hitori = new Nikoli.Hitori(document.getElementById('board'));
hitori.generate(game);
});

View File

@ -7,11 +7,4 @@ script type="text/javascript" src="/nurikabe.js"
javascript:
document.addEventListener("DOMContentLoaded", function() {
nurikabe = new Nikoli.Nurikabe(document.getElementById('board'));
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "#{url('/data/nurikabe/5x5.json')}");
xmlhttp.addEventListener('load', function(evt) {
nurikabe.generate(JSON.parse(evt.target.responseText)) })
xmlhttp.send();
});

View File

@ -7,11 +7,4 @@ script type="text/javascript" src="/sudoku.js"
javascript:
document.addEventListener("DOMContentLoaded", function() {
sudoku = new Nikoli.Sudoku(document.getElementById('board'));
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "#{url('/data/sudoku/gnome-sudoku_easy.json')}");
xmlhttp.addEventListener('load', function(evt) {
sudoku.generate(JSON.parse(evt.target.responseText)) })
xmlhttp.send();
});