Tic tac toe works
This commit is contained in:
parent
f81fec6b9c
commit
620b19bff6
4 changed files with 50 additions and 29 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit a94993dc5a626605bdf45c45f398270fe33eaaf1
|
Subproject commit 2d5eaad6fdeb6e07d8b7f86ba53cda3e6351835a
|
|
@ -1,5 +1,5 @@
|
||||||
function playerCommand(player_id, command, args) {
|
function playerCommand(player_id, command, args) {
|
||||||
if (commaned == "hi") {
|
if (command == "hi") {
|
||||||
hi(player_id);
|
hi(player_id);
|
||||||
} else if (command == "set") {
|
} else if (command == "set") {
|
||||||
move(player_id, args);
|
move(player_id, args);
|
||||||
|
@ -13,12 +13,14 @@ var ROWS = 3;
|
||||||
function hi(player_id) {
|
function hi(player_id) {
|
||||||
var p1_id = GGS.localStorage.getItem("p1_id");
|
var p1_id = GGS.localStorage.getItem("p1_id");
|
||||||
var p2_id = GGS.localStorage.getItem("p2_id");
|
var p2_id = GGS.localStorage.getItem("p2_id");
|
||||||
if (p1_id == "") {
|
if (!p1_id) {
|
||||||
GGS.localStorage.setItem("p1_id", player_id);
|
GGS.localStorage.setItem("p1_id", player_id);
|
||||||
|
GGS.localStorage.setItem("next_player", 1);
|
||||||
GGS.sendCommand(player_id, "welcome", "1");
|
GGS.sendCommand(player_id, "welcome", "1");
|
||||||
} else if (p2_id == "") {
|
} else if (!p2_id) {
|
||||||
GGS.localStorage.setItem("p2_id", player_id);
|
GGS.localStorage.setItem("p2_id", player_id);
|
||||||
GGS.sendCommand(player_id, "welcome", "2");
|
GGS.sendCommand(player_id, "welcome", "2");
|
||||||
|
newGame();
|
||||||
} else {
|
} else {
|
||||||
GGS.sendCommand(player_id, "not_welcome", "Already have 2 players on this table");
|
GGS.sendCommand(player_id, "not_welcome", "Already have 2 players on this table");
|
||||||
}
|
}
|
||||||
|
@ -37,7 +39,6 @@ function move(player_id, args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valid) {
|
if (valid) {
|
||||||
|
|
||||||
var p = nextPlayer;
|
var p = nextPlayer;
|
||||||
var props = JSON.parse(args);
|
var props = JSON.parse(args);
|
||||||
var gameBoard = JSON.parse(GGS.localStorage.getItem("game_board"));
|
var gameBoard = JSON.parse(GGS.localStorage.getItem("game_board"));
|
||||||
|
@ -65,9 +66,8 @@ function move(player_id, args) {
|
||||||
GGS.localStorage.setItem("next_player", 1);
|
GGS.localStorage.setItem("next_player", 1);
|
||||||
GGS.sendCommand(p2_id, "yourturn", "");
|
GGS.sendCommand(p2_id, "yourturn", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
GGS.sendCommand(plaer_id, "warning", "Already set, chose something else.");
|
GGS.sendCommand(player_id, "warning", "Already set, chose something else.");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -126,14 +126,14 @@ function newGame() {
|
||||||
// Initiate game with empty rows and columns
|
// Initiate game with empty rows and columns
|
||||||
var gameBoard = [];
|
var gameBoard = [];
|
||||||
for (var i=0; i < ROWS; i++) {
|
for (var i=0; i < ROWS; i++) {
|
||||||
gameBoard[i] = [];
|
gameBoard[i] = [""];
|
||||||
for (var j=0; i < ROWS; i++) {
|
for (var j=0; j < ROWS; j++) {
|
||||||
gameBoard[i][j] = '';
|
gameBoard[i][j] = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GGS.localStorage.setItem("game_board", JSON.stringify(gameBoard));
|
GGS.localStorage.setItem("game_board", JSON.stringify(gameBoard));
|
||||||
GGS.sendCommandToAll("new_game", "");
|
GGS.sendCommandToAll("new_game", "");
|
||||||
GGS.sendCommandToAll("game_board", boardAsString(gameBoard));
|
GGS.sendCommandToAll("game_board", boardAsString(gameBoard));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,15 +23,15 @@ class GGSTTT:
|
||||||
|
|
||||||
#Create our dictionay and connect it
|
#Create our dictionay and connect it
|
||||||
dic = { "on_window1_destroy_event" : gtk.main_quit
|
dic = { "on_window1_destroy_event" : gtk.main_quit
|
||||||
,"on_x0y0_clicked" : lambda x: self.sendMove("{'x':0,'y':0}")
|
,"on_x0y0_clicked" : lambda x: self.sendMove("{\"x\":0,\"y\":0}")
|
||||||
,"on_x0y1_clicked" : lambda x: self.sendMove("{'x':0,'y':1}")
|
,"on_x0y1_clicked" : lambda x: self.sendMove("{\"x\":0,\"y\":1}")
|
||||||
,"on_x0y2_clicked" : lambda x: self.sendMove("{'x':0,'y':2}")
|
,"on_x0y2_clicked" : lambda x: self.sendMove("{\"x\":0,\"y\":2}")
|
||||||
,"on_x1y0_clicked" : lambda x: self.sendMove("{'x':1,'y':0}")
|
,"on_x1y0_clicked" : lambda x: self.sendMove("{\"x\":1,\"y\":0}")
|
||||||
,"on_x1y1_clicked" : lambda x: self.sendMove("{'x':1,'y':1}")
|
,"on_x1y1_clicked" : lambda x: self.sendMove("{\"x\":1,\"y\":1}")
|
||||||
,"on_x1y2_clicked" : lambda x: self.sendMove("{'x':1,'y':2}")
|
,"on_x1y2_clicked" : lambda x: self.sendMove("{\"x\":1,\"y\":2}")
|
||||||
,"on_x2y0_clicked" : lambda x: self.sendMove("{'x':2,'y':0}")
|
,"on_x2y0_clicked" : lambda x: self.sendMove("{\"x\":2,\"y\":0}")
|
||||||
,"on_x2y1_clicked" : lambda x: self.sendMove("{'x':2,'y':1}")
|
,"on_x2y1_clicked" : lambda x: self.sendMove("{\"x\":2,\"y\":1}")
|
||||||
,"on_x2y2_clicked" : lambda x: self.sendMove("{'x':2,'y':2}")
|
,"on_x2y2_clicked" : lambda x: self.sendMove("{\"x\":2,\"y\":2}")
|
||||||
,"on_connectBtn_clicked" : lambda x: self.doConnect()
|
,"on_connectBtn_clicked" : lambda x: self.doConnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,10 +53,6 @@ class GGSTTT:
|
||||||
"Content-Length: %s\n" % len(token)+
|
"Content-Length: %s\n" % len(token)+
|
||||||
"\n"+
|
"\n"+
|
||||||
token)
|
token)
|
||||||
self.s.send("Game-Command: hi\n" +
|
|
||||||
"Content-Type: text\n" +
|
|
||||||
"Content-Length: 0\n"+
|
|
||||||
"\n")
|
|
||||||
|
|
||||||
def sendMove(self, move):
|
def sendMove(self, move):
|
||||||
print "Sending move", move
|
print "Sending move", move
|
||||||
|
@ -93,15 +89,39 @@ class GGSTTT:
|
||||||
if defined == "false":
|
if defined == "false":
|
||||||
print "Defining game"
|
print "Defining game"
|
||||||
js = open("server.js").read()
|
js = open("server.js").read()
|
||||||
|
self.wTree.get_widget("token").set_text(table_token)
|
||||||
self.s.send("Server-Command: define\n"+
|
self.s.send("Server-Command: define\n"+
|
||||||
"Content-Type: text\n" +
|
"Content-Type: text\n" +
|
||||||
("Content-Length: %s\n" % len(js))+
|
"Content-Length: %s\n" % str(len(js))+
|
||||||
"\n%s" % js)
|
"\n" +
|
||||||
|
js)
|
||||||
|
if defined == "true":
|
||||||
|
self.s.send("Game-Command: hi\n" +
|
||||||
|
"Content-Type: text\n" +
|
||||||
|
"Content-Length: 0\n"+
|
||||||
|
"\n")
|
||||||
|
|
||||||
elif msg["Client-Command"] == "welcome":
|
elif msg["Client-Command"] == "welcome":
|
||||||
self.setStatus("You are player %s" % msg["data"])
|
self.setStatus("You are player %s" % msg["DATA"])
|
||||||
elif msg["Client-Command"] == "chat":
|
elif msg["Client-Command"] == "warning":
|
||||||
gobject.idle_add(self.updateChatText, msg["DATA"])
|
self.setStatus("Warning: %s" % msg["DATA"])
|
||||||
|
elif msg["Client-Command"] == "not_welcome":
|
||||||
|
self.setStatus("You are not welcome: %s" % msg["DATA"])
|
||||||
|
elif msg["Client-Command"] == "game_board":
|
||||||
|
self.wTree.get_widget("x0y0").set_label(msg["DATA"][0])
|
||||||
|
self.wTree.get_widget("x0y1").set_label(msg["DATA"][1])
|
||||||
|
self.wTree.get_widget("x0y2").set_label(msg["DATA"][2])
|
||||||
|
self.wTree.get_widget("x1y0").set_label(msg["DATA"][3])
|
||||||
|
self.wTree.get_widget("x1y1").set_label(msg["DATA"][4])
|
||||||
|
self.wTree.get_widget("x1y2").set_label(msg["DATA"][5])
|
||||||
|
self.wTree.get_widget("x2y0").set_label(msg["DATA"][6])
|
||||||
|
self.wTree.get_widget("x2y1").set_label(msg["DATA"][7])
|
||||||
|
self.wTree.get_widget("x2y2").set_label(msg["DATA"][8])
|
||||||
|
elif msg["Client-Command"] == "defined":
|
||||||
|
self.s.send("Game-Command: hi\n" +
|
||||||
|
"Content-Type: text\n" +
|
||||||
|
"Content-Length: 0\n"+
|
||||||
|
"\n")
|
||||||
elif msg["Client-Command"] == "lusers":
|
elif msg["Client-Command"] == "lusers":
|
||||||
print msg
|
print msg
|
||||||
gobject.idle_add(self.updateUsers, msg["DATA"])
|
gobject.idle_add(self.updateUsers, msg["DATA"])
|
||||||
|
|
|
@ -102,6 +102,7 @@ handle_cast({srv_cmd, "hello", _Headers, TableToken}, State) ->
|
||||||
{noreply, State#state{ table = TPid } }
|
{noreply, State#state{ table = TPid } }
|
||||||
end;
|
end;
|
||||||
handle_cast({srv_cmd, "define", _Headers, Data}, #state { table = Table } = State) ->
|
handle_cast({srv_cmd, "define", _Headers, Data}, #state { table = Table } = State) ->
|
||||||
|
erl:display(Data),
|
||||||
ggs_table:notify(Table, self(), {server, define, Data}),
|
ggs_table:notify(Table, self(), {server, define, Data}),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
handle_cast({game_cmd, Command, _Headers, Data}, #state { table = Table } = State) ->
|
handle_cast({game_cmd, Command, _Headers, Data}, #state { table = Table } = State) ->
|
||||||
|
|
Reference in a new issue