-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeam.php
88 lines (71 loc) · 1.98 KB
/
Team.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
class Team
{
public $players = [];
public $wins = 0;
public function won()
{
$this->wins++;
foreach ($this->players as $p) {
$p->add_money(2700);
}
}
public function lose()
{
foreach ($this->players as $p) {
$p->add_money(2400);
}
}
/**
* join a player to a team.
*/
public function join_player(Player $player): bool // void
{
// ! $this->player_exists($player->name) ?: exception('you are already in this game');
if ($this->player_exists($player->name))
exception('you are already in this game'); // TODO: ask: maybe one line these errors?!
if (count($this->players) >= 10)
exception('this team is full');
$this->players[] = $player;
$player->add_money(1000);
return true;
}
/**
* checks if player exists in team or not.
*/
public function player_exists($name): bool
{
return (bool) $this->get_player($name);
}
/**
* if player exists in team. returns the player. otherwise returns false.
*/
public function get_player($name)
{
foreach ($this->players as $player) {
if ($player->name === $name)
return $player;
}
return null;
}
/**
* prints team score-board.
*/
public function board() {
echo $this->name . ":\n";
$players = [];
foreach ($this->players as $player) {
$players[] = (array) $player;
}
array_multisort(
array_column($players, 'kills'), SORT_DESC,
array_column($players, 'deaths'), SORT_ASC,
array_column($players, 'joined_at'), SORT_ASC,
$players);
foreach ($players as $rank => $p) {
echo $rank+1 . " {$p['name']} {$p['kills']} {$p['deaths']}";
if ($rank !== array_key_last_php7($players))
echo "\n";
}
}
}