All pastes #930401 Raw Edit

Someone

public text v1 · immutable
·published 2008-03-06 15:58 UTC
rendered paste body
<pre>
<?php

set_time_limit(0);
ob_implicit_flush();

$dbhost = 'localhost'; 
$dbname = 'gmo'; 
$dbuser = 'root'; 
$dbpass = ''; 

//$connection = mysql_connect($dbhost, $dbuser, $dbpass);
//mysql_select_db($dbname, $connection); 

define("OPC_AUTH", 0);
define("OPC_CHAT", 1);
define("OPC_MOVE", 2);
define("OPC_TURN", 3);
define("OPC_STRAFE", 4);
define("OPC_FIRE", 5);

$ptime = microtime(true);
function delta() {
	global $ptime;
	$r = microtime(true) - $ptime;
	$ptime = microtime(true);
	return ($r/1000000) * 60;
}

class Element
{
    protected $solid = 0;
    protected $moving = 0;
	protected $direction = 0;
	protected $turning = 0;
	protected $strafing = 0;
	protected $x = 0;
	protected $y = 0;
	protected $hspeed = 0;
	protected $vspeed = 0;
    
    function __construct() {
        
    }
    
    protected function move($action) {
    	switch ($moving) {
    		case 0:
    			
    			break;
    		case 1:
    			
    			break;
    		case 2:
    			
    			break;
    	}
    }
    
}
class server
{
	private $socket;
	public $clients = array();
	protected $id_counter;
	
	public function server() 
	{
		$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		echo "IP: " . ($ip = "0.0.0.0") . "\n";
		echo "PORT: " . ($port = 27016) . "\n";
		echo socket_bind($this->socket, $ip, $port) ? "Socket bound to $ip:$port\n" : "ERROR: Could not bind the socket to $ip:$port\n";
		echo socket_listen($this->socket) ? "Listening...\n" : "ERROR: Could not listen...\n";
		
		echo "Entering the main loop\n";
		while (true) {
			$r = array($this->socket);
			$w = NULL;
			$e = NULL;
			if (socket_select($r, $w, $e, 0) > 0) {
				$client = new client(socket_accept($this->socket), &$this);
				if ($client->auth()) {
					$this->clients[] = clone $client;
					echo "### A client was added! ###\n";
				} else {
					$client = false;
					echo "ERROR: A new client failed to auth...\n";
				}
			}
			
			foreach ($this->clients as $client) {
				if (!is_object($client)) { break; }
				if (socket_last_error($client->socket) != 0) {
					socket_close($client->socket);
					echo "{$client->get_name()} disconnected...)";
					$this->client = NULL;
					
				}
				
			}
			
			
			foreach ($this->clients as $client) {
				if (!is_object($client)) { break; }
				$r = @$client->get_input();
				if ($r == true) {
					$client->step();
				}
				
			}
						
			usleep(100);	
		}
		
	}
	
	private function process_clients_pending() {
		
	}
	
} $server = new server();



class client extends Element
{
	var $socket;
	var $buffer_in;
	var $buffer_out;
	var $added;
	var $authenticated;
	protected $name;
	var $server;
	protected $pid;
	
	public function client($socket, $server)
	{
		$this->server = $server;
		$this->added = time();
		$this->authenticated = false;
		$this->socket = $socket;
		$this->name = "Some Guest";
	}
	
	public function auth($info = '')
	{
		if ($info == '') {
		    echo "New client... sending auth request...\n";
		    $this->addMessage(OPC_AUTH, "whoyou?");
			$this->authenticated = false;
		} else if ($this->authenticated == false) {
			$a = explode('#', $info);
			if (in_array($info, array("TyR#passwordlol", "Bozebo#w00t"))) {
				$this->authenticated;
				$this->name = $a[0];
				$this->authenticated = true;
				echo "{$this->name} Logged in!\n";
			} else {
				echo "{$this->name} Failed to authenticate\n";
			}
		}
		
		return true;
	}
	
