Logo
~Sockets~
~Examples~
~Contact~

Race.cpp

Go to the documentation of this file.
00001 // Race.cpp
00002 // released 2006-09-25
00003 /*
00004 Copyright (C) 2006  Anders Hedstrom (grymse@alhem.net)
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 */
00020 #include <libfuture.h>
00021 #include <Utility.h>
00022 
00023 #include "Race.h"
00024 #include "InSocket.h"
00025 
00026 
00027 // statics
00028 races_t Race::m_cache;
00029 
00030 
00031 Race::Race(Database& db,long num)
00032 :Object(db)
00033 ,m_race(GetRace(db, num))
00034 {
00035 }
00036 
00037 
00038 Race::Race(Database& db,const std::string& name)
00039 :Object(db)
00040 ,m_race(GetRace(db, name))
00041 {
00042 }
00043 
00044 
00045 Race::~Race()
00046 {
00047 }
00048 
00049 
00050 db::Race *Race::GetRace(Database& db,long num)
00051 {
00052         for (std::map<std::string,db::Race *>::iterator it = m_cache.begin(); it != m_cache.end(); it++)
00053         {
00054                 db::Race *p = (*it).second;
00055                 if (p -> num == num)
00056                         return p;
00057         }
00058         db::Race *p = new db::Race(db, num);
00059         if (p -> num)
00060         {
00061                 m_cache[p -> name] = p;
00062                 return p;
00063         }
00064         delete p;
00065         return NULL; // bang
00066 }
00067 
00068 
00069 db::Race *Race::GetRace(Database& db,const std::string& name)
00070 {
00071         db::Race *p = m_cache[name];
00072         if (p)
00073                 return p;
00074         p = new db::Race(db, name);
00075         p -> name = name;
00076         m_cache[name] = p;
00077         return p;
00078 }
00079 
00080 
00081 bool Race::Exists()
00082 {
00083         if (m_race && m_race -> num)
00084                 return true;
00085         return false;
00086 }
00087 
00088 
00089 void Race::Save()
00090 {
00091         m_race -> save();
00092 }
00093 
00094 
00095 long Race::GetNum()
00096 {
00097         return m_race -> num;
00098 }
00099 
00100 
00101 void Race::SetName(const std::string& x)
00102 {
00103         for (std::map<std::string,db::Race *>::iterator it = m_cache.begin(); it != m_cache.end(); it++)
00104         {
00105                 std::string name = (*it).first;
00106                 if (name == m_race -> name)
00107                 {
00108                         m_cache.erase(it);
00109                         break;
00110                 }
00111         }
00112         m_race -> name = x;
00113         m_cache[x] = m_race;
00114 }
00115 
00116 
00117 const std::string& Race::GetName()
00118 {
00119         return m_race -> name;
00120 }
00121 
00122 
00123 void Race::SetSightRange(double x)
00124 {
00125         m_race -> sightrange = x;
00126 }
00127 
00128 
00129 double Race::GetSightRange()
00130 {
00131         return m_race -> sightrange;
00132 }
00133 
00134 
00135 void Race::SetHearing(double x)
00136 {
00137         m_race -> hearing = x;
00138 }
00139 
00140 
00141 double Race::GetHearing()
00142 {
00143         return m_race -> hearing;
00144 }
00145 
00146 
00147 int Race::DisplayRaces(InSocket *from)
00148 {
00149         Database& db = from -> GetDatabase();
00150         Query q(db);
00151         int n = 0;
00152         q.get_result("select * from race order by name");
00153         while (q.fetch_row())
00154         {
00155                 db::Race r(&db, &q);
00156                 from -> Send(Utility::l2string(r.num) + ") " + r.name + "\n");
00157                 n = MAX(n, r.num);
00158         }
00159         q.free_result();
00160         return n;
00161 }
00162 
00163 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4