Exemplo n.º 1
0
    def get(self):
        blocked = {}

        blockedplayers = redis_connection.zrevrange("blockedplayers",
                                                    0,
                                                    -1,
                                                    withscores=True)
        blockedplayers_puzzle = redis_connection.zrange(
            "blockedplayers:puzzle", 0, -1, withscores=True)

        # Add ip -> user -> timestamp
        for (ip_user, timestamp) in blockedplayers:
            (ip, user) = ip_user.split("-")
            if not blocked.get(ip):
                blocked[ip] = {}
            blocked[ip][user] = {"timestamp": timestamp}

        # Set puzzles and recent points
        for (ip_user_puzzle, recent_points) in blockedplayers_puzzle:
            (ip, user, puzzle) = ip_user_puzzle.split("-")
            if not blocked[ip][user].get("puzzles"):
                blocked[ip][user]["puzzles"] = []
            blocked[ip][user]["puzzles"].append({
                "puzzle": puzzle,
                "points": recent_points
            })
            # sorted by asc so the last will be the highest
            blocked[ip][user]["recent_points"] = recent_points

        return encoder.encode(blocked)
Exemplo n.º 2
0
def transferAll(cleanup=False):
    # Get all puzzles
    puzzles = redis_connection.zrange("pcupdates", 0, -1)
    current_app.logger.info("transferring puzzles: {0}".format(puzzles))
    for puzzle in puzzles:
        current_app.logger.info("transfer puzzle: {0}".format(puzzle))
        transfer(puzzle, cleanup=cleanup)
        memory = redis_connection.info(section="memory")
        if cleanup:
            current_app.logger.info(
                "used_memory: {used_memory_human}".format(**memory))
Exemplo n.º 3
0
    def get(self, puzzle_id):
        ""
        cur = db.cursor()
        result = cur.execute(
            fetch_query_string("_select-puzzle-by-puzzle_id.sql"),
            {"puzzle_id": puzzle_id},
        ).fetchall()
        if not result:
            # 404 if puzzle does not exist
            abort(404)

        (result, col_names) = rowify(result, cur.description)
        puzzle = result[0].get("id")
        now = int(time.time())

        timeline = redis_connection.zrevrange(
            "timeline:{puzzle}".format(puzzle=puzzle), 0, -1, withscores=True
        )
        score_puzzle = redis_connection.zrange(
            "score:{puzzle}".format(puzzle=puzzle), 0, -1, withscores=True
        )
        user_score = dict(score_puzzle)
        user_rank = {}
        for index, item in enumerate(score_puzzle):
            user_rank[int(item[0])] = index + 1

        players = []
        for index, item in enumerate(timeline):
            (user, timestamp) = item
            players.append(
                {
                    "id": int(user),
                    "score": int(user_score.get(user, 0)),
                    "rank": user_rank.get(
                        int(user), 0
                    ),  # a 0 value means the player hasn't joined any pieces
                    "seconds_from_now": int(now - timestamp),
                }
            )

        puzzle_stats = {"now": now, "players": players}

        cur.close()
        return encoder.encode(puzzle_stats)
Exemplo n.º 4
0
def transferOldest(target_memory):

    # No puzzles that have been modified in the last 30 minutes
    newest = int(time.time()) - (30 * 60)

    # Get the 10 oldest puzzles
    puzzles = redis_connection.zrange("pcupdates", 0, 10, withscores=True)
    # print('cycle over old puzzles: {0}'.format(puzzles))
    for (puzzle, timestamp) in puzzles:
        # There may be a chance that since this process has started that
        # a puzzle could have been updated.
        latest_timestamp = redis_connection.zscore("pcupdates", puzzle)

        if latest_timestamp < newest:
            # print('transfer: {0}'.format(puzzle))
            transfer(puzzle)
            memory = redis_connection.info(section="memory")
            # print('used_memory: {used_memory_human}'.format(**memory))
            if memory.get("used_memory") < target_memory:
                break
Exemplo n.º 5
0
def find_puzzles_in_redis(results={}):
    """
    For each puzzle that is active in redis (is in pcupdates); check the
    immovable piece group counts.  Fail any that do not equal the count for the
    top left piece group.
    """
    _results = results.copy()
    cur = db.cursor()

    puzzles_in_redis = redis_connection.zrange("pcupdates", 0, -1)
    for puzzle in puzzles_in_redis:
        test_result = _results.get(puzzle, {
            "puzzle": puzzle,
            "msg": "",
            "test": []
        })
        test_result["test"].append("redis")
        _results.update({puzzle: test_result})
        (result, col_names) = rowify(
            cur.execute(
                read_query_file("select_puzzle_top_left_piece_for_puzzle.sql"),
                {
                    "id": puzzle
                },
            ).fetchall(),
            cur.description,
        )
        if not result or not result[0]:
            # Test failed.
            test_result[
                "msg"] = "{msg} Failed to find top left piece for puzzle {puzzle}".format(
                    msg=test_result.get("msg", ""), puzzle=puzzle)
            test_result["status"] = "fail"
            test_result["reason"] = "fail_no_top_left_piece"
            continue

        top_left_piece = result[0]
        test_result["puzzle_id"] = top_left_piece["puzzle_id"]

        # Compare the counts for the pcfixed and the top left piece group.  They
        # should be the same.
        pcfixed_count = redis_connection.scard(
            "pcfixed:{puzzle}".format(puzzle=puzzle))
        pcg_for_top_left = redis_connection.hget(
            "pc:{puzzle}:{id}".format(puzzle=puzzle, id=top_left_piece["id"]),
            "g")
        immovable_top_left_group_count = redis_connection.scard(
            "pcg:{puzzle}:{group}".format(puzzle=puzzle,
                                          group=pcg_for_top_left))
        if pcfixed_count == immovable_top_left_group_count:
            # Test passed.
            test_result[
                "msg"] = "{msg} {puzzle_id} {puzzle} all immovable pieces are in the same group as top left".format(
                    msg=test_result.get("msg", ""),
                    puzzle_id=top_left_piece["puzzle_id"],
                    puzzle=puzzle,
                )
            test_result["status"] = "pass"
            test_result["reason"] = "pass"
            continue
        else:
            # Test failed.
            test_result[
                "msg"] = "{msg} {puzzle_id} {puzzle} not all immovable pieces are in the same group as top left".format(
                    msg=test_result.get("msg", ""),
                    puzzle_id=top_left_piece["puzzle_id"],
                    puzzle=puzzle,
                )
            test_result["status"] = "fail"
            test_result["reason"] = "fail_pcfixed_outside_of_top_left"
    cur.close()
    return _results