tierlist done(without sortables)
This commit is contained in:
+55
-1
@@ -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
@@ -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, {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user