all repos — xrxs @ e469044b23878803a9eab622c0261068476622fb

experimental networked application/game server with 9p

user.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "util.h"
#include "cart.h"
#include "realm.h"
#include "user.h"

extern UserInfo users_table[64];

UserInfo* find_user(UserInfo* table, char* uname) {
  UserInfo* u = table;
  int i;
  for (i = 0; i < 64; i++) {
    if (scmp(uname, u->name))
      return u;
    u++;
  }
  return nil;
}

int load_cart(UserInfo* table, char* uname, char* cart_name) {
  Cart* c = find_cart(table, cart_name);
  UserInfo* u = find_user(table, uname);

  if (u == nil)
    return 0;

  if (c != nil) {
    u->cart = c;
    return 1;
  } else {
    u->cart = create_cart(cart_name);
    if (u->cart == nil) {
      fprintf(stderr, "failed creating cart\n");
      return 0;
    } else {
      return 1;
    }
  }
}

int enter_realm(UserInfo* table, char* uname, char* realm_name) {
  Realm* r = find_realm(table, realm_name);
  UserInfo* u = find_user(table, uname);
  int i, j = 0;

  if (u == nil) {
    fprintf(stderr, "no user\n");
    return 0;
  }
  if (u->cart == nil) {
    fprintf(stderr, "no cart\n");
    return 0;
  }

  for (i = 0; i < 64; i++) {
    fprintf(stderr, "iterating through users table: slot %d\n", i);
    if (table[i].realm != nil && scmp(table[i].realm->name, realm_name))
      j++;
  }

  if (r != nil) {
    if (j < r->max) {
      u->realm = r;
      return 1;
    } else {
      return 0;
    }
  } else {
    r = parse_realm(u->cart->name, realm_name);
    if (r == nil)
      return 0;
    if (j <= r->max)
      return 0;
    u->realm = r;
    return 1;
  }
}

int leave_realm(UserInfo* table, char* uname) {
  UserInfo* u = find_user(table, uname);
  Realm* r;

  if (u == nil) {
    fprintf(stderr, "couldn't find user: %s\n", uname);
    return 0;
  }

  r = u->realm;
  if (r == nil) {
    fprintf(stderr, "%s is not in a realm!\n", uname);
    return 0;
  }

  if (u->cart == nil)
    return 0;

  save_realm(u->cart->name, r);
  u->realm = nil;
  if (find_realm(table, r->name) == nil)
    destroy_realm(r);
  return 1;
}

int unload_cart(UserInfo* table, char* uname) {
  UserInfo* u = find_user(table, uname);
  Cart* c;

  if (u == nil)
    return 0;

  c = u->cart;
  if (c == nil)
    return 0;

  u->cart = nil;
  if (find_cart(table, c->name) == nil)
    destroy_cart(c);
  return 1;
}