Crossfire Server, Trunk  R20805
account_char.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
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 
59 #define NUM_ACCOUNT_CHAR_FIELDS 8
60 
68 #define ACCOUNT_DIR "account"
69 
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 
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 
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, "# This file should not be edited while the server is running.\n");
168  fprintf(fp, "# Otherwise, any changes made may be overwritten by the server\n");
169  for (ac = chars; ac; ac = ac->next) {
170  fprintf(fp, "%s:%s:%s:%d:%s:%s:%s:%d\n",
171  ac->name, ac->character_class, ac->race, ac->level,
172  ac->face, ac->party, ac->map, ac->isDead);
173  }
174  of_close(&of);
175 }
176 
194 
195  Account_Char *ap, *last = NULL;
196 
197  for (ap = chars; ap; ap = ap->next) {
198  if (!strcmp(ap->name, pl->ob->name)) break;
199  last = ap;
200  }
201  /* If ap is not NULL, it means we found a match.
202  * Rather than checking to see if values have changed, just
203  * update them.
204  */
205  if (ap) {
206  /* We know the name can not be changing, as otherwise
207  * we wouldn't have gotten a match. So no need to
208  * update that.
209  */
210 #if 0
211  /* As of right now, the class of the character is not stored
212  * anyplace, so we don't know what it is. Keep this code here
213  * until it can be determined.
214  */
216  ap->character_class = add_string();
217 #else
218  ap->character_class = add_string("");
219 #endif
220 
221  free_string(ap->race);
222  /* This looks pretty nasty. Basically, the player object is
223  * the race archetype, but its name has been changed to the player
224  * name. So we have to go back to the actual original archetype,
225  * the clone object in there, to get the name.
226  */
227  ap->race = add_string(pl->ob->arch->clone.name);
228 
229  ap->level = pl->ob->level;
230 
231  /* We should try and get the best face (front view) of
232  * the character - right now, we just get whatever view the character
233  * happens to be facing. Unfortunately, the animation code is such
234  * that it isn't a simple matter as most of that logic is not
235  * conveniently exposed.
236  */
237  free_string(ap->face);
238  ap->face = add_string(pl->ob->face->name);
239 
240  free_string(ap->party);
241  if (pl->party)
242  ap->party = add_string(pl->party->partyname);
243  else
244  ap->party = add_string("");
245 
246  free_string(ap->map);
247 
248  /* If there is a real name set for the map, use that instead
249  * of the pathname, which is what maplevel holds. This is
250  * more friendly (eg, Scorn Inn vs /scorn/inns/....)
251  */
252  if (pl->ob->map && pl->ob->map->name) {
253  ap->map = add_string(pl->ob->map->name);
254  } else {
255  /* Use the stored value - this may not be as up to date, but is
256  * probably more reliable, as depending when this charapter is added,
257  * it may not really be on any map.
258  */
259  ap->map = add_string(pl->maplevel);
260  }
261  } else {
262  /* In this case, we are adding a new entry */
263  ap = malloc(sizeof (Account_Char));
264  ap->name = add_string(pl->ob->name);
265  ap->character_class = add_string("");
266  ap->race = add_string(pl->ob->arch->clone.name);
267  ap->level = pl->ob->level;
268  ap->face = add_string(pl->ob->face->name);
269  if (pl->party)
270  ap->party = add_string(pl->party->partyname);
271  else
272  ap->party = add_string("");
273  ap->map = add_string(pl->maplevel);
274  /* The character cannot be dead already */
275  ap->isDead = 0;
276 
277  ap->next = NULL;
278  if (last)
279  last->next = ap;
280  else
281  chars = ap;
282  }
283  return chars;
284 }
285 
297 Account_Char *account_char_remove(Account_Char *chars, const char *pl_name) {
298  Account_Char *ap, *last = NULL;
299 
300  for (ap = chars; ap; ap = ap->next) {
301  if (!strcmp(ap->name, pl_name)) break;
302  last = ap;
303  }
304  /* If we didn't find this character, nothing to do */
305  if (!ap) return (chars);
306 
307  /* As per previous notes, these should never be NULL */
308  free_string(ap->name);
310  free_string(ap->race);
311  free_string(ap->face);
312  free_string(ap->party);
313  free_string(ap->map);
314 
315  /* remove this link, or update head of list as appropriate */
316  if (last) {
317  last->next = ap->next;
318  } else {
319  chars = ap->next;
320  }
321  free(ap);
322  return (chars);
323 
324 }
325 
334  Account_Char *ap, *next;
335 
336  for (ap = chars; ap; ap = next) {
337  next = ap->next;
338 
339  free_string(ap->name);
341  free_string(ap->race);
342  free_string(ap->face);
343  free_string(ap->party);
344  free_string(ap->map);
345  free(ap);
346  }
347 }
348 
358 int make_perma_dead(object *op) {
359  player *pl = op->contr;
360  Account_Char *chars, *ac;
361 
362  if (!pl) {
363  return 1;
364  }
365  /* Is this necessary? I'm not sure. It was in the code I found to use as an example */
366  pl = get_player(pl);
367  /* Make sure there is an account name to do things to */
368  if (!pl->socket.account_name) {
369  return 1;
370  }
371 
372  /* Load the appropriate account for the action. */
374 
375  /* Find the right character. */
376  for (ac = chars; ac; ac = ac->next) {
377  if (strcmp(ac->name, op->name) == 0)
378  break;
379  }
380 
381  /* This character is dead */
382  ac->isDead = 1;
384  return 0;
385 }
386 
398 int unmake_perma_dead(char *account, char *player) {
399  Account_Char *chars, *ac;
400 
401  /*
402  * If no account name, then there is nothing to do here.
403  * The character was dead before the account was kept track of.
404  */
405  if (!account) {
406  return 1;
407  }
408 
409  struct pl* pl = account_get_logged_in_player(account);
410  if (pl == NULL) {
411  /* Load the appropriate account for the action. */
412  chars = account_char_load(account);
413  } else {
414  chars = pl->socket.account_chars;
415  }
416 
417  /* Find the right character. */
418  for (ac = chars; ac; ac = ac->next) {
419  if (strcmp(ac->name, player) == 0)
420  break;
421  }
422 
423  /* This character is alive */
424  ac->isDead = 0;
425  account_char_save(account, chars);
426  return 0;
427 }
const char * party
Definition: account_char.h:32
Definition: player.h:92
const char * name
Definition: account_char.h:27
char * account_name
Definition: newserver.h:138
void account_char_free(Account_Char *chars)
Definition: account_char.c:333
Account_Char * account_char_load(const char *account_name)
Definition: account_char.c:80
void free_string(sstring str)
Definition: shstr.c:280
object clone
Definition: object.h:470
struct account_struct * next
Definition: account.c:85
socket_struct socket
Definition: player.h:94
#define ACCOUNT_DIR
Definition: account_char.c:68
const char * map
Definition: account_char.h:33
char * partyname
Definition: party.h:14
partylist * party
Definition: player.h:186
struct account_char_struct * next
Definition: account_char.h:35
char * name
Definition: map.h:328
const char * name
Definition: face.h:20
Account_Char * account_chars
Definition: newserver.h:139
void account_char_save(const char *account, Account_Char *chars)
Definition: account_char.c:146
#define NUM_ACCOUNT_CHAR_FIELDS
Definition: account_char.c:59
struct mapdef * map
Definition: object.h:297
#define snprintf
Definition: win32.h:46
int unmake_perma_dead(char *account, char *player)
Definition: account_char.c:398
int of_close(OutputFile *of)
Definition: output_file.c:61
const char * name
Definition: object.h:311
Account_Char * account_char_remove(Account_Char *chars, const char *pl_name)
Definition: account_char.c:297
player * account_get_logged_in_player(const char *name)
Definition: account.c:586
player * get_player(player *p)
Definition: player.c:280
struct pl * contr
Definition: object.h:276
#define MAX_BUF
Definition: define.h:35
const char * character_class
Definition: account_char.h:28
object * ob
Definition: player.h:158
Account_Char * account_char_add(Account_Char *chars, player *pl)
Definition: account_char.c:193
int make_perma_dead(object *op)
Definition: account_char.c:358
#define VERY_BIG_BUF
Definition: define.h:36
const char * localdir
Definition: global.h:243
struct archt * arch
Definition: object.h:412
#define unlink(__a)
Definition: win32.h:56
struct Settings settings
Definition: init.c:40
FILE * of_open(OutputFile *of, const char *fname)
Definition: output_file.c:30
sstring add_string(const char *str)
Definition: shstr.c:124
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Definition: utils.c:500
char maplevel[MAX_BUF]
Definition: player.h:96
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
const New_Face * face
Definition: object.h:332
int16_t level
Definition: object.h:351
const char * face
Definition: account_char.h:31
const char * race
Definition: account_char.h:29