all repos — xrxs @ c8ba412121282b7e4d51126b5da615d75b7faf1a

experimental networked application/game server with 9p

realm.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
122
123
124
125
126
127
128
129
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "util.h"
#include "user.h"
#include "cart.h"
#include "universe.h"
#include "realm.h"

Realm* create_realm(UserInfo* table, char* uname, char* name) {
  Realm* self;
  UserInfo* u = find_user(table, uname);
  char cart[32];
  int max = 4;
  char* n = name;
  char path[128] = {0};

  if (u == nil || u->cart == nil)
    return 0;
  scpy(u->cart->name, cart, 32);

  while (*n && *n != ' ') {
    n++;
  }
  if (*n) {
    *n = 0;
    n++;
    max = atoi(n);
  }

  scat(path, "carts/");
  scat(path, cart);
  scat(path, "/realms/");
  if (open(path, OREAD) < 0)
    create(path, OREAD, DMDIR | 0755);

  scat(path, name);
  fprintf(stderr, "trying to create realm: %s\n", path);
  if (create(path, OREAD, DMDIR | 0755) < 0) {
    fprintf(stderr, "failed to create realm backing store\n");
    return nil;
  } else {
    self = malloc(sizeof(Realm));
    scpy(name, self->name, 32);
    self->max = max;
    self->password = 0;
    self->universe = create_universe();
    scpy(uname, self->master, 32);
    save_realm(cart, self);
    fprintf(stderr, "created realm '%s'\n", name);
    return self;
  }
}

Realm* parse_realm(char* cart, char* name) {
  Realm* self;
  FILE* f;
  char path[128];
  char file[128];
  char buf[256];
  scat(path, "carts/");
  scat(path, cart);
  scat(path, "/realms/");
  scat(path, name);
  scpy(path, file, 128);

  scat(file, "/realm");
  f = fopen(file, "r");
  if (f != nil) {
    if (fgets(buf, 256, f)) {
      self = malloc(sizeof(Realm));
      sscanf(buf, "%hu %32s %llu", &(self->max), self->master, &(self->password));
      fclose(f);
    } else {
      return nil;
    }
  } else {
    return nil;
  }

  scpy(path, file, 128);
  scat(file, "/universe");
  self->universe = parse_universe(path);
  return self;
}

Realm* find_realm(UserInfo* table, char* name) {
  UserInfo* u = table;
  int i;

  for (i = 0; i < 64; i++) {
    if (slen(u->name) > 0 && u->realm != nil && scmp(u->realm->name, name))
      return u->realm;
    u++;
  }
  return nil;
}

void save_realm(char* cart, Realm* self) {
  FILE* f;
  char path[128] = {0};
  char file[128] = {0};
  scat(path, "carts/");
  scat(path, cart);
  scat(path, "/realms/");
  scat(path, self->name);
  scpy(path, file, 128);

  scat(file, "/realm");
  f = fopen(file, "w");
  if (f != nil) {
    fprintf(f, "%hu %s, %llu", self->max, self->master, self->password);
    fclose(f);
    save_universe(cart, self->universe, self->name);
    fprintf(stderr, "saved realm data");
  } else {
    fprintf(stderr, "error saving realm: '%s'", file);
  }
}

void destroy_realm(Realm* self) {
  if (self != nil) {
    if (self->universe != nil) {
      destroy_universe(self->universe);
    }
    free(self);
    self = nil;
  }
}