<?php
class Game
{
private $start_time; // Game instance start time //
private $last_loop; // Last Execution Loop //
private $timer_registry; // Array of all timed actions pending //
private $action_queue; // Array of all pending actions //
private $connections;
private $characters;
private $rooms;
private $lastPoll;
function __construct()
{
$this->start_time = $this->lastPoll = time();
$this->action_queue = array();
$this->connections = array();
$this->characters = array();
$this->rooms = array();
}
function getConnections()
{
return $this->connections;
}
function addConnection($id, $conn)
{
$this->connections[$id] = $conn;
}
function getConnection($id)
{
if (is_object($this->connections[$id]))
return $this->connections[$id];
else
return false;
}
function removeConnection($id)
{
unset($this->connections[$id]);
}
function ExecuteLoop()
{
try {
// Every 10 seconds, move our NPCs, check for AFKs/disconnects, etc. //
if (time()-$this->lastPoll >= 10)
{
global $rooms;
foreach ($this->getCharacters() as $key => $char)
{
if ($char->getType() == GO_TYPE_NPC)
{
$newRoom = false;
$rand = rand(0,7);
if ($rand == 0) $dir = "nw";
if ($rand == 1) $dir = "n";
if ($rand == 2) $dir = "ne";
if ($rand == 3) $dir = "w";
if ($rand == 4) $dir = "e";
if ($rand == 5) $dir = "sw";
if ($rand == 6) $dir = "s";
if ($rand == 7) $dir = "se";
move($char->getEID(), $dir);
}
if ($char->getType() == GO_TYPE_CHAR )
{
// If no activity in x seconds (5 minutes is the default) //
if (is_object($char->getConnection()) && microtime(true) - $char->getConnection()->getLastTime() > CONN_TIMEOUT)
{
$char->send("You've been kicked for inactivity.", "sysmessage");
$char->setOffline();
$this->removeCharacter($char);
} elseif (!is_object($char->getConnection()))
{
// We've got a character with no connection! AHHHH!! //
// Actually, it just means the browser was closed or the connection was uncleanly
// cut for some other reason. No biggie. //
$char->setOffline();
$this->removeCharacter($char);
} else {
// Send a server status update //
if ($char->getConnection()->getClientType() == GC_WEBSOCKET)
$char->sendDirect(json_encode(array("server_status" => count($this->getConnections()) . " online")));
}
}
}
$this->lastPoll = time();
}
$this->cleanup();
$this->last_loop = microtime(true);
} catch (Exception $e)
{
$exception = "Exception: {$e->getMessage()} in {$e->getFile()} at line "
. "{$e->getLine()} with trace:<br/>{$e->getTraceAsString()}";
echo "Caught exception: " . $e->getMessage() . "\n";
}
}
function addCharacter($c)
{
if (is_object($c))
$this->characters[$c->getEID()] = $c;
}
function removeCharacter($c)
{
unset($this->characters[$c->getEID()]);
}
function getCharacter($id)
{
if (is_object($this->characters[$id]))
return $this->characters[$id];
else
return false;
}
function getCharacters()
{
return $this->characters;
}
function addRoom($r)
{
if (is_object($r))
$this->rooms[$r->getEID()] = $r;
}
function getRoom($id)
{
if (is_object($this->rooms[$id]))
return $this->rooms[$id];
else
return false;
}
function getRooms()
{
return $this->rooms;
}
function GetUptime()
{
return time() - $this->start_time;
}
function isRunning()
{
return true;
}
function cleanup()
{
// Check connections for lack of comm and kill if necessary //
foreach ($this->connections as $key => $conn)
{
if (microtime(true) - $conn->getLastTime() > CONN_TIMEOUT + 60)
{
$this->removeConnection($key);
echo "Killed connection [$key] in cleanup...\n";
}
}
}
function dump()
{
echo "Game Dump:\n===============\n";
echo "Characters:\n";
foreach ($this->characters as $key => $char)
echo $char->getName() . "\n";
echo "\nRooms:\n";
foreach ($this->rooms as $key => $room)
echo $room->getName() . "\n";
}
}
?>