$thissock, 'coords' => array('x'=>'0','y'=>'0','z'=>'0'), 'hookgo'=>'', 'nick'=>'Newcomer'); $allsocks[] = $thissock; $thissockid = array_search($thissock,$allsocks); $thissock['id'] = $thissockid; $allsocks[$thissockid] = $thissock; initsock($thissock); } readsockets(); } } else { socket_close($mysock); exit; } function readsockets() { global $allsocks; foreach ($allsocks as $s) { readsocket($s); } } function initsock($thissock) { $motd = file('motd.txt'); send($thissock,implode('',$motd)); $sq = getsquare($thissock,'nowhere'); movetosquare($thissock,$sq); } function readsocket($thissock) { global $allsocks; $data = ''; while (1) { socket_set_nonblock($thissock['sock']); $temp = socket_read($thissock['sock'],1024,PHP_NORMAL_READ); if ($temp === false) { socket_close($thissock['sock']); unset($allsocks[$thissock['id']]); break; } $data .= $temp; if (substr($data,-1) == "\n" || substr($data,-1) == "\r") { handle_event($thissock,trim($data)); break; } elseif ($data == '') { break; } } } function handle_event($thissock,$data) { #socket_write($thissock['sock'],$data); global $allsocks; lvlog('RCVD: '.$data); $data = explode(' ',trim($data)); $data[0] == strtolower($data[0]); if ($data[0] == 'walk') { if (!isset($data[1]) || $data[1] == '') { send($thissock,'Aimlessly? That\'s kinda pointless.'); return; } if (($sq = getsquare($thissock,$data[1])) !== false) { movetosquare($thissock,$sq); } else { return; } } elseif ($data[0] == 'nick') { array_shift($data); $thissock['nick'] = ucwords(strtolower(implode(' ',$data))); $allsocks[$thissock['id']] = $thissock; } elseif ($data[0] == 'say') { array_shift($data); sayall($thissock,implode(' ',$data)); } elseif ($data[0] == 'closethesock') { global $mysock; lvlog("Shutting down server..."); socket_shutdown($mysock,2); socket_close($mysock); lvlog("Done."); exit; } } function getsquare($thissock,$direction) { $direction = strtolower($direction); if (array_search($direction,array('east','west','south','north','up','down','nowhere')) === false) { send($thissock,'Sure, you can go '.$direction.', but you\'ll have to eat some of these shrooms first.'); return false; } $newlocation = $thissock['coords']; if ($direction == 'east') { $newlocation['x'] = $newlocation['x'] + 1; } elseif ($direction == 'west') { $newlocation['x'] = $newlocation['x'] - 1; } elseif ($direction == 'south') { $newlocation['y'] = $newlocation['y'] - 1; } elseif ($direction == 'north') { $newlocation['y'] = $newlocation['y'] + 1; } elseif ($direction == 'up') { $newlocation['z'] = $newlocation['z'] + 1; } elseif ($direction == 'down') { $newlocation['z'] = $newlocation['z'] - 1; } $square = mysql2array("select * from squares where xcoord='".$newlocation['x']."' and ycoord='".$newlocation['y']."' and zcoord='".$newlocation['z']."'"); if (isset($square[0])) { return $square[0]; } else { send($thissock,"You can't go that direction."); return false; } } function movetosquare($thissock,$sq) { global $allsocks; sendall($thissock,'You see '.$thissock['nick'].' approaching.'); if ($thissock['hookgo'] != '') { eval($thissock['hookgo'].'($thissock);'); } send($thissock,str_repeat("~\n",10)); send($thissock,$sq['description']); $thissock['coords'] = array('x'=>$sq['xcoord'],'y'=>$sq['ycoord'],'z'=>$sq['zcoord']); $thissock['hookgo'] = $sq['hookgo']; $allsocks[$thissock['id']] = $thissock; if ($sq['hookcome'] != '') { eval($hookcome.'($thissock);'); } send($thissock,'You may walk: '.implode(', ',getdirections($sq))); } function getdirections($sq) { $query = "select * from squares where (xcoord in (".($sq['xcoord'] + 1).",".($sq['xcoord'] - 1).") and ycoord=".$sq['ycoord']." and zcoord=".$sq['zcoord'].") or (xcoord=".$sq['xcoord']." and ycoord in (".($sq['ycoord'] + 1).",".($sq['ycoord'] - 1).") and zcoord=".$sq['zcoord'].") or (xcoord=".$sq['xcoord']." and ycoord=".$sq['ycoord']." and zcoord in (".($sq['zcoord'] + 1).",".($sq['zcoord'] - 1).")) "; $sqs = mysql2array($query); $directions = array(); foreach ($sqs as $s) { if ($s['xcoord'] == ($sq['xcoord'] - 1)) { $directions[] = 'west'; } if ($s['xcoord'] == ($sq['xcoord'] + 1)) { $directions[] = 'east'; } if ($s['ycoord'] == ($sq['ycoord'] - 1)) { $directions[] = 'south'; } if ($s['ycoord'] == ($sq['ycoord'] + 1)) { $directions[] = 'north'; } if ($s['zcoord'] == ($sq['zcoord'] - 1)) { $directions[] = 'down'; } if ($s['zcoord'] == ($sq['zcoord'] + 1)) { $directions[] = 'up'; } } return $directions; } function sayall($thissock,$str) { global $allsocks; sendall($thissock,$thissock['nick'].' says: "'.$str.'"'); } function sendall($thissock,$str) { global $allsocks; foreach ($allsocks as $sid=>$s) { if ($sid == $thissock['id']) { continue; } send($s,$str); } } function lvlog($str) { $logfile = fopen('lvlog.log','a'); fwrite($logfile,'['.date('Y-m-d H:i:s').'] '.$str."\n"); fclose($logfile); } function send($thissock,$str) { socket_write($thissock['sock'],$str."\n"); } function mysql2array($query) { $return = array(); $resource = mysql_query($query); while (($row = mysql_fetch_assoc($resource)) !== false) { $return[] = $row; } return $return; } ?>