Skip to content

Commit

Permalink
initial save support
Browse files Browse the repository at this point in the history
  • Loading branch information
psema4 committed Nov 13, 2012
1 parent 143f65e commit 7cba737
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions games/.gitignore
@@ -0,0 +1 @@
game_*.json
1 change: 1 addition & 0 deletions games/README.md
@@ -0,0 +1 @@
Temporary storage for game data
19 changes: 19 additions & 0 deletions server.js
@@ -1,10 +1,29 @@
var port = (process.env.NODE_ENV == 'production') ? 80 : 3000
, fs = require('fs')
, util = require('util')
, express = require('express')
, app = express()
, gameId = 0
, gameFolder = 'games'
;

app.use(express.bodyParser());
app.use(express.static(__dirname + '/static'));

app.post('/save', function(req, res) {
gameId++;

var gameData = req.body.data
, gameFilename = gameFolder+'/game_'+gameId+'.json'
;

fs.writeFile(gameFilename, gameData, function(err) {
var buf = gameFilename;
if (err) buf = 'ERR';
res.send(buf);
});
});

app.get('/', function (req, res) {
res.send('Hello');
});
Expand Down
3 changes: 3 additions & 0 deletions static/index.html
Expand Up @@ -97,9 +97,12 @@ <h4>Game</h4>
<ul>
<li><label for="game-title">Title</label><input id="game-title" class="setting" type="text" /></li>
<li><label for="game-subtitle">Subtitle</label><input id="game-subtitle" class="setting" type="text" /></li>
<!--
<li align="center"><br>Game.json</li>
<li align="center"><button id="export">Show</button> <button onclick="$('#export-out').style.visibility='hidden';">Hide</button></li>
<li align="center"><textarea id="export-out" readonly></textarea></li>
-->
<li align="center"><br /><button onclick="game.saveGame()">Save</button></li>
</ul>
</div>

Expand Down
23 changes: 23 additions & 0 deletions static/js/game.js
Expand Up @@ -482,6 +482,28 @@ try {
}

}

, saveGame = function() {
var data = 'data=' + JSON.stringify(game.getGameData())
, saveURL = '/save'
;

var xhr = new XMLHttpRequest();
xhr.open('POST', saveURL, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.responseType = 'text';

xhr.onload = function() {
var result = xhr.responseText;
if (result != 'ERR')
console.log('saved: ' + result)
else
console.warn('error saving game!')
;
}

xhr.send(encodeURI(data));
}
;

// Constructor
Expand Down Expand Up @@ -535,6 +557,7 @@ try {
, playerEventStart: playerEventStart
, playerEventStop: playerEventStop
, addWaveToLevel: addWaveToLevel
, saveGame: saveGame

// objects
, audio: audio
Expand Down

0 comments on commit 7cba737

Please sign in to comment.