-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.php
139 lines (115 loc) · 3.22 KB
/
Player.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
class Player
{
public $name;
public $team;
public $guns = [];
public $health = 0;
public $money = 0;
public $kills = 0;
public $deaths = 0;
public $joined_at;
/**
* create CTPlayer or TPlayer.
*/
public static function create(string $name, string $time): Player
{
$player = new self;
$player->name = $name;
$player->set_join_time($time);
$player->set_join_health($time);
return $player;
}
/**
* joins the given player to the relevant team.
*/
public function join(string $team): Player
{
if ($team === ct()->name)
ct()->join_player($this);
else
t()->join_player($this);
$this->team = $team;
return $this;
}
public function shoot(Player $attacked, string $gun_type)
{
if ($this->health === 0)
exception('attacker is dead');
if ($attacked->health === 0)
exception('attacked is dead');
if (! ($gun = $this->gun($gun_type)))
exception('no such gun');
if ($this->team === $attacked->team)
exception('friendly fire');
$attacked->shot($this, $gun);
}
public function shot(Player $attacker, Gun $gun)
{
$health = $this->decrease_health($gun->damage);
if ($health !== 0)
return;
$this->die();
$attacker->add_money($gun->reward);
$attacker->kills++;
}
private function die()
{
$this->deaths++;
$this->guns = [];
}
/**
* buy gun for player.
*/
public function buy(string $gun_name)
{
$gun = shop()->buy($gun_name);
if ($gun->team !== $this->team && $gun->team !== null)
exception('invalid category gun');
if ($this->gun($gun->type))
exception("you have a {$gun->type}");
if ($gun->price > $this->money)
exception('no enough money');
$this->guns[] = $gun;
$this->subtract_money($gun->price);
}
/**
* return player's gun with given type. and null if not found between player's guns.
*/
private function gun(string $gun_type)
{
foreach ($this->guns as $gun) {
if ($gun->type === $gun_type)
return $gun;
}
return null;
}
public function decrease_health(int $damage): int
{
$this->health -= $damage;
$this->health > 0 ?: $this->health = 0;
return $this->health;
}
public function add_money (int $money)
{
$this->money += $money;
$this->money < 10000 ?: $this->money = 10000;
}
public function subtract_money(int $money)
{
$this->money -= $money;
}
private function set_join_time(string $time)
{
$milliseconds = explode(':', $time)[2];
$this->joined_at = strtotime(str_replace(':' . $milliseconds, '', $time)) . $milliseconds;
}
private function set_join_health(string $time)
{
$time = (int) (explode(':', $time)[1] . explode(':', $time)[2]);
if ($time <= 3000) {
$this->health = 100;
$this->guns[] = new Gun('Knife', null, 'knife', 0, 43, 500);
}
}
}