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

MainForm.cpp

Go to the documentation of this file.
00001 #include "MainForm.h" 00002 #include "SimpleWeb.h" 00003 #include <libsimple.h> 00004 00005 00006 MainForm::MainForm(SimpleWeb& www) 00007 :BaseForm(www, "MainForm") 00008 { 00009 } 00010 00011 00012 MainForm::~MainForm() 00013 { 00014 } 00015 00016 00017 void MainForm::Process() 00018 { 00019 Database& db = GetWeb().GetDatabase(); 00020 Query q(db); 00021 std::string action = GetForm() -> getvalue(GetFormName()); 00022 long skill = GetWeb().GetSkill(); 00023 long building = GetWeb().GetBuilding(); 00024 char sql[1000]; 00025 00026 if (action == "logout") 00027 { 00028 sprintf(sql, "delete from valid where account=%ld", GetWeb().GetAccount()); 00029 q.execute(sql); 00030 GetWeb().SetAccount(0); 00031 GetWeb().SetValid(0); 00032 GetWeb().SetPlayer(0); 00033 return; 00034 } 00035 else 00036 if (action == "player") 00037 { 00038 GetWeb().SetPlayer(0); 00039 return; 00040 } 00041 else 00042 if (action == "hire") 00043 { 00044 long num = atol(GetForm() -> getvalue("hire").c_str()); 00045 db::Npc npc(db, num); 00046 db::Player pl(db, GetWeb().GetPlayer()); 00047 sprintf(sql, "select count(*) from npc where player=%ld and location=%ld", GetWeb().GetPlayer(), pl.location); 00048 long qnpcs = q.get_count(sql); 00049 long qbeds = 0; 00050 sprintf(sql, "select * from playerbuilding where player=%ld and location=%ld", GetWeb().GetPlayer(), pl.location); 00051 q.get_result(sql); 00052 while (q.fetch_row()) 00053 { 00054 db::Playerbuilding x(&db, &q); 00055 db::Building b(db, x.building); 00056 qbeds += b.beds; 00057 } 00058 q.free_result(); 00059 00060 if (!npc.player && qnpcs < qbeds) 00061 { 00062 sprintf(sql, "select * from npcskill where npc=%ld", npc.num); 00063 db::Npcskill npcsk(&db, sql); 00064 db::Resource gold(db, "Gold"); 00065 db::Linkskillresource x(db, npcsk.skill, gold.num); 00066 db::Playerresource y(db, GetWeb().GetPlayer(), gold.num); 00067 if (y.amount >= x.base_amount) 00068 { 00069 y.amount -= x.base_amount; 00070 y.save(); 00071 npc.player = GetWeb().GetPlayer(); 00072 npc.paid = 1; 00073 npc.location = pl.location; 00074 npc.save(); 00075 } 00076 } 00077 return; 00078 } 00079 else 00080 if (action == "fire") 00081 { 00082 long num = atol(GetForm() -> getvalue("fire").c_str()); 00083 db::Npc npc(db, num); 00084 if (npc.player == GetWeb().GetPlayer()) 00085 { 00086 npc.player = 0; 00087 npc.save(); 00088 } 00089 return; 00090 } 00091 else 00092 if (action == "travel") 00093 { 00094 long num = atol(GetForm() -> getvalue("travel").c_str()); 00095 db::Player pl(db, GetWeb().GetPlayer()); 00096 db::Locationlink x(db, pl.location, num); 00097 if (x.location1 == pl.location && x.location2 == num) // ok 00098 { 00099 pl.location = num; 00100 pl.save(); 00101 } 00102 return; 00103 } 00104 db::Account acct(db, GetWeb().GetAccount()); 00105 if (!acct.superuser) 00106 { 00107 return; 00108 } 00109 // superuser functions 00110 if (action == "skill_create") 00111 { 00112 std::string name = GetForm() -> getvalue("skill_name"); 00113 std::string rname = GetForm() -> getvalue("resource_name"); 00114 std::string prod = GetForm() -> getvalue("prod"); // p / c 00115 long num = atol(GetForm() -> getvalue("skill").c_str()); 00116 long rtoadd = atol(GetForm() -> getvalue("resource").c_str()); 00117 long amount = atol(GetForm() -> getvalue("amount").c_str()); 00118 if (rname.size()) 00119 { 00120 db::Resource r(db, rname); 00121 r.name = rname; 00122 r.save(); 00123 } 00124 if (name.size()) 00125 { 00126 db::Skill sk(db, name); 00127 sk.name = name; 00128 sk.save(); 00129 } 00130 if (num) 00131 { 00132 GetWeb().SetSkill(num); 00133 skill = num; 00134 } 00135 if (skill && rtoadd) 00136 { 00137 db::Linkskillresource x(db, skill, rtoadd); 00138 x.skill = skill; 00139 x.resource = rtoadd; 00140 x.type = prod; 00141 x.base_amount = amount; 00142 x.save(); 00143 } 00144 } 00145 else 00146 if (action == "unlink" && skill) 00147 { 00148 long resource = atol(GetForm() -> getvalue("unlink").c_str()); 00149 db::Linkskillresource x(db, skill, resource); 00150 x.erase(); 00151 } 00152 else 00153 if (action == "building") 00154 { 00155 long num = atol(GetForm() -> getvalue("building").c_str()); 00156 long rtoadd = atol(GetForm() -> getvalue("resource").c_str()); 00157 long amount = atol(GetForm() -> getvalue("amount").c_str()); 00158 std::string name = GetForm() -> getvalue("building_name"); 00159 long beds = atol(GetForm() -> getvalue("beds").c_str()); 00160 if (name.size()) 00161 { 00162 db::Building b(db, name); 00163 b.name = name; 00164 b.beds = beds; 00165 b.save(); 00166 } 00167 if (num) 00168 { 00169 GetWeb().SetBuilding(num); 00170 building = num; 00171 } 00172 if (building && rtoadd) 00173 { 00174 db::Buildingresource x(db, building, rtoadd); 00175 x.building = building; 00176 x.resource = rtoadd; 00177 x.amount = amount; 00178 x.save(); 00179 } 00180 } 00181 else 00182 if (action == "unlinkbr" && building) 00183 { 00184 long resource = atol(GetForm() -> getvalue("unlinkbr").c_str()); 00185 db::Buildingresource x(db, building, resource); 00186 x.erase(); 00187 } 00188 else 00189 if (action == "run") 00190 { 00191 GetWeb().Run(); 00192 } 00193 else 00194 if (action == "supervy") 00195 { 00196 GetWeb().SetSuperuservy(1); 00197 } 00198 else 00199 if (action == "normalvy") 00200 { 00201 GetWeb().SetSuperuservy(0); 00202 } 00203 else 00204 if (action == "loc_create") 00205 { 00206 std::string name = GetForm() -> getvalue("location_name"); 00207 long max = atol(GetForm() -> getvalue("max").c_str()); 00208 long num = atol(GetForm() -> getvalue("location").c_str()); 00209 db::Location loc(db, name); 00210 if (num) 00211 { 00212 GetWeb().SetLocation(num); 00213 } 00214 if (!loc.num && name.size()) 00215 { 00216 db::Player pl(db, GetWeb().GetPlayer()); 00217 loc.name = name; 00218 loc.max_buildings = max; 00219 loc.save(); 00220 { 00221 db::Locationlink x(db, loc.num, pl.location); 00222 x.location1 = loc.num; 00223 x.location2 = pl.location; 00224 x.save(); 00225 } 00226 { 00227 db::Locationlink x(db, pl.location, loc.num); 00228 x.location1 = pl.location; 00229 x.location2 = loc.num; 00230 x.save(); 00231 } 00232 q.get_result("select * from resource"); 00233 while (q.fetch_row()) 00234 { 00235 db::Resource r(&db, &q); 00236 if (random() % 100 >= 10) 00237 { 00238 db::Locationresource x(db, loc.num, r.num); 00239 x.location = loc.num; 00240 x.resource = r.num; 00241 x.amount = 1000 + random() % 10000; 00242 x.save(); 00243 } 00244 } 00245 q.free_result(); 00246 } 00247 if (name.size()) 00248 { 00249 loc.name = name; 00250 loc.max_buildings = max; 00251 loc.save(); 00252 } 00253 } 00254 } 00255 00256 00257 void MainForm::Display(long) 00258 { 00259 Database& db = GetWeb().GetDatabase(); 00260 Query q(db); 00261 db::Account acct(db, GetWeb().GetAccount()); 00262 db::Player pl(db, GetWeb().GetPlayer()); 00263 db::Location loc(db, pl.location); 00264 char sql[1000]; 00265 00266 sprintf(sql, "select count(*) from npc where player=%ld and location=%ld", pl.num, pl.location); 00267 long qnpcs = q.get_count(sql); 00268 long qbeds = 0; 00269 00270 sprintf(sql, "select * from playerbuilding where player=%ld and location=%ld", pl.num, pl.location); 00271 q.get_result(sql); 00272 while (q.fetch_row()) 00273 { 00274 db::Playerbuilding x(&db, &q); 00275 db::Building b(db, x.building); 00276 qbeds += b.beds; 00277 } 00278 q.free_result(); 00279 00280 // ... 00281 Header(db, q, acct, pl, loc, sql); 00282 00283 if (!GetWeb().GetSuperuservy() || !acct.superuser) 00284 { 00285 printf("<table cellpadding=0 cellspacing=0><tr>"); 00286 printf("<td valign='top'>"); 00287 // available npc's 00288 printf("<div class=box style='background: #e0ffe0'>"); 00289 printf("<u>NPC's available for hire</u><br>"); 00290 if (qnpcs < qbeds) 00291 { 00292 q.get_result("select * from npc where player=0 order by name"); 00293 while (q.fetch_row()) 00294 { 00295 db::Npc npc(&db, &q); 00296 sprintf(sql, "select * from npcskill where npc=%ld", npc.num); 00297 db::Npcskill npcsk(&db, sql); 00298 db::Skill sk(db, npcsk.skill); 00299 printf("<b>%s</b>, age <b>%ld</b> days, <b>%s</b> [ <a class=button href='%s?%s=hire&hire=%ld'>Hire</a> ]<br>", 00300 npc.name.c_str(), npc.age_days, sk.name.c_str(), 00301 GetWeb().GetCgiName().c_str(), GetFormName().c_str(), npc.num); 00302 } 00303 q.free_result(); 00304 } 00305 else 00306 { 00307 printf("<p>No space available - build more buildings.</p>"); 00308 } 00309 printf("</div>"); 00310 printf("</td>"); 00311 // npcs 00312 sprintf(sql, "select * from npc where player=%ld", pl.num); 00313 q.get_result(sql); 00314 if (q.num_rows()) 00315 { 00316 printf("<td valign='top'>"); 00317 printf("<div class=box style='background: #ffffe0'>"); 00318 00319 printf("<u>Your hired npc's</u><br>"); 00320 while (q.fetch_row()) 00321 { 00322 db::Npc npc(&db, &q); 00323 sprintf(sql, "select * from npcskill where npc=%ld", npc.num); 00324 db::Npcskill npcsk(&db, sql); 00325 db::Skill sk(db, npcsk.skill); 00326 printf("<b>%s</b>, age <b>%ld</b> days, <b>%s</b> [ <a class=button href='%s?%s=fire&fire=%ld'>Fire</a> ]<br>", 00327 npc.name.c_str(), npc.age_days, sk.name.c_str(), 00328 GetWeb().GetCgiName().c_str(), GetFormName().c_str(), npc.num); 00329 } 00330 printf("</div>"); 00331 printf("</td>"); 00332 } 00333 q.free_result(); 00334 00335 // resources 00336 printf("<td valign='top'>"); 00337 printf("<div class=box style='background: #ffffe0'>"); 00338 00339 printf("<u>Your resources</u><br>"); 00340 sprintf(sql, "select * from playerresource where player=%ld", pl.num); 00341 q.get_result(sql); 00342 while (q.fetch_row()) 00343 { 00344 db::Playerresource x(&db, &q); 00345 db::Resource r(db, x.resource); 00346 printf("<b>%s</b>, amount <b>%ld</b><br>", r.name.c_str(), x.amount); 00347 } 00348 q.free_result(); 00349 00350 printf("</div>"); 00351 printf("</td>"); 00352 // location / travel / found location / found road 00353 printf("<td valign='top'>"); 00354 printf("<div class=box style='background: #ffffe0'>"); 00355 printf("<u>Current location</u><br>"); 00356 printf("<b>%s</b><br>", loc.name.c_str()); 00357 sprintf(sql, "select * from locationlink where location1=%ld", loc.num); 00358 q.get_result(sql); 00359 while (q.fetch_row()) 00360 { 00361 db::Locationlink x(&db, &q); 00362 db::Location l(db, x.location2); 00363 printf("<a class=button href='%s?%s=travel&travel=%ld'>Travel to %s</a><br>", 00364 GetWeb().GetCgiName().c_str(), GetFormName().c_str(), x.location2, l.name.c_str()); 00365 } 00366 q.free_result(); 00367 printf("</div>"); 00368 // buildings / build 00369 printf("<div class=box style='background: #ffffe0'>"); 00370 00371 printf("<u>Your buildings in <b>%s</b></u><br>", loc.name.c_str()); 00372 sprintf(sql, "select * from playerbuilding where player=%ld and location=%ld", pl.num, pl.location); 00373 q.get_result(sql); 00374 while (q.fetch_row()) 00375 { 00376 db::Playerbuilding x(&db, &q); 00377 db::Building b(db, x.building); 00378 printf("<b>%s</b>, <b>%ld</b> beds<br>", 00379 b.name.c_str(), b.beds); 00380 } 00381 q.free_result(); 00382 printf("<a class=button href='%s?BuildForm=try_build'>Build</a>", GetWeb().GetCgiName().c_str()); 00383 00384 printf("</div>"); 00385 printf("</td>"); 00386 printf("</tr></table>"); 00387 } 00388 00389 // superuser 00390 if (GetWeb().GetSuperuservy() && acct.superuser) 00391 { 00392 // create skill 00393 printf("<table cellpadding=0 cellspacing=0><tr><td valign='top'>"); 00394 printf("<div class=box style='background: #ffe0e0'>"); 00395 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00396 printf("<input type=hidden name='%s' value='skill_create'>", GetFormName().c_str()); 00397 q.get_result("select * from skill order by name"); 00398 printf("Edit Skill: <select name=skill><option>"); 00399 while (q.fetch_row()) 00400 { 00401 db::Skill sk(&db, &q); 00402 printf("<option value=%ld%s>%s", sk.num, 00403 (sk.num == GetWeb().GetSkill()) ? " selected" : "", 00404 sk.name.c_str()); 00405 } 00406 q.free_result(); 00407 printf("</select><input type=submit name=submit value=' Select '><br>"); 00408 if (GetWeb().GetSkill()) 00409 { 00410 db::Skill sk(db, GetWeb().GetSkill()); 00411 printf("<div class=box style='background: #ffc0c0'>"); 00412 printf("Selected skill: <b>%s</b><br>", sk.name.c_str()); 00413 printf("<div class=box style='background: #ffe0e0'>"); 00414 printf("Resource: <select name=resource><option>"); 00415 q.get_result("select * from resource order by name"); 00416 while (q.fetch_row()) 00417 { 00418 db::Resource r(&db, &q); 00419 printf("<option value=%ld>%s", r.num, r.name.c_str()); 00420 } 00421 q.free_result(); 00422 printf("</select><br>"); 00423 printf("Type: <input type=radio name=prod value='Producer'>Producer\n"); 00424 printf("<input type=radio name=prod value='Consumer'>Consumer<br>"); 00425 printf("BaseAmount: <input type=text name=amount size=8> units/day"); 00426 printf("<br>"); 00427 printf("<input type=submit name=submit value=' Add selected resource '><br>"); 00428 printf("</div>"); 00429 sprintf(sql, "select * from linkskillresource where skill=%ld", sk.num); 00430 q.get_result(sql); 00431 while (q.fetch_row()) 00432 { 00433 db::Linkskillresource l(&db, &q); 00434 db::Resource r(db, l.resource); 00435 printf("Linked resource: <b>%s</b> %s, %ld units/day [ <a href='%s?%s=unlink&unlink=%ld'>unlink resource</a> ]<br>", 00436 r.name.c_str(), l.type.c_str(), l.base_amount, 00437 GetWeb().GetCgiName().c_str(), GetFormName().c_str(), r.num); 00438 } 00439 q.free_result(); 00440 printf("</div>"); 00441 } 00442 printf("Skill name to create<br>"); 00443 printf("<input type=text name=skill_name><br>"); 00444 printf("Resource name to create<br>"); 00445 printf("<input type=text name=resource_name><br>"); 00446 printf("<input type=submit name=submit value=' Edit / Create Skill '>"); 00447 // 00448 printf("</form>"); 00449 printf("</div>"); // create skill 00450 printf("</td><td valign='top'>"); 00451 printf("<div class=box style='background: #ffe0e0'>"); 00452 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00453 printf("<input type=hidden name='%s' value='building'>", GetFormName().c_str()); 00454 q.get_result("select * from building order by name"); 00455 printf("Edit building: <select name=building><option>"); 00456 while (q.fetch_row()) 00457 { 00458 db::Building b(&db, &q); 00459 printf("<option value=%ld%s>%s", b.num, 00460 (b.num == GetWeb().GetBuilding()) ? " selected" : "", 00461 b.name.c_str()); 00462 } 00463 q.free_result(); 00464 printf("</select><input type=submit name=submit value=' Select '><br>"); 00465 // 00466 if (GetWeb().GetBuilding()) 00467 { 00468 db::Building sk(db, GetWeb().GetBuilding()); 00469 printf("<div class=box style='background: #ffc0c0'>"); 00470 printf("Selected building: <b>%s</b><br>", sk.name.c_str()); 00471 printf("Beds: <b>%ld</b><br>", sk.beds); 00472 printf("<div class=box style='background: #ffe0e0'>"); 00473 printf("Resource: <select name=resource><option>"); 00474 q.get_result("select * from resource order by name"); 00475 while (q.fetch_row()) 00476 { 00477 db::Resource r(&db, &q); 00478 printf("<option value=%ld>%s", r.num, r.name.c_str()); 00479 } 00480 q.free_result(); 00481 printf("</select><br>"); 00482 printf("Amount: <input type=text name=amount size=8> units"); 00483 printf("<br>"); 00484 printf("<input type=submit name=submit value=' Add selected resource '><br>"); 00485 printf("</div>"); 00486 sprintf(sql, "select * from buildingresource where building=%ld", sk.num); 00487 q.get_result(sql); 00488 while (q.fetch_row()) 00489 { 00490 db::Buildingresource l(&db, &q); 00491 db::Resource r(db, l.resource); 00492 printf("Linked resource: <b>%s</b>, <b>%ld</b> units [ <a href='%s?%s=unlinkbr&unlinkbr=%ld'>unlink resource</a> ]<br>", 00493 r.name.c_str(), l.amount, 00494 GetWeb().GetCgiName().c_str(), GetFormName().c_str(), r.num); 00495 } 00496 q.free_result(); 00497 printf("</div>"); 00498 } 00499 printf("Building name to create<br>"); 00500 printf("<input type=text name=building_name><br>"); 00501 printf("Number of beds<br>"); 00502 printf("<input type=text name=beds size=10><br>"); 00503 printf("<input type=submit name=submit value=' Submit '>"); 00504 00505 printf("</form>"); 00506 printf("</div>"); 00507 // 00508 sprintf(sql, "select count(*) from locationlink where location1=%ld", pl.location); 00509 long qlinks = q.get_count(sql); 00510 if (qlinks < 3) 00511 { 00512 printf("</td><td valign='top'>"); 00513 printf("<div class=box style='background: #ffe0e0'>"); 00514 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00515 printf("<input type=hidden name='%s' value='loc_create'>", GetFormName().c_str()); 00516 00517 q.get_result("select * from location order by name"); 00518 printf("Edit location: <select name=location><option>"); 00519 while (q.fetch_row()) 00520 { 00521 db::Location sk(&db, &q); 00522 printf("<option value=%ld%s>%s", sk.num, 00523 (sk.num == GetWeb().GetLocation()) ? " selected" : "", 00524 sk.name.c_str()); 00525 } 00526 q.free_result(); 00527 printf("</select><input type=submit name=submit value=' Select '><br>"); 00528 // 00529 if (GetWeb().GetLocation()) 00530 { 00531 db::Location sk(db, GetWeb().GetLocation()); 00532 printf("<div class=box style='background: #ffc0c0'>"); 00533 printf("Selected location: <b>%s</b><br>", sk.name.c_str()); 00534 printf("Max buildings: <b>%ld</b><br>", sk.max_buildings); 00535 sprintf(sql, "select * from locationresource where location=%ld", sk.num); 00536 q.get_result(sql); 00537 while (q.fetch_row()) 00538 { 00539 db::Locationresource l(&db, &q); 00540 db::Resource r(db, l.resource); 00541 printf("Linked resource: <b>%s</b>, <b>%ld</b> units<br>", 00542 r.name.c_str(), l.amount); 00543 } 00544 q.free_result(); 00545 printf("</div>"); 00546 } 00547 00548 printf("Create location<br>(Current location: <b>%s</b>)<br>", loc.name.c_str()); 00549 printf("<input type=text name=location_name><br>"); 00550 printf("Max buildings<br>"); 00551 printf("<input type=text name=max size=10><br>"); 00552 printf("<input type=submit name=submit value=' Create '>"); 00553 // 00554 printf("</form>"); 00555 printf("</div>"); 00556 } 00557 printf("</td></tr></table>"); 00558 } 00559 } 00560 00561

Generated for simple by doxygen 1.3.7