2021-07-10 06:46:04 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "user.h"
|
2021-07-19 04:58:08 +00:00
|
|
|
#include "cart.h"
|
2021-07-10 06:46:04 +00:00
|
|
|
#include "universe.h"
|
|
|
|
#include "realm.h"
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
Realm* create_realm(UserInfo* table, char* uname, char* name) {
|
2021-07-10 06:46:04 +00:00
|
|
|
Realm* self;
|
2021-07-19 04:58:08 +00:00
|
|
|
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);
|
|
|
|
|
2021-07-10 06:46:04 +00:00
|
|
|
scat(path, name);
|
2021-07-19 04:58:08 +00:00
|
|
|
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");
|
2021-07-10 06:46:04 +00:00
|
|
|
return nil;
|
|
|
|
} else {
|
|
|
|
self = malloc(sizeof(Realm));
|
|
|
|
scpy(name, self->name, 32);
|
2021-07-19 04:58:08 +00:00
|
|
|
self->max = max;
|
2021-07-10 06:46:04 +00:00
|
|
|
self->password = 0;
|
|
|
|
self->universe = create_universe();
|
2021-07-19 04:58:08 +00:00
|
|
|
save_realm(cart, self);
|
|
|
|
fprintf(stderr, "created realm '%s'\n", name);
|
2021-07-10 06:46:04 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
Realm* parse_realm(char* cart, char* name) {
|
2021-07-10 06:46:04 +00:00
|
|
|
Realm* self;
|
|
|
|
FILE* f;
|
2021-07-19 04:58:08 +00:00
|
|
|
char path[128];
|
|
|
|
char file[128];
|
2021-07-10 06:46:04 +00:00
|
|
|
char buf[256];
|
2021-07-19 04:58:08 +00:00
|
|
|
scat(path, "carts/");
|
|
|
|
scat(path, cart);
|
|
|
|
scat(path, "/realms/");
|
2021-07-10 06:46:04 +00:00
|
|
|
scat(path, name);
|
2021-07-19 04:58:08 +00:00
|
|
|
scpy(path, file, 128);
|
2021-07-10 06:46:04 +00:00
|
|
|
|
|
|
|
scat(file, "/realm");
|
|
|
|
f = fopen(file, "r");
|
2021-07-13 07:34:34 +00:00
|
|
|
if (f != nil) {
|
|
|
|
if (fgets(buf, 256, f)) {
|
|
|
|
self = malloc(sizeof(Realm));
|
|
|
|
sscanf(buf, "%hu %llu", &(self->max), &(self->password));
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
return nil;
|
|
|
|
}
|
2021-07-10 06:46:04 +00:00
|
|
|
} else {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
scpy(path, file, 128);
|
2021-07-10 06:46:04 +00:00
|
|
|
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++;
|
|
|
|
}
|
2021-07-13 07:34:34 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
void save_realm(char* cart, Realm* self) {
|
2021-07-13 07:34:34 +00:00
|
|
|
FILE* f;
|
2021-07-19 04:58:08 +00:00
|
|
|
char path[128] = {0};
|
|
|
|
char file[128] = {0};
|
|
|
|
scat(path, "carts/");
|
|
|
|
scat(path, cart);
|
|
|
|
scat(path, "/realms/");
|
2021-07-13 07:34:34 +00:00
|
|
|
scat(path, self->name);
|
2021-07-19 04:58:08 +00:00
|
|
|
scpy(path, file, 128);
|
2021-07-13 07:34:34 +00:00
|
|
|
|
|
|
|
scat(file, "/realm");
|
|
|
|
f = fopen(file, "w");
|
|
|
|
if (f != nil) {
|
|
|
|
fprintf(f, "%hu %llu", self->max, self->password);
|
|
|
|
fclose(f);
|
2021-07-19 04:58:08 +00:00
|
|
|
save_universe(cart, self->universe, self->name);
|
|
|
|
fprintf(stderr, "saved realm data");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error saving realm: '%s'", file);
|
2021-07-13 07:34:34 +00:00
|
|
|
}
|
2021-07-10 06:46:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:34:34 +00:00
|
|
|
void destroy_realm(Realm* self) {
|
|
|
|
if (self != nil) {
|
|
|
|
if (self->universe != nil) {
|
|
|
|
destroy_universe(self->universe);
|
|
|
|
}
|
|
|
|
free(self);
|
|
|
|
self = nil;
|
|
|
|
}
|
|
|
|
}
|