Exemplo n.º 1
0
def game_info(game_id):
    p = redis.pipeline()
    p.hget('game:%s' % game_id, 'p1')
    p.hget('game:%s' % game_id, 'p2')
    p.hget('game:%s' % game_id, 'elodiff')
    player1, player2, elodiff = p.execute()

    rank1 = redis.zrank('scoreboard', player1)
    if rank1 is None:
        rank1 = -1
    else:
        rank1 = rank1 + 1
    rank2 = redis.zrank('scoreboard', player2)

    if rank2 is None:
        rank2 = -1
    else:
        rank2 = rank2 + 1

    game_log_name = "log/%08d/%04d.json" % (game_id / 1000, game_id % 1000)

    return jsonify(
        game_id=game_id,
        game_log_name=game_log_name,
        player1=player1,
        player2=player2,
        rank1=rank1,
        rank2=rank2,
        finished=elodiff is not None,
        elodiff=(float(elodiff) if elodiff else None),
    )
Exemplo n.º 2
0
def get_run_info(username=None):
    if username:
        rank = redis.zrank('scoreboard', username)
        highscore_first = max(0, rank - 10)

        p = redis.pipeline()
        p.zrange('scoreboard',
                 highscore_first,
                 highscore_first + 20,
                 withscores=True)
        p.lrange('player:%s:games' % username, -40, -1)
        p.llen('player:%s:games' % username)
        raw_highscores, last_game_ids, num_games = p.execute()
    else:
        rank = None
        p = redis.pipeline()
        p.zrange('scoreboard', 0, 39, withscores=True)
        p.lrange('games', -40, -1)
        p.llen('games')
        raw_highscores, last_game_ids, num_games = p.execute()

    highscores = []
    for score_user, score in raw_highscores:
        p = redis.pipeline()
        p.lindex('player:%s:games' % score_user, -1)
        (last_game, ) = p.execute()
        inactive = (int(last_game_ids[-1]) - int(last_game) > INACTIVE_COUNT)
        highscores.append([score_user, -score, inactive])

    last_games = make_game_list(list(reversed(last_game_ids)))

    if username:
        return highscores, last_games, num_games, (rank + 1), highscore_first
    else:
        return highscores, last_games, num_games
Exemplo n.º 3
0
def deletelocation(locationkey):
    locations = querylocationkeys('L-' + str(locationkey))

    if (len(locations)) <= 0:
        return jsonify({'result': False})
    else:
        locationfullname = locations[0]['acname'] + '%L-' + str(
            locationkey) + '%'
        start = redis.zrank('locationfragments', locationfullname)

        previous = start - 1
        locationfragment = locationfullname

        commonfragment = redis.zrange('locationfragments', start + 1,
                                      start + 1)
        while (len(locationfragment) > 0):
            locationfragment = redis.zrange('locationfragments', previous,
                                            previous)

            if (locationfragment[0][-1] == '%'
                    or (len(commonfragment) > 0
                        and locationfragment[0] == commonfragment[0][0:-1])):
                break
            else:
                previous = previous - 1

        redis.zremrangebyrank('locationfragments', previous + 1, start)
        redis.delete('L-' + str(locationkey))

        return jsonify({'result': True})
Exemplo n.º 4
0
    def pass_turn(self, player):
        """
        If the given player is the active player, pass the turn to the next in
        line.
        """
        # Get the player's current rank
        rank = redis.zrank(self.turns_key, player.name)

        # Check if it is his/her turn
        if rank == 0:
            self._pass_turn(player.name)
Exemplo n.º 5
0
    def pass_turn(self, player):
        """
        If the given player is the active player, pass the turn to the next in
        line.
        """
        # Get the player's current rank
        rank = redis.zrank(self.turns_key, player.name)

        # Check if it is his/her turn
        if rank == 0:
            self._pass_turn(player.name)
Exemplo n.º 6
0
def gethotelfragments(prefix, pagelength):
  prefix = prefix.lower()
  listpart = 50

  start = redis.zrank('hotelfragments', prefix)
  if start < 0: return []

  hotelarray = []
  while (len(hotelarray) != pagelength):
    range = redis.zrange('hotelfragments', start, start + listpart - 1)
    start += listpart

    if not range or len(range) <= 0: 
      break

    for entry in range:
      minlen = min(len(entry), len(prefix))
      
      if entry[0:minlen] != prefix[0:minlen]:
        pagelength = len(hotelarray)
        break

      if entry[-1] == '%' and len(hotelarray) != pagelength: 
        hotel = {}
        
        hotelfull = entry[0:-1]
        indexwithperc = hotelfull.rfind('%')

        hotelid = entry[indexwithperc + 1:-1]
        hotelname = entry[0:indexwithperc] 
        
        hotelproperties = redis.lrange(hotelid, 0, -1)
        if len(hotelproperties) > 0:
          hotel['id'] = hotelproperties[0]
          hotel['displayname'] = hotelproperties[1]
          hotel['acname'] = hotelproperties[2]
          hotel['image'] = hotelproperties[3]
          hotel['latitude'] = hotelproperties[4]
          hotel['longitude'] = hotelproperties[5]
          hotel['thirdpartyrating'] = hotelproperties[6]
        
          hotelarray.append(hotel)

  return hotelarray
Exemplo n.º 7
0
def getlocationfragments(prefix, pagelength):
    prefix = prefix.lower()
    listpart = 50

    start = redis.zrank('locationfragments', prefix)
    if start < 0: return []

    locationarray = []
    while (len(locationarray) != pagelength):
        range = redis.zrange('locationfragments', start, start + listpart - 1)
        start += listpart

        if not range or len(range) <= 0:
            break

        for entry in range:
            minlen = min(len(entry), len(prefix))

            if entry[0:minlen] != prefix[0:minlen]:
                pagelength = len(locationarray)
                break

            if entry[-1] == '%' and len(locationarray) != pagelength:
                location = {}

                locationfull = entry[0:-1]
                indexwithperc = locationfull.rfind('%')

                locationid = entry[indexwithperc + 1:-1]
                locationname = entry[0:indexwithperc]

                locationproperties = redis.lrange(locationid, 0, -1)
                if len(locationproperties) > 0:
                    location['id'] = locationproperties[0]
                    location['displayname'] = locationproperties[1]
                    location['acname'] = locationproperties[2]
                    location['icon'] = locationproperties[3]
                    location['latitude'] = locationproperties[4]
                    location['longitude'] = locationproperties[5]

                    locationarray.append(location)

    return locationarray
Exemplo n.º 8
0
if arguments.zadd:
    if arguments.key:
        if arguments.score:
            if arguments.member:
                # redis.zadd("key",{"value":score}) syntax of zadd for newer update of redis-----------------
                redis.zadd(arguments.key, {arguments.member: arguments.score})
                print("zadd", arguments.key, arguments.score, arguments.member)
            else:
                print("provide member for zadd")
        else:
            print("provide score for zadd")
    else:
        print("provide the key for zadd")

if arguments.zrange:
    if arguments.key:
        data = redis.zrange(arguments.key, 0, -1)
        print(data[:])
    else:
        print("provide the key for zrange")

if arguments.zrank:
    if arguments.key:
        if arguments.member:
            data = redis.zrank(arguments.key, arguments.member)
            print(data)
        else:
            print("provide member for zrank")
    else:
        print("provide the key for zrank")