From 352d160090bd3519f2c4f1976b1fd7022e2351e0 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Fri, 24 Nov 2017 15:09:34 +0100 Subject: [PATCH] update: Changed trophy rewward system. Deliver trophy to the TOP x% defined in the config file. Also, added more info in the trophy modal --- config/ranking.cfg | 3 +- contributor_helper.py | 72 +++++++++++++++++++++++++++--------------- server.py | 7 ++++ templates/contrib.html | 28 ++++++++++++++++ 4 files changed, 83 insertions(+), 27 deletions(-) diff --git a/config/ranking.cfg b/config/ranking.cfg index 13cc9b0..5413363 100644 --- a/config/ranking.cfg +++ b/config/ranking.cfg @@ -70,7 +70,8 @@ regularlyDays=7 5=Has published loads of valuable content for the community [TrophyDifficulty] -difficulty=10 +#represent the % of org that can have this rank. Rank 1 is ignored as only 1 org can have it. +trophyMapping=[2, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10] [HonorTrophy] 0=No trophy diff --git a/contributor_helper.py b/contributor_helper.py index d835348..43be8a9 100644 --- a/contributor_helper.py +++ b/contributor_helper.py @@ -22,7 +22,7 @@ class Contributor_helper: for badgeNum in range(1, self.honorBadgeNum+1): #get Num of honorBadge self.org_honor_badge_title[badgeNum] = self.cfg_org_rank.get('HonorBadge', str(badgeNum)) - self.trophyDifficulty = self.cfg_org_rank.getfloat('TrophyDifficulty', 'difficulty') + self.trophyMapping = json.loads(self.cfg_org_rank.get('TrophyDifficulty', 'trophyMapping')) self.trophyNum = len(self.cfg_org_rank.options('HonorTrophy'))-1 #0 is not a trophy self.categories_in_trophy = json.loads(self.cfg_org_rank.get('HonorTrophyCateg', 'categ')) self.trophy_title = {} @@ -264,25 +264,58 @@ class Contributor_helper: ''' TROPHIES ''' def getOrgTrophies(self, org): - keyname = 'CONTRIB_TROPHY:{org}:{orgCateg}' + self.getAllOrgsTrophyRanking() + keyname = 'CONTRIB_TROPHY:{orgCateg}' trophy = [] for categ in self.categories_in_trophy: - key = keyname.format(org=org, orgCateg=categ) - trophy_Pnts = self.serv_redis_db.get(key) - if trophy_Pnts is not None: #existing - trophy_Pnts = float(trophy_Pnts.decode('utf8')) - trophy_rank = self.getRankTrophy(trophy_Pnts) - trophy_true_rank = self.getTrueRankTrophy(trophy_Pnts) - trophy.append({ 'categ': categ, 'trophy_points': trophy_Pnts, 'trophy_rank': trophy_rank, 'trophy_true_rank': trophy_true_rank, 'trophy_title': self.trophy_title[trophy_true_rank]}) + key = keyname.format(orgCateg=categ) + totNum = self.serv_redis_db.zcard(key) + if totNum == 0: + continue + pos = self.serv_redis_db.zrank(key, org) + if pos is None: + continue + trophy_rank = self.posToRankMapping(pos, totNum) + trophy_Pnts = self.serv_redis_db.zscore(key, org) + trophy.append({ 'categ': categ, 'trophy_points': trophy_Pnts, 'trophy_rank': trophy_rank, 'trophy_true_rank': trophy_rank, 'trophy_title': self.trophy_title[trophy_rank]}) return trophy + def getOrgsTrophyRanking(self, categ): + keyname = 'CONTRIB_TROPHY:{orgCateg}' + res = self.serv_redis_db.zrange(keyname.format(orgCateg=categ), 0, -1, withscores=True, desc=True) + res = [[org.decode('utf8'), score] for org, score in res] + return res + + def getAllOrgsTrophyRanking(self): + dico_categ = {} + for categ in self.categories_in_trophy: + res = self.getOrgsTrophyRanking(categ) + dico_categ[categ] = res + + def posToRankMapping(self, pos, totNum): + mapping = self.trophyMapping + mapping_num = [math.ceil(float(float(totNum*i)/float(100))) for i in mapping] + # print(pos, totNum) + if pos == 0: #first + position = 1 + else: + temp_pos = pos + counter = 1 + for num in mapping_num: + if temp_pos < num: + position = counter + else: + temp_pos -= num + counter += 1 + return self.trophyNum+1 - position + def giveTrophyPointsToOrg(self, org, categ, points): - keyname = 'CONTRIB_TROPHY:{org}:{orgCateg}' - self.serv_redis_db.incrby(keyname.format(org=org, orgCateg=categ), points) + keyname = 'CONTRIB_TROPHY:{orgCateg}' + self.serv_redis_db.zincrby(keyname.format(orgCateg=categ), org, points) def removeTrophyPointsFromOrg(self, org, categ, points): - keyname = 'CONTRIB_TROPHY:{org}:{orgCateg}' - self.serv_redis_db.incrby(keyname.format(org=org, orgCateg=categ), -points) + keyname = 'CONTRIB_TROPHY:{orgCateg}' + self.serv_redis_db.zincrby(keyname.format(orgCateg=categ), org, -points) ''' AWARDS HELPER ''' def getLastAwardsFromRedis(self): @@ -469,19 +502,6 @@ class Contributor_helper: prev = i return { 'remainingPts': 0, 'stepPts': self.rankMultiplier**self.levelMax } - def getRankTrophy(self, points): - if points <= 0: - return 0 - elif points == 1: - return 1 - else: - rank = math.sqrt(points/self.trophyDifficulty) - rank = min(self.trophyNum, rank) - return float("{:.2f}".format(rank)) - - def getTrueRankTrophy(self, ptns): - return int(self.getRankTrophy(ptns)) - ''' ''' ''' TEST DATA ''' ''' ''' diff --git a/server.py b/server.py index 2cd581d..ee564a0 100755 --- a/server.py +++ b/server.py @@ -169,6 +169,11 @@ def contrib(): trophy_categ_list = contributor_helper.categories_in_trophy trophy_categ_list_str = [ s[0].upper() + s[1:].replace('_', ' ') for s in trophy_categ_list] trophy_title = contributor_helper.trophy_title + trophy_title_str = [] + for i in range(contributor_helper.trophyNum+1): + trophy_title_str.append(trophy_title[i]) + trophy_mapping = ["Top 1"] + [ str(x)+"%" for x in contributor_helper.trophyMapping] + [" "] + trophy_mapping.reverse() currOrg = request.args.get('org') if currOrg is None: @@ -190,6 +195,8 @@ def contrib(): trophy_categ_list_id=trophy_categ_list, trophy_categ_list_str=trophy_categ_list_str, trophy_title=json.dumps(trophy_title), + trophy_title_str=trophy_title_str, + trophy_mapping=trophy_mapping, min_between_reload=cfg.getint('CONTRIB', 'min_between_reload') ) diff --git a/templates/contrib.html b/templates/contrib.html index 45594c4..43ec8b6 100644 --- a/templates/contrib.html +++ b/templates/contrib.html @@ -182,6 +182,34 @@

Trophies:

Shows your skills in information sharing

(earned via upvotes or sightings from other organisation) + +
+ + + + {% for title in trophy_title_str %} + + {% endfor %} + + + + + {% for perc in trophy_mapping %} + + {% endfor %} + + + {% for title in trophy_title_str %} + + {% endfor %} + + +
{{ title }}
{{ perc }}
+ +
+
+ +

Acquired trophies: