Draft support of trophies in ZMQ

pull/7/head
Sami Mokaddem 2017-11-14 14:05:54 +01:00
parent c13e39d5a6
commit f22aaee21d
6 changed files with 91 additions and 38 deletions

View File

@ -70,7 +70,7 @@ regularlyDays=7
5=Has published loads of valuable content for the community 5=Has published loads of valuable content for the community
[TrophyDifficulty] [TrophyDifficulty]
difficulty=1.5 difficulty=5
[HonorTrophy] [HonorTrophy]
0=No trophy 0=No trophy

View File

@ -133,14 +133,17 @@ class Contributor_helper:
return {'rank': final_rank, 'status': to_ret, 'totPoints': self.getOrgContributionTotalPoints(org)} return {'rank': final_rank, 'status': to_ret, 'totPoints': self.getOrgContributionTotalPoints(org)}
# return the awards given to the organisation # return the awards given to the organisation
def updateOrgContributionRank(self, orgName, pnts_to_add, action, contribType, eventTime, isLabeled): def updateOrgContributionRank(self, orgName, pnts_to_add, action, contribType, eventTime, isLabeled, categ=""):
ContributionStatus = chelper.getCurrentContributionStatus(org) ContributionStatus = self.getCurrentContributionStatus(orgName)
oldRank = ContributionStatus['final_rank']
oldContributionStatus = ContributionStatus['status'] oldContributionStatus = ContributionStatus['status']
oldHonorBadges = self.getOrgHonorBadges(orgName)
keyname = 'CONTRIB_ORG:{org}:{orgCateg}' keyname = 'CONTRIB_ORG:{org}:{orgCateg}'
# update total points # update total points
totOrgPnts = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='points'), pnts_to_add) totOrgPnts = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='points'), pnts_to_add)
#FIXME TEMPORARY, JUST TO TEST IF IT WORKS CORRECLTY
self.giveTrophyPointsToOrg(orgName, categ, 1)
# update date variables # update date variables
if contribType == 'Attribute': if contribType == 'Attribute':
attributeWeekCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='ATTR_WEEK_COUNT'), 1) attributeWeekCount = self.serv_redis_db.incrby(keyname.format(org=orgName, orgCateg='ATTR_WEEK_COUNT'), 1)
@ -204,22 +207,23 @@ class Contributor_helper:
if totOrgPnts >= self.org_rank_requirement_pnts[14] and contribType == 'Event' and eventWeekCount>heavilyCount and isLabeled: if totOrgPnts >= self.org_rank_requirement_pnts[14] and contribType == 'Event' and eventWeekCount>heavilyCount and isLabeled:
contrib.append([14, util.ONE_DAY*regularlyDays]) contrib.append([14, util.ONE_DAY*regularlyDays])
print([r for r, ttl in contrib])
for rankReq, ttl in contrib: for rankReq, ttl in contrib:
self.serv_redis_db.set(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), 1) self.serv_redis_db.set(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), 1)
self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), ttl) self.serv_redis_db.expire(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), ttl)
ContributionStatus = chelper.getCurrentContributionStatus(org) ContributionStatus = self.getCurrentContributionStatus(orgName)
newRank = ContributionStatus['final_rank']
newContributionStatus = ContributionStatus['status'] newContributionStatus = ContributionStatus['status']
newHonorBadges = self.getOrgHonorBadges(orgName)
awards_given = [] awards_given = []
if newRank > oldRank: for i in newContributionStatus.keys():
awards_given.append(['rank', newRank]) if oldContributionStatus[i] < newContributionStatus[i] and i != ContributionStatus['rank']:
for i in range(len(oldContributionStatus)):
if oldContributionStatus[i] != newContributionStatus[i]:
awards_given.append(['contribution_status', i]) awards_given.append(['contribution_status', i])
print(awards_given) for badgeNum in newHonorBadges:
if badgeNum not in oldHonorBadges:
awards_given.append(['badge', badgeNum])
return awards_given return awards_given
''' HONOR BADGES ''' ''' HONOR BADGES '''
@ -448,15 +452,17 @@ class Contributor_helper:
return { 'remainingPts': 0, 'stepPts': self.rankMultiplier**self.levelMax } return { 'remainingPts': 0, 'stepPts': self.rankMultiplier**self.levelMax }
def getRankTrophy(self, points): def getRankTrophy(self, points):
if points == 0: if points <= 0:
return 0 return 0
elif points == 1: elif points == 1:
return 1 return 1
else: else:
return float("{:.2f}".format(math.log(points, self.trophyDifficulty))) return float("{:.2f}".format(math.sqrt(points/self.trophyDifficulty)))
def getTrueRankTrophy(self, ptns): def getTrueRankTrophy(self, ptns):
return int(self.getRankTrophy(ptns)) to_ret = int(self.getRankTrophy(ptns))
to_ret = len(self.trophy_title) if to_ret > len(self.trophy_title) else to_ret
return to_ret
''' ''' ''' '''
''' TEST DATA ''' ''' TEST DATA '''

View File

@ -76,6 +76,7 @@ def main():
ContributionStatus = chelper.getCurrentContributionStatus(org) ContributionStatus = chelper.getCurrentContributionStatus(org)
OLD_org_c_status = ContributionStatus['status'] OLD_org_c_status = ContributionStatus['status']
OLD_org_honor_badge = chelper.getOrgHonorBadges(org) OLD_org_honor_badge = chelper.getOrgHonorBadges(org)
OLD_org_trophy = chelper.getOrgTrophies(org)
# ranks # ranks
while True: while True:
@ -180,16 +181,29 @@ def main():
ContributionStatus = chelper.getCurrentContributionStatus(org) ContributionStatus = chelper.getCurrentContributionStatus(org)
NEW_org_c_status = ContributionStatus['status'] NEW_org_c_status = ContributionStatus['status']
NEW_org_honor_badge = chelper.getOrgHonorBadges(org) NEW_org_honor_badge = chelper.getOrgHonorBadges(org)
NEW_org_trophy = chelper.getOrgTrophies(org)
awards_given = [] awards_given = []
for i in NEW_org_c_status.keys(): for i in NEW_org_c_status.keys():
if OLD_org_c_status[i] < NEW_org_c_status[i] and i != ContributionStatus['rank']: if OLD_org_c_status[i] < NEW_org_c_status[i] and i != ContributionStatus['rank']:
awards_given.append(['contribution_status', i]) awards_given.append(['contribution_status', ContributionStatus['rank']])
for badgeNum in NEW_org_honor_badge: for badgeNum in NEW_org_honor_badge:
if badgeNum not in OLD_org_honor_badge: if badgeNum not in OLD_org_honor_badge:
awards_given.append(['badge', badgeNum]) awards_given.append(['badge', badgeNum])
temp = {}
for item in OLD_org_trophy:
categ = item['categ']
rank = item['trophy_true_rank']
temp[categ] = rank
for item in NEW_org_trophy:
categ = item['categ']
rank = item['trophy_true_rank']
if rank > temp[categ]:
awards_given.append(['trophy', [categ, rank]])
for award in awards_given: for award in awards_given:
# update awards given # update awards given
serv_redis_db.zadd('CONTRIB_LAST_AWARDS:'+util.getDateStrFormat(now), nowSec, json.dumps({'org': org, 'award': award, 'epoch': nowSec })) serv_redis_db.zadd('CONTRIB_LAST_AWARDS:'+util.getDateStrFormat(now), nowSec, json.dumps({'org': org, 'award': award, 'epoch': nowSec }))

View File

@ -95,6 +95,12 @@
min-width: 45px; min-width: 45px;
} }
#panelawards .dataTables_scrollBody {
overflow-x:hidden !important;
overflow-y:auto !important;
}
.dataTables_filter > label { .dataTables_filter > label {
margin-bottom: 0px; margin-bottom: 0px;
} }

View File

