Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

lib/guild.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  This program is free software; you can redistribute it and/or
00004  modify it under the terms of the GNU General Public License
00005  as published by the Free Software Foundation; either version 2
00006  of the License, or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful,
00009  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  GNU General Public License for more details.
00012 
00013  You should have received a copy of the GNU General Public License
00014  along with this program; if not, write to the Free Software
00015  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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             // guild does not exist
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             // guild with this name or tag already exists
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         // first get the member count of each guild
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         // now get count of towns
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         // now get the guilds
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 }

Generated on Sun May 8 19:29:45 2005 for PhpMap by  doxygen 1.4.2