<?php
/*
game:
[
{
team: string,
game: string,
player: string,
score: integer
}
]
team:
{
team_name: {
game_name: {
maximum: integer,
minimum: integer,
players: {
player_name: score
}
}
}
}
player:
{
team_name: {
player_name: {
game_name: {
difference: integer,
maximum: integer,
minimum: integer,
points: double,
score: integer
}
}
}
}
*/
class Game
{
public string $team;
public string $game;
public string $player;
public int $score;
public function __construct(string $team, string $game, string $player, int $score)
{
$this->team = $team;
$this->game = $game;
$this->player = $player;
$this->score = $score;
}
}
$games = [
# Game1
new Game('Team1', 'Game1', 'Player1', 50000),
new Game('Team1', 'Game1', 'Player2', 45000),
new Game('Team1', 'Game1', 'Player3', 40000),
new Game('Team1', 'Game1', 'Player4', 35000),
new Game('Team1', 'Game1', 'Player5', 30000),
new Game('Team1', 'Game1', 'Player6', 25000),
new Game('Team1', 'Game1', 'Player7', 20000),
new Game('Team1', 'Game1', 'Player8', 15000),
new Game('Team1', 'Game1', 'Player9', 10000),
new Game('Team1', 'Game1', 'Player10', 5000),
# Game2
new Game('Team1', 'Game2', 'Player1', 50000),
new Game('Team1', 'Game2', 'Player2', 45000)
];
# ===== ===== ===== ===== =====
(new class($games)
{
private function create_team_structure(array $games): array
{
$response = array();
foreach ($games as $game_object)
{
$team = $game_object->team;
$game = $game_object->game;
$player = $game_object->player;
$score = $game_object->score;
$maximum = 0;
$minimum = 50000;
# create team if not exists
if (array_key_exists($team, $response) === false)
{
$response[$team] = array();
}
# create game if not exists
if (array_key_exists($game, $response[$team]) === false)
{
$response[$team][$game] = array(
'maximum' => $maximum,
'minimum' => $minimum,
'players' => []
);
}
# calc max and min
$game_response = $response[$team][$game];
$response[$team][$game]['maximum'] = max($game_response['maximum'], $score);
$response[$team][$game]['minimum'] = min($game_response['minimum'], $score);
# add player
$response[$team][$game]['players'][$player] = $score;
}
return $response;
}
private function create_player_structure(array $games): array
{
$response = array();
foreach ($games as $game_name => $game)
{
$maximum = $game['maximum'];
$minimum = $game['minimum'];
$players = $game['players'];
foreach ($players as $player_name => $player_score)
{
# create player if not exists
if (array_key_exists($player_name, $response) === false)
{
$response[$player_name] = array();
}
$difference = $maximum - $minimum;
$points = ($player_score - $minimum) / $difference;
$response[$player_name][$game_name] = array(
'difference' => $difference,
'maximum' => $maximum,
'minimum' => $minimum,
'points' => $points,
'score' => $player_score
);
}
}
return $response;
}
public function __construct(array $games)
{
echo 'run' . PHP_EOL;
$teams = $this->create_team_structure($games);
$players = array();
foreach ($teams as $team_name => $team)
{
$players[$team_name] = $this->create_player_structure($team);
}
print_r($games);
print_r($teams);
print_r($players);
echo 'end' . PHP_EOL;
}
});
To embed this project on your website, copy the following code and paste it into your website's HTML: