tierlist done(without sortables)

This commit is contained in:
Dreamgineer
2025-10-09 04:02:22 -04:00 Unverified
parent 4a58cdfb4e
commit ba6143f63b
17 changed files with 547 additions and 90 deletions
+55 -1
View File
@@ -1,6 +1,6 @@
"use server";
import { eq, inArray, not, sql } from "drizzle-orm";
import { and, eq, inArray, not, or, sql } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { apiAuthCheck } from "./auth";
@@ -11,6 +11,8 @@ import {
characters,
settings,
submissions,
tierlistStates,
tierlistVersions,
} from "./db/schema";
import { sse } from "./utils";
@@ -138,6 +140,58 @@ export async function random() {
else throw "ไม่พบผู้ลงทะเบียนที่ยังไม่ตรวจสอบ";
}
export async function tlState(
data: Partial<typeof tierlistStates.$inferInsert>,
) {
if (!(await apiAuthCheck())) throw "Unauthorized";
const [existing] = await db
.select()
.from(tierlistStates)
.where(
or(
eq(tierlistStates.uuid, `${data.uuid}`),
and(
eq(tierlistStates.char, `${data.char}`),
eq(tierlistStates.list, `${data.list}`),
),
),
);
if (existing)
await db
.update(tierlistStates)
.set(data)
.where(eq(tierlistStates.uuid, `${data.uuid}`));
else
await db
.insert(tierlistStates)
.values(data as typeof tierlistStates.$inferInsert);
const list = data.list || existing?.list;
const states = await db
.select()
.from(tierlistStates)
.where(eq(tierlistStates.list, list));
revalidatePath(`/api/tl/${list}/states`);
sse.publish(states, { topic: `tl-${list}`, event: "update_states" });
}
export async function tlPlacements(
list: string,
placements: Record<string, string[]>,
) {
if (!(await apiAuthCheck())) throw "Unauthorized";
await db
.update(tierlistVersions)
.set({
placements,
})
.where(eq(tierlistVersions.id, list));
revalidatePath(`/api/tl/${list}`);
sse.publish(placements, { topic: `tl-${list}`, event: "update_placements" });
}
/*export async function reorder(
table: PgTable & { id: PgColumn; order: PgColumn },
target: string | { id: string; order: number },
+2 -2
View File
@@ -162,11 +162,11 @@ export const tierlistVersions = tierlist.table("versions", {
.notNull()
.references(() => versions.id),
order: integer().notNull(),
states: jsonb().notNull().$type<{ [x: string]: string[] }>().default({}),
placements: jsonb().notNull().$type<{ [x: string]: string[] }>().default({}),
});
export const tierlistStates = tierlist.table("states", {
uuid: text().primaryKey().$defaultFn(uuidv7),
uuid: text().primaryKey().unique().$defaultFn(uuidv7),
char: text()
.notNull()
.references(() => characters.id, {
+3
View File
@@ -24,6 +24,7 @@ if (!globalThis.sseList) {
export class EventSourceManager {
private id = 0;
new(topic = "_global"): Response {
console.log(` SUB #${topic}`);
let timeout: NodeJS.Timeout | undefined;
this.id++;
if (this.id > 100000) this.id = 0;
@@ -37,6 +38,7 @@ export class EventSourceManager {
globalThis.sseList.findIndex((e) => e.id === id),
1,
);
console.log(` DSC #${topic}`);
};
return new Response(
new ReadableStream({
@@ -83,6 +85,7 @@ export class EventSourceManager {
data: object,
{ event, topic = "_global" }: { event?: string; topic?: string },
) {
console.log(` PUB #${topic}`);
for (const s of globalThis.sseList) {
if (s.topic !== topic) continue;
try {