Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import json
- import sys
- from collections import OrderedDict
- PARTICIPATION_POINTS = 2
- TOURNAMENT_MAX_POINTS = 11
- RANK_1_MULTIPLIER = 1
- RANK_2_MULTIPLIER = 2
- RANK_3_MULTIPLIER = 3
- NO_RANK_MULTIPLIER = 4
- clanlist = {}
- ranking = []
- sumpoints = 0
- class Clan():
- '''Clan object holding information about points and rank.'''
- def __init__(self, name):
- '''Initialize Clan object. Use get_clan instead.'''
- self.points = 0
- self.rank = 0
- self.name = name
- def get_clan(name):
- '''Retrieves Clan object from global dictionary or creates new object
- with all necessary initializations.'''
- try:
- return clanlist[name]
- except KeyError:
- clan = Clan(name)
- clanlist[name] = clan
- ranking.append (clan)
- return clan
- class Tournament():
- '''Tournament object. Invoke finish method after adding all participants
- to update ranking. '''
- count = 0
- def __init__(self, name, participants):
- self.name = name
- self.participants = participants
- Tournament.count += 1
- def add(self, clan, wins, place):
- global sumpoints
- #
- # Add points based on place
- #
- # addpoints = TOURNAMENT_MAX_POINTS - place
- # if addpoints > 0:
- # clan.points += addpoints
- # sumpoints += addpoints
- # # clan.points += addpoints/self.participants
- # # sumpoints += addpoints/self.participants
- #
- # Diminishing returns system
- #
- addpoints = 0
- if wins == 0:
- addpoints += PARTICIPATION_POINTS
- else:
- if clan.rank == 1:
- addpoints = wins*RANK_1_MULTIPLIER
- elif clan.rank == 2:
- addpoints += wins*RANK_2_MULTIPLIER
- elif clan.rank == 3:
- addpoints += wins*RANK_3_MULTIPLIER
- else:
- addpoints += wins*NO_RANK_MULTIPLIER
- clan.points += addpoints
- sumpoints += addpoints
- def finish(self):
- # print("\n", self.name, sep = "")
- # if Tournament.count == 100:
- # update_ranks(True)
- # self.reset()
- # else:
- update_ranks()
- def reset(self):
- # print("reset")
- Tournament.count = 0
- del ranking[:]
- clanlist.clear()
- def update_ranks(show = False):
- ranking.sort(key = lambda x: x.points, reverse = True)
- rank = 0
- prevpoints = -1
- # print(ranking[0].name, "first")
- for clan in ranking:
- if prevpoints != clan.points:
- rank += 1
- prevpoints = clan.points
- # elif rank == 1:
- # print("same:", clan.name)
- clan.rank = rank
- if show:
- print("%2d: %12s %4s)" % (clan.rank, clan.name, "(" + str(clan.points)))
- if show:
- print()
- def load_data(file):
- with open (file) as f:
- # OrderedDict to keep key/values in order for ranking
- return json.load(f, object_pairs_hook = OrderedDict)
- if __name__ == "__main__":
- try:
- filename = sys.argv[1]
- except IndexError:
- filename = "leaderboard.json"
- json = load_data(filename)
- for tournament in json["Leaderboard"]:
- eventobj = json["Leaderboard"][tournament]
- event = Tournament(tournament, eventobj["participants"])
- update = True
- skip = False
- if "options" in eventobj:
- opts = eventobj["options"]
- if "skip" in opts and opts["skip"]:
- skip = True
- continue
- if "update" in opts and not opts["update"]:
- update = False
- clans = eventobj["clans"]
- for clan in clans:
- event.add(get_clan(clan), clans[clan]["wins"], clans[clan]["place"])
- if not skip and update:
- event.finish()
- rank = 0
- prevpoints = -1
- for clan in ranking:
- if prevpoints != clan.points:
- rank += 1
- prevpoints = clan.points
- # normalized display
- # print("%2d: %12s %4s%%)" % (clan.rank, clan.name, "(" + str(round(clan.points*100/sumpoints, 2))))
- print("%2d: %12s %4s)" % (clan.rank, clan.name, "(" + str(clan.points)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement