]> jfr.im git - irc/gameservirc.git/blame - gameserv/devel.cpp
completed migrating all lists over to STL list
[irc/gameservirc.git] / gameserv / devel.cpp
CommitLineData
a46254e3 1#include "options.h"
a46254e3 2#include "aClient.h"
3#include "extern.h"
4#include "flags.h"
5#include "player.h"
6#include "item.h"
7#include "pouch.h"
8
9#include <fstream>
10#include <iostream>
c10b78ac 11#include <list>
12#include <algorithm>
a46254e3 13
b6bf4226 14int sock = 1;
a46254e3 15
16using namespace std;
17
18char *PACKAGE = "GameServ";
c10b78ac 19char *VERSION = "1.3.2 +devel";
a46254e3 20
21long lastrefresh;
c10b78ac 22long lastrollover;
a46254e3 23
c10b78ac 24list<aClient*> clients[U_TABLE_SIZE];
7f56a68b 25list <item*> items;
a46254e3 26
27void save_lastrefresh();
28void load_lastrefresh();
29void prettyIntro();
30void check_idles();
a46254e3 31
32int main(int argc, char *argv[])
33{
34 char buffer[1024], buf[1024];
35 memset(buffer, 0, 1024);
36 memset(buf, 0, 1024);
37 int connected;
38
39 char *conf;
40 srand(time(NULL));
41 conf = new char[16];
42 strcpy(conf, "gameserv.conf");
43
44 /*
45 * This needs to be fixed to work for any number of arguments in any
46 * order
47 *
48 */
49 if (argc > 1)
50 {
51 if ( argc > 2 || stricmp(argv[1], "--help") == 0)
52 {
53 cout << "Usage: gameserv [options] [configfile]" << endl;
54 cout << "Options:" << endl;
55 cout << "--help Displays this help dialogue" << endl;
56 return 1;
57 }
58 delete []conf;
59 conf = argv[1];
60 }
61
62 prettyIntro();
63
64 if (load_config_file(conf))
65 {
66 cout << "Config file loaded ok...\n"
67 << "Turning into a daemon" << endl;
68 }
69 else
70 exit(2);
71
72 if (argc <= 1)
73 delete []conf;
74
75 // Turn into a daemon
76 /*
77 if (daemon(1,0) < 0)
78 {
79 perror("Could not turn into a daemon");
80 exit(3);
81 }
82 */
83
84 load_gs_dbase();
85 loadNews(newsdata, todaysnews);
86
87 if (load_masters() == false)
88 {
89 log("Error loading masters");
90 goto end;
91 }
92
93 if (load_monsters() == false)
94 {
95 log("Error loading monsters");
96 goto end;
97 }
98
99 if (!load_dragon())
100 {
101 log("Error loading dragon");
102 goto end;
103 }
104
105 if (load_levels() == false)
106 {
107 log("Error loading levels");
108 goto end;
109 }
110
111 shuttingdown = false;
112
113 char ignoreservers[32][256];
114 char *currentserver;
115 currentserver = strtok(ignoreserverslist, " ");
116 for (int server = 0; server < 32 && currentserver != NULL; server++)
117 {
118 strncpy(ignoreservers[server], currentserver, 255);
119 log("Placing %s on the server ignore list", currentserver);
120 currentserver = strtok(NULL, " ");
121 }
122
123 // This loop will retry the connection 3 times
7f56a68b 124 connected = 1;
125 load_lastrefresh();
a46254e3 126
c10b78ac 127 {
128 aClient *tempPtr, *tempPtr2;
129
130 list<aClient*>::iterator iter;
131 char *nick = "testing";
132 unsigned long hv = iHASH((unsigned char *) nick);
133 tempPtr = new aClient(nick);
134 tempPtr2 = new aClient("testing2");
135
136 tempPtr->stats = new Player;
137 clients[hv].push_back(tempPtr);
138 clients[hv].push_back(tempPtr);
139 clients[hv].push_back(tempPtr);
140 clients[hv].push_back(tempPtr);
141 clients[hv].push_back(tempPtr2);
142 clients[hv].push_back(tempPtr2);
143 clients[hv].push_back(tempPtr2);
144 delete tempPtr->stats;
145
146
147 tempPtr = find(nick);
148
149
150 //iter = find((clients[0].begin()), (clients[0].end()), tempPtr);
151 // cout << "Nick: " << (*iter)->getNick() << endl;
152 clients[hv].remove(tempPtr);
153 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
154 {
155 for (iter = clients[x].begin(); iter != clients[x].end(); iter++)
156 {
157 cout << (*iter)->getNick() << endl;
158 }
159 }
160
161 }
b6bf4226 162 {
e696687e 163 // This is where you can test stuff
7f56a68b 164 /*
165 string itemname;
166 item *tempitem;
167
168 cout << "Enter an item name: ";
169 cin >> itemname;
170 char num[16];
171 for (int x = 1; x < 6000001; x++)
172 {
173 sprintf(num, "%d", x);
174 tempitem = new weapon((itemname + num));
175 items.push_back(tempitem);
176 }
177
178 item *tester = items.front();
179 cout << tester->getName() << endl;
180 item *tester2 = items.back();
181 cout << tester2->getName() << endl;
182 items.clear();
183 */
a46254e3 184 }
185
186 end:
187
188 save_gs_dbase();
189 save_dragon();
190 saveNews(newsdata, todaysnews);
191
192 delete_monsters();
193
194#ifdef DEBUGMODE
195 log("<CLOSED>");
196#endif
197
198 unload_config_file();
199 return 0;
200}
7f56a68b 201
a46254e3 202aClient *find(char *nick)
203{
204 return findbynick(nick);
205}
206
207aClient *find(const char *nick)
208{
209 return findbynick(nick);
210}
211
212#ifdef P10
213
214aClient *findbyrealnick(char *realnick)
215{
c10b78ac 216 ListNode <aClient*> *newPtr;
a46254e3 217 unsigned long hv = sHASH((unsigned char *) realnick);
218 newPtr = clients[hv].First();
219
220 aClient *client = NULL;
221
222 while (newPtr)
223 {
224 client = newPtr->getData();
225 if (stricmp(client->getRealNick(), realnick) == 0)
226 return client;
227 client = NULL;
228 newPtr = newPtr->Next();
229 }
230 return client;
231}
232
233#else
234
235aClient *findbyrealnick(char *realnick)
236{
237 return findbynick(realnick);
238}
239
240#endif
241
242aClient *findbynick(char *nick)
243{
c10b78ac 244 list<aClient*>::iterator iter;
a46254e3 245#ifdef P10
246 unsigned long hv = sHASH((unsigned char *) nick);
247#else
248 unsigned long hv = iHASH((unsigned char *) nick);
249#endif
250
a46254e3 251 aClient *client = NULL;
252
c10b78ac 253 for(iter = clients[hv].begin(); iter != clients[hv].end(); iter++)
254 {
255 client = (*iter);
a46254e3 256#ifdef P10
c10b78ac 257 if (strcmp(client->getNick(), nick) == 0)
258 {
a46254e3 259#else
c10b78ac 260 if (stricmp(client->getNick(), nick) == 0)
261 {
a46254e3 262#endif
c10b78ac 263 return client;
264 }
265 client = NULL;
266 }
267 return client;
a46254e3 268}
c10b78ac 269
a46254e3 270aClient *findIRCplayer(const char *nick)
271{
c10b78ac 272 list<aClient*>::iterator iter;
a46254e3 273 aClient *p = NULL;
274
275 p = find(nick);
276
277 if (!is_playing(p))
c10b78ac 278 return NULL;
a46254e3 279
c10b78ac 280 unsigned long hv = iHASH((unsigned char *) p->stats->getName().c_str());
a46254e3 281
c10b78ac 282 for (iter = players[hv].begin(); iter != players[hv].end(); iter++)
283 {
284 p = (*iter);
a46254e3 285#ifdef P10
c10b78ac 286 if (strcmp(p->getNick(), nick) == 0)
287 {
a46254e3 288#else
c10b78ac 289 if (stricmp(p->getNick(), nick) == 0)
290 {
a46254e3 291#endif
c10b78ac 292 return p;
293 }
294 p = NULL;
295 }
a46254e3 296 return NULL;
297}
c10b78ac 298 aClient *findplayer(const char *name)
a46254e3 299 {
c10b78ac 300 list<aClient*>::iterator iter;
301 Player *p = NULL;
302 unsigned long hv = iHASH((unsigned char *) name);
303 for (iter = players[hv].begin(); iter != players[hv].end(); iter++)
a46254e3 304 {
c10b78ac 305 p = (*iter)->stats;
306 if (stricmp(p->getName().c_str(), name) == 0)
307 return (*iter);
308 p = NULL;
a46254e3 309 }
c10b78ac 310 return NULL;
311 }
312
313 void check_idles()
314 {
315 list<aClient*>::iterator iter;
316
317 Player *p = NULL;
318
319 for (int x = 0; x < U_TABLE_SIZE; x++)
a46254e3 320 {
c10b78ac 321 for (iter = players[x].begin(); iter != players[x].end(); iter++)
322 {
323 p = (*iter)->stats;
324
325 switch(p->getLevel())
326 {
327 case 1:
328 if ((time(NULL) - p->lastlogin) / 86400 >= level1expire)
329 {
330 logout((*iter));
331 players[x].remove((*iter));
332 return;
333 }
334 break;
335
336 default:
337 if ((time(NULL) - p->lastlogin) / 86400 >= defaultexpire)
338 {
339 logout((*iter));
340 players[x].remove((*iter));
341 return;
342 }
343 break;
344 }
345 if (timedOut(p))
346 {
347 timeOutEvent(p);
348 }
349 }
a46254e3 350 }
a46254e3 351 }
c10b78ac 352
353 aClient *findbynick(const char *nick)
354 {
355 list<aClient*>::iterator iter;
a46254e3 356#ifdef P10
c10b78ac 357 unsigned long hv = sHASH((unsigned char *) nick);
a46254e3 358#else
c10b78ac 359 unsigned long hv = iHASH((unsigned char *) nick);
a46254e3 360#endif
c10b78ac 361
362 aClient *client = NULL;
363
364 for (iter = clients[hv].begin(); iter != clients[hv].end(); iter++)
365 {
366 client = (*iter);
a46254e3 367#ifdef P10
c10b78ac 368 if (strcmp(client->getNick(), nick) == 0)
a46254e3 369#else
c10b78ac 370 if (stricmp(client->getNick(), nick) == 0)
a46254e3 371#endif
c10b78ac 372 return client;
373 client = NULL;
374 }
a46254e3 375 return client;
c10b78ac 376 }
377
a46254e3 378void prettyIntro()
379{
380 cout << endl;
381 cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl;
382 cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl;
383 cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl;
384 cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl;
385 cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl;
386 cout << " GGGGG V\n\n" << endl;
387 cout << "Version: " << VERSION << endl;
388}
389
390void load_lastrefresh()
391{
392 ifstream infile;
393 infile.open(".gsrefresh");
394 if (infile.fail())
395 {
396#ifdef DEBUGMODE
397 log("Error opening .gsrefresh");
398#endif
399
400 generate:
401 long mytime = time(NULL);
402#ifdef DEBUGMODE
403 log("Generating new refresh time");
404#endif
405
406 // Just a safety measure... tho no one should
407 // get anywhere near the time as their refreshperiod
408 if (refreshperiod >= mytime)
409 refreshperiod = 86400;
410
411 lastrefresh = mytime - (mytime % refreshperiod);
412 refreshall();
413 save_lastrefresh();
414 return;
415 }
416 infile >> lastrefresh;
417 infile.close();
418 if (lastrefresh < 0)
419 goto generate;
420}
421
422void save_lastrefresh()
423{
424 ofstream outfile;
425
426 outfile.open(".gsrefresh");
427
428 if (outfile.fail())
429 {
430 log("Error creating new file .gsrefresh");
431 return;
432 }
433 outfile << lastrefresh << endl;
434
435 outfile.close();
436}