forked from zombieb/galvanic-corrosion-rewrite
changed network log format, player settings
This commit is contained in:
45
src/server/profiles/content/Settings.ts
Normal file
45
src/server/profiles/content/Settings.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import z from "zod";
|
||||
import ProfileContentManager from "./base.ts";
|
||||
|
||||
export enum ProfileSetting {
|
||||
Test = "test"
|
||||
}
|
||||
|
||||
const genericSettingSchema = z.object({
|
||||
Key: z.string(),
|
||||
Value: z.string()
|
||||
});
|
||||
const settingsSchema = z.array(genericSettingSchema);
|
||||
type Setting = z.infer<typeof genericSettingSchema>;
|
||||
|
||||
export class ProfileSettingsManager extends ProfileContentManager {
|
||||
|
||||
static settingsKey = "settings";
|
||||
#key = this.profile.constructProfilePropertyKey(ProfileSettingsManager.settingsKey);
|
||||
|
||||
async getAllSettings(): Promise<Setting[]> {
|
||||
const items = await this.kv.getKv().get(this.#key);
|
||||
if (items.value == null) return [];
|
||||
const parsed = settingsSchema.safeParse(items.value);
|
||||
if (parsed.success) return parsed.data;
|
||||
else return [];
|
||||
}
|
||||
|
||||
async getSetting(setting: ProfileSetting) {
|
||||
const settings = await this.getAllSettings();
|
||||
return settings.find(s => s.Key == setting)?.Value || null;
|
||||
}
|
||||
|
||||
async #updateSettings(settings: Setting[]) {
|
||||
await this.kv.getKv().set(this.#key, settings);
|
||||
}
|
||||
|
||||
async setSetting(Key: ProfileSetting, Value: string) {
|
||||
const settings = await this.getAllSettings();
|
||||
const s = settings.find(setting => setting.Key === Key);
|
||||
if (!s) settings.push({ Key, Value });
|
||||
else s.Value = Value;
|
||||
await this.#updateSettings(settings);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,15 @@
|
||||
import type KV from "../../persistence/kv.ts";
|
||||
import type Profile from "../profile.ts";
|
||||
|
||||
class ProfileContentManager {
|
||||
|
||||
|
||||
protected profile: Profile;
|
||||
protected kv: KV;
|
||||
constructor(profile: Profile, kv: KV) {
|
||||
this.profile = profile;
|
||||
this.kv = kv;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default ProfileContentManager;
|
||||
@@ -2,6 +2,7 @@ import KV from "../persistence/kv.ts";
|
||||
import { ProfileRole } from "../platforms/base.ts";
|
||||
import { type ServerBase } from "../server.ts";
|
||||
import { type SignalRSocketHandler } from "../socket/signalr/socket.ts";
|
||||
import { ProfileSettingsManager } from "./content/Settings.ts";
|
||||
import ProfileManagerBase from "./manager.ts";
|
||||
import { recNetAccountSchema, SelfAccount, type RecNetAccount } from "./types/profile.ts";
|
||||
|
||||
@@ -15,19 +16,23 @@ class Profile {
|
||||
|
||||
#selfAcc: SelfAccount;
|
||||
|
||||
Settings: ProfileSettingsManager;
|
||||
|
||||
constructor(acc: SelfAccount, kv: KV, server: ServerBase) {
|
||||
this.#id = acc.accountId;
|
||||
this.#selfAcc = acc;
|
||||
this.#kv = kv;
|
||||
this.#server = server;
|
||||
|
||||
this.Settings = new ProfileSettingsManager(this, this.#kv);
|
||||
}
|
||||
|
||||
async #saveSelfAcc() {
|
||||
await this.#kv.getKv().set(this.#constructProfilePropertyKey(), this.#selfAcc);
|
||||
await this.#kv.getKv().set(this.constructProfilePropertyKey(), this.#selfAcc);
|
||||
this.#server.emit('profile.update', { profile: this });
|
||||
}
|
||||
|
||||
#constructProfilePropertyKey(...keys: (string | undefined)[]) {
|
||||
constructProfilePropertyKey(...keys: (string | undefined)[]) {
|
||||
return [ ProfileManagerBase.profilesKey, this.#id, ...keys.filter(val => typeof val == 'string') ];
|
||||
}
|
||||
|
||||
@@ -51,24 +56,24 @@ class Profile {
|
||||
}
|
||||
|
||||
async getBio(){
|
||||
const key = this.#constructProfilePropertyKey('bio');
|
||||
const key = this.constructProfilePropertyKey('bio');
|
||||
const val = await this.#kv.getKv().get<string>(key);
|
||||
if (!val.value) return null;
|
||||
else return val.value;
|
||||
}
|
||||
async setBio(bio: string) {
|
||||
const key = this.#constructProfilePropertyKey('bio');
|
||||
const key = this.constructProfilePropertyKey('bio');
|
||||
await this.#kv.getKv().set(key, bio);
|
||||
}
|
||||
|
||||
async getRole(): Promise<ProfileRole> {
|
||||
const val = (await this.#kv.getKv().get<ProfileRole>(this.#constructProfilePropertyKey('role'))).value;
|
||||
const val = (await this.#kv.getKv().get<ProfileRole>(this.constructProfilePropertyKey('role'))).value;
|
||||
if (!val) return ProfileRole.User;
|
||||
else return val;
|
||||
}
|
||||
|
||||
async setRole(role: ProfileRole) {
|
||||
await this.#kv.getKv().set(this.#constructProfilePropertyKey('role'), role);
|
||||
await this.#kv.getKv().set(this.constructProfilePropertyKey('role'), role);
|
||||
this.#server.emit('profile.roleupdate', { profile: this, newRole: role });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user