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