This commit is contained in:
Dreamgineer
2025-09-23 10:10:01 -04:00 Unverified
parent bdef3c3bd0
commit 3391e41503
18 changed files with 625 additions and 147 deletions
+59
View File
@@ -0,0 +1,59 @@
"use server";
import { eq, inArray } from "drizzle-orm";
import { uidRegex } from "./const";
import { db } from "./db";
import {
artifactSettings,
characters,
settings,
submissions,
} from "./db/schema";
export async function getCharacters(chars: string[]) {
return await db
.select()
.from(characters)
.where(inArray(characters.amber, chars));
}
export async function getArtifactConfig() {
const [art] = await db.select().from(artifactSettings).limit(1);
const [glob] = await db.select().from(settings).limit(1);
// @ts-expect-error
return { locked: false, limit: -1, enka: true, ...art, ...glob };
}
export async function submitArtifact(formData: FormData) {
const config = await getArtifactConfig();
const name = formData.get("name")?.toString();
const uid = formData.get("password")?.toString();
const character = formData.get("character")?.toString();
const comment = formData.get("comment")?.toString();
if (!name || !uid || !character || !comment) throw "กรุณากรอกข้อมูลให้ครบถ้วน";
if (name.length > 32) throw "ชื่อยาวเกินไป ต้องไม่เกิน 32 ตัวอักษร";
if (!uidRegex.test(uid)) throw "UID ไม่ถูกต้อง ต้องเป็นเลข 9 หลัก";
if (comment.length > 256) throw "ข้อความเพิ่มเติมยาวเกินไป ต้องไม่เกิน 256 ตัวอักษร";
if (config.locked) throw "ปิดรับลงทะเบียนชั่วคราว เนื่องจากมีผู้ลงจำนวนมาก";
const count = await db.$count(submissions);
if (config.limit !== -1 && count >= config.limit)
throw `คิวลงทะเบียนเต็มแล้ว (${config.limit} ครั้ง)`;
const [char] = await db
.select()
.from(characters)
.where(eq(characters.name, character))
.limit(1);
if (!char) throw "ไม่พบตัวละครที่เลือก";
const [existing] = await db
.select()
.from(submissions)
.where(eq(submissions.uid, uid))
.limit(1);
if (existing) throw "คุณลงทะเบียนไปแล้ว";
await db.insert(submissions).values({
uid,
name,
comment,
char: char.name,
});
}
+18 -1
View File
@@ -1 +1,18 @@
export const base = process.env.BASE_URL!;
import type { element } from "./db/schema";
export const base = process.env.BASE_URL!;
export const AmberElementMap: Record<
string,
(typeof element.enumValues)[number]
> = {
Ice: "cryo",
Water: "hydro",
Fire: "pyro",
Wind: "anemo",
Rock: "geo",
Grass: "dendro",
Electric: "electro",
};
export const uidRegex = /^(?:[0-35-9]|18)[0-9]{8}$/;
+3
View File
@@ -19,6 +19,7 @@ export const element = pgEnum("character_element", [
"hydro",
"pyro",
"cryo",
"electro",
]);
// #region Shared
@@ -47,6 +48,7 @@ export const characters = pgTable("characters", {
.notNull()
.references(() => cdn.id),
weapon: text().notNull(),
amber: text().notNull(), // Amber character ID
});
export const versions = pgTable("versions", {
@@ -68,6 +70,7 @@ export const artifact = pgSchema("artifact");
export const submissions = artifact.table("submissions", {
id: text().primaryKey().$defaultFn(uuidv7),
uid: text().notNull().unique(),
name: text().notNull(),
comment: text().notNull(),
char: text()