def debug(): data = json.loads(request.data) game = data.get("game") n = redis_server().get("%s_north_v" % game) s = redis_server().get("%s_south_v" % game) e = redis_server().get("%s_east_v" % game) w = redis_server().get("%s_west_v" % game) print "visits:%s" % {"n": n, "s": s, "e": e, "w": w}
def debug_redis(): data = {} data['keys'] = redis_server().keys() for key in redis_server().keys(): try: data[key] = redis_server().get(key) except: try: data[key] = list(redis_server().smembers("active_games")) except Exception as e: data[key] = str(e) print e return jsonify(data)
def update_quality(board): quality_key = "%s_quality" % redis_key quality_dict = board.quality() quality = sum(quality_dict.values()) current_quality = redis_server().get(quality_key) try: current_quality = int(current_quality) if redis_server().llen(redis_key) > 0: if not current_quality or quality < current_quality: print "quality: %s" % quality_dict redis_server().set(quality_key, quality) except Exception as e: print e
def __init__(self, payload=None): self.redis_server = redis_server() super(RedisBoard, self).__init__(payload)
def clear_game(): data = request.get_json(force=True) game = data.get("game") redis_server().delete("%s_north" % game) redis_server().delete("%s_south" % game) redis_server().delete("%s_east" % game) redis_server().delete("%s_west" % game) redis_server().delete("%s_north_v" % game) redis_server().delete("%s_south_v" % game) redis_server().delete("%s_east_v" % game) redis_server().delete("%s_west_v" % game) redis_server().delete("%s_north_quality" % game) redis_server().delete("%s_south_quality" % game) redis_server().delete("%s_east_quality" % game) redis_server().delete("%s_west_quality" % game)
def set_head_board(): data = request.get_json(force=True) game = data.get("game") redis_server().sadd("active_games", str(game)) redis_server().delete("%s_north" % game) redis_server().delete("%s_south" % game) redis_server().delete("%s_east" % game) redis_server().delete("%s_west" % game) redis_server().set("%s_north_quality" % game, 0) redis_server().set("%s_south_quality" % game, 0) redis_server().set("%s_east_quality" % game, 0) redis_server().set("%s_west_quality" % game, 0) redis_server().set("%s_north_v" % game, 0) redis_server().set("%s_south_v" % game, 0) redis_server().set("%s_east_v" % game, 0) redis_server().set("%s_west_v" % game, 0) board = RedisBoard(data) no_go_coords = [] for other_snake_id in board.other_snake_ids: snake = board.snake(other_snake_id) head = snake.get('coords', [])[0] no_go_coords += list(board.adjacent_empty_cells(head)) children = board.worstcase_children_dict() for direction in children.keys(): proposed_head = board.head() + list( settings.DIRECTION_TUPLES[direction]) if len(children.keys()) > 1 and proposed_head not in no_go_coords: payload = json.dumps(children[direction]) board_direction_key = "%s_%s" % (game, direction) length = redis_server().lpush(board_direction_key, payload) return board
def nuke_redis(): for key in redis_server().keys('*'): if key != "active_games": redis_server().delete(key) return jsonify({'nuked': 'all the things'})
def end(): data = json.loads(request.data) redis_server().srem("active_games", data.get("game")) nuke_redis() return jsonify(settings.ME)
def visit_children(board, redis_key): # # visit children children = board.worstcase_children_dict() for direction in children.keys(): payload = children[direction] redis_server().lpush(redis_key, json.dumps(payload))
def update_visits(board): # visit counter visit_key = "%s_v" % redis_key visits = redis_server().get(visit_key) current_count = int(visits) if redis_server().get(visit_key) else 0 redis_server().set(visit_key, current_count+1)
update_visits(board) visit_children(board, redis_key) except Exception as e: import traceback traceback.print_exc() print e def visit_children(board, redis_key): # # visit children children = board.worstcase_children_dict() for direction in children.keys(): payload = children[direction] redis_server().lpush(redis_key, json.dumps(payload)) print "Worker up and monitoring" while True: try: for game in redis_server().smembers("active_games"): for redis_key in ["%s_north" % game, "%s_south" % game, "%s_east" % game,"%s_west" % game]: if redis_server().llen(redis_key) > 0: payload = redis_server().rpop(redis_key) visit(game, payload, redis_key) time.sleep(0) time.sleep(0) time.sleep(0) except Exception as e: import traceback traceback.print_exc() print e