5 private $config_file = '/etc/mud.ini';
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();
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 if ($args) call_user_func_array(array($statement, 'bind_param'), $args);
27 $statement->execute();
28 $statement->store_result();
29 $statement->store_result();
30 $data = $statement->result_metadata();
31 $return = $row = $fields = array();
32 $fields[0] = &$statement;
33 while($field = $data->fetch_field())
34 $fields[] = &$row[$field->name
];
35 call_user_func_array("mysqli_stmt_bind_result", $fields);
37 while ($statement->fetch()) {
38 foreach ($row as $key=>$value) $return[$i][$key] = $value;
41 $statement->free_result();
45 public function insert() {
46 $args = func_get_args();
47 $statement = $this->db
->prepare(array_shift($args));
48 call_user_func_array(array($statement, 'bind_param'), $args);
49 $statement->execute();
50 return $this->db
->insert_id
;
53 public function update() {
54 $args = func_get_args();
55 $statement = $this->db
->prepare(array_shift($args));
56 call_user_func_array(array($statement, 'bind_param'), $args);
57 $statement->execute();
58 return $this->db
->insert_id
;
66 public $rooms = array();
68 public function __construct($db) {
69 $rooms = $db->query('SELECT id,x,y,state FROM rooms');
71 foreach ($rooms as $room) {
72 $this->rooms
[$room['x']][$room['y']] =
73 array('id' => $room['id'], 'state' => $room['state']);
76 $sql = 'INSERT INTO rooms (x,y,state) VALUES(?,?,?)';
78 for ($x = 0; $x < $this->columns
; $x++
) {
80 for ($y = 0; $y < $this->rows
; $y++
) {
81 $state = (rand(0, 9) < 2 ?
true : false);
82 $id = $db->insert($sql, $types, $x, $y, $state);
83 $this->rooms
[$x][$y] =
84 array('id' => $id, 'state' => $state);
90 public function serialize() {
92 for ($x = 0; $x < $this->columns
; $x++
) {
93 for ($y = 0; $y < $this->rows
; $y++
) {
94 $serial[$x][$y] = $this->rooms
[$x][$y]['state'];
110 public function __construct($db) {
111 $player = $db->query(
112 'SELECT players.id, players.name, players.room, rooms.x, rooms.y'
113 . ' FROM players LEFT JOIN rooms ON players.room = rooms.id'
114 . ' WHERE players.id = ?',
117 $player = $player[0];
118 $this->id
= $player['id'];
119 $this->name
= $player['name'];
120 $this->room
= $player['room'];
121 $this->x
= $player['x'];
122 $this->y
= $player['y'];
127 class mud
extends model
{
131 public function __construct() {
132 parent
::__construct();
134 $this->universe
= new universe($this);
137 private function join() {
140 $x = rand(0, count($this->universe
->rooms
)-1);
141 $y = rand(0, count($this->universe
->rooms
[0])-1);
142 } while ($this->universe
->rooms
[$x][$y]['state']);
143 $id = $this->insert('INSERT INTO players (name,room) VALUES(?,?)',
144 'si', $_GET['name'], $this->universe
->rooms
[$x][$y]['id']);
145 $_SESSION['id'] = $id;
146 //$others = $this->query('SELECT id, room FROM players WHERE id != ?', 'i', $this->player->id);
147 return array('x' => $x, 'y' => $y, 'id' => $id, 'poll' => $this->poll());
150 private function yell($msg) {
151 if (!$msg) $this->error('400', 'expected `msg` field');
153 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
154 'ssii', $msg, 'yell', $this->player
->room
, $this->player
->id
);
157 private function say($msg) {
158 if (!$msg) $this->error('400', 'expected `dest` field');
160 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
161 'ssii', $msg, 'say', $this->player
->room
, $this->player
->id
);
164 private function tell($dest, $msg) {
165 if (!$dest ||
!$msg) $this->error('400', 'expected `dest` and `msg` fields');
166 $player = $this->query('SELECT id FROM players where name = ?', 's', $dest);
167 if (!($player && $player['id']))
168 $this->error(400, "could not find a player with `name` == $dest");
170 'INSERT INTO messages (message,type,destination,source) VALUES(?,?,?,?)',
171 'ssii', $msg, 'say', $player['id'], $this->player
->id
);
174 private function move($direction) {
176 'north' => array('x' => $this->player
->x
, 'y' => $this->player
->y
-1),
177 'east' => array('x' => $this->player
->x+
1, 'y' => $this->player
->y
),
178 'south' => array('x' => $this->player
->x
, 'y' => $this->player
->y+
1),
179 'west' => array('x' => $this->player
->x
-1, 'y' => $this->player
->y
),
181 if ($directions[$direction]) {
182 $rooms = $this->query(
183 'SELECT id, state, description FROM rooms WHERE x = ? and y = ?',
184 'ii', $directions[$direction]['x'], $directions[$direction]['y']);
185 if ($rooms[0] && !$rooms[0]['state']) {
186 $this->update('UPDATE players SET room = ? WHERE players.id = ?',
187 'si', $rooms[0]['id'], $this->player
->id
);
188 return array('description' => $rooms[0]['description']);
190 $this->error(403, 'your path is blocked');
193 $this->error(400, 'expected `direction` field with value {north|east|south|west}');
196 private function poll() {
197 $time = isset($_SESSION['last_polled']) ?
$_SESSION['last_polled'] : 0;
198 $messages = $this->query(
199 'SELECT message, type, name, sent, players.id FROM messages LEFT JOIN players'
200 . ' ON destination = players.id'
201 . ' OR source = players.id'
202 . ' WHERE TIMESTAMPDIFF(MINUTE, sent, NOW()) < 5'
203 . ' AND UNIX_TIMESTAMP(sent) >= ?'
204 . ' AND (type = "yell"'
205 . ' OR (type = "tell" AND destination = ?)'
206 . ' OR (type = "say" AND messages.room = ?))',
207 'iii', $time, $this->player
->id
, $this->player
->room
);
208 $players = $this->query(
209 'SELECT players.id, name, x ,y '
210 . 'FROM players JOIN rooms '
211 . 'ON players.room = rooms.id');
212 $_SESSION['last_polled'] = time();
213 return array('messages' => $messages, 'players' => $players);
216 public function response($content) {
217 header('Content-Type: application/json');
218 echo json_encode($content);
221 public function command($cmd) {
223 $this->error(400, 'Missing command: expected `cmd` field');
224 if (($_GET['cmd'] != 'join' && $_GET['cmd'] != 'kick')
225 && $_GET['cmd'] != 'start' && !isset($_SESSION['id']))
226 $this->error(401, 'Missing user ID: please join first');
227 if (isset($_SESSION['id'])) $this->player
= new player($this);
230 $this->response($this->universe
->serialize());
233 $this->response($this->join());
236 $this->response($this->move($_GET['direction']));
239 $this->tell($_GET['msg']);
242 $this->yell($_GET['msg']);
245 $this->say($_GET['msg']);
248 $this->tell($_GET['dest'], $_GET['msg']);
251 $this->response($this->poll());
254 $this->update('DELETE FROM players');
257 $this->error(400, 'unknown command');
261 private function error($status, $msg) {
262 header("HTTP/1.0 $status");
270 $mud->command($_GET['cmd']);