<?php
 
// Überprüfung, dass ein User nicht mehrere Charerstellungsthreads gleichzeitig laufen hat
if(isset($_SESSION['dirtychar']) && $_SESSION['dirtychar'] === true) {
	for($sleeper=0;$sleeper<40;$sleeper++) {
		usleep(2000000);
		if($_SESSION['dirtychar'] != true) {
			break;
		}
	}
	exit();
	// später laden wir ein Fehlerbild
}
ignore_user_abort(false);
require_once('./core.php');
if( !isset( $user )) $_REQUEST['action'] = 'not_loggedin';

// Skript zur Darstellung eines Charakters

switch($_REQUEST['action']) {
	case 'admin_view_char':
		require_once('lib/char.cls.php');
	break;
	case 'view_char':
		require_once('lib/char.cls.php');
		
		if(isset($_SESSION['chargen']['char'])) {
			
			$char = unserialize($_SESSION['chargen']['char']);
			
		} else {
			// springe in Default
			continue;
		}
		
		if(is_a($char, 'Char') === false) {
			// kein gültiger Charakter gespeichert => Sprung nach default
			continue;
		}
		
		$equipped_items = $char->get('equipped_items');
		switch((int)$char->get('gumpid_body')) {
			case 0x24A: $index = 0xC5A9;
			break;
			case 0x24B: $index = 0xC5AA;
			break;
			case 0x24C: $index = 0xC5F6;
			break;
			case 0x24D: $index = 0xC5F7;
			break;
			case 0x24E: $index = 0xC56D;
			break;
			case 0x24F: $index = 0xC56D;
			break;
			case 0x250: $index = 0xC573;
			break;
			case 0x251: $index = 0xC573;
			break;
			case 400: $index = 12;
			break;
			case 401: $index = 13;
			break;
			case 0x252: $index = 12;
			break;
			case 0x253: $index = 13;			
			break;			
			case 0x254: $index = 12;
			break;
			case 0x255: $index = 13;
			break;		
			case 0x256: $index = 12;
			break;
			case 0x257: $index = 13;
			break;
			default:
				// TODO: Logging
			break;
		}
		$hue = $char->get('hautfarbe');
		$isgump = '1';
		
		if($char->get('geschlecht') == 'm') {
			$female = '0';
			$femaletyp = '0';
		} else {
			$female = '1';
			$femaletyp = '1';
		}
		$layers =  array(22, 23, 4, 3, 2, 21, 13, 9, 10, 15, 16, 17, 7, 11, 12, 19, 8, 20, 6, 1, 24, 17, 14, 5);
		//$layers =  array(5, 14, 17, 24, 1, 6, 20, 8, 19, 12, 11, 7, 17, 16, 15, 10, 9, 13, 21, 2, 3, 4, 23, 22);
		for($i=0;$i<=23;$i++) {
			if(isset($equipped_items[$layers[$i]]) && $equipped_items[$layers[$i]] != null) {
				//if(isset($item['itemid'])===false || isset($item['hexfarbe'])===false) {
				if(isset($equipped_items[$layers[$i]]['itemid'])===false) {
					//	logError("Wichtige Daten nicht gesetzt!");
					$dump.= sprintf("Wichtige Daten: %s", Print_R($GLOBALS, true));
					$sql = sprintf("INSERT INTO logdb.me_errors SET typ = 69, accountID = %d, zeit = NOW(), meldung = '%s', service = 'homepage'", $user->ID, me_escape($dump));
					$MySQL->query($sql);
					continue;
				}
				if(isset($equipped_items[$layers[$i]]['hexfarbe'])===false) {
					$hue.=",0";
				} else {
					$hue.=",".$equipped_items[$layers[$i]]['hexfarbe'];
				}
				$index.=",".$equipped_items[$layers[$i]]['itemid'];
				
				if($equipped_items[$layers[$i]]['isgump'] === true) {
					$isgump.=',1';
				} else {
					$isgump.=',0';
				}
				$female.=','.$femaletyp;	
			}
			
		}
		$indexA = $index;
		$femaleA = $female;
		$hueA = $hue;
		$isgumpA = $isgump;
		$name = $char->get('name');
		require_once('./lib/gumpreader.inc.php');
	die();
	break;
	case 'staff': 
		$char = $MySQL->query_first(sprintf("SELECT * FROM server.chars WHERE charid = %d", (int)$_REQUEST['charid']));
		if($user->can_access('activation') || $user->can_access('char_administration') || $char['accountid'] = $user->ID) {
				require_once('lib/char.cls.php');
	            try {
					$char = new Char((int)$_REQUEST['charid'], (int)$char['accountid'], false, (bool)$char['testserver'], true);            	
	            } catch(IException $e) {
					// TODO
					error($e->getNotice(), true);
					die();
				}
				// TODO: better
				$equipped_items = $char->get('equipped_items');
		switch((int)$char->get('gumpid_body')) {
			case 0x24A: $index = 0xC5A9;
			break;
			case 0x24B: $index = 0xC5AA;
			break;
			case 0x24C: $index = 0xC5F6;
			break;
			case 0x24D: $index = 0xC5F7;
			break;
			case 0x24E: $index = 0xC56D;
			break;
			case 0x24F: $index = 0xC56D;
			break;
			case 0x250: $index = 0xC573;
			break;
			case 0x251: $index = 0xC573;
			break;
			case 400: $index = 12;
			break;
			case 401: $index = 13;
			break;
			case 0x252: $index = 12;
			break;
			case 0x253: $index = 13;			
			break;			
			case 0x254: $index = 12;
			break;
			case 0x255: $index = 13;
			break;		
			case 0x256: $index = 12;
			break;
			case 0x257: $index = 13;
			break;
			default:
				// TODO: Logging
			break;
		}
		$hue = $char->get('hautfarbe');
		$isgump = '1';
		
		if($char->get('geschlecht') == 'm') {
			$female = '0';
			$femaletyp = '0';
		} else {
			$female = '1';
			$femaletyp = '1';
		}
		//$layers =  array(5, 14, 17, 24, 1, 6, 20, 8, 19, 12, 11, 7, 17, 16, 15, 10, 9, 13, 21, 2, 3, 4, 23, 22);
		$layers =  array( 22, 23, 04, 03, 02, 21, 13, 09, 10, 15, 16, 17, 07, 11, 12, 19, 08, 20, 06, 01, 24, 17, 14, 05 );

		
		for($i=0;$i<=23;$i++) {
			if(isset($equipped_items[$layers[$i]]) && $equipped_items[$layers[$i]] != null) {
				//if(isset($item['itemid'])===false || isset($item['hexfarbe'])===false) {
				if(isset($equipped_items[$layers[$i]]['itemid'])===false) {
					//	logError("Wichtige Daten nicht gesetzt!");
					$dump.= sprintf("Wichtige Daten: %s", Print_R($GLOBALS, true));
					$sql = sprintf("INSERT INTO logdb.me_errors SET typ = 69, accountID = %d, zeit = NOW(), meldung = '%s', service = 'homepage'", $user->ID, me_escape($dump));
					$MySQL->query($sql);
					continue;
				}
				if(isset($equipped_items[$layers[$i]]['hexfarbe'])===false) {
					$hue.=",0";
				} else {
					$hue.=",".$equipped_items[$layers[$i]]['hexfarbe'];
				}
				$index.=",".$equipped_items[$layers[$i]]['itemid'];
				
				if($equipped_items[$layers[$i]]['isgump'] === true) {
					$isgump.=',1';
				} else {
					$isgump.=',0';
				}
				$female.=','.$femaletyp;	
			}
			
		}
		$indexA = $index;
		$femaleA = $female;
		$hueA = $hue;
		$isgumpA = $isgump;
		$name = $char->get('name');
		require_once('./lib/gumpreader.inc.php');
		}
	case 'not_loggedin':
		// ebenfalls Fehlerbild: nicht berechtigt oder nicht eingeloggt
	break;
	default:
		// gebe ein Fehlerbild aus
		// TODO
	break;
}
ignore_user_abort(false);