Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

SimpleWeb Class Reference

#include <SimpleWeb.h>

Collaboration diagram for SimpleWeb:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SimpleWeb ()
 ~SimpleWeb ()
void ReadCookies ()
void FormInput ()
void CreateHeader ()
void GenerateDocument ()
void SetTheCookie ()
void SetAccount (long x)
long GetAccount ()
void SetPlayer (long x)
long GetPlayer ()
void SetValid (long x)
long GetValid ()
void SetSkill (long x)
long GetSkill ()
void SetBuilding (long x)
long GetBuilding ()
void SetSuperuservy (short x)
short GetSuperuservy ()
void SetLocation (long x)
long GetLocation ()
void SetBuild (short x)
short GetBuild ()
Database & GetDatabase ()
std::string GetRandomName ()
long CreateNPC ()
void Run ()

Private Attributes

Database m_db
bool m_b_set_cookie
long m_account
long m_player
long m_valid
long m_skill
long m_building
short m_superuservy
long m_location
short m_build
LobbyForm m_form_lobby
MainForm m_form_main
BuildForm m_form_build

Constructor & Destructor Documentation

SimpleWeb::SimpleWeb  ) 
 

Definition at line 8 of file SimpleWeb.cpp.

References COOKIE_DOMAIN, COOKIE_PATH, CreateNPC(), m_form_build, m_form_lobby, and m_form_main.

00009 :Web(COOKIE_DOMAIN, COOKIE_PATH) 00010 ,m_db("localhost","root","","simple") 00011 ,m_b_set_cookie(false) 00012 ,m_account(0) 00013 ,m_player(0) 00014 ,m_valid(0) 00015 ,m_skill(0) 00016 ,m_building(0) 00017 ,m_superuservy(0) 00018 ,m_location(0) 00019 ,m_build(0) 00020 // 00021 ,m_form_lobby(*this) 00022 ,m_form_main(*this) 00023 ,m_form_build(*this) 00024 { 00025 RegWebForm(&m_form_lobby); 00026 RegWebForm(&m_form_main); 00027 RegWebForm(&m_form_build); 00028 00029 // refill npc's 00030 CreateNPC(); 00031 }

SimpleWeb::~SimpleWeb  ) 
 

Definition at line 34 of file SimpleWeb.cpp.

00035 { 00036 }


Member Function Documentation

void SimpleWeb::CreateHeader  ) 
 

Definition at line 68 of file SimpleWeb.cpp.

References m_b_set_cookie, and SetTheCookie().

00069 { 00070 if (m_b_set_cookie) 00071 SetTheCookie(); 00072 printf("Content-type: text/html\n"); 00073 printf("\n"); 00074 }

long SimpleWeb::CreateNPC  ) 
 

Definition at line 171 of file SimpleWeb.cpp.

References GetRandomName(), and m_db.

Referenced by SimpleWeb().

00172 { 00173 Query q(m_db); 00174 long qaccts = q.get_count("select count(*) from account"); 00175 long qfree = q.get_count("select count(*) from npc where player=0"); 00176 if (qfree > 10) 00177 return 0; 00178 00179 std::string name = GetRandomName(); 00180 if (random() % 100 < 10) 00181 name += " of " + GetRandomName(); 00182 db::Npc npc(m_db, name); 00183 if (npc.num) 00184 return 0; 00185 npc.name = name; 00186 npc.save(); 00187 00188 std::vector<long> skills; 00189 q.get_result("select num from skill"); 00190 while (q.fetch_row()) 00191 { 00192 skills.push_back(q.getval()); 00193 } 00194 q.free_result(); 00195 long skill = skills[random() % skills.size()]; 00196 db::Npcskill npcsk(m_db, npc.num, skill); 00197 npcsk.npc = npc.num; 00198 npcsk.skill = skill; 00199 npcsk.save(); 00200 00201 return npc.num; 00202 }

void SimpleWeb::FormInput  ) 
 

Definition at line 62 of file SimpleWeb.cpp.

00063 { 00064 Web::FormInput(); // loop reg'd forms... 00065 }

void SimpleWeb::GenerateDocument  ) 
 

