Skip to content

Commit

Permalink
Database cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoTN committed Feb 24, 2017
1 parent fd8f527 commit c905f9a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 172 deletions.
1 change: 0 additions & 1 deletion api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def allow_accepting(self, request, view):
is_correct_team = view.get_object().team.name in request_teams
return is_correct_team


def has_permission(self, request, view):
if request.method == 'GET':
return True
Expand Down
4 changes: 1 addition & 3 deletions tfoosball/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import Player, MatchLegacy, ExpHistoryLegacy, Team, Member, ExpHistory, Match
from .models import Player, Team, Member, ExpHistory, Match


class ExpHistoryAdmin(admin.ModelAdmin):
Expand All @@ -14,8 +14,6 @@ class TeamAdmin(admin.ModelAdmin):

admin.site.register(Player)
admin.site.register(Match)
admin.site.register(MatchLegacy)
admin.site.register(Member)
admin.site.register(Team, TeamAdmin)
admin.site.register(ExpHistoryLegacy, ExpHistoryAdmin)
admin.site.register(ExpHistory, ExpHistoryAdmin)
22 changes: 0 additions & 22 deletions tfoosball/management/commands/migrate_exphistory.py

This file was deleted.

27 changes: 0 additions & 27 deletions tfoosball/management/commands/migrate_match.py

This file was deleted.

85 changes: 85 additions & 0 deletions tfoosball/migrations/0020_auto_20170224_2245.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-02-24 21:45
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('tfoosball', '0019_auto_20170219_1457'),
]

operations = [
migrations.RemoveField(
model_name='exphistorylegacy',
name='match',
),
migrations.RemoveField(
model_name='exphistorylegacy',
name='player',
),
migrations.RemoveField(
model_name='matchlegacy',
name='blue_att',
),
migrations.RemoveField(
model_name='matchlegacy',
name='blue_def',
),
migrations.RemoveField(
model_name='matchlegacy',
name='red_att',
),
migrations.RemoveField(
model_name='matchlegacy',
name='red_def',
),
migrations.RemoveField(
model_name='player',
name='curr_lose_streak',
),
migrations.RemoveField(
model_name='player',
name='curr_win_streak',
),
migrations.RemoveField(
model_name='player',
name='defence',
),
migrations.RemoveField(
model_name='player',
name='exp',
),
migrations.RemoveField(
model_name='player',
name='highest_exp',
),
migrations.RemoveField(
model_name='player',
name='lose_streak',
),
migrations.RemoveField(
model_name='player',
name='lowest_exp',
),
migrations.RemoveField(
model_name='player',
name='offence',
),
migrations.RemoveField(
model_name='player',
name='played',
),
migrations.RemoveField(
model_name='player',
name='win_streak',
),
migrations.DeleteModel(
name='ExpHistoryLegacy',
),
migrations.DeleteModel(
name='MatchLegacy',
),
]
119 changes: 0 additions & 119 deletions tfoosball/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,9 @@ def __str__(self):


class Player(AbstractUser):
exp = models.IntegerField(blank=False, null=False, default=1000)
offence = models.IntegerField(default=0)
defence = models.IntegerField(default=0)
played = models.IntegerField(default=0)
win_streak = models.IntegerField(default=0)
curr_win_streak = models.IntegerField(default=0)
lose_streak = models.IntegerField(default=0)
curr_lose_streak = models.IntegerField(default=0)
lowest_exp = models.IntegerField(default=1000)
highest_exp = models.IntegerField(default=1000)
hidden = models.BooleanField(default=False)
teams = models.ManyToManyField(Team, through='Member')

@property
def won(self):
return self.offence + self.defence

@property
def lost(self):
return self.played - self.won

@property
def att_ratio(self):
return round(self.offence / self.won if self.won > 0 else 0, 2)

@property
def def_ratio(self):
return round(self.defence / self.won if self.won > 0 else 0, 2)

@property
def win_ratio(self):
return round(self.won / self.played if self.played > 0 else 0, 2)

def update_extremes(self):
self.win_streak = max(self.curr_win_streak, self.win_streak)
self.lose_streak = max(self.curr_lose_streak, self.lose_streak)
self.lowest_exp = min(self.lowest_exp, self.exp)
self.highest_exp = max(self.highest_exp, self.exp)

def after_match_update(self, points, is_winner, is_offence):
self.exp += points
self.played += 1

if is_winner:
self.curr_win_streak += 1
self.curr_lose_streak = 0

if is_offence:
self.offence += 1
else:
self.defence += 1
else:
self.curr_win_streak = 0
self.curr_lose_streak += 1

self.update_extremes()

self.save()

def get_latest_matches(self):
return MatchLegacy.objects.all() \
.filter(Q(red_att=self.id) | Q(red_def=self.id) | Q(blue_att=self.id) | Q(blue_def=self.id)) \
.order_by('-date')


class Member(models.Model):
team = models.ForeignKey(Team, on_delete=models.CASCADE)
Expand All @@ -102,26 +41,6 @@ class Member(models.Model):
is_team_admin = models.BooleanField(default=False)
is_accepted = models.BooleanField(default=False)

@property
def won(self):
return self.offence + self.defence

@property
def lost(self):
return self.played - self.won

@property
def att_ratio(self):
return round(self.offence / self.won if self.won > 0 else 0, 2)

@property
def def_ratio(self):
return round(self.defence / self.won if self.won > 0 else 0, 2)

@property
def win_ratio(self):
return round(self.won / self.played if self.played > 0 else 0, 2)

def __str__(self):
return '{0} ({1})'.format(self.username, self.team.name)

Expand Down Expand Up @@ -176,37 +95,6 @@ def after_match_update(self, points, is_winner, is_offence):
self.save()


class MatchLegacy(models.Model):
red_att = models.ForeignKey(Player, related_name='red_att', db_index=False)
red_def = models.ForeignKey(Player, related_name='red_def', db_index=False)
blue_att = models.ForeignKey(Player, related_name='blue_att', db_index=False)
blue_def = models.ForeignKey(Player, related_name='blue_def', db_index=False)
date = models.DateTimeField(auto_now_add=True, blank=True)
red_score = models.IntegerField()
blue_score = models.IntegerField()
points = models.IntegerField()
status = models.IntegerField(default=20)

def calculate_points(self):
"""
:return: The amount of points that red team should gain and information whether red team won
"""
K = int(self.status)
G = (11+abs(self.red_score - self.blue_score)) / 8
dr = ((self.red_att.exp + self.red_def.exp) - (self.blue_att.exp + self.blue_def.exp))
We = 1 / ((10 ** -(dr/400))+1)
W = 1 if self.red_score > self.blue_score else 0
return int(K*G*(W-We)), self.red_score > self.blue_score

def save(self, *args, **kwargs):
self.points, is_red_winner = self.calculate_points()
self.red_att.after_match_update(self.points, is_red_winner, True)
self.red_def.after_match_update(self.points, is_red_winner, False)
self.blue_att.after_match_update(-self.points, not is_red_winner, True)
self.blue_def.after_match_update(-self.points, not is_red_winner, False)
super(MatchLegacy, self).save(*args, **kwargs)


class MatchQuerySet(models.QuerySet):
def by_team(self, team_id):
return self.filter(
Expand Down Expand Up @@ -273,13 +161,6 @@ def save(self, *args, **kwargs):
super(Match, self).save(*args, **kwargs)


class ExpHistoryLegacy(models.Model):
player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='exp_history_legacy')
date = models.DateField(auto_now_add=True, blank=True)
match = models.ForeignKey(MatchLegacy, on_delete=models.CASCADE, related_name='exp_history_legacy', blank=True, null=True)
exp = models.IntegerField()


class ExpHistory(models.Model):
player = models.ForeignKey(Member, on_delete=models.CASCADE, related_name='exp_history')
date = models.DateField(auto_now_add=True, blank=True)
Expand Down

0 comments on commit c905f9a

Please sign in to comment.