location)?HAUS_DEFAULT_LOC:$quater->location; $id = haus_build::generate_id(); $nr = haus_build::generate_id($quater_id); $params = array($id,$nr,$cost['sgold'],$cost['sgems'],$cost['sturn'],-3,$quater_id); $sqlstr = 'INSERT INTO `'.HAUS_TABELLE.'` (`'.HAUS_ID_FIELD.'`,`'.HAUS_NR_FIELD.'`,`gold`,`gems`,`turn`,`level`,`location`) VALUES(%u,%u,%u,%u,%u,%s,%u)'; db_query(vsprintf($sqlstr,$params)) or stirb('Erzeugen eines neuen Hauses schlug fehl!',null,__FILE__,__LINE__); wvKey::generateKeys($id,1,$user['acctid']); $retid = $id; } return $retid; } function generate_id($location=false) { $loc_str = $location!==false ? 'WHERE `location`='.$location : ''; $loc_str2 = $location!==false ? 'AND `h`.`location`='.$location : ''; $field_str = $location!==false ? HAUS_NR_FIELD : HAUS_ID_FIELD; $sql = 'SELECT IF((SELECT MIN(`'.$field_str.'`) FROM `'.HAUS_TABELLE.'` '.$loc_str.')>1,1,(SELECT IF(ISNULL(MIN(`h`.`'.$field_str.'`)+1),1,MIN(`h`.`'.$field_str.'`)+1) AS `new` FROM `'.HAUS_TABELLE.'` `h` LEFT OUTER JOIN `'.HAUS_TABELLE.'` `n` ON `n`.`'.$field_str.'` = `h`.`'.$field_str.'`+1 WHERE ISNULL(`n`.`'.$field_str.'`) '.$loc_str2.')) AS `neu`'; $res = db_query($sql) or stirb('Generieren eines neuen HausIndexes schlug fehl',null,__FILE__,__LINE__); $neu = db_fetch_assoc($res); db_free_result($res); return $neu['neu']; } function getMissingCosts() { $cost = haus_build::getCosts(); return array( 'gold'=>$cost['gold']-$this->get('gold'), 'gems'=>$cost['gems']-$this->get('gems'), 'turn'=>$cost['turn']-$this->get('turn')); } function canBuild($user) { $cost = haus_build::getCosts(); $own = haus_basic::findByUser($user['acctid'],true,-3); $miss = array(); if($user['gold'] < $cost['sgold']) $miss['gold'] = $cost['sgold'] - $user['gold']; if($user['gems'] < $cost['sgems']) $miss['gems'] = $cost['sgems'] - $user['gems']; if($user['turns']< $cost['sturn']) $miss['turn'] = $cost['sturn'] - $user['turns']; if($user['dragonkills'] < $cost['dk']) $miss['dk'] = $cost['dk'] - $user['dragonkills']; if(count($own) > 0) $miss['building'] = 1; return empty($miss) ? true : $miss; } function getCosts() { return array( 'gold' => getsetting('baukostengold',25000), 'gems' => getsetting('baukostengems',50), 'dk' => getsetting('abwannbauen',3), 'turn' => getsetting('baukostenturn',30), 'sgold' => getsetting('baukostengold_start',10000), 'sgems' => getsetting('baukostengems_start',10), 'sturn' => getsetting('baukostenturn_start',5), ); } function corrigatePrefVal($user,$valname,$prefval,$maxval = false) { if (!$maxval) { $mcost = $this->getMissingCosts(); $maxval = $mcost[$valname]; } if ($maxval > $prefval) $prefval = $maxval; if ($user[($valname=='turn'?'turns':$valname)] < $prefval) $prefval = $user[($valname=='turn'?'turns':$valname)]; return $prefval; } function pay(&$user,$initial = false,$pref=array('gold'=>0,'gems'=>0,'turn'=>0)) { if ($initial) { $cost = haus_build::getCosts(); $pref = array('gold'=>$cost['sgold'],'gems'=>$cost['sgems'],'turn'=>$cost['sturn']); } $gold = haus_build::corrigatePrefVal($user,'gold',$pref['gold'],$pref['gold']);//,$maxvals['gold']); $gems = haus_build::corrigatePrefVal($user,'gems',$pref['gems'],$pref['gems']);//,$maxvals['gems']); $turn = haus_build::corrigatePrefVal($user,'turn',$pref['turn'],$pref['turn']);//,$maxvals['turn']); if (!$initial) $this->spend($gold,$gems,$turn); $user['gold'] -= $gold; $user['gems'] -= $gems; $user['turns'] -= $turn; return array('gold'=>$gold,'gems'=>$gems,'turn'=>$turn); } function spend($gold=0,$gems=0,$turn=0) { $this->set('gold',$this->get('gold')+$gold); $this->set('gems',$this->get('gems')+$gems); $this->set('turn',$this->get('turn')+$turn); } function isFinished() { $miss = $this->getMissingCosts(); return ($this->get('level') >= 0) || ($miss['gold'] <= 0 && $miss['gems'] <= 0 && $miss['turn'] <= 0); } function getProgressText($user) { global $wvtxt, $wvnav; $cost = haus_build::getCosts(); $miss = $this->getMissingCosts(); $str = $wvtxt->massGet(array('process_intro','process_main')); $sea = array( '{GOLD}' , '{GEMS}' , '{TURN}', '{P_GOLD}' , '{P_GEMS}' , '{P_TURN}', '{L_GOLD}' , '{L_GEMS}' , '{L_TURN}', '{GOLD_BAR}', '{GEMS_BAR}', '{TURN_BAR}'); $rep = array( $cost['gold'], $cost['gems'], $cost['turn'], $cost['gold']-$miss['gold'], $cost['gems']-$miss['gems'], $cost['turn']-$miss['turn'], $miss['gold'], $miss['gems'], $miss['turn'], grafbar($cost['gold'],$cost['gold']-$miss['gold'],150,15), grafbar($cost['gems'],$cost['gems']-$miss['gems'],150,15), grafbar($cost['turn'],$cost['turn']-$miss['turn'],150,15)); $str .= str_replace($sea,$rep,$wvtxt->get('process_cost')); if($user['turns'] > 0) { $sea = array('{LINK}','{GOLD}','{GEMS}'); $formlink = $wvnav->link('house_id='.$this->get(HAUS_ID_FIELD).'&act=work',true); addnav('',$formlink); $rep = array($formlink,haus_build::corrigatePrefVal($user,'gold',$miss['gold'],$miss['gold']),haus_build::corrigatePrefVal($user,'gems',$miss['gems'],$miss['gems'])); $str .= str_replace($sea,$rep,$wvtxt->get('process_spend')); } return $str; } function getWorkText(&$user) { global $wvtxt, $wvnav; $str = ''; if($user['turns'] > 0) { $paid = $this->pay($user,false,array('gold'=>$_POST['spend_gold'],'gems'=>$_POST['spend_gems'],'turn'=>$_POST['spend_turn'])); if ($this->isFinished()) { $wvnav->clear(); $str .= $wvtxt->get('work_finish'); $formlink = $wvnav->link('act=finish&'.HAUS_ID_FIELD.'='.$this->get(HAUS_ID_FIELD),true); addnav('',$formlink); $sea = array('{LINK}','{NAME}'); $rep = array($formlink,$user['login']."s Haus"); $str .= str_replace($sea,$rep,$wvtxt->get('work_config')); } else { $str .= $wvtxt->get('work_intro'); } $sea = array('{GOLD}','{GEMS}','{TURN}'); $rep = array($paid['gold'],$paid['gems'],$paid['turn']); $str .= str_replace($sea,$rep,$wvtxt->get('work_cost')); } else { $str .= $wvtxt->get('work_fail'); } return $str; } function getFinishText($user) { global $wvtxt; $_POST['house_name'] = urldecode($_POST['house_name']); $baselevel = new ausbaustufe(); wvKey::generateKeys($this->get(HAUS_ID_FIELD),$baselevel->keys-1,$user['acctid'],2); $this->set('level',1); $this->set('name',$_POST['house_name']); $this->set('gold',0); $this->set('gems',0); $this->set('turn','NULL'); $str = $wvtxt->get('finish_intro'); $sea = array('{NAME}','{KEYS}'); $rep = array($_POST['house_name'],$baselevel->keys); $str .= str_replace($sea,$rep,$wvtxt->get('finish_main')); return $str; } function getInfoText() { global $wvtxt; $cost = haus_build::getCosts(); $sea = array('{GOLD}','{GEMS}','{TURN}','{S_GOLD}','{S_GEMS}','{S_TURN}','{DK}'); $rep = array($cost['gold'],$cost['gems'],$cost['turn'],$cost['sgold'],$cost['sgems'],$cost['sturn'],$cost['dk']); $str = $wvtxt->massGet(array('info_intro','info_main')); $str .= str_replace($sea,$rep,$wvtxt->get('info_cost')); $str .= $wvtxt->get('info_end'); return $str; } function getStartText() { global $wvtxt; $cost = haus_build::getCosts(); $sea = array('{GOLD}','{GEMS}','{TURN}'); $rep = array($cost['sgold'],$cost['sgems'],$cost['sturn']); $str = $wvtxt->get('start_intro'); $str .= str_replace($sea,$rep,$wvtxt->get('start_cost')); return $str; } } ?>