00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <libfuture.h>
00021 #include <Utility.h>
00022
00023 #include "Race.h"
00024 #include "InSocket.h"
00025
00026
00027
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;
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