Commit 34e82b3c authored by Hubert Denkmair's avatar Hubert Denkmair

add stats command

parent a398f2e4
from import BaseCommand, CommandError
from django.db.models import Count
from core.models import SnakeGame, SnakeVersion
from django.contrib.auth.models import User
class Command(BaseCommand):
help = "show stats"
def handle(self, *args, **options):
num_games = SnakeGame.objects.all().count()
print("total number of games: ", num_games)
print("ten players with the most versions:")
ten_most_versions = SnakeVersion.objects.values('user__username').annotate(num_versions=Count('id')).order_by('-num_versions')[:10]
for v in ten_most_versions:
print(v['num_versions'], v['user__username'])
# most kills
kills = []
deaths = []
for user in User.objects.all():
kills.append([SnakeGame.objects.filter(killer=user).count(), user.username])
deaths.append([SnakeGame.objects.filter(user=user).count(), user.username])
print("ten players with the most kills:")
for k in sorted(kills, key=lambda v: v[0], reverse=True)[:10]:
print(k[0], ": ", k[1])
print("ten players killed most often:")
for k in sorted(deaths, key=lambda v: v[0], reverse=True)[:10]:
print(k[0], ": ", k[1])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment