#include #include #include #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] = {0}; char file[128] = {0}; char buf[256] = {0}; scat(path, "carts/"); scat(path, cart); scat(path, "/realms/"); scat(path, name); scpy(path, file, 128); scat(file, "/realm"); fprintf(stderr, "realm path: %s\n", file); 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(cart, name); 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; } }