@ -5,7 +5,9 @@ var datatableFameQuant;
var refresh_speed = min_between_reload*60; var refresh_speed = min_between_reload*60;
var will_reload = $("#reloadCheckbox").is(':checked'); var will_reload = $("#reloadCheckbox").is(':checked');
var sec_before_reload = refresh_speed; var sec_before_reload = refresh_speed;
var plotLineChart var plotLineChart;
var source_awards;
var source_lastContrib;
/* CONFIG */ /* CONFIG */
var maxRank = 16; var maxRank = 16;
@ -65,6 +67,8 @@ var optionDatatable_light = {
}; };
var optionDatatable_top = jQuery.extend({}, optionDatatable_light) var optionDatatable_top = jQuery.extend({}, optionDatatable_light)
var optionDatatable_last = jQuery.extend({}, optionDatatable_light) var optionDatatable_last = jQuery.extend({}, optionDatatable_light)
optionDatatable_last["ordering"] = true;
optionDatatable_last["order"] = [[ 0, "dec" ]];
optionDatatable_last.columnDefs = [ optionDatatable_last.columnDefs = [
{ className: "small", "targets": [ 0 ] }, { className: "small", "targets": [ 0 ] },
{ className: "verticalAlign", "targets": [ 1 ] }, { className: "verticalAlign", "targets": [ 1 ] },
@ -72,12 +76,7 @@ optionDatatable_last.columnDefs = [
{ className: "centerCellPicOrgLogo", "targets": [ 3 ] }, { className: "centerCellPicOrgLogo", "targets": [ 3 ] },
{ className: "centerCellPicOrgLogo verticalAlign", "targets": [ 4 ] }, { className: "centerCellPicOrgLogo verticalAlign", "targets": [ 4 ] },
{ className: "centerCellPicOrgLogo verticalAlign", "targets": [ 5 ] }, { className: "centerCellPicOrgLogo verticalAlign", "targets": [ 5 ] },
{ className: "verticalAlign", "targets": [ 6 ] }, { className: "verticalAlign", "targets": [ 6 ] }
{ 'orderData':[6], 'targets': [0] },
{
'targets': [6],
'searchable': false
},
] ]
var optionDatatable_fame = jQuery.extend({}, optionDatatable_light) var optionDatatable_fame = jQuery.extend({}, optionDatatable_light)
optionDatatable_fame.scrollY = '45vh'; optionDatatable_fame.scrollY = '45vh';
@ -100,10 +99,12 @@ var optionDatatable_Categ = {
var optionDatatable_awards = jQuery.extend({}, optionDatatable_light); var optionDatatable_awards = jQuery.extend({}, optionDatatable_light);
optionDatatable_awards["ordering"] = true; optionDatatable_awards["ordering"] = true;
optionDatatable_awards["order"] = [[ 0, "dec" ]]; optionDatatable_awards["order"] = [[ 0, "dec" ]];
optionDatatable_awards["scrollX"] = false;
optionDatatable_awards.columnDefs = [ optionDatatable_awards.columnDefs = [
{ className: "small", "targets": [ 0 ] }, { className: "small", "targets": [ 0 ] },
{ className: "centerCellPicOrgLogo", "targets": [ 1 ] }, { className: "centerCellPicOrgLogo", "targets": [ 1 ] },
{ className: "centerCellPicOrgLogo verticalAlign", "targets": [ 2 ] } { className: "centerCellPicOrgLogo verticalAlign", "targets": [ 2 ] },
{ className: "centerCellPicOrgLogo", "targets": [ 4 ] },
]; ];
var typeaheadOption = { var typeaheadOption = {
@ -178,11 +179,25 @@ function createImg(source, size) {
return obj.outerHTML; return obj.outerHTML;
} }
function createTrophyImg(rank, size, categ) {
var obj = document.createElement('img');
obj.height = size;
obj.width = size;
obj.style.margin = 'auto';
obj.src = url_baseTrophyLogo+rank+'.png';;
obj.title = trophy_title[rank] + " in " + categ;
obj.type = "image/png"
obj.alt = ""
return obj.outerHTML;
}
function createHonorImg(array, size) { function createHonorImg(array, size) {
size = 32; size = 32;
var div = document.createElement('div'); var div = document.createElement('div');
div.style.boxShadow = '0px 0px 5px #00000099'; div.style.boxShadow = '0px 0px 5px #00000099';
div.style.backgroundColor = '#e1e1e1'; div.style.backgroundColor = '#e1e1e1';
if (!Array.isArray(array))
array = [array];
for (badgeNum of array) { for (badgeNum of array) {
var obj = document.createElement('img'); var obj = document.createElement('img');
obj.height = size; obj.height = size;
@ -321,8 +336,9 @@ function addLastFromJson(datatable, url) {
function addLastContributor(datatable, data, update) { function addLastContributor(datatable, data, update) {
var date = new Date(data.epoch*1000); var date = new Date(data.epoch*1000);
date.toString = function() {return this.toTimeString().slice(0,-15) +' '+ this.toLocaleDateString(); };
var to_add = [ var to_add = [
date.toTimeString().slice(0,-15) +' '+ date.toLocaleDateString(), date,
data.pnts, data.pnts,
getMonthlyRankIcon(data.rank), getMonthlyRankIcon(data.rank),
getOrgRankIcon(data.orgRank, 60), getOrgRankIcon(data.orgRank, 60),
@ -332,23 +348,32 @@ function addLastContributor(datatable, data, update) {
]; ];
if (update == undefined || update == false) { if (update == undefined || update == false) {
datatable.row.add(to_add); datatable.row.add(to_add);
datatable.draw();
} else if(update == true) { } else if(update == true) {
var row_added = false;
datatable.rows().every( function() { datatable.rows().every( function() {
if($(this.data()[6])[0].text == data.org) { if($(this.data()[6])[0].text == data.org) {
var node = $(datatable.row( this ).node());
datatable.row( this ).data( to_add ); datatable.row( this ).data( to_add );
$(this).addClass( "warning" ); row_added = true;
} }
datatable.draw();
}); });
if (!row_added) {
var node = $(datatable.row.add(to_add).draw().node());
node.effect("slide", 700);
}
} }
} }
function addAwards(datatableAwards, json) { function addAwards(datatableAwards, json, playAnim) {
if(json.award[0] == 'contribution_status') { if(json.award[0] == 'contribution_status') {
var award = getOrgRankIcon(json.award[1], 60) var award = getOrgRankIcon(json.award[1], 60);
} else if (json.award[0] == 'badge') { } else if (json.award[0] == 'badge') {
var award = createHonorImg([json.award[1]], 20) var award = createHonorImg(json.award[1], 20);
} else if (json.award[0] == 'trophy') { } else if (json.award[0] == 'trophy') {
var award = createHonorImg(json.award[1], 20) var categ = json.award[1][0];
var award = createTrophyImg(json.award[1][1], 40, categ);
} }
var date = new Date(json.epoch*1000); var date = new Date(json.epoch*1000);
date.toString = function() {return this.toTimeString().slice(0,-15) +' '+ this.toLocaleDateString(); }; date.toString = function() {return this.toTimeString().slice(0,-15) +' '+ this.toLocaleDateString(); };
@ -359,7 +384,9 @@ function addAwards(datatableAwards, json) {
createOrgLink(json.org), createOrgLink(json.org),
award, award,
]; ];
datatableAwards.row.add(to_add); var node = $(datatableAwards.row.add(to_add).draw().node());
if(playAnim)
node.effect("slide", 700);
} }
function updateProgressBar(org) { function updateProgressBar(org) {
@ -503,7 +530,7 @@ function updateProgressHeader(org) {
source = url_baseTrophyLogo+rank+'.png' source = url_baseTrophyLogo+rank+'.png'
$('#trophy_'+categ).attr('src', source); $('#trophy_'+categ).attr('src', source);
$('#trophy_'+categ).attr('title', trophy_title[rank]); $('#trophy_'+categ).attr('title', trophy_title[rank]);
$('#trophy_'+categ).popover({title: trophy_title[rank], content: rank, trigger: "hover", placement: "bottom"}); $('#trophy_'+categ).popover({title: trophy_title[rank], content: 'Level: '+rank+' ('+trophy_points+' points)', trigger: "hover", placement: "bottom"});
} }
}); });
@ -528,6 +555,7 @@ function updateTimer() {
sec_before_reload--; sec_before_reload--;
if (sec_before_reload < 1) { if (sec_before_reload < 1) {
source_lastContrib.close(); source_lastContrib.close();
source_awards.close();
location.reload(); location.reload();
} else { } else {
$('#labelRemainingTime').text(timeToString(sec_before_reload)); $('#labelRemainingTime').text(timeToString(sec_before_reload));
@ -583,9 +611,8 @@ $(document).ready(function() {
// latest awards // latest awards
$.getJSON( url_getLatestAwards, function( data ) { $.getJSON( url_getLatestAwards, function( data ) {
for (i in data) { for (i in data) {
addAwards(datatableAwards, data[i]); addAwards(datatableAwards, data[i], false);
} }
datatableAwards.draw();
}); });
if(currOrg != "") // currOrg selected if(currOrg != "") // currOrg selected
@ -596,7 +623,6 @@ $(document).ready(function() {
source_lastContrib.onmessage = function(event) { source_lastContrib.onmessage = function(event) {
var json = jQuery.parseJSON( event.data ); var json = jQuery.parseJSON( event.data );
addLastContributor(datatableLast, json, true); addLastContributor(datatableLast, json, true);
datatableLast.draw();
updateProgressBar(json.org); updateProgressBar(json.org);
updateOvertakePnts(); updateOvertakePnts();
sec_before_reload = refresh_speed; //reset timer at each contribution sec_before_reload = refresh_speed; //reset timer at each contribution
@ -605,7 +631,6 @@ $(document).ready(function() {
source_awards = new EventSource(url_eventStreamAwards); source_awards = new EventSource(url_eventStreamAwards);
source_awards.onmessage = function(event) { source_awards.onmessage = function(event) {
var json = jQuery.parseJSON( event.data ); var json = jQuery.parseJSON( event.data );
addAwards(datatableAwards, json); addAwards(datatableAwards, json, true);
datatableAwards.draw();
}; };
}); });

View File

@ -152,13 +152,15 @@ def handleContribution(zmq_name, org, contribType, categ, action, pntMultiplier=
#CONTRIB_CATEG retain the contribution per category, not the point earned in this categ #CONTRIB_CATEG retain the contribution per category, not the point earned in this categ
push_to_redis_zset('CONTRIB_CATEG', org, count=1, endSubkey=':'+noSpaceLower(categ)) push_to_redis_zset('CONTRIB_CATEG', org, count=1, endSubkey=':'+noSpaceLower(categ))
publish_log(zmq_name, 'CONTRIBUTION', {'org': org, 'categ': categ, 'action': action, 'epoch': nowSec }, channel=CHANNEL_LASTCONTRIB) publish_log(zmq_name, 'CONTRIBUTION', {'org': org, 'categ': categ, 'action': action, 'epoch': nowSec }, channel=CHANNEL_LASTCONTRIB)
else:
categ = ""
serv_redis_db.sadd('CONTRIB_ALL_ORG', org) serv_redis_db.sadd('CONTRIB_ALL_ORG', org)
serv_redis_db.zadd('CONTRIB_LAST:'+util.getDateStrFormat(now), nowSec, org) serv_redis_db.zadd('CONTRIB_LAST:'+util.getDateStrFormat(now), nowSec, org)
serv_redis_db.expire('CONTRIB_LAST:'+util.getDateStrFormat(now), ONE_DAY*7) #expire after 7 day serv_redis_db.expire('CONTRIB_LAST:'+util.getDateStrFormat(now), ONE_DAY*7) #expire after 7 day
awards_given = contributor_helper.updateOrgContributionRank(org, pnts_to_add, action, contribType, eventTime=datetime.datetime.now(), isLabeled=isLabeled) awards_given = contributor_helper.updateOrgContributionRank(org, pnts_to_add, action, contribType, eventTime=datetime.datetime.now(), isLabeled=isLabeled, categ=noSpaceLower(categ))
for award in awards_given: for award in awards_given:
# update awards given # update awards given