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 $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);
36 while ($statement->fetch()) {
37 foreach ($row as $key=>$value) $return[$i][$key] = $value;
40 $statement->free_result();
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
;
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
;
65 public $rooms = array();
67 public function __construct($db) {
68 $rooms = $db->query('SELECT id,x,y,state FROM rooms');
70 foreach ($rooms as $room) {
71 $this->rooms
[$room['x']][$room['y']] =
72 array('id' => $room['id'], 'state' => $room['state']);
75 $sql = 'INSERT INTO rooms (x,y,state) VALUES(?,?,?)';
77 for ($x = 0; $x < $this->columns
; $x++
) {
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);
89 public function serialize() {
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'];
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 = ?',
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'];
126 class mud
extends model
{
130 public function __construct() {
131 parent
::__construct();
133 $this->universe
= new universe($this);
136 private function join() {
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());
149 private function yell($msg) {
150 if (!$msg) $this->error('400', 'expected `msg` field');
152 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
153 'ssii', $msg, 'yell', $this->player
->room
, $this->player
->id
);
156 private function say($msg) {
157 if (!$msg) $this->error('400', 'expected `dest` field');
159 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
160 'ssii', $msg, 'say', $this->player
->room
, $this->player
->id
);
163 private function tell($dest, $msg) {
164 if (!$dest ||
!$msg) $this->error('400', 'expected `dest` and `msg` fields');
165 $player = $this->query('SELECT id FROM players where name = ?', 's', $dest);
166 if (!($player && $player['id']))
167 $this->error('400', "could not find a player with `name` == $dest");
169 'INSERT INTO messages (message,type,destination,source) VALUES(?,?,?,?)',
170 'ssii', $msg, 'say', $player['id'], $this->player
->id
);
173 private function move($direction) {
175 'north' => array('x' => $this->player
->x
, 'y' => $this->player
->y
-1),
176 'east' => array('x' => $this->player
->x+
1, 'y' => $this->player
->y
),
177 'south' => array('x' => $this->player
->x
, 'y' => $this->player
->y+
1),
178 'west' => array('x' => $this->player
->x
-1, 'y' => $this->player
->y
),
180 if ($directions[$direction]) {
181 $rooms = $this->query(
182 'SELECT id, state, description FROM rooms WHERE x = ? and y = ?',
183 'ii', $directions[$direction]['x'], $directions[$direction]['y']);
184 if ($rooms[0] && !$rooms[0]['state']) {
185 $this->update('UPDATE players SET room = ? WHERE players.id = ?',
186 'si', $rooms[0]['id'], $this->player
->id
);
187 return array('description' => $rooms[0]['description']);
189 $this->error(403, 'your path is blocked');
192 $this->error(400, 'expected `direction` field with value {north|east|south|west}');
195 private function poll() {
196 $time = isset($_SESSION['last_polled']) ?
$_SESSION['last_polled'] : 0;
197 $messages = $this->query(
198 'SELECT message, type, name, sent, players.id FROM messages LEFT JOIN players'
199 . ' ON destination = players.id'
200 . ' OR source = players.id'
201 . ' WHERE TIMESTAMPDIFF(MINUTE, sent, NOW()) < 5'
202 . ' AND UNIX_TIMESTAMP(sent) >= ?'
203 . ' AND (type = "yell"'
204 . ' OR (type = "tell" AND destination = ?)'
205 . ' OR (type = "say" AND messages.room = ?))',
206 'iii', $time, $this->player
->id
, $this->player
->room
);
207 $players = $this->query(
208 'SELECT players.id, name, x ,y '
209 . 'FROM players JOIN rooms '
210 . 'ON players.room = rooms.id');
211 $_SESSION['last_polled'] = time();
212 return array('messages' => $messages, 'players' => $players);
215 public function response($content) {
216 header('Content-Type: application/json');
217 echo json_encode($content);
220 public function command($cmd) {
222 $this->error(400, 'Missing command: expected `cmd` field');
223 if ($_GET['cmd'] != 'join' && $_GET['cmd'] != 'start' && !isset($_SESSION['id']))
224 $this->error(401, 'Missing user ID: please join first');
225 if (isset($_SESSION['id'])) $this->player
= new player($this);
228 $this->response($this->universe
->serialize());
231 $this->response($this->join());
234 $this->response($this->move($_GET['direction']));
237 $this->tell($_GET['msg']);
240 $this->yell($_GET['msg']);
243 $this->say($_GET['dest'], $_GET['msg']);
249 echo json_encode($this->poll());
252 $this->error(400, 'Unknown command');
256 private function error($status, $msg) {
257 header("HTTP/1.0 $status");
265 $mud->command($_GET['cmd']);