Repository navigation
Expand file tree
/
Copy pathCommand.php
More file actions
136 lines (108 loc) · 3.2 KB
/
Copy pathCommand.php
File metadata and controls
136 lines (108 loc) · 3.2 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
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
<?php
class Command
{
private $output = '';
private $player_name;
public $command;
public function __construct($command)
{
$this->command = explode(' ', trim($command));
$method = toUnderline(strtolower($this->command[0]));
if (method_exists($this, $method))
try {
if ($this->command[0] !== 'ROUND' && --game()->round_commands_left === 0) {
$GLOBALS['winner'] = game()->get_round_winner()->name . ' won';
}
$this->$method();
} catch (CsException $exception){
error($exception->getMessage(), false);
}
}
/**
* add user to a team.
*/
public function add_user()
{
$name = $this->command[1];
$team = $this->command[2];
$time = $this->command[3];
if (game()->player_exists($name))
exception('you are already in this game');
// Player::create($name)->join($team); // TODO: how do i write this syntax.
Player::create($name, $time)->join($team);
$this->output = 'this user added to ' . $team;
}
/**
* return a user's money amount.
*/
public function get_money()
{
$this->player_name = $this->command[1];
$player = $this->find_or_fail();
$this->output = $player->money;
}
/**
* return a user's health amount.
*/
public function get_health()
{
$this->player_name = $this->command[1];
$player = $this->find_or_fail();
$this->output = $player->health;
}
/**
* shoot a player.
*/
public function tap()
{
$attacker = $this->command[1];
$attacked = $this->command[2];
$gun_type = $this->command[3];
$attacker = $this->find_or_fail($attacker);
$attacked = $this->find_or_fail($attacked);
$attacker->shoot($attacked, $gun_type);
$this->output = 'nice shot';
}
/**
* player buy gun.
*/
public function buy()
{
$this->player_name = $this->command[1];
$gun = $this->command[2];
$time = substr_replace($this->command[3] ,"", -1);
$player = $this->find_or_fail();
if ($player->health === 0)
exception('deads can not buy'); // TODO: exception()
if (strtotime($time) >= strtotime('00:45:00'))
exception('you are out of time');
$player->buy($gun);
$this->output = 'I hope you can use it';
}
/**
* print player's <rank> <name> <kills> <deaths>
*/
public function score_board()
{
game()->board();
}
/**
* set number of this round commands. and do start_round needed functionalities.
*/
public function round()
{
game()->round_commands_left = (int) $this->command[1];
game()->start_round();
}
public function output(): string
{
return $this->output;
}
/**
* return player if find it. otherwise throw 'invalid username' exception.
*/
private function find_or_fail(string $player_name = null)
{
return game()->get_player($player_name ?? $this->player_name) ?: exception('invalid username');
}
}