Use XMLHttpRequest to load Nurikabe game data

master
Guillaume Dott 2014-12-08 18:02:28 +01:00
parent a2a70ab76c
commit 8fd83c8aa7
3 changed files with 1520 additions and 3 deletions

1502
data/nurikabe/5x5.yml 100644

File diff suppressed because it is too large Load Diff

View File

@ -2,13 +2,17 @@
require 'sinatra' require 'sinatra'
require 'sinatra/content_for' require 'sinatra/content_for'
require 'sinatra/json'
require 'yaml'
require 'coffee-script' require 'coffee-script'
require 'slim' require 'slim'
require 'sass' require 'sass'
GAMES = %i{hitori nurikabe sudoku} GAMES = %i{hitori nurikabe sudoku}
set :data_folder, File.expand_path('./data')
get('/application.css') { scss :application } get('/application.css') { scss :application }
get('/application.js') { coffee :application } get('/application.js') { coffee :application }
@ -18,3 +22,10 @@ GAMES.each do |game|
get("/#{game}") { slim game } get("/#{game}") { slim game }
get("/#{game}.js") { coffee game } get("/#{game}.js") { coffee game }
end end
get "/data/:game/:file.json" do |game, file|
data_file = File.join(settings.data_folder, game, "#{file}.yml")
halt(404) unless File.exist?(data_file)
json YAML.load_file(data_file).sample
end

View File

@ -6,8 +6,12 @@ h1 Nurikabe
script type="text/javascript" src="/nurikabe.js" script type="text/javascript" src="/nurikabe.js"
javascript: javascript:
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
var game = JSON.parse("[[4,-1,-1,0,2],[0,-1,2,-1,-1],[0,-1,0,-1,3],[0,-1,-1,-1,0],[-1,-1,1,-1,0]]");
nurikabe = new Nurikabe(document.getElementById('board')); nurikabe = new Nurikabe(document.getElementById('board'));
nurikabe.generate(game);
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();
}); });