Debian Codegraph @

server/account_char.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file server/account_char.c
16 *
17 * This file contains logic of dealing with characters that associated with an
18 * account. The main purpose of this file is to retrieve and store information
19 * about characters to communicate this to the player. For example, the
20 * amount of experience, class, race, etc of the character.
21 *
22 * In order to help performance, this information is stored in a file
23 * for each account. In this way, opening one file and reading the
24 * data gets the information needed - much more efficient than opening
25 * and reading many player files. In general, this information is
26 * only loaded/used when the account is logged in, and the data is stored
27 * in the socket structure.
28 *
29 * Format of the file is:
30 * Account Name:Class:Race:Level:Face (text form):Party:Map
31 * Mark:Fighter:Orc:18:warrior.111:foobar:Scorn
32 *
33 * Addtional fields can be added as necessary.
34 *
35 * Note about characters vs players:
36 * In many parts of the code, a character may be called a player,
37 * but that is misleading, since a player suggests the person sitting
38 * at the computer, and they play a character. In the past, there was
39 * a 1:1 mapping between players and characters, but with accounts, there
40 * can be several _characters_ associated with an account, and it will
41 * be more typical that the account maps 1:1 with a player. As such,
42 * saying this account has 10 players may be confusing or misconstrued -
43 * what is really the case is that this account has 10 characters.
44 */
45
46 #include "global.h"
47
48 #include <assert.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "account_char.h"
53 #include "object.h"
54 #include "output_file.h"
55 #include "sproto.h"
56 #include "player.h"
57
58 /** Number of fields in the accounts file. These are colon seperated */
59 #define NUM_ACCOUNT_CHAR_FIELDS 8
60
61 /**
62 * Name of the directory containing account information. I can not ever see a reason why this
63 * name would not work, but may as well still make it easy to change it.
64 */
65 /**
66 * FixMe: Shouldn't something like this go in the settings file?
67 */
68 #define ACCOUNT_DIR "account"
69
70 /**
71 * For a given account name, load the character information and
72 * return it. It is the responsibility of the caller to call account_char_free()
73 * on the returned value to free it.
74 * @param account_name
75 * Name of the account. The name should be validated before this routine
76 * is called (eg, passed checks for legitimate characters and logged in)
77 * @return
78 * linked list of the character data.
79 */
80 Account_Char *account_char_load(const char *account_name) {
81 char fname[MAX_BUF], buf[VERY_BIG_BUF];
82 FILE *fp;
83 Account_Char *first = NULL, *ac, *last = NULL;
84
85 snprintf(fname, MAX_BUF, "%s/%s/%s", settings.localdir, ACCOUNT_DIR, account_name);
86 fp = fopen(fname, "r");
87 if (!fp) {
88 /* This may not in fact be a critical error - for a new account, there
89 * may not be any data associated with it.
90 */
91 LOG(llevInfo, "Warning: Unable to open %s\n", fname);
92 return NULL;
93 }
94 while (fgets(buf, VERY_BIG_BUF, fp)) {
95 char *tmp[NUM_ACCOUNT_CHAR_FIELDS], *cp;
96
97 /* Ignore any comment lines */
98 if (buf[0] == '#') continue;
99
100 /* remove newline */
101 cp = strchr(buf, '\n');
102 if (cp) *cp = '\0';
103
104 if (split_string(buf, tmp, NUM_ACCOUNT_CHAR_FIELDS, ':') != NUM_ACCOUNT_CHAR_FIELDS) {
105 if (!tmp[7]) {
106 LOG(llevInfo, "Outdated entry in %s: %s\n", fname, buf);
107 tmp[7] = (char *) add_string("0");
108 } else {
109 LOG(llevError, "Corrupt entry in %s: %s\n", fname, buf);
110 continue;
111 }
112 }
113 ac = malloc(sizeof (Account_Char));
114 ac->name = add_string(tmp[0]);
115 ac->character_class = add_string(tmp[1]);
116 ac->race = add_string(tmp[2]);
117 ac->level = strtoul(tmp[3], (char**) NULL, 10);
118 ac->face = add_string(tmp[4]);
119 ac->party = add_string(tmp[5]);
120 ac->map = add_string(tmp[6]);
121 ac->isDead = strtoul(tmp[7], (char**) NULL, 10);
122
123 ac->next = NULL;
124
125 /* We tack on to the end of the list - in this way,
126 * the order of the file remains the same.
127 */
128 if (last)
129 last->next = ac;
130 else
131 first = ac;
132 last = ac;
133
134 }
135 fclose(fp);
136 return (first);
137 }
138
139 /**
140 * Saves the character information for the given account.
141 * @param account
142 * account name to save data for.
143 * @param chars
144 * previously loaded/generated list of character information for this account.
145 */
146 void account_char_save(const char *account, Account_Char *chars) {
147 char fname[MAX_BUF];
148 FILE *fp;
149 OutputFile of;
150 Account_Char *ac;
151
152 snprintf(fname, MAX_BUF, "%s/%s/%s", settings.localdir, ACCOUNT_DIR, account);
153
154 /* It is certanly possibly that all characters for an account have
155 * been removed/deleted - in that case, we just want to remove this
156 * file.
157 */
158 if (chars == NULL) {
159 unlink(fname);
160 return;
161 }
162
163 fp = of_open(&of, fname);
164 if (fp == NULL)
165 return;
166
167 fprintf(fp, "# IMPORTANT: Do not edit this file while the server is running. This file is\n"
168 "# only read when the server starts, and any changes will be overwritten when\n"
169 "# the server exits.\n");
170 for (ac = chars; ac; ac = ac->next) {
171 fprintf(fp, "%s:%s:%s:%d:%s:%s:%s:%d\n",
172 ac->name, ac->character_class, ac->race, ac->level,
173 ac->face, ac->party, ac->map, ac->isDead);
174 }
175 of_close(&of);
176 }
177
178 /**
179 * This adds a player to the list of accounts. We check to see if the player
180 * has already been added to this account - if so, we just update
181 * the infromation. Note that all strings are filled in, even if that
182 * may just be a blank field. This simplifies a lot of the code instead
183 * of having to deal with NULL values.
184 * Note that this routine is also used to update existing entries -
185 * if the character already exists, we update it, otherwise it is added.
186 *
187 * @param chars
188 * Existing list of characters for account. May be NULL.
189 * @param pl
190 * Player structure to add
191 * @return
192 * Returns new list of characters for account.
193 */
194 Account_Char *account_char_add(Account_Char *chars, player *pl) {
195
196 Account_Char *ap, *last = NULL;
197
198 for (ap = chars; ap; ap = ap->next) {
199 if (!strcmp(ap->name, pl->ob->name)) break;
200 last = ap;
201 }
202 /* If ap is not NULL, it means we found a match.
203 * Rather than checking to see if values have changed, just
204 * update them.
205 */
206 if (ap) {
207 /* We know the name can not be changing, as otherwise
208 * we wouldn't have gotten a match. So no need to
209 * update that.
210 */
211 #if 0
212 /* As of right now, the class of the character is not stored
213 * anyplace, so we don't know what it is. Keep this code here
214 * until it can be determined.
215 */
216 free_string(ap->character_class);
217 ap->character_class = add_string();
218 #else
219 ap->character_class = add_string("");
220 #endif
221
222 free_string(ap->race);
223 /* This looks pretty nasty. Basically, the player object is
224 * the race archetype, but its name has been changed to the player
225 * name. So we have to go back to the actual original archetype,
226 * the clone object in there, to get the name.
227 */
228 ap->race = add_string(pl->ob->arch->clone.name);
229
230 ap->level = pl->ob->level;
231
232 /* We should try and get the best face (front view) of
233 * the character - right now, we just get whatever view the character
234 * happens to be facing. Unfortunately, the animation code is such
235 * that it isn't a simple matter as most of that logic is not
236 * conveniently exposed.
237 */
238 free_string(ap->face);
239 ap->face = add_string(pl->ob->face->name);
240
241 free_string(ap->party);
242 if (pl->party)
243 ap->party = add_string(pl->party->partyname);
244 else
245 ap->party = add_string("");
246
247 free_string(ap->map);
248
249 /* If there is a real name set for the map, use that instead
250 * of the pathname, which is what maplevel holds. This is
251 * more friendly (eg, Scorn Inn vs /scorn/inns/....)
252 */
253 if (pl->ob->map && pl->ob->map->name) {
254 ap->map = add_string(pl->ob->map->name);
255 } else {
256 /* Use the stored value - this may not be as up to date, but is
257 * probably more reliable, as depending when this charapter is added,
258 * it may not really be on any map.
259 */
260 ap->map = add_string(pl->maplevel);
261 }
262 } else {
263 /* In this case, we are adding a new entry */
264 ap = malloc(sizeof (Account_Char));
265 ap->name = add_string(pl->ob->name);
266 ap->character_class = add_string("");
267 ap->race = add_string(pl->ob->arch->clone.name);
268 ap->level = pl->ob->level;
269 ap->face = add_string(pl->ob->face->name);
270 if (pl->party)
271 ap->party = add_string(pl->party->partyname);
272 else
273 ap->party = add_string("");
274 ap->map = add_string(pl->maplevel);
275 /* The character cannot be dead already */
276 ap->isDead = 0;
277
278 ap->next = NULL;
279 if (last)
280 last->next = ap;
281 else
282 chars = ap;
283 }
284 return chars;
285 }
286
287 /**
288 * This removes a character on this account. This is typically used
289 * when a character has been deleted, and not for general cleanup
290 *
291 * @param chars
292 * Existing list of characters for account.
293 * @param pl_name
294 * The name of the character
295 * @return
296 * Returns new list of characters for account.
297 */
298 Account_Char *account_char_remove(Account_Char *chars, const char *pl_name) {
299 Account_Char *ap, *last = NULL;
300
301 for (ap = chars; ap; ap = ap->next) {
302 if (!strcmp(ap->name, pl_name)) break;
303 last = ap;
304 }
305 /* If we didn't find this character, nothing to do */
306 if (!ap) return (chars);
307
308 /* As per previous notes, these should never be NULL */
309 free_string(ap->name);
310 free_string(ap->character_class);
311 free_string(ap->race);
312 free_string(ap->face);
313 free_string(ap->party);
314 free_string(ap->map);
315
316 /* remove this link, or update head of list as appropriate */
317 if (last) {
318 last->next = ap->next;
319 } else {
320 chars = ap->next;
321 }
322 free(ap);
323 return (chars);
324
325 }
326
327 /**
328 * This frees all data associated with the character information.
329 *
330 * @param chars
331 * Data to free. The caller should make sure it no longer uses
332 * any data in this list.
333 */
334 void account_char_free(Account_Char *chars) {
335 Account_Char *ap, *next;
336
337 for (ap = chars; ap; ap = next) {
338 next = ap->next;
339
340 free_string(ap->name);
341 free_string(ap->character_class);
342 free_string(ap->race);
343 free_string(ap->face);
344 free_string(ap->party);
345 free_string(ap->map);
346 free(ap);
347 }
348 }
349
350 /**
351 * This will edit the character account information so that the character
352 * that just died in permadeath will be listed as such in the accounts file.
353 *
354 * @param op
355 * The player experiencing permadeath.
356 * @return
357 * 0 for success, 1 for failure
358 */
359 int make_perma_dead(object *op) {
360 player *pl = op->contr;
361 Account_Char *chars, *ac;
362
363 if (!pl) {
364 return 1;
365 }
366 /* Is this necessary? I'm not sure. It was in the code I found to use as an example */
367 pl = get_player(pl);
368 /* Make sure there is an account name to do things to */
369 if (!pl->socket.account_name) {
370 return 1;
371 }
372
373 /* Load the appropriate account for the action. */
374 chars = account_char_load(pl->socket.account_name);
375
376 /* Find the right character. */
377 for (ac = chars; ac; ac = ac->next) {
378 if (strcmp(ac->name, op->name) == 0)
379 break;
380 }
381
382 /* This character is dead */
383 ac->isDead = 1;
384 account_char_save(pl->socket.account_name, chars);
385 return 0;
386 }
387
388 /**
389 * This will edit the character account information so that the character
390 * that was just resurrected in permadeath will be listed as such in the accounts file.
391 *
392 * @param account
393 * The account of the resurrected character.
394 * @param player
395 * The name of the resurrected character.
396 * @return
397 * 0 for success, 1 for failure
398 */
399 int unmake_perma_dead(char *account, char *player) {
400 Account_Char *chars, *ac;
401
402 /*
403 * If no account name, then there is nothing to do here.
404 * The character was dead before the account was kept track of.
405 */
406 if (!account) {
407 return 1;
408 }
409
410 struct pl* pl = account_get_logged_in_player(account);
411 if (pl == NULL) {
412 /* Load the appropriate account for the action. */
413 chars = account_char_load(account);
414 } else {
415 chars = pl->socket.account_chars;
416 }
417
418 /* Find the right character. */
419 for (ac = chars; ac; ac = ac->next) {
420 if (strcmp(ac->name, player) == 0)
421 break;
422 }
423
424 /* This character is alive */
425 ac->isDead = 0;
426 account_char_save(account, chars);
427 return 0;
428 }