implemented movement updates & room descriptions
[mudd.git] / mud.php
1 <?php
2
3 abstract class model {
4
5 private $config_file = '/etc/mud.ini';
6 private $db;
7
8 public function __construct() {
9 $config = parse_ini_file($this->config_file, true);
10 $this->db = new mysqli(
11 $config['database']['domain'],
12 $config['database']['user'],
13 $config['database']['password'],
14 $config['database']['database']);
15 if (mysqli_connect_errno()) {
16 echo "Problem connecting to database: ";
17 echo mysqli_connect_error();
18 exit();
19 }
20 }
21
22 public function query() {
23 $args = func_get_args();
24 $statement = $this->db->prepare(array_shift($args));
25 if ($args) call_user_func_array(array($statement, 'bind_param'), &$args);
26 $statement->execute();
27 $statement->store_result();
28 $statement->store_result();
29 $data = $statement->result_metadata();
30 $return = $row = $fields = array();
31 $fields[0] = &$statement;
32 while($field = $data->fetch_field())
33 $fields[] = &$row[$field->name];
34 call_user_func_array("mysqli_stmt_bind_result", $fields);
35 $i = 0;
36 while ($statement->fetch()) {
37 foreach ($row as $key=>$value) $return[$i][$key] = $value;
38 $i++;
39 }
40 $statement->free_result();
41 return $return;
42 }
43
44 public function insert() {
45 $args = func_get_args();
46 $statement = $this->db->prepare(array_shift($args));
47 call_user_func_array(array($statement, 'bind_param'), &$args);
48 $statement->execute();
49 return $this->db->insert_id;
50 }
51
52 public function update() {
53 $args = func_get_args();
54 $statement = $this->db->prepare(array_shift($args));
55 call_user_func_array(array($statement, 'bind_param'), &$args);
56 $statement->execute();
57 return $this->db->insert_id;
58 }
59
60 }
61
62 class universe {
63 public $rows = 30;
64 public $columns = 30;
65 public $rooms = array();
66
67 public function __construct($db) {
68 $rooms = $db->query('SELECT id,x,y,state FROM rooms');
69 if ($rooms) {
70 foreach ($rooms as $room) {
71 $this->rooms[$room['x']][$room['y']] =
72 array('id' => $room['id'], 'state' => $room['state']);
73 }
74 } else {
75 $sql = 'INSERT INTO rooms (x,y,state) VALUES(?,?,?)';
76 $types = 'iii';
77 for ($x = 0; $x < $this->columns; $x++) {
78 $column = array();
79 for ($y = 0; $y < $this->rows; $y++) {
80 $state = (rand(0, 9) < 2 ? true : false);
81 $id = $db->insert($sql, $types, $x, $y, $state);
82 $this->rooms[$x][$y] =
83 array('id' => $id, 'state' => $state);
84 }
85 }
86 }
87 }
88
89 public function serialize() {
90 $serial = array();
91 for ($x = 0; $x < $this->columns; $x++) {
92 for ($y = 0; $y < $this->rows; $y++) {
93 $serial[$x][$y] = $this->rooms[$x][$y]['state'];
94 }
95 }
96 return $serial;
97 }
98
99 }
100
101 class player {
102
103 public $id;
104 public $name;
105 public $room;
106 public $x;
107 public $y;
108
109 public function __construct($db) {
110 $player = $db->query(
111 'SELECT players.id, players.name, players.room, rooms.x, rooms.y'
112 . ' FROM players LEFT JOIN rooms ON players.room = rooms.id'
113 . ' WHERE players.id = ?',
114 'i', $_SESSION['id']
115 );
116 $player = $player[0];
117 $this->id = $player['id'];
118 $this->name = $player['name'];
119 $this->room = $player['room'];
120 $this->x = $player['x'];
121 $this->y = $player['y'];
122 }
123
124 }
125
126 class mud extends model {
127
128 private $universe;
129
130 public function __construct() {
131 parent::__construct();
132 session_start();
133 $this->universe = new universe($this);
134 }
135
136 private function join() {
137 $x = $y = 0;
138 do {
139 $x = rand(0, count($this->universe->rooms)-1);
140 $y = rand(0, count($this->universe->rooms[0])-1);
141 } while ($this->universe->rooms[$x][$y]['state']);
142 $id = $this->insert('INSERT INTO players (name,room) VALUES(?,?)',
143 'si', $_GET['name'], $this->universe->rooms[$x][$y]['id']);
144 $_SESSION['id'] = $id;
145 //$others = $this->query('SELECT id, room FROM players WHERE id != ?', 'i', $this->player->id);
146 return array('x' => $x, 'y' => $y, 'id' => $id, 'poll' => $this->poll());
147 }
148
149 private function yell($msg) {
150 if (!$msg) {
151 $this->error('400', 'expected `msg` field');
152 return;
153 }
154 $this->insert(
155 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
156 'ssii', $msg, 'yell', $this->player->room, $this->player->id);
157 }
158
159 private function say($msg) {
160 if (!$msg) {
161 $this->error('400', 'expected `dest` field');
162 return;
163 }
164 $this->insert(
165 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
166 'ssii', $msg, 'say', $this->player->room, $this->player->id);
167 }
168
169 private function tell($dest, $msg) {
170 if (!$dest || !$msg) {
171 $this->error('400', 'expected `dest` and `msg` fields');
172 return;
173 }
174 $player = $this->query('SELECT id FROM players where name = ?', 's', $dest);
175 if (!($player && $player['id'])) {
176 $this->error('400', "could not find a player with `name` == $dest");
177 return;
178 }
179 $this->insert(
180 'INSERT INTO messages (message,type,destination,source) VALUES(?,?,?,?)',
181 'ssii', $msg, 'say', $player['id'], $this->player->id);
182 }
183
184 private function move($direction) {
185 $directions = array(
186 'north' => array('x' => $this->player->x, 'y' => $this->player->y-1),
187 'east' => array('x' => $this->player->x+1, 'y' => $this->player->y),
188 'south' => array('x' => $this->player->x, 'y' => $this->player->y+1),
189 'west' => array('x' => $this->player->x-1, 'y' => $this->player->y),
190 );
191 if ($directions[$direction]) {
192 $rooms = $this->query(
193 'SELECT id, state, description FROM rooms WHERE x = ? and y = ?',
194 'ii', $directions[$direction]['x'], $directions[$direction]['y']);
195 if ($rooms[0] && !$rooms[0]['state']) {
196 $this->update('UPDATE players SET room = ? WHERE players.id = ?',
197 'si', $rooms[0]['id'], $this->player->id);
198 return array('description' => $rooms[0]['description']);
199 } else {
200 $this->error(403, 'your path is blocked');
201 }
202 } else
203 $this->error(400, 'expected `direction` field with value {north|east|south|west}');
204 }
205
206 private function poll() {
207 $time = isset($_SESSION['last_polled']) ? $_SESSION['last_polled'] : 0;
208 $messages = $this->query(
209 'SELECT message, type, name, sent, players.id FROM messages LEFT JOIN players'
210 . ' ON destination = players.id'
211 . ' OR source = players.id'
212 . ' WHERE TIMESTAMPDIFF(MINUTE, sent, NOW()) < 5'
213 . ' AND UNIX_TIMESTAMP(sent) >= ?'
214 . ' AND (type = "yell"'
215 . ' OR (type = "tell" AND destination = ?)'
216 . ' OR (type = "say" AND messages.room = ?))',
217 'iii', $time, $this->player->id, $this->player->room);
218 $players = $this->query(
219 'SELECT players.id, name, x ,y '
220 . 'FROM players JOIN rooms '
221 . 'ON players.room = rooms.id');
222 $_SESSION['last_polled'] = time();
223 return array('messages' => $messages, 'players' => $players);
224 }
225
226 public function response($content) {
227 header('Content-Type: application/json');
228 echo json_encode($content);
229 }
230
231 public function command($cmd) {
232 if (!$_GET['cmd'])
233 $this->error(400, 'Missing command: expected `cmd` field');
234 if ($_GET['cmd'] != 'join' && $_GET['cmd'] != 'start' && !isset($_SESSION['id']))
235 $this->error(401, 'Missing user ID: please join first');
236 if (isset($_SESSION['id'])) $this->player = new player($this);
237 switch ($cmd) {
238 case 'start':
239 $this->response($this->universe->serialize());
240 break;
241 case 'join':
242 $this->response($this->join());
243 break;
244 case 'move':
245 $this->response($this->move($_GET['direction']));
246 break;
247 case 'tell':
248 $this->tell($_GET['msg']);
249 break;
250 case 'yell':
251 $this->yell($_GET['msg']);
252 break;
253 case 'say':
254 $this->say($_GET['dest'], $_GET['msg']);
255 break;
256 case 'tell':
257 $this->tell();
258 break;
259 case 'poll':
260 echo json_encode($this->poll());
261 break;
262 default:
263 $this->error(400, 'Unknown command');
264 }
265 }
266
267 private function error($status, $msg) {
268 header("HTTP/1.0 $status");
269 echo $msg;
270 }
271
272 }
273
274 $mud = new mud();
275 $mud->command($_GET['cmd']);
276
277 ?>