이제 닉네임이 뺏기더라도 버그가 안납니다!

이걸 시뮬레이션만 가지고 잡네 역시나야
This commit is contained in:
kdh8219 2023-05-03 19:24:07 +09:00
parent 315f12b2c1
commit 616f7e52a0
No known key found for this signature in database
GPG key ID: 2B5B609E12BB4C19

View file

@ -3,24 +3,24 @@ import axios from "axios";
import TwoWayMap from "../utils/two-way-map.js"; import TwoWayMap from "../utils/two-way-map.js";
class MojangAPI { class MojangAPI {
private cached: TwoWayMap<string, string> = new TwoWayMap<string, string>(); // uuid, id private cache: TwoWayMap<string, string> = new TwoWayMap<string, string>(); // uuid, id
async getIdFromUUID(minecraft_uuid: string): Promise<string> { async getIdFromUUID(minecraft_uuid: string): Promise<string> {
let id = this.cached.get_by_first(minecraft_uuid); let id = this.cache.get_by_first(minecraft_uuid);
if (id) return id; if (id) return id;
const request = await axios.get( const request = await axios.get(
`https://api.mojang.com/user/profile/${minecraft_uuid}` `https://api.mojang.com/user/profile/${minecraft_uuid}`
); );
id = request.data.name; id = request.data.name;
if (!id) throw new Error("Failed to get minecraft id from api"); if (!id) throw new Error("Failed to get minecraft id from api");
this.cached.set_by_first(minecraft_uuid, id); this.cache.remove_by_second(id);
this.cache.set_by_first(minecraft_uuid, id);
return id; return id;
} }
async getUUIDFromId(minecraft_id): Promise<string> { async getUUIDFromId(minecraft_id): Promise<string> {
let uuid = this.cached.get_by_second(minecraft_id); let uuid = this.cache.get_by_second(minecraft_id);
if (uuid) return uuid; if (uuid) return uuid;
const response = await axios.get( const response = await axios.get(
@ -29,8 +29,8 @@ class MojangAPI {
uuid = response.data.id; uuid = response.data.id;
if (!uuid) throw new Error("Failed to get minecraft id from api"); if (!uuid) throw new Error("Failed to get minecraft id from api");
this.cached.remove_by_first(uuid); this.cache.remove_by_first(uuid);
this.cached.set_by_second(uuid, minecraft_id); this.cache.set_by_second(uuid, minecraft_id);
return uuid; return uuid;
} }
} }