	public function get_input()
	{
		$r = array($this->socket);
		$w = NULL;
		$e = NULL;
		if (socket_select($r, $w, $e, 0) > 0) {
			if ($r = ($this->buffer_in .= socket_read($this->socket, 1024))) {
				return $r;			
			}
		} else {
			return $r;
		}
	}
	
	public function get_name() {
		return $this->name;
	}
	
	public function step()
	{
		
	    while (strlen($this->buffer_in) > 0) {
	        $len = converter::read_ushort($this->buffer_in);
	        $opc = converter::read_ushort($this->buffer_in, 2);
	        $msg = "" . substr($this->buffer_in, 4, $len);
	        $this->buffer_in = substr($this->buffer_in, $len+4);        
	        //echo "[$len][" . OPCodes::getName($opc) . "][$msg]\n";
	        $this->handleInput($opc, $msg);
	    }
	    
	    while (strlen($this->buffer_out) > 0) {
	        socket_write($this->socket, $this->buffer_out);
	        $this->buffer_out = '';
	    }	
	}
	
	public function addMessage($OpCode, $message)
	{
		$this->buffer_out .= converter::write_ushort(strlen($message)) . converter::write_ushort($OpCode) . $message;
	}
	
	private function handleInput($OpCode, $message)
	{
	    switch ($OpCode) {
	        case OPC_AUTH:
	            $this->auth($message);
	            break;
	        case OPC_CHAT:
	            //echo "{$this->name}: Chat: $message\n";
	            $this->chatSend($message);	            
	            break;
	        case OPC_MOVE:
	            echo "{$this->name}: Moving\n";
	            break;
	        case OPC_TURN:
	            echo "{$this->name}: Turning\n";
	            break;
	        case OPC_STRAFE:
	            echo "{$this->name}: Strafing\n";
	            break;
	        case OPC_FIRE:
	            echo "Booom!\n";
	            $this->addMessage(1, "Pang pang! woot!");
	            $this->addMessage(2, "Fagballs");
	            $this->addMessage(1, "Tredje");
	            break;
	    }
	}
	
	private function chatSend($message)
	{
		foreach ($this->server->clients as $c) {
			if (!is_object($c)) { break; }
			$c->addMessage(OPC_CHAT, $message);
		}
	}
		
	
	
}

class converter
{
	public function read_ushort($data, $offset = 0)
	{
		$b1 = ord(substr($data, $offset, 1));
		$b2 = ord(substr($data, $offset+1, 1));
		return ($b2*256) + $b1; 
	}
	public function write_ushort($int)
	{
		$n = decbin($int);
		$n = substr("0000000000000000", strlen("$n")) . $n;	
		return chr(bindec(substr($n, 8))).chr(bindec(substr($n, 0, 8)));
	}
}


class OPCodes
{
	public function getName($id)
	{
		$opcodes = array(
			0 => "Auth",
			1 => "Chat",
			2 => "Move", //0 = stop 1 = fw, 2 = bw
			3 => "Turn", //0 = stop 1= l 2 = r
			4 => "Strafe", //0 =stop 1 = l 2 = r
			5 => "Fire" // 0 = stop 1 = Start
		);
		return $opcodes[sprintf("%u", $id)];
	}
}

class Map
{
    protected $gridSize;
    protected $layers = array();
    function __construct()
    {
        $this->gridSize = 32;
        $this->layers[0] = array();
    }
    
    public function setGridSize($size)
    {
        $this->gridSize = $size;
    }
    
    public function save()
    {
        $result = '';
    }
    
    public function addSolid($layer)
    {
        $this->layers[$layer][] = new ElementSolid();
    }
}



class ElementSolid extends Element
{
    function __construct() {
        $this->solid = true;        
    }
}
class ElementBullet extends Element
{
	function __construct() {
		
	}
}
?>
</pre>