Created
February 8, 2013 21:24
-
-
Save nateware/4742050 to your computer and use it in GitHub Desktop.
DynamoDB example Leaderboard using boto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto | |
from boto.dynamodb.exceptions import * | |
from boto.dynamodb.batch import * | |
import pprint | |
class Leaderboard: | |
def __init__(self, game_mode): | |
self.conn = boto.connect_dynamodb() | |
self.table = self.conn.get_table("leaderboard_" + game_mode) | |
def add_score(self, user, score): | |
item = self.table.new_item( | |
hash_key=user, | |
attrs={'score': score} | |
) | |
item.put() | |
def get_board(self, friends): | |
scores = self.table.batch_get_item(friends) | |
return sorted(scores, reverse=True, key=lambda item: item['score']) | |
lb = Leaderboard("deathmatch") | |
lb.add_score('nateware', 923) | |
lb.add_score('LaZyb0ne', 1492) | |
lb.add_score('grizzly99', 1612) | |
lb.add_score('LaZyb0ne', 2819) # new high score | |
lb.add_score('xMotherx', 1209) | |
# Retrieve friends in-game somehow | |
#friends = ['nateware','missing','grizzly99','xMotherx','absent','LaZyb0ne'] | |
friends = current_user.friends_list | |
scores = lb.get_board(friends) | |
# Display leaderboard | |
print("=== friends leaderboard ===") | |
for item in scores: | |
print("%s: %s" % (item['username'], item['score'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment