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

LobbyForm.cpp

Go to the documentation of this file.
00001 #include "LobbyForm.h" 00002 #include "SimpleWeb.h" 00003 #include <libsimple.h> 00004 00005 00006 LobbyForm::LobbyForm(SimpleWeb& www) 00007 :BaseForm(www, "LobbyForm") 00008 { 00009 } 00010 00011 00012 LobbyForm::~LobbyForm() 00013 { 00014 } 00015 00016 00017 void LobbyForm::Process() 00018 { 00019 Database& db = GetWeb().GetDatabase(); 00020 char action[200]; 00021 GetForm() -> getvalue(GetFormName(), action, 200); 00022 if (!strcmp(action, "login")) 00023 { 00024 char login[200]; 00025 char passwd[200]; 00026 GetForm() -> getvalue("login", login, 200); 00027 GetForm() -> getvalue("passwd", passwd, 200); 00028 db::Account acct(db, login, passwd); // constructor does safestr 00029 if (acct.num) 00030 { 00031 long valid = random(); 00032 db::Valid v(db, valid, acct.num); 00033 v.valid = valid; 00034 v.account = acct.num; 00035 v.save(); 00036 GetWeb().SetAccount(acct.num); 00037 GetWeb().SetValid(valid); 00038 } 00039 } 00040 else 00041 if (!strcmp(action, "register")) 00042 { 00043 Query q(db); 00044 char login[200]; 00045 char pw1[200]; 00046 char pw2[200]; 00047 char sql[1000]; 00048 GetForm() -> getvalue("login", login, 200); 00049 GetForm() -> getvalue("passwd", pw1, 200); 00050 GetForm() -> getvalue("passwd2", pw2, 200); 00051 sprintf(sql, "select num from account where login='%s'", q.safestr(login).c_str()); 00052 long num = q.get_count(sql); 00053 if (!num && !strcmp(pw1, pw2) && *pw1) 00054 { 00055 db::Account acct(&db); 00056 acct.name = login; 00057 acct.pw = pw1; 00058 acct.save(); 00059 } 00060 } 00061 else 00062 if (!strcmp(action, "player_select")) 00063 { 00064 long num = atol(GetForm() -> getvalue("player").c_str()); 00065 db::Player pl(db, num); 00066 if (pl.num && pl.account == GetWeb().GetAccount()) 00067 { 00068 GetWeb().SetPlayer(pl.num); 00069 } 00070 } 00071 else 00072 if (!strcmp(action, "player_create")) 00073 { 00074 char name[200]; 00075 GetForm() -> getvalue("player_name", name, 200); 00076 db::Player pl(db, name); 00077 if (!pl.num) 00078 { 00079 pl.account = GetWeb().GetAccount(); 00080 pl.name = name; 00081 pl.save(); 00082 // add 1000 gold 00083 { 00084 db::Resource r(&db, "select * from resource where name='Gold'"); 00085 r.name = "Gold"; 00086 r.save(); 00087 db::Playerresource x(db, pl.num, r.num); 00088 x.player = pl.num; 00089 x.resource = r.num; 00090 x.amount = 1000; 00091 x.save(); 00092 } 00093 // add "Simple cottage" building 00094 { 00095 db::Location loc(&db, "select * from location where name='Homesville'"); 00096 loc.name = "Homesville"; 00097 loc.save(); 00098 pl.location = loc.num; 00099 db::Building b(&db, "select * from building where name='Simple cottage'"); 00100 b.name = "Simple cottage"; 00101 b.beds = 2; 00102 b.save(); 00103 db::Playerbuilding x(db, pl.num, b.num); 00104 x.player = pl.num; 00105 x.building = b.num; 00106 x.location = loc.num; 00107 x.save(); 00108 } 00109 pl.save(); 00110 } 00111 } 00112 } 00113 00114 00115 void LobbyForm::Display(long) 00116 { 00117 Database& db = GetWeb().GetDatabase(); 00118 if (!GetWeb().GetAccount()) 00119 { 00120 // account login 00121 printf("<h3>Login</h3>"); 00122 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00123 printf("<input type=hidden name='%s' value='login'>", GetFormName().c_str()); 00124 printf("Account Login<br>"); 00125 printf("<input type=text name=login><br>"); 00126 printf("Account Password<br>"); 00127 printf("<input type=password name=passwd><br>"); 00128 // 00129 printf("<input type=submit name=submit value=' Login '>"); 00130 printf("</form>"); 00131 00132 // account create 00133 printf("<h3>Register</h3>"); 00134 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00135 printf("<input type=hidden name='%s' value='register'>", GetFormName().c_str()); 00136 printf("Account Login<br>"); 00137 printf("<input type=text name=login><br>"); 00138 printf("Account Password<br>"); 00139 printf("<input type=password name=passwd><br>"); 00140 printf("Repeat Password<br>"); 00141 printf("<input type=password name=passwd2><br>"); 00142 // 00143 printf("<input type=submit name=submit value=' Register '>"); 00144 printf("</form>"); 00145 } 00146 else 00147 if (!GetWeb().GetPlayer()) 00148 { 00149 Query q(db); 00150 char sql[1000]; 00151 00152 // player select 00153 sprintf(sql, "select * from player where account=%ld", GetWeb().GetAccount()); 00154 q.get_result(sql); 00155 long num_players = q.num_rows(); 00156 if (q.num_rows()) 00157 { 00158 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00159 printf("<input type=hidden name='%s' value='player_select'>", GetFormName().c_str()); 00160 printf("Select Player<br><select name='player'><option>"); 00161 while (q.fetch_row()) 00162 { 00163 db::Player pl(&db, &q); 00164 printf("<option value=%ld>%s", pl.num, pl.name.c_str()); 00165 } 00166 printf("</select><br>"); 00167 printf("<input type=submit name=submit value=' Select '>"); 00168 // 00169 printf("</form>"); 00170 } 00171 q.free_result(); 00172 00173 // player create 00174 if (num_players < 5) 00175 { 00176 printf("<form method='POST' action='%s'>", GetWeb().GetCgiName().c_str()); 00177 printf("<input type=hidden name='%s' value='player_create'>", GetFormName().c_str()); 00178 printf("Player Name<br>"); 00179 printf("<input type=text name=player_name><br>"); 00180 printf("<input type=submit name=submit value=' Create Player '>"); 00181 // 00182 printf("</form>"); 00183 } 00184 } 00185 else 00186 { 00187 // huh? 00188 00189 } 00190 } 00191 00192

Generated for simple by doxygen 1.3.7