galvanic corrosion rewrite
commit this before something goes horribly wrong
This commit is contained in:
@@ -4,7 +4,7 @@ import Profile from "./profile.ts";
|
||||
import { SelfAccount, type RecNetAccount } from "./types/profile.ts";
|
||||
import Command from "./../commands/command.ts";
|
||||
import z from "zod";
|
||||
import { ProfileRole } from "../platforms/base.ts";
|
||||
import { PlatformMask, PlatformType, ProfileRole } from "../platforms/types.ts";
|
||||
|
||||
const profiles: Map<number, Profile> = new Map();
|
||||
|
||||
@@ -13,15 +13,15 @@ class ProfileManagerBase extends ServerContentBase {
|
||||
static profilesKey = "profiles";
|
||||
static profileByNameKey = "profileName";
|
||||
|
||||
/*async exists(id: number) {
|
||||
return (await this.kv.getKv().get([ ProfileManagerBase.profilesKey, id ])).value !== null
|
||||
}*/
|
||||
|
||||
async #getUnusedId() {
|
||||
let id = Math.round(Math.random() * 2_147_483_647);
|
||||
if (await this.get(id)) id = await this.#getUnusedId();
|
||||
return id;
|
||||
}
|
||||
|
||||
getActiveProfileReferences() {
|
||||
return profiles.values().toArray();
|
||||
}
|
||||
|
||||
async #getUnusedUsername() {
|
||||
const adjective = NameDictionary.Adjectives[Math.floor(Math.random() * NameDictionary.Adjectives.length)];
|
||||
@@ -31,20 +31,28 @@ class ProfileManagerBase extends ServerContentBase {
|
||||
if (await this.getByUsername(username)) username = await this.#getUnusedUsername();
|
||||
return username;
|
||||
}
|
||||
async #getUsernameDefault(username: string) {
|
||||
const prof = await this.getByUsername(username);
|
||||
if (!prof) return username;
|
||||
else return await this.#getUnusedUsername();
|
||||
}
|
||||
|
||||
async create(username?: string) {
|
||||
async create(platform: PlatformType, platformId: string, username?: string) {
|
||||
const id = await this.#getUnusedId();
|
||||
const newUsername = username? username : await this.#getUnusedUsername();
|
||||
const newUsername = username ? await this.#getUsernameDefault(username) : await this.#getUnusedUsername();
|
||||
const newProfile: RecNetAccount = {
|
||||
accountId: id,
|
||||
username: newUsername,
|
||||
displayName: newUsername,
|
||||
platforms: PlatformMask.None,
|
||||
profileImage: "DefaultProfileImage.png",
|
||||
createdAt: new Date()
|
||||
}
|
||||
await this.kv.getKv().set([ ProfileManagerBase.profilesKey, id ], newProfile);
|
||||
await this.kv.getKv().set([ ProfileManagerBase.profilesKey, newUsername ], id);
|
||||
|
||||
await this.server.Platforms.addCachedLogin(platform, platformId, id);
|
||||
|
||||
return this.get(id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user