Skip to content

Commit

Permalink
Reset player Elos when most recent match is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Sep 24, 2017
1 parent f95fb12 commit d0e3a02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 11 additions & 3 deletions controllers/MatchController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -79,9 +80,9 @@ public function createAction(Player $me)
public function deleteAction(Player $me, Match $match)
{
return $this->delete($match, $me, function () use ($match, $me) {
if ($match->getTeamA()->isLastMatch($match)
&& $match->getTeamB()->isLastMatch($match)) {
$match->resetELOs();
if ($match->getTeamA()->isLastMatch($match) && $match->getTeamB()->isLastMatch($match)) {
$match->resetTeamElos();
$match->resetPlayerElos();
} elseif ($me->canEdit($match)) {
$url = Service::getGenerator()->generate('match_recalculate', array(
'match' => $match->getId(),
Expand Down Expand Up @@ -176,6 +177,9 @@ protected function getMessages($type, $name = '')
return $messages;
}

/**
* @param Form $form
*/
protected function validate($form)
{
// Make sure that two different teams participated in a match, i.e. a team
Expand Down Expand Up @@ -217,6 +221,10 @@ protected function validate($form)
}
}

/**
* @param Form $form
* @param Match $match
*/
protected function validateEdit($form, $match)
{
if ($match->isOfficial() && $form->get('type')->getData() !== Match::OFFICIAL) {
Expand Down
12 changes: 10 additions & 2 deletions models/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ public function isOfficial()
*
* @return self
*/
public function resetELOs()
public function resetTeamElos()
{
if ($this->match_type === self::OFFICIAL) {
$this->getTeamA()->supportsMatchCount() && $this->getTeamA()->changeELO(-$this->elo_diff);
Expand Down Expand Up @@ -1123,6 +1123,14 @@ public function isEloCorrect()
);
}

/**
* Remove Elo recordings for players participating in this match
*/
public function resetPlayerElos()
{
$this->db->execute('DELETE FROM player_elo WHERE match_id = ?', [$this->getId()]);
}

/**
* Recalculate the match's elo and adjust the team ELO values
*/
Expand All @@ -1135,7 +1143,7 @@ public function recalculateElo()
$a = $this->getTeamA();
$b = $this->getTeamB();

$this->db->execute('DELETE FROM player_elo WHERE match_id = ?', [$this->getId()]);
$this->resetPlayerElos();

foreach ($this->getPlayers() as $player) {
$player->invalidateMatchFromCache($this);
Expand Down

0 comments on commit d0e3a02

Please sign in to comment.