Skip to content

Commit

Permalink
Top updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Maris Jankovskis authored and Maris Jankovskis committed Jan 16, 2018
1 parent 09a71e2 commit 5682bc1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
61 changes: 61 additions & 0 deletions bot/FootballState.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,65 @@ public function isFinishedGame()

return false;
}

/**
* @param array $rankByWonGames
* @return array
*/
public function getRankByWinRate(array $rankByWonGames = [])
{
usort($rankByWonGames, function ($userA, $userB) {
$scoreA = $userA['games_played'] ? $userA['games_won'] / $userA['games_played'] : 0;
$scoreB = $userB['games_played'] ? $userB['games_won'] / $userB['games_played'] : 0;

if ($scoreA > $scoreB) {
return -1;
} else if ($scoreA < $scoreB) {
return 1;
} else {
if ($userA['games_won'] > $userB['games_won']) {
return -1;
} else if ($userA['games_won'] < $userB['games_won']) {
return 1;
}
}

return -1;
});

return $rankByWonGames;
}

/**
* @param array $rankByWonGames
* @param array $rankByWinRate
* @return array
*/
public function mergeRanks(array $rankByWonGames, array $rankByWinRate)
{
$userIdDictionary = [];
foreach ($rankByWinRate as $index => $user) {
$userIdDictionary[$user['id']] = [
'user' => $user,
'rank' => $index,
];
}

foreach ($rankByWonGames as $index => $user) {
$userIdDictionary[$user['id']]['rank'] += $index;
$userIdDictionary[$user['id']]['rank'] /= 2.0;
}

usort($userIdDictionary, function ($a, $b) {
return $a['rank'] > $b['rank'];
});

$rank = [];

foreach ($userIdDictionary as $userId => $rankData) {
$rank[] = $rankData['user'];
}

return $rank;
}
}
10 changes: 6 additions & 4 deletions bot/MessageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ protected function onPing(Message $message): string
*/
protected function onTop(Message $message)
{
$userList = $this->state->db->getTopList();
$rankByWonGames = $this->state->db->getTopList();

if (empty($userList)) {
if (empty($rankByWonGames)) {
return 'No players in top!';
}

$result = '';
$rankByWinRate = $this->state->getRankByWinRate($rankByWonGames); // Arrays are copied instead of referenced
$topList = $this->state->mergeRanks($rankByWonGames, $rankByWinRate);

foreach ($userList as $k => $user) {
$result = '';
foreach ($topList as $k => $user) {
$score = $user['games_played'] ? number_format((($user['games_won'] * 100) / $user['games_played']),0) : 0;
$result .= ($k + 1) . '. <@' . $user['id'] . '> - Won: *' . $user['games_won'] . '* - Games played: *' . $user['games_played'] . "* - Win rate: *" . $score . "%*\n";
}
Expand Down

0 comments on commit 5682bc1

Please sign in to comment.