-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.php
More file actions
98 lines (84 loc) · 2.34 KB
/
Game.php
File metadata and controls
98 lines (84 loc) · 2.34 KB
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
89
90
91
92
93
94
95
96
97
98
<?php
class Game
{
// public int $rounds_left;
public $round_commands_left;
private static $instance = null;
private function __construct() {}
public static function get_instance() {
if (self::$instance === null)
self::$instance = new self;
return self::$instance;
}
/**
* checks if player exists in game or not.
*/
public function player_exists($name): bool
{
return (bool) $this->get_player($name);
}
/**
* if player exists in game. returns the player. otherwise returns false.
*/
public function get_player($name)
{
foreach (array_merge(ct()->players, t()->players) as $player) {
if ($player->name === $name)
return $player;
}
return null;
}
/**
* if player exists in game. returns the player. otherwise returns false.
*/
public function players(): array
{
return array_merge(ct()->players, t()->players);
}
/**
* prints full game score-board.
*/
public function board() {
ct()->board();
echo "\n";
t()->board();
}
/**
* specifies round winner. increase wins count of that team and return it.
*/
public function get_round_winner()
{
$ct_players = count(ct()->players);
$t_players = count(t()->players);
$ct_alives = count($this->get_alives(ct()->players));
$t_alives = count($this->get_alives(t()->players));
if ($ct_players && ! $ct_alives)
$winner = t();
if ($t_players && ! $t_alives)
$winner = ct();
// increase wins count
($winner = $winner ?? ct())->won();
$winner instanceof CounterTerrorist ? t()->lose() : ct()->lose();
return $winner;
}
private function get_alives(array $players)
{
$alives = [];
foreach ($players as $p) {
if ($p->health !== 0)
$alives[] = $p;
}
return $alives;
}
// public function get_game_winner(): CounterTerrorist|Terrorist
// {
// return ct()->wins > t()->wins ? ct() : t();
// }
public function start_round()
{
foreach (game()->players() as $p) {
$p->health = 100;
$p->guns[] = new Gun('Knife', null, 'knife', 0, 43, 500);
}
}
}