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
;
57 public $rooms = array();
59 public function __construct($db) {
60 $rooms = $db->query("SELECT id,x,y,state FROM rooms");
62 foreach ($rooms as $room) {
63 $this->rooms
[$room['x']][$room['y']] =
64 array('id' => $room['id'], 'state' => $room['state']);
67 $sql = 'INSERT INTO rooms (x,y,state) VALUES(?,?,?)';
69 for ($x = 0; $x < $this->columns
; $x++
) {
71 for ($y = 0; $y < $this->rows
; $y++
) {
72 $state = (rand(0, 9) < 2 ?
true : false);
73 $id = $db->insert($sql, $types, $x, $y, $state);
74 $this->rooms
[$x][$y] =
75 array('id' => $id, 'state' => $state);
81 public function serialize() {
83 for ($x = 0; $x < $this->columns
; $x++
) {
84 for ($y = 0; $y < $this->rows
; $y++
) {
85 $serial[$x][$y] = $this->rooms
[$x][$y]['state'];
101 public function __construct($db) {
102 $player = $db->query(
103 'SELECT players.id, players.name, players.room, rooms.x, rooms.y'
104 . ' FROM players LEFT JOIN rooms ON players.room = rooms.id'
105 . ' WHERE players.id = ?',
108 $player = $player[0];
109 $this->id
= $player['id'];
110 $this->name
= $player['name'];
111 $this->room
= $player['room'];
112 $this->x
= $player['x'];
113 $this->y
= $player['y'];
118 class mud
extends model
{
122 public function __construct() {
123 parent
::__construct();
125 $this->universe
= new universe($this);
128 private function join() {
131 if (isset($_SESSION['id'])) {
132 $x = $this->player
->x
;
133 $y = $this->player
->y
;
134 $id = $this->player
->id
;
137 $x = rand(0, count($this->universe
->rooms
)-1);
138 $y = rand(0, count($this->universe
->rooms
[0])-1);
139 } while ($this->universe
->rooms
[$x][$y]['state']);
140 $id = $this->insert('INSERT INTO players (name,room) VALUES(?,?)',
141 'si', 'majuscule', $this->universe
->rooms
[$x][$y]['id']);
142 $_SESSION['id'] = $id;
144 return array('x' => $x, 'y' => $y, 'id' => $id);
147 private function yell($msg) {
149 'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
150 'ssii', $msg, 'yell', $this->player
->room
, $this->player
->id
);
153 private function poll() {
154 $time = isset($_SESSION['last_polled']) ?
$_SESSION['last_polled'] : 0;
155 $messages = $this->query(
156 'SELECT message, type, name, sent, players.id FROM messages LEFT JOIN players'
157 . ' ON destination = players.id'
158 . ' OR source = players.id'
159 . ' WHERE TIMESTAMPDIFF(MINUTE, sent, NOW()) < 5'
160 . ' AND UNIX_TIMESTAMP(sent) >= ?'
161 . ' AND (type = "yell"'
162 . ' OR (type = "tell" AND destination = ?)'
163 . ' OR (type = "say" AND messages.room = ?))',
164 'iii', $time, $this->player
->id
, $this->player
->room
);
165 $_SESSION['last_polled'] = time();
169 public function response($content) {
170 header('Content-Type: application/json');
171 echo json_encode($content);
174 public function command($cmd) {
176 $this->error(400, 'Missing command: expected `cmd` field');
177 if ($_GET['cmd'] != 'join' && $_GET['cmd'] != 'start' && !isset($_SESSION['id']))
178 $this->error(401, 'Missing user ID: please join first');
179 if (isset($_SESSION['id'])) $this->player
= new player($this);
182 $this->response($this->universe
->serialize());
185 $this->response($this->join());
188 $this->response(array('valid' => $this->move()));
194 $this->yell($_GET['msg']);
203 echo json_encode($this->poll());
206 $this->error(400, 'Unknown command');
210 private function error($status, $msg) {
211 header("HTTP/1.0 $status");
218 $mud->command($_GET['cmd']);