Definition at line 77 of file SimpleWeb.cpp.

References MainForm::Display(), BuildForm::Display(), LobbyForm::Display(), m_account, m_build, m_form_build, m_form_lobby, m_form_main, and m_player.

00078 { 00079 printf("<html><head>" 00080 "<style type='text/css'>" 00081 ".button {" 00082 "border-left: 2px #f0f0f0 solid;" 00083 "border-top: 2px #f0f0f0 solid;" 00084 "border-right: 2px #6f6f6f solid;" 00085 "border-bottom: 2px #6f6f6f solid;" 00086 "background: #e0e0e0;" 00087 "padding-left: 5px;" 00088 "padding-right: 5px;" 00089 "text-decoration: none;" 00090 "color: #0000ff;" 00091 "}" 00092 "div.box {" 00093 "margin: 5px;" 00094 "padding: 5px;" 00095 "border: 1px #000000 solid;" 00096 "}" 00097 "</style>" 00098 "</head>"); 00099 printf("<body>"); 00100 if (!m_account || !m_player) 00101 { 00102 m_form_lobby.Display( 0 ); 00103 } 00104 else 00105 if (m_build) 00106 { 00107 m_form_build.Display( 0 ); 00108 } 00109 else 00110 { 00111 m_form_main.Display( 0 ); 00112 } 00113 // 00114 printf("</body>"); 00115 printf("</html>"); 00116 }

long SimpleWeb::GetAccount  )  [inline]
 

Definition at line 29 of file SimpleWeb.h.

References m_account.

Referenced by LobbyForm::Display(), and LobbyForm::Process().

00029 { return m_account; }

short SimpleWeb::GetBuild  )  [inline]
 

Definition at line 43 of file SimpleWeb.h.

References m_build.

Referenced by BaseForm::Header().

00043 { return m_build; }

long SimpleWeb::GetBuilding  )  [inline]
 

Definition at line 37 of file SimpleWeb.h.

References m_building.

Referenced by MainForm::Display(), MainForm::Process(), and BuildForm::Process().

00037 { return m_building; }

Database& SimpleWeb::GetDatabase  )  [inline]
 

Definition at line 45 of file SimpleWeb.h.

References m_db.

Referenced by MainForm::Display(), LobbyForm::Display(), BuildForm::Display(), MainForm::Process(), LobbyForm::Process(), and BuildForm::Process().

00045 { return m_db; }

long SimpleWeb::GetLocation  )  [inline]
 

Definition at line 41 of file SimpleWeb.h.

References m_location.

Referenced by MainForm::Display().

00041 { return m_location; }

long SimpleWeb::GetPlayer  )  [inline]
 

Definition at line 31 of file SimpleWeb.h.

References m_player.

Referenced by LobbyForm::Display(), and MainForm::Process().

00031 { return m_player; }

std::string SimpleWeb::GetRandomName  ) 
 

Definition at line 128 of file SimpleWeb.cpp.

Referenced by CreateNPC().

00129 { 00130 std::vector<std::string> name_beg; 00131 std::vector<std::string> name_end; 00132 00133 name_beg.push_back( "naz" ); 00134 name_beg.push_back( "mor" ); 00135 name_beg.push_back( "gnar" ); 00136 name_beg.push_back( "aahr" ); 00137 name_beg.push_back( "more" ); 00138 name_beg.push_back( "dark" ); 00139 name_beg.push_back( "bam" ); 00140 name_beg.push_back( "raab" ); 00141 name_beg.push_back( "rake" ); 00142 name_beg.push_back( "lor" ); 00143 name_beg.push_back( "smur" ); 00144 name_beg.push_back( "him" ); 00145 name_beg.push_back( "bar" ); 00146 00147 name_end.push_back( "guz" ); 00148 name_end.push_back( "kill" ); 00149 name_end.push_back( "gul" ); 00150 name_end.push_back( "gok" ); 00151 name_end.push_back( "tan" ); 00152 name_end.push_back( "tok" ); 00153 name_end.push_back( "bul" ); 00154 name_end.push_back( "zod" ); 00155 name_end.push_back( "zed" ); 00156 name_end.push_back( "dor" ); 00157 name_end.push_back( "grim" ); 00158 name_end.push_back( "yohn" ); 00159 name_end.push_back( "fan" ); 00160 name_end.push_back( "self" ); 00161 name_end.push_back( "ton" ); 00162 00163 std::string name = 00164 name_beg[random() % name_beg.size()] + 00165 name_end[random() % name_end.size()]; 00166 name[0] -= 32; 00167 return name; 00168 }

long SimpleWeb::GetSkill  )  [inline]
 

Definition at line 35 of file SimpleWeb.h.

References m_skill.

Referenced by MainForm::Display(), MainForm::Process(), and BuildForm::Process().

00035 { return m_skill; }

short SimpleWeb::GetSuperuservy  )  [inline]
 

Definition at line 39 of file SimpleWeb.h.

References m_superuservy.

Referenced by MainForm::Display(), and BaseForm::Header().

00039 { return m_superuservy; }

long SimpleWeb::GetValid  )  [inline]
 

Definition at line 33 of file SimpleWeb.h.

References m_valid.

00033 { return m_valid; }

void SimpleWeb::ReadCookies  ) 
 

Definition at line 39 of file SimpleWeb.cpp.

References m_account, m_build, m_building, m_db, m_location, m_player, m_skill, m_superuservy, m_valid, and SetAccount().

00040 { 00041 char slask[1000]; 00042 GetCookies() -> getvalue("s", slask, 1000); 00043 cgi::Parse pa(slask, ":"); 00044 m_account = pa.getvalue(); 00045 m_player = pa.getvalue(); 00046 m_valid = pa.getvalue(); 00047 m_skill = pa.getvalue(); 00048 m_building = pa.getvalue(); 00049 m_superuservy = pa.getvalue(); 00050 m_location = pa.getvalue(); 00051 m_build = pa.getvalue(); 00052 00053 if (m_account) 00054 { 00055 db::Valid valid(m_db, m_valid, m_account); 00056 if (valid.account != m_account) 00057 SetAccount(0); 00058 } 00059 }

void SimpleWeb::Run  ) 
 

Definition at line 205 of file SimpleWeb.cpp.

References m_db.

Referenced by MainForm::Process().

00206 { 00207 Query q(m_db); 00208 Query q2(m_db); 00209 char sql[1000]; 00210 db::Resource gold(m_db, "Gold"); 00211 db::Location home(m_db, "Homesville"); 00212 00213 q.get_result("select * from npc where player<>0 order by player"); 00214 while (q.fetch_row()) 00215 { 00216 db::Npc npc(&m_db, &q); 00217 sprintf(sql, "select * from npcskill where npc=%ld", npc.num); 00218 db::Npcskill npcsk(&m_db, sql); 00219 bool failed = false; 00220 00221 // gold check first 00222 { 00223 db::Linkskillresource x(m_db, npcsk.skill, gold.num); 00224 db::Playerresource y(m_db, npc.player, gold.num); 00225 if (y.amount < x.base_amount) // not enough gold 00226 { 00227 if (!npc.paid) // bye bye 00228 { 00229 // %! add message 00230 npc.player = 0; 00231 } 00232 else 00233 { 00234 npc.paid = 0; 00235 } 00236 } 00237 else 00238 { 00239 y.amount -= x.base_amount; 00240 y.save(); 00241 npc.paid = 1; 00242 } 00243 } 00244 00245 if (npc.paid) 00246 { 00247 // search for consumer that isn't gold 00248 bool consumer = false; 00249 sprintf(sql, "select * from linkskillresource where skill=%ld and resource<>%ld", npcsk.skill, gold.num); 00250 q2.get_result(sql); 00251 while (q2.fetch_row()) 00252 { 00253 db::Linkskillresource x(&m_db, &q2); 00254 db::Playerresource y(m_db, npc.player, x.resource); 00255 if (x.type == "Consumer") 00256 { 00257 consumer = true; 00258 break; 00259 } 00260 } 00261 q2.free_result(); 00262 /* 00263 consumer == false means that produced resources should be taken 00264 from current location 00265 */ 00266 00267 // check failed 00268 sprintf(sql, "select * from linkskillresource where skill=%ld and resource<>%ld", npcsk.skill, gold.num); 00269 q2.get_result(sql); 00270 while (q2.fetch_row()) 00271 { 00272 db::Linkskillresource x(&m_db, &q2); 00273 db::Playerresource y(m_db, npc.player, x.resource); 00274 db::Locationresource z(m_db, npc.location, x.resource); 00275 if (x.type == "Consumer") 00276 { 00277 if (y.amount < x.base_amount) 00278 { 00279 failed = true; 00280 break; 00281 } 00282 } 00283 if (x.type == "Producer" && !consumer) 00284 { 00285 if (z.amount < x.base_amount && npc.location != home.num) 00286 { 00287 failed = true; 00288 break; 00289 } 00290 } 00291 } 00292 q2.free_result(); 00293 00294 // execute if not failed 00295 if (!failed) 00296 { 00297 sprintf(sql, "select * from linkskillresource where skill=%ld and resource<>%ld", npcsk.skill, gold.num); 00298 q2.get_result(sql); 00299 while (q2.fetch_row()) 00300 { 00301 db::Linkskillresource x(&m_db, &q2); 00302 db::Playerresource y(m_db, npc.player, x.resource); 00303 db::Locationresource z(m_db, npc.location, x.resource); 00304 y.player = npc.player; 00305 y.resource = x.resource; 00306 if (x.type == "Producer") 00307 { 00308 y.amount += x.base_amount; 00309 if (!consumer && npc.location != home.num) 00310 { 00311 z.amount -= x.base_amount; 00312 z.save(); 00313 } 00314 } 00315 else 00316 if (x.type == "Consumer") 00317 { 00318 y.amount -= x.base_amount; 00319 } 00320 y.save(); 00321 } 00322 q2.free_result(); 00323 } 00324 } // if (npc.paid) 00325 npc.age_days++; 00326 npc.save(); 00327 } 00328 q.free_result(); 00329 }

void SimpleWeb::SetAccount long  x  )  [inline]
 

Definition at line 28 of file SimpleWeb.h.

References m_account, m_b_set_cookie, and SetAccount().

Referenced by MainForm::Process(), LobbyForm::Process(), ReadCookies(), and SetAccount().

00028 { if (x != m_account) m_b_set_cookie = true; m_account = x; }

void SimpleWeb::SetBuild short  x  )  [inline]
 

Definition at line 42 of file SimpleWeb.h.

References m_b_set_cookie, m_build, and SetBuild().

Referenced by BuildForm::Process(), and SetBuild().

00042 { if (x != m_build) m_b_set_cookie = true; m_build = x; }

void SimpleWeb::SetBuilding long  x  )  [inline]
 

Definition at line 36 of file SimpleWeb.h.

References m_b_set_cookie, m_building, and SetBuilding().

Referenced by MainForm::Process(), and SetBuilding().

00036 { if (x != m_building) m_b_set_cookie = true; m_building = x; }

void SimpleWeb::SetLocation long  x  )  [inline]
 

Definition at line 40 of file SimpleWeb.h.

References m_b_set_cookie, m_location, and SetLocation().

Referenced by MainForm::Process(), and SetLocation().

00040 { if (x != m_location) m_b_set_cookie = true; m_location = x; }

void SimpleWeb::SetPlayer long  x  )  [inline]
 

Definition at line 30 of file SimpleWeb.h.

References m_b_set_cookie, m_player, and SetPlayer().

Referenced by MainForm::Process(), LobbyForm::Process(), and SetPlayer().

00030 { if (x != m_player) m_b_set_cookie = true; m_player = x; }

void SimpleWeb::SetSkill long  x  )  [inline]
 

Definition at line 34 of file SimpleWeb.h.

References m_b_set_cookie, m_skill, and SetSkill().

Referenced by MainForm::Process(), and SetSkill().

00034 { if (x != m_skill) m_b_set_cookie = true; m_skill = x; }

void SimpleWeb::SetSuperuservy short  x  )  [inline]
 

Definition at line 38 of file SimpleWeb.h.

References m_b_set_cookie, m_superuservy, and SetSuperuservy().

Referenced by MainForm::Process(), and SetSuperuservy().

00038 { if (x != m_superuservy) m_b_set_cookie = true; m_superuservy = x; }

void SimpleWeb::SetTheCookie  ) 
 

Definition at line 119 of file SimpleWeb.cpp.

References m_account, m_build, m_building, m_location, m_player, m_skill, m_superuservy, and m_valid.

Referenced by CreateHeader().

00120 { 00121 char slask[1000]; 00122 sprintf(slask, "%ld:%ld:%ld:%ld:%ld:%d:%ld:%d", 00123 m_account, m_player, m_valid, m_skill, m_building, m_superuservy, m_location, m_build); 00124 SetCookie("s", slask); 00125 }

void SimpleWeb::SetValid long  x  )  [inline]
 

Definition at line 32 of file SimpleWeb.h.

References m_b_set_cookie, m_valid, and SetValid().

Referenced by MainForm::Process(), LobbyForm::Process(), and SetValid().

00032 { if (x != m_valid) m_b_set_cookie = true; m_valid = x; }


Member Data Documentation

long SimpleWeb::m_account [private]
 

Definition at line 54 of file SimpleWeb.h.

Referenced by GenerateDocument(), GetAccount(), ReadCookies(), SetAccount(), and SetTheCookie().

bool SimpleWeb::m_b_set_cookie [private]
 

Definition at line 53 of file SimpleWeb.h.

Referenced by CreateHeader(), SetAccount(), SetBuild(), SetBuilding(), SetLocation(), SetPlayer(), SetSkill(), SetSuperuservy(), and SetValid().

short SimpleWeb::m_build [private]
 

Definition at line 61 of file SimpleWeb.h.

Referenced by GenerateDocument(), GetBuild(), ReadCookies(), SetBuild(), and SetTheCookie().

long SimpleWeb::m_building [private]
 

Definition at line 58 of file SimpleWeb.h.

Referenced by GetBuilding(), ReadCookies(), SetBuilding(), and SetTheCookie().

Database SimpleWeb::m_db [private]
 

Definition at line 52 of file SimpleWeb.h.

Referenced by CreateNPC(), GetDatabase(), ReadCookies(), and Run().

BuildForm SimpleWeb::m_form_build [private]
 

Definition at line 65 of file SimpleWeb.h.

Referenced by GenerateDocument(), and SimpleWeb().

LobbyForm SimpleWeb::m_form_lobby [private]
 

Definition at line 63 of file SimpleWeb.h.

Referenced by GenerateDocument(), and SimpleWeb().

MainForm SimpleWeb::m_form_main [private]
 

Definition at line 64 of file SimpleWeb.h.

Referenced by GenerateDocument(), and SimpleWeb().

long SimpleWeb::m_location [private]
 

Definition at line 60 of file SimpleWeb.h.

Referenced by GetLocation(), ReadCookies(), SetLocation(), and SetTheCookie().

long SimpleWeb::m_player [private]
 

Definition at line 55 of file SimpleWeb.h.

Referenced by GenerateDocument(), GetPlayer(), ReadCookies(), SetPlayer(), and SetTheCookie().

long SimpleWeb::m_skill [private]
 

Definition at line 57 of file SimpleWeb.h.

Referenced by GetSkill(), ReadCookies(), SetSkill(), and SetTheCookie().

short SimpleWeb::m_superuservy [private]
 

Definition at line 59 of file SimpleWeb.h.

Referenced by GetSuperuservy(), ReadCookies(), SetSuperuservy(), and SetTheCookie().

long SimpleWeb::m_valid [private]
 

Definition at line 56 of file SimpleWeb.h.

Referenced by GetValid(), ReadCookies(), SetTheCookie(), and SetValid().


The documentation for this class was generated from the following files:
Generated for simple by doxygen 1.3.7