Skip to content

Commit

Permalink
Merge pull request #7 from betacompany/develop
Browse files Browse the repository at this point in the history
StakeCalculator refactoring, 90 min rules
  • Loading branch information
ortemij committed Jul 1, 2014
2 parents 7d70d62 + 63cf185 commit 3f97b96
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 37 deletions.
74 changes: 41 additions & 33 deletions classes/StakeCalculator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

function sign($n) {
return $n > 0 ? 1 : ($n < 0 ? -1 : 0);
}

class StakeCalculator {

/**
Expand All @@ -10,46 +14,50 @@ class StakeCalculator {
* @return int
*/
public static function calculateScore($real_score1, $real_score2, $want_score1, $want_score2) {
$score = 0;

$real_diff = $real_score1 - $real_score2;
$want_diff = $want_score1 - $want_score2;

$real_sign = sign($real_diff);
$want_sign = sign($want_diff);

if ($real_sign != $want_sign) {
// nothing guessed
return 0;
}

if ($real_score1 == $want_score1 && $real_score2 == $want_score2) {
// score guessed
$score = 4;
} else if ($real_score1 - $real_score2 == $want_score1 - $want_score2) {
// difference guessed
if (abs($real_score1 - $want_score1) <= 1) {
// near
$score = 2;
} else {
// far
$score = 1;
return 4;
}

$real_winner_score = max($real_score1, $real_score2);
$real_loser_score = min($real_score1, $real_score2);

$want_winner_score = max($want_score1, $want_score2);
$want_loser_score = min($want_score1, $want_score2);

$one_score_guessed = $real_winner_score == $want_winner_score || $real_loser_score == $want_loser_score;

if (abs($want_diff - $real_diff) <= 1 && $one_score_guessed) {
if (abs($real_diff) >= 3 && abs($want_diff) >= 3) {
if ($real_winner_score >= 4 && $want_winner_score >= 4) {
// almost guessed completely crushing victory
return 3;
}
}
} else if (
abs($real_score1 - $real_score2) >= 2 &&
abs($want_score1 - $want_score2) >= 2 &&
($real_score1 == $want_score1 && abs($real_score2 - $want_score2) <= 1 ||
$real_score2 == $want_score2 && abs($real_score1 - $want_score1) <= 1)
) {
if (
abs($real_score1 - $real_score2) >= 3 &&
abs($want_score1 - $want_score2) >= 3 &&
max($real_score1, $real_score2) >=4 &&
max($want_score1, $want_score2) >=4
) {
// almost guessed completely crushing victory
$score = 3;
} else {

if (abs($real_diff) >= 2 && abs($want_diff) >= 2) {
// almost guessed convincing/confident (?) victory
$score = 2;
return 2;
}
} else if (
($real_score1 > $real_score2 && $want_score1 > $want_score2) ||
($real_score1 < $real_score2 && $want_score1 < $want_score2)
) {
//winner guessed
$score = 1;
}

return $score;
if ($real_diff == $want_diff && abs($real_score1 - $want_score1) <= 1) {
// near difference guessed
return 2;
}

return 1;
}
}
3 changes: 1 addition & 2 deletions rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
сделанные ставки доступны для просмотра пользователям.
</li>
<li>
Ставки на матчи плей-офф принимаются без учёта пенальти: т.е. на основное (90 минут),
или в случае такового на дополнительное время (120 минут).
Ставки на матчи плей-офф принимаются только на основное время (90 минут).
</li>
<li>
За угаданные результаты игроки получают рейтинговые очки по следующей схеме:
Expand Down
6 changes: 6 additions & 0 deletions templates/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@
</div>
</div>
</div>
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<strong>Внимание!</strong>
Мы опять коварно изменили правила: теперь всегда ставим на 90 минут,
даже в случае дополнительного времени!
</div>
30 changes: 28 additions & 2 deletions tests/StakeCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,38 @@ class StakeCalculatorTest extends PHPUnit_Framework_TestCase {

public function testResultCalculation() {
$this->check(1, 0, 1, 0, 4);

$this->check(2, 0, 1, 0, 1);
$this->check(2, 0, 2, 0, 4);

$this->check(2, 1, 1, 0, 2);
$this->check(4, 1, 4, 0, 3);
$this->check(2, 1, 2, 0, 1);
$this->check(2, 1, 2, 1, 4);

$this->check(3, 0, 4, 0, 2);
// todo ...

$this->check(4, 1, 1, 0, 1);
$this->check(4, 1, 2, 0, 1);
$this->check(4, 1, 2, 1, 1);
$this->check(4, 1, 3, 0, 2);
$this->check(4, 1, 3, 1, 2);
$this->check(4, 1, 3, 2, 1);
$this->check(4, 1, 4, 0, 3);
$this->check(4, 1, 4, 1, 4);

$this->check(5, 0, 4, 0, 3);

$this->check(5, 1, 4, 0, 2);
$this->check(5, 1, 4, 1, 3);
$this->check(5, 1, 5, 0, 3);

$this->check(5, 2, 4, 1, 2);
$this->check(5, 2, 4, 2, 2);
$this->check(5, 2, 5, 1, 3);

$this->check(5, 3, 5, 2, 2);

$this->check(6, 2, 5, 1, 2);
}

public function check($stake1, $stake2, $score1, $score2, $expected) {
Expand Down

0 comments on commit 3f97b96

Please sign in to comment.