From 620b19bff6ce775775131bafaeb9cbefded81253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20P=C3=A5lsson?= Date: Mon, 16 May 2011 16:29:27 +0200 Subject: [PATCH] Tic tac toe works --- doc/report | 2 +- games/tic-tac-toe-python/server.js | 20 +++++------ games/tic-tac-toe-python/ttt.py | 56 ++++++++++++++++++++---------- src/ggs_player.erl | 1 + 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/doc/report b/doc/report index a94993d..2d5eaad 160000 --- a/doc/report +++ b/doc/report @@ -1 +1 @@ -Subproject commit a94993dc5a626605bdf45c45f398270fe33eaaf1 +Subproject commit 2d5eaad6fdeb6e07d8b7f86ba53cda3e6351835a diff --git a/games/tic-tac-toe-python/server.js b/games/tic-tac-toe-python/server.js index d98eaf7..f924659 100644 --- a/games/tic-tac-toe-python/server.js +++ b/games/tic-tac-toe-python/server.js @@ -1,5 +1,5 @@ function playerCommand(player_id, command, args) { - if (commaned == "hi") { + if (command == "hi") { hi(player_id); } else if (command == "set") { move(player_id, args); @@ -13,12 +13,14 @@ var ROWS = 3; function hi(player_id) { var p1_id = GGS.localStorage.getItem("p1_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("next_player", 1); GGS.sendCommand(player_id, "welcome", "1"); - } else if (p2_id == "") { + } else if (!p2_id) { GGS.localStorage.setItem("p2_id", player_id); GGS.sendCommand(player_id, "welcome", "2"); + newGame(); } else { GGS.sendCommand(player_id, "not_welcome", "Already have 2 players on this table"); } @@ -37,7 +39,6 @@ function move(player_id, args) { } if (valid) { - var p = nextPlayer; var props = JSON.parse(args); 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.sendCommand(p2_id, "yourturn", ""); } - } else { - GGS.sendCommand(plaer_id, "warning", "Already set, chose something else."); + GGS.sendCommand(player_id, "warning", "Already set, chose something else."); } } else { @@ -126,14 +126,14 @@ function newGame() { // Initiate game with empty rows and columns var gameBoard = []; for (var i=0; i < ROWS; i++) { - gameBoard[i] = []; - for (var j=0; i < ROWS; i++) { - gameBoard[i][j] = ''; + gameBoard[i] = [""]; + for (var j=0; j < ROWS; j++) { + gameBoard[i][j] = ""; } } GGS.localStorage.setItem("game_board", JSON.stringify(gameBoard)); - GGS.sendCommandToAll("new_game", ""); + GGS.sendCommandToAll("new_game", ""); GGS.sendCommandToAll("game_board", boardAsString(gameBoard)); } diff --git a/games/tic-tac-toe-python/ttt.py b/games/tic-tac-toe-python/ttt.py index 06ef9bd..6cbe104 100644 --- a/games/tic-tac-toe-python/ttt.py +++ b/games/tic-tac-toe-python/ttt.py @@ -23,15 +23,15 @@ class GGSTTT: #Create our dictionay and connect it dic = { "on_window1_destroy_event" : gtk.main_quit - ,"on_x0y0_clicked" : lambda x: self.sendMove("{'x':0,'y':0}") - ,"on_x0y1_clicked" : lambda x: self.sendMove("{'x':0,'y':1}") - ,"on_x0y2_clicked" : lambda x: self.sendMove("{'x':0,'y':2}") - ,"on_x1y0_clicked" : lambda x: self.sendMove("{'x':1,'y':0}") - ,"on_x1y1_clicked" : lambda x: self.sendMove("{'x':1,'y':1}") - ,"on_x1y2_clicked" : lambda x: self.sendMove("{'x':1,'y':2}") - ,"on_x2y0_clicked" : lambda x: self.sendMove("{'x':2,'y':0}") - ,"on_x2y1_clicked" : lambda x: self.sendMove("{'x':2,'y':1}") - ,"on_x2y2_clicked" : lambda x: self.sendMove("{'x':2,'y':2}") + ,"on_x0y0_clicked" : lambda x: self.sendMove("{\"x\":0,\"y\":0}") + ,"on_x0y1_clicked" : lambda x: self.sendMove("{\"x\":0,\"y\":1}") + ,"on_x0y2_clicked" : lambda x: self.sendMove("{\"x\":0,\"y\":2}") + ,"on_x1y0_clicked" : lambda x: self.sendMove("{\"x\":1,\"y\":0}") + ,"on_x1y1_clicked" : lambda x: self.sendMove("{\"x\":1,\"y\":1}") + ,"on_x1y2_clicked" : lambda x: self.sendMove("{\"x\":1,\"y\":2}") + ,"on_x2y0_clicked" : lambda x: self.sendMove("{\"x\":2,\"y\":0}") + ,"on_x2y1_clicked" : lambda x: self.sendMove("{\"x\":2,\"y\":1}") + ,"on_x2y2_clicked" : lambda x: self.sendMove("{\"x\":2,\"y\":2}") ,"on_connectBtn_clicked" : lambda x: self.doConnect() } @@ -53,10 +53,6 @@ class GGSTTT: "Content-Length: %s\n" % len(token)+ "\n"+ token) - self.s.send("Game-Command: hi\n" + - "Content-Type: text\n" + - "Content-Length: 0\n"+ - "\n") def sendMove(self, move): print "Sending move", move @@ -93,15 +89,39 @@ class GGSTTT: if defined == "false": print "Defining game" js = open("server.js").read() + self.wTree.get_widget("token").set_text(table_token) self.s.send("Server-Command: define\n"+ "Content-Type: text\n" + - ("Content-Length: %s\n" % len(js))+ - "\n%s" % js) + "Content-Length: %s\n" % str(len(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": - self.setStatus("You are player %s" % msg["data"]) - elif msg["Client-Command"] == "chat": - gobject.idle_add(self.updateChatText, msg["DATA"]) + self.setStatus("You are player %s" % msg["DATA"]) + elif msg["Client-Command"] == "warning": + 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": print msg gobject.idle_add(self.updateUsers, msg["DATA"]) diff --git a/src/ggs_player.erl b/src/ggs_player.erl index a7b9b36..f9f42da 100644 --- a/src/ggs_player.erl +++ b/src/ggs_player.erl @@ -102,6 +102,7 @@ handle_cast({srv_cmd, "hello", _Headers, TableToken}, State) -> {noreply, State#state{ table = TPid } } end; handle_cast({srv_cmd, "define", _Headers, Data}, #state { table = Table } = State) -> + erl:display(Data), ggs_table:notify(Table, self(), {server, define, Data}), {noreply, State}; handle_cast({game_cmd, Command, _Headers, Data}, #state { table = Table } = State) ->