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>
|
||||
var game_name ="tictactoe"
|
||||
var GGS = new GGSI(game_name);
|
||||
var GGS = new GGS(game_name);
|
||||
|
||||
function init() {
|
||||
GameServerI.addGame(game_name, main());
|
||||
GameServerI.addClient(game_name, new TicTacToeClient(frames.player1.document.getElementById("p1"), GameServerI));
|
||||
GameServerI.addClient(game_name, new TicTacToeClient(frames.player2.document.getElementById("p2"), GameServerI));
|
||||
GameServer.addGame(game_name, main());
|
||||
GameServer.addClient(game_name, new TicTacToeClient(frames.player1.document.getElementById("p1"), GameServer));
|
||||
GameServer.addClient(game_name, new TicTacToeClient(frames.player2.document.getElementById("p2"), GameServer));
|
||||
}
|
||||
</script>
|
||||
<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");
|
||||
this.__defineGetter__("world", function(){
|
||||
return world;
|
||||
|
@ -11,6 +11,6 @@ function GGSI(game_name) {
|
|||
|
||||
var game_n = game_name;
|
||||
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") {
|
||||
var user = null;
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ GameServer.prototype.clear = function(game_name, type) {
|
|||
} else throw "GGS: Unknown game " + game_name
|
||||
}
|
||||
|
||||
var GameServerI = new GameServer();
|
||||
var GameServer = new GameServer();
|
||||
|
||||
|
||||
|
||||
|
@ -143,9 +143,11 @@ function User(id, client) {
|
|||
|
||||
return {
|
||||
id: id,
|
||||
client: client,
|
||||
sendCommand: function(command, args) {
|
||||
client.commandCalled(command, args);
|
||||
},
|
||||
isClient: function(c) {
|
||||
return c == client;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -159,24 +161,24 @@ function Storage(game_name, type) {
|
|||
|
||||
return {
|
||||
setItem: function(key, value) {
|
||||
GameServerI.set(self.gameName, self.type, key, value);
|
||||
GameServer.set(self.gameName, self.type, key, value);
|
||||
},
|
||||
getItem: function(key) {
|
||||
return GameServerI.get(self.gameName, self.type, key);
|
||||
return GameServer.get(self.gameName, self.type, key);
|
||||
},
|
||||
key: function(position) {
|
||||
return GameServerI.key(self.gameName, self.type, position);
|
||||
return GameServer.key(self.gameName, self.type, position);
|
||||
},
|
||||
length: {
|
||||
get: function() {
|
||||
return GameServerI.length(self.gameName, self.type);
|
||||
return GameServer.length(self.gameName, self.type);
|
||||
}
|
||||
},
|
||||
removeItem: function(key) {
|
||||
GameServerI.remove(self.gameName, self.type, key);
|
||||
GameServer.remove(self.gameName, self.type, key);
|
||||
},
|
||||
clear: function() {
|
||||
GameServerI.clear(self.gameName, self.type);
|
||||
GameServer.clear(self.gameName, self.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) {
|
||||
if(GGS.users.length <= 2) return true
|
||||
else return false;
|
||||
|
@ -58,8 +81,10 @@ TicTacToe.prototype.userCommand = function(user, command, args) {
|
|||
var gameBoard = JSON.parse(GGS.world.getItem("game_board"));
|
||||
|
||||
if (gameBoard[props.x][props.y] == 0) {
|
||||
|
||||
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 (p == 1) {
|
||||
this.getUser(p1_id).sendCommand("winner", "You win!");
|
||||
|
@ -92,44 +117,36 @@ TicTacToe.prototype.checkIfWon = function(player, gameBoard) {
|
|||
var rows = gameBoard.length;
|
||||
|
||||
for (i = 0; i < rows; ++i) {
|
||||
for (j = 0; j < rows; ++j) {
|
||||
if (gameBoard[i][j] != player) {
|
||||
for (j = 0; j < rows; ++j)
|
||||
if (gameBoard[i][j] != player)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == rows) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (j = 0; j < rows; ++j) {
|
||||
if (gameBoard[j][i] != player) {
|
||||
if (j == rows)
|
||||
return true;
|
||||
|
||||
for (j = 0; j < rows; ++j)
|
||||
if (gameBoard[j][i] != player)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == rows) {
|
||||
|
||||
if (j == rows)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Now check diagnols
|
||||
for (i = 0; i < rows; ++i) {
|
||||
if (gameBoard[i][i] != player) {
|
||||
for (i = 0; i < rows; ++i)
|
||||
if (gameBoard[i][i] != player)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (i == rows) {
|
||||
if (i == rows)
|
||||
return true;
|
||||
}
|
||||
|
||||
for (i = 0; i < rows; ++i) {
|
||||
if (gameBoard[i][rows - i - 1] != player) {
|
||||
for (i = 0; i < rows; ++i)
|
||||
if (gameBoard[i][rows - i - 1] != player)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == rows) {
|
||||
|
||||
if (i == rows)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
// 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) {
|
||||
this.server = server;
|
||||
|
@ -6,7 +8,6 @@ function TicTacToeClient(container, server) {
|
|||
this.init();
|
||||
}
|
||||
|
||||
|
||||
TicTacToeClient.prototype.init = function() {
|
||||
this.rows = 3;
|
||||
this.spots = [];
|
||||
|
|
Reference in a new issue