simplified, added some documentation
This commit is contained in:
parent
20a07dbd6a
commit
8e468b5eec
5 changed files with 64 additions and 44 deletions
|
@ -8,12 +8,12 @@
|
||||||
<script src="js/TicTacToeClient.js"></script>
|
<script src="js/TicTacToeClient.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var game_name ="tictactoe"
|
var game_name ="tictactoe"
|
||||||
var GGS = new GGSI(game_name);
|
var GGS = new GGS(game_name);
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
GameServerI.addGame(game_name, main());
|
GameServer.addGame(game_name, main());
|
||||||
GameServerI.addClient(game_name, new TicTacToeClient(frames.player1.document.getElementById("p1"), GameServerI));
|
GameServer.addClient(game_name, new TicTacToeClient(frames.player1.document.getElementById("p1"), GameServer));
|
||||||
GameServerI.addClient(game_name, new TicTacToeClient(frames.player2.document.getElementById("p2"), GameServerI));
|
GameServer.addClient(game_name, new TicTacToeClient(frames.player2.document.getElementById("p2"), GameServer));
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen">
|
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen">
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
function GGSI(game_name) {
|
function GGS(game_name) {
|
||||||
var world = new Storage(game_name, "world");
|
var world = new Storage(game_name, "world");
|
||||||
this.__defineGetter__("world", function(){
|
this.__defineGetter__("world", function(){
|
||||||
return world;
|
return world;
|
||||||
|
@ -11,6 +11,6 @@ function GGSI(game_name) {
|
||||||
|
|
||||||
var game_n = game_name;
|
var game_n = game_name;
|
||||||
this.__defineGetter__("users", function(){
|
this.__defineGetter__("users", function(){
|
||||||
return GameServerI.users(game_n);
|
return GameServer.users(game_n);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ GameServer.prototype.callCommand = function(game_name, client, command, attrs) {
|
||||||
if(typeof game.instance.userCommand == "function") {
|
if(typeof game.instance.userCommand == "function") {
|
||||||
var user = null;
|
var user = null;
|
||||||
for (var i=0; i < game.users.length; i++) {
|
for (var i=0; i < game.users.length; i++) {
|
||||||
if (game.users[i].client == client) {
|
if (game.users[i].isClient(client)) {
|
||||||
user = game.users[i];
|
user = game.users[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ GameServer.prototype.clear = function(game_name, type) {
|
||||||
} else throw "GGS: Unknown game " + game_name
|
} else throw "GGS: Unknown game " + game_name
|
||||||
}
|
}
|
||||||
|
|
||||||
var GameServerI = new GameServer();
|
var GameServer = new GameServer();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -143,9 +143,11 @@ function User(id, client) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: id,
|
id: id,
|
||||||
client: client,
|
|
||||||
sendCommand: function(command, args) {
|
sendCommand: function(command, args) {
|
||||||
client.commandCalled(command, args);
|
client.commandCalled(command, args);
|
||||||
|
},
|
||||||
|
isClient: function(c) {
|
||||||
|
return c == client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,24 +161,24 @@ function Storage(game_name, type) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setItem: function(key, value) {
|
setItem: function(key, value) {
|
||||||
GameServerI.set(self.gameName, self.type, key, value);
|
GameServer.set(self.gameName, self.type, key, value);
|
||||||
},
|
},
|
||||||
getItem: function(key) {
|
getItem: function(key) {
|
||||||
return GameServerI.get(self.gameName, self.type, key);
|
return GameServer.get(self.gameName, self.type, key);
|
||||||
},
|
},
|
||||||
key: function(position) {
|
key: function(position) {
|
||||||
return GameServerI.key(self.gameName, self.type, position);
|
return GameServer.key(self.gameName, self.type, position);
|
||||||
},
|
},
|
||||||
length: {
|
length: {
|
||||||
get: function() {
|
get: function() {
|
||||||
return GameServerI.length(self.gameName, self.type);
|
return GameServer.length(self.gameName, self.type);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
removeItem: function(key) {
|
removeItem: function(key) {
|
||||||
GameServerI.remove(self.gameName, self.type, key);
|
GameServer.remove(self.gameName, self.type, key);
|
||||||
},
|
},
|
||||||
clear: function() {
|
clear: function() {
|
||||||
GameServerI.clear(self.gameName, self.type);
|
GameServer.clear(self.gameName, self.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else throw "GGS: No such storage available " + type;
|
} else throw "GGS: No such storage available " + type;
|
||||||
|
|
|
@ -1,8 +1,31 @@
|
||||||
function TicTacToe() {};
|
/*
|
||||||
TicTacToe.prototype.init = function() {
|
This code is public domain, it's just a example how a game
|
||||||
|
on our Generic Game Server could look like.
|
||||||
|
|
||||||
|
API:
|
||||||
|
|
||||||
|
GGS.world // webstorage interface
|
||||||
|
GGS.localStorage // webstorage interface
|
||||||
|
http://dev.w3.org/html5/webstorage/
|
||||||
|
|
||||||
|
GGS.users is a array with User objects:
|
||||||
|
User {
|
||||||
|
id // integer attribute
|
||||||
|
sendCommand: function(command, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
There are optional and mandatory methods a Game should implement:
|
||||||
|
|
||||||
|
Game main() // m - should return a instance of the game, is called at startup
|
||||||
|
userCommand(user, command, args) // m - is called when a client sends a message
|
||||||
|
userAllowed(user) // o - should return a bolean, is called when a new is added
|
||||||
|
userAdded(user) // o - is called when a client was added
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
function TicTacToe() {};
|
||||||
|
|
||||||
TicTacToe.prototype.userAllowed = function(user) {
|
TicTacToe.prototype.userAllowed = function(user) {
|
||||||
if(GGS.users.length <= 2) return true
|
if(GGS.users.length <= 2) return true
|
||||||
else return false;
|
else return false;
|
||||||
|
@ -58,8 +81,10 @@ TicTacToe.prototype.userCommand = function(user, command, args) {
|
||||||
var gameBoard = JSON.parse(GGS.world.getItem("game_board"));
|
var gameBoard = JSON.parse(GGS.world.getItem("game_board"));
|
||||||
|
|
||||||
if (gameBoard[props.x][props.y] == 0) {
|
if (gameBoard[props.x][props.y] == 0) {
|
||||||
|
|
||||||
gameBoard[props.x][props.y] = p;
|
gameBoard[props.x][props.y] = p;
|
||||||
GGS.world.setItem("game_board", JSON.stringify(gameBoard))
|
GGS.world.setItem("game_board", JSON.stringify(gameBoard));
|
||||||
|
|
||||||
if (this.checkIfWon(p, gameBoard)) {
|
if (this.checkIfWon(p, gameBoard)) {
|
||||||
if (p == 1) {
|
if (p == 1) {
|
||||||
this.getUser(p1_id).sendCommand("winner", "You win!");
|
this.getUser(p1_id).sendCommand("winner", "You win!");
|
||||||
|
@ -92,44 +117,36 @@ TicTacToe.prototype.checkIfWon = function(player, gameBoard) {
|
||||||
var rows = gameBoard.length;
|
var rows = gameBoard.length;
|
||||||
|
|
||||||
for (i = 0; i < rows; ++i) {
|
for (i = 0; i < rows; ++i) {
|
||||||
for (j = 0; j < rows; ++j) {
|
for (j = 0; j < rows; ++j)
|
||||||
if (gameBoard[i][j] != player) {
|
if (gameBoard[i][j] != player)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
if (j == rows) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (j = 0; j < rows; ++j) {
|
if (j == rows)
|
||||||
if (gameBoard[j][i] != player) {
|
return true;
|
||||||
|
|
||||||
|
for (j = 0; j < rows; ++j)
|
||||||
|
if (gameBoard[j][i] != player)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
if (j == rows)
|
||||||
if (j == rows) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now check diagnols
|
// Now check diagnols
|
||||||
for (i = 0; i < rows; ++i) {
|
for (i = 0; i < rows; ++i)
|
||||||
if (gameBoard[i][i] != player) {
|
if (gameBoard[i][i] != player)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i == rows) {
|
if (i == rows)
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < rows; ++i) {
|
for (i = 0; i < rows; ++i)
|
||||||
if (gameBoard[i][rows - i - 1] != player) {
|
if (gameBoard[i][rows - i - 1] != player)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
if (i == rows)
|
||||||
if (i == rows) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
// This first version doesn't take networking into account yet.
|
// This first version doesn't take networking into account yet.
|
||||||
|
// Only tested in FireFox, Safari, Chrome and Opera
|
||||||
|
// Will not work in IE <9 because getters and setters are used.
|
||||||
|
|
||||||
function TicTacToeClient(container, server) {
|
function TicTacToeClient(container, server) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
|
@ -6,7 +8,6 @@ function TicTacToeClient(container, server) {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
TicTacToeClient.prototype.init = function() {
|
TicTacToeClient.prototype.init = function() {
|
||||||
this.rows = 3;
|
this.rows = 3;
|
||||||
this.spots = [];
|
this.spots = [];
|
||||||
|
|
Reference in a new issue