00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00028 class Guild
00029 {
00030 var $oDB;
00031 var $oLog;
00032 var $oUser;
00033
00041 function Guild ( &$oDB, &$oLog, &$oUser )
00042 {
00043 $this->oUser =& $oUser;
00044 $this->oDB =& $oDB;
00045 $this->oLog =& $oLog;
00046 }
00047
00053 function get ( $intId = 0 )
00054 {
00055 $this->oDB->query("SELECT `guild_id`, `name`, `tag`, `fgcolor`, `bgcolor`, `diplomacy` FROM `".$this->oDB->tblGuild."` WHERE `guild_id`='$intId';");
00056 $aRow = $this->oDB->fetch_assoc();
00057
00058 if ( $aRow['guild_id'] != $intId )
00059 {
00060
00061 return false;
00062 }
00063
00064 return $aRow;
00065 }
00066
00076 function set ( $intId, $strName, $strTag, $strFgColor, $strBgColor, $intDiplomacy )
00077 {
00078 $this->oDB->query("UPDATE `".$this->oDB->tblGuild."`
00079 SET `name`='$strName',`tag`='$strTag', `bgcolor`='$strBgColor', `fgcolor`='$strFgColor', `diplomacy`='$intDiplomacy', `last_update`='".time()."', `last_update_by`='".$this->oUser->intUserID."'
00080 WHERE `guild_id`='$intId';");
00081 $this->oLog->write( 'Gildenupdate: [' . $strTag . '] (id=' . $intId . ')', 4 );
00082 }
00083
00093 function add ( $strName, $strTag, $strFgColor='', $strBgColor='', $intDiplomacy=0 )
00094 {
00095 $this->oDB->query("SELECT `guild_id` FROM `".$this->oDB->tblGuild."` WHERE `name`='$strName' OR `tag`='$strTag';");
00096 $aRow = $this->oDB->fetch_assoc();
00097 if ( $aRow['guild_id'] > 0 )
00098 {
00099
00100 return false;
00101 }
00102
00103 $strMore = '';
00104 if ( strlen($strFgColor) > 0 )
00105 {
00106 $strMore .= ", `fgcolor`='$strFgColor'";
00107 }
00108 if ( strlen($strBgColor) > 0 )
00109 {
00110 $strMore .= ", `bgcolor`='$strBgColor'";
00111 }
00112 if ( $intDiplomacy != 0 )
00113 {
00114 $strMore .= ", `diplomacy`='$intDiplomacy'";
00115 }
00116
00117 $this->oDB->query("INSERT INTO `".$this->oDB->tblGuild."` ".
00118 "SET `name`='$strName', `tag`='$strTag', `last_update`='".time()."', `last_update_by`='".$this->oUser->intUserID."', ".
00119 "`created`='".time()."', `created_by`='".$this->oUser->intUserID."'$strMore;");
00120 $this->oLog->write( "Gilde angelegt: [" . $strTag . "]", 4 );
00121 return true;
00122 }
00123
00129 function delete ( $intGuild )
00130 {
00131 if ( $intGuild < 2 )
00132 {
00133 return false;
00134 }
00135 $this->oDB->query("UPDATE ".$this->oDB->tblPlayer." SET guild_id='1' WHERE guild_id='".$intGuild."';");
00136 $this->oDB->query("DELETE FROM ".$this->oDB->tblGuild." WHERE guild_id='".$intGuild."';");
00137 return true;
00138 }
00139
00140
00145 function getAll()
00146 {
00147 $aReturn = array();
00148 $aMembers = array();
00149 $aTowns = array();
00150
00151 $sqlResult = $this->oDB->query("SELECT COUNT(player_id) as count, guild_id FROM ".$this->oDB->tblPlayer." GROUP BY guild_id;");
00152 while ( $aRow = $this->oDB->fetch_assoc ( $sqlResult ) )
00153 {
00154 $aMembers[$aRow['guild_id']] = $aRow['count'];
00155 }
00156
00157
00158 $sqlResult = $this->oDB->query("SELECT COUNT(t.town_id) as count, p.guild_id FROM ".$this->oDB->tblTown." t LEFT JOIN ".$this->oDB->tblPlayer." p USING (player_id) LEFT JOIN `".$this->oDB->tblMapData."` m ON (t.x=m.x AND t.y=m.y) WHERE m.`building` > '-10' GROUP BY p.guild_id;");
00159 while ( $aRow = $this->oDB->fetch_assoc ( $sqlResult ) )
00160 {
00161 $aTowns[$aRow['guild_id']] = $aRow['count'];
00162 }
00163
00164
00165 $sqlResult = $this->oDB->query("SELECT `guild_id`, `name`, `tag`, `bgcolor`, `fgcolor`, `diplomacy` FROM `".$this->oDB->tblGuild."` ORDER BY `tag`;");
00166 while ( $aRow = $this->oDB->fetch_assoc ( $sqlResult ) )
00167 {
00168 $aRow['members'] = isset ( $aMembers[$aRow['guild_id']] ) ? $aMembers[$aRow['guild_id']] : 0;
00169 $aRow['towns'] = isset ( $aTowns[$aRow['guild_id']] ) ? $aTowns[$aRow['guild_id']] : 0;
00170 $aReturn[] = $aRow;
00171 }
00172 return $aReturn;
00173 }
00174 }