def __init__( self, environ, start_response, path=None ): self.config = config self.query = Request( environ ) self.response = Response( start_response, path=self.query.path ) self.path = path self.name = "ems" if hasattr(config,"app_name"): self.name = config.app_name self.db_path = os.path.join( self.path, self.name+".db" ) self.open_db() sid = None if self.name+".sid" in self.query.parms: sid = self.query.parms[self.name+".sid"] elif "sid" in self.query.parms: sid = self.query.parms["sid"] self.session = Session( self, sid=sid ) self.logfile = None if hasattr(config,"logfile"): self.logfile = open( os.path.join(self.path, config.logfile), "a" ) self.tracefile = None if hasattr(config,"tracefile"): self.tracefile = open( os.path.join(self.path, config.tracefile), "a" ) def trace( message ): self.trace( message ) self.db.set_trace_callback( trace ) self.trace( self ) self.access_cache = {} self.user = user.get_anonymous_user(self) if "user_id" in self.session.parms: self.user = user.User( self, user_id=int(self.session.parms["user_id"]) )
def run( ws_server ): result = {} if ws_server.app.user.can_read( ws_server.game_id ): c = ws_server.app.db.cursor() max_move_id = None if ws_server.board_history==[] else ws_server.board_history[-1]["move_id"] max_move_condition = "" if max_move_id==None else (" and id>%d" % (max_move_id)) c.execute( """select id, game_id, player_id, mtime, figure, from_field, to_field from chess_games where game_id=? %s""" \ % (max_move_condition), [ws_server.game_id] ) for row in c: move_id, _game_id, player_id, mtime, figure, from_field, to_field = row ws_server.board_state[from_field] = None ws_server.board_state[to_field] = figure move = { "move_id" : move_id, "game_id" : ws_server.game_id, "player_id" : player_id, "mtime" : mtime, "figure" : figure, "from_field" : from_field, "to_field" : to_field } ws_server.board_history.append( move ) if not ws_server.max_move_sent_offset and not ws_server.board_history: c = ws_server.app.db.cursor() initial_board_setting = "♜♞♝♛♚♝♞♜" \ +"♟♟♟♟♟♟♟♟" \ +" " \ +" " \ +" " \ +" " \ +"♙♙♙♙♙♙♙♙" \ +"♖♘♗♕♔♗♘♖" initial_board_setting = "".join( reversed(initial_board_setting) ) c.execute( """insert into chess_games (game_id, player_id, mtime, figure) values(?,?,?,?)""", [ws_server.game_id, user.get_anonymous_user(ws_server.app).id, time.time(), initial_board_setting] ) result.update({ "succeeded" : True, "actions" : ws_server.board_history[ws_server.max_move_sent_offset:] }) ws_server.max_move_sent_offset = len(ws_server.board_history) if ws_server.app.user.can_write( ws_server.game_id ): if ws_server.cmd_queue: action = ws_server.cmd_queue.pop(0) result["succeeded"] = True # TODO: Aktion validieren whites = "♔♕♖♗♘♙" blacks = "♚♛♜♝♞♟" get_color = lambda f: "white" if f in whites else "black" if f in blacks else None if get_color(action.figure)==None: result.update({ "succeeded" : False, "error" : { "message" : "Unknown figure code!" } }) elif ws_server.board_history and get_color(ws_server.board_history[-1].figure)==get_color(action.figure): result.update({ "succeeded" : False, "error" : { "message" : "Wrong color!" } }) elif ws_server.board_history and ws_server.board_history[-1].player_id==ws_server.app.user.id: result.update({ "succeeded" : False, "error" : { "message" : "Double action not permitted!" } }) else: c = ws_server.app.db.cursor() c.execute( """insert into chess_games (game_id, player_id, mtime, figure, from_field, to_field) values(?,?,?,?,?,?)""", [ws_server.game_id, ws_server.app.user.id, time.time(), action["figure"], action["from_field"], action["to_field"]] ) if result: ws_server.ws.send( json.dumps(result) )
def run( self ): def read_thread(): while not self.quitting: data = self.ws.read(1000) if data: self.con.send( data ) else: print( "%s disconnected." % (str(self.addr)) ) break t = threading.Thread( target=read_thread ) t.start() sent_state = {} self.app.open_db() # App-DB für diesen Thread neu öffnen... if self.uri == "/player_state" : while not self.quitting: if self.app.user.can_write( self.app.user.id ): while self.cmd_queue: action = self.cmd_queue.pop(0) c = self.app.db.cursor() state = c.execute( """select object_id, x, y from player_positions where object_id=?""", [self.app.user.id] ).fetchone() if state: c.execute( """update player_positions set x=x+?, y=y+? where object_id=?""", [action[0], action[1], self.app.user.id] ) self.app.db.commit() else: c.execute( """insert into player_positions (object_id, x, y) values (?,?,?)""", [self.app.user.id, action[0], action[1]] ) self.app.db.commit() else: self.cmd_queue.clear() result = {} c = self.app.db.cursor() c.execute( """select p.object_id, p.x, p.y, u.nick from player_positions p inner join users u on p.object_id=u.object_id""" ) for row in c: if self.app.user.can_read( row[0] ): state = {"id":row[0], "x":row[1], "y":row[2], "nick":row[3]} str_id = "id"+str(state["id"]) if str_id not in sent_state or state!=sent_state[str_id]: sent_state[ str_id ] = result[ str_id ] = state if result: self.ws.send( str(result) ) time.sleep( 0.2 ) elif self.uri.starts_with( "/chessgame_state/" ): empty, channel, game_id = self.uri.split("/") game_id = int(game_id) board_state = {} board_history = [] max_move_sent_offset = None while not self.quitting: result = {} if self.app.user.can_read( game_id ): c = self.app.db.cursor() max_move_id = None if board_history==[] else board_history[-1]["move_id"] max_move_condition = "" if max_move_id==None else (" and id>%d" % (max_move_id)) c.execute( """select id, game_id, player_id, mtime, figure, from_field, to_field from chess_games where game_id=? %s""" \ % (max_move_condition), [game_id] ) for row in c: move_id, _game_id, player_id, mtime, figure, from_field, to_field = row board_state[from_field] = None board_state[to_field] = figure move = { "move_id" : move_id, "game_id" : game_id, "player_id" : player_id, "mtime" : mtime, "figure" : figure, "from_field" : from_field, "to_field" : to_field } board_history.append( move ) if not max_move_sent_offset and not board_history: c = self.app.db.cursor() initial_board_setting = "♜♞♝♛♚♝♞♜" \ +"♟♟♟♟♟♟♟♟" \ +" " \ +" " \ +" " \ +" " \ +"♙♙♙♙♙♙♙♙" \ +"♖♘♗♕♔♗♘♖" initial_board_setting = "".join( reversed(initial_board_setting) ) c.execute( """insert into chess_games (game_id, player_id, mtime, figure) values(?,?,?,?)""", [game_id, user.get_anonymous_user(self.app).id, time.time(), initial_board_setting] ) self.app.db.commit() result.update({ "succeeded" : True, "actions" : board_history[max_move_sent_offset:] }) max_move_sent_offset = len(board_history) if self.app.user.can_write( game_id ): if self.cmd_queue: action = self.cmd_queue.pop(0) result["succeeded"] = True # TODO: Aktion validieren whites = "♔♕♖♗♘♙" blacks = "♚♛♜♝♞♟" get_color = lambda f: "white" if f in whites else "black" if f in blacks else None if get_color(action.figure)==None: result.update({ "succeeded" : False, "error" : { "message" : "Unknown figure code!" } }) elif board_history and get_color(board_history[-1].figure)==get_color(action.figure): result.update({ "succeeded" : False, "error" : { "message" : "Wrong color!" } }) elif board_history and board_history[-1].player_id==self.app.user.id: result.update({ "succeeded" : False, "error" : { "message" : "Double action not permitted!" } }) else: c = self.app.db.cursor() c.execute( """insert into chess_games (game_id, player_id, mtime, figure, from_field, to_field) values(?,?,?,?,?,?)""", [game_id, self.app.user.id, time.time(), action["figure"], action["from_field"], action["to_field"]] ) self.app.db.commit() if result: self.ws.send( json.dumps(result) ) time.sleep( 1 )