Skip to content

Commit

Permalink
Display elo update plot graph to supporter users
Browse files Browse the repository at this point in the history
  • Loading branch information
lladdy committed Feb 15, 2022
1 parent 9e12482 commit dd17562
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aiarena/core/models/competition_participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def elo_graph_upload_to(instance, filename):
return '/'.join(['graphs', f'{instance.competition_id}_{instance.bot.id}_{instance.bot.name}.png'])

def elo_graph_update_plot_upload_to(instance, filename):
return '/'.join(['graphs', f'{instance.competition_id}_{instance.bot.id}_{instance.bot.name}_update_plot.png'])
return '/'.join(['competitions', 'stats', f'{instance.id}_elo_graph_update_plot.png'])


class CompetitionParticipation(models.Model, LockableModelMixin):
Expand Down
5 changes: 3 additions & 2 deletions aiarena/core/stats/stats_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ def _generate_plot_images(df, update_date: datetime):
legend = []

fig, ax1 = plt.subplots(1, 1, figsize=(12, 9), sharex='all', sharey='all')
legend.append('ELO')
ax1.plot(df["Date"], df['ELO'], color='#86c232')
# ax.plot(df["Date"], df['ELO'], color='#86c232')
ax1.spines["top"].set_visible(False)
Expand All @@ -226,17 +225,19 @@ def _generate_plot_images(df, update_date: datetime):
ax1.tick_params(axis='y', colors='#86c232', labelsize=16)
# if update_date:

legend.append('ELO')
ax1.legend(legend, loc='lower center', fontsize='xx-large')

plt.title('ELO over time', fontsize=20, color=('#86c232'))

plt.tight_layout() # Avoids savefig cutting off x-label
plt.savefig(plot1, format="png", transparent=True)

legend.append('Last update')
ax1.legend(legend, loc='lower center', fontsize='xx-large')
ax1.vlines([update_date],
min(df['ELO']), max(df['ELO']), colors='r', linestyles='--')
legend.append('Last update')
ax1.legend(legend, loc='lower center', fontsize='xx-large')
plt.savefig(plot2, format="png", transparent=True)
plt.cla() # Clears axis in preparation for new graph
return plot1, plot2
Expand Down
7 changes: 6 additions & 1 deletion aiarena/frontend/templates/bot_competition_stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@
<br/>
</div>

{% if competitionparticipation.elo_graph %}
{# If this user owns the bot, and is a supporter, display the private ELO graph #}
{% if competitionparticipation.bot.user == user and user.patreon_level != 'none' and competitionparticipation.elo_graph_update_plot %}
<div id="elo_graph">
<img src="{{ competitionparticipation.elo_graph_update_plot.url }}" alt="ELOGraph"/>
</div>
{% elif competitionparticipation.elo_graph %}
<div id="elo_graph">
<img src="{{ competitionparticipation.elo_graph.url }}" alt="ELOGraph"/>
</div>
Expand Down
25 changes: 25 additions & 0 deletions aiarena/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,31 @@ def get_context_data(self, **kwargs):
return context


class BotCompetitionStatsEloUpdatePlot(DetailView):
model = CompetitionParticipation
template_name = 'bot_competition_stats.html'

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['competition_bot_matchups'] = self.object.competition_matchup_stats.filter(
opponent__competition=context['competitionparticipation'].competition).order_by('-win_perc').distinct()
context['updated'] = context['competition_bot_matchups'][0].updated if context['competition_bot_matchups'] else "Never"
return context


class BotCompetitionStatsEloGraphUpdatePlot(PrivateStorageDetailView):
model = CompetitionParticipation
model_file_field = 'elo_graph_update_plot'

content_disposition = 'inline'

def get_content_disposition_filename(self, private_file):
return 'elo_graph_update_plot.png'

def can_access_file(self, private_file):
# Allow if the owner of the bot and a patreon supporter
return private_file.parent_object.bot.user == private_file.request.user and private_file.request.user.patreon_level != 'None'


class BotUpdateForm(forms.ModelForm):
"""
Expand Down
1 change: 1 addition & 0 deletions aiarena/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

path('competitions/stats/<int:pk>/', core_views.BotCompetitionStatsDetail.as_view()),
path('competitions/stats/<int:pk>/<slug:slug>', core_views.BotCompetitionStatsDetail.as_view(), name='bot_competition_stats'),
path('competitions/stats/<int:pk>_elo_graph_update_plot.png', core_views.BotCompetitionStatsEloGraphUpdatePlot.as_view()),

path('botupload/', core_views.BotUpload.as_view(), name='botupload'),
path('requestmatch/', core_views.RequestMatch.as_view(), name='requestmatch'),
Expand Down

0 comments on commit dd17562

Please sign in to comment.