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
[TrophyDifficulty]
difficulty=1.5
difficulty=5
[HonorTrophy]
0=No trophy

View File

@ -133,14 +133,17 @@ class Contributor_helper:
return {'rank': final_rank, 'status': to_ret, 'totPoints': self.getOrgContributionTotalPoints(org)}
# return the awards given to the organisation
def updateOrgContributionRank(self, orgName, pnts_to_add, action, contribType, eventTime, isLabeled):
ContributionStatus = chelper.getCurrentContributionStatus(org)
oldRank = ContributionStatus['final_rank']
def updateOrgContributionRank(self, orgName, pnts_to_add, action, contribType, eventTime, isLabeled, categ=""):
ContributionStatus = self.getCurrentContributionStatus(orgName)
oldContributionStatus = ContributionStatus['status']
oldHonorBadges = self.getOrgHonorBadges(orgName)
keyname = 'CONTRIB_ORG:{org}:{orgCateg}'
# update total points
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
if contribType == 'Attribute':
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:
contrib.append([14, util.ONE_DAY*regularlyDays])
print([r for r, 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.expire(keyname.format(org=orgName, orgCateg='CONTRIB_REQ_'+str(rankReq)), ttl)
ContributionStatus = chelper.getCurrentContributionStatus(org)
newRank = ContributionStatus['final_rank']
ContributionStatus = self.getCurrentContributionStatus(orgName)
newContributionStatus = ContributionStatus['status']
newHonorBadges = self.getOrgHonorBadges(orgName)
awards_given = []
if newRank > oldRank:
awards_given.append(['rank', newRank])
for i in range(len(oldContributionStatus)):
if oldContributionStatus[i] != newContributionStatus[i]:
for i in newContributionStatus.keys():
if oldContributionStatus[i] < newContributionStatus[i] and i != ContributionStatus['rank']:
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
''' HONOR BADGES '''
@ -448,15 +452,17 @@ class Contributor_helper:
return { 'remainingPts': 0, 'stepPts': self.rankMultiplier**self.levelMax }
def getRankTrophy(self, points):
if points == 0:
if points <= 0:
return 0
elif points == 1:
return 1
else:
return float("{:.2f}".format(math.log(points, self.trophyDifficulty)))
return float("{:.2f}".format(math.sqrt(points/self.trophyDifficulty)))
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 '''

View File

@ -76,6 +76,7 @@ def main():
ContributionStatus = chelper.getCurrentContributionStatus(org)
OLD_org_c_status = ContributionStatus['status']
OLD_org_honor_badge = chelper.getOrgHonorBadges(org)
OLD_org_trophy = chelper.getOrgTrophies(org)
# ranks
while True:
@ -180,16 +181,29 @@ def main():
ContributionStatus = chelper.getCurrentContributionStatus(org)
NEW_org_c_status = ContributionStatus['status']
NEW_org_honor_badge = chelper.getOrgHonorBadges(org)
NEW_org_trophy = chelper.getOrgTrophies(org)
awards_given = []
for i in NEW_org_c_status.keys():
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:
if badgeNum not in OLD_org_honor_badge:
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:
# update awards given
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;
}
#panelawards .dataTables_scrollBody {
overflow-x:hidden !important;
overflow-y:auto !important;
}
.dataTables_filter > label {
margin-bottom: 0px;
}

View File

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

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
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)
else:
categ = ""
serv_redis_db.sadd('CONTRIB_ALL_ORG', 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
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:
# update awards given