diff options
author | makefu <root@pigstarter.de> | 2014-04-25 16:57:32 +0200 |
---|---|---|
committer | makefu <root@pigstarter.de> | 2014-04-25 16:57:32 +0200 |
commit | 8ba1237707cf9da18344d2725e5d867f93dcd3c3 (patch) | |
tree | faaa3253b9340a640d76f9686e9739957052b30c /titlebot/commands | |
parent | c35e62e336afdcfc0123ff29ec99266a29a6e812 (diff) |
fix ranking in highest
watch out for same ranking of things
Diffstat (limited to 'titlebot/commands')
-rwxr-xr-x | titlebot/commands/highest | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/titlebot/commands/highest b/titlebot/commands/highest index 5ac9ba9..d0408ac 100755 --- a/titlebot/commands/highest +++ b/titlebot/commands/highest @@ -8,15 +8,23 @@ import poll f = 'suggestions.json' title=" ".join(sys.argv[1:]) db = poll.load_db(f) +# only print the last N values (default 1) limit = int(sys.argv[1]) if len(sys.argv) > 1 else 1 -num = 1 +num = 0 +last_vote = 9001 # stolen from http://stackoverflow.com/questions/9647202/ordinal-numbers-replacement suffixes = ["th", "st", "nd", "rd", ] + ["th"] * 16 for entry in poll.sort_by_votes(db): + # if two entries have the same number of upvotes, do not increment the rank + current_vote = sum(entry['votes'].values()) + if current_vote < last_vote: + num = num + 1 + last_vote = current_vote + # exit if we are above the limit + if num > limit: + sys.exit(0) + suffixed_num = str(num) + suffixes[num % 100] print("%s: '%s' (%d votes)" % (suffixed_num,entry['title'],sum(entry['votes'].values()))) - num = num + 1 - if num > limit: - sys.exit(0) |