core: problem-sort: build with namespace

This commit is contained in:
undefined committed 2024-10-08 20:16:19 +08:00
1 parent 89188d1598
commit 543cf92455
2 files changed
+12 -8

No files matched your search

-1
View File
@@ -145,7 +145,6 @@ export class ProblemMainHandler extends Handler {
: await problem.list( : await problem.list(
domainId, query, sort.length ? 1 : page, limit, domainId, query, sort.length ? 1 : page, limit,
quick ? ['title', 'pid', 'domainId', 'docId'] : undefined, quick ? ['title', 'pid', 'domainId', 'docId'] : undefined,
this.user._id,
); );
if (total) { if (total) {
pcount = total; pcount = total;
+12 -7
View File
@@ -23,6 +23,7 @@ import {
import { buildProjection } from '../utils'; import { buildProjection } from '../utils';
import { PERM, STATUS } from './builtin'; import { PERM, STATUS } from './builtin';
import * as document from './document'; import * as document from './document';
import DomainModel from './domain';
import RecordModel from './record'; import RecordModel from './record';
import SolutionModel from './solution'; import SolutionModel from './solution';
import storage from './storage'; import storage from './storage';
@@ -32,9 +33,10 @@ export interface ProblemDoc extends Document { }
export type Field = keyof ProblemDoc; export type Field = keyof ProblemDoc;
const logger = new Logger('problem'); const logger = new Logger('problem');
function sortable(source: string) { function sortable(source: string, namespaces: Record<string, string> = {}) {
const [namespace, pid] = source.includes('-') ? source.split('-') : ['', source]; const [namespace, pid] = source.includes('-') ? source.split('-') : ['default', source];
return (namespace ? namespace.padEnd(6, ' ') : '') + pid.replace(/(\d+)/g, (str) => (str.length >= 6 ? str : ('0'.repeat(6 - str.length) + str))); return ((namespaces ? `${namespaces[namespace]}-` : '') + pid)
.replace(/(\d+)/g, (str) => (str.length >= 6 ? str : ('0'.repeat(6 - str.length) + str)));
} }
function findOverrideContent(dir: string, base: string) { function findOverrideContent(dir: string, base: string) {
@@ -149,8 +151,9 @@ export class ProblemModel {
content: string, owner: number, tag: string[] = [], content: string, owner: number, tag: string[] = [],
meta: ProblemCreateOptions = {}, meta: ProblemCreateOptions = {},
) { ) {
const ddoc = await DomainModel.get(domainId);
const args: Partial<ProblemDoc> = { const args: Partial<ProblemDoc> = {
title, tag, hidden: meta.hidden || false, nSubmit: 0, nAccept: 0, sort: sortable(pid || `P${docId}`), title, tag, hidden: meta.hidden || false, nSubmit: 0, nAccept: 0, sort: sortable(pid || `P${docId}`, ddoc?.namespaces),
}; };
if (pid) args.pid = pid; if (pid) args.pid = pid;
if (meta.difficulty) args.difficulty = meta.difficulty; if (meta.difficulty) args.difficulty = meta.difficulty;
@@ -171,9 +174,10 @@ export class ProblemModel {
rawConfig = false, rawConfig = false,
): Promise<ProblemDoc | null> { ): Promise<ProblemDoc | null> {
if (Number.isSafeInteger(+pid)) pid = +pid; if (Number.isSafeInteger(+pid)) pid = +pid;
const ddoc = await DomainModel.get(domainId);
const res = typeof pid === 'number' const res = typeof pid === 'number'
? await document.get(domainId, document.TYPE_PROBLEM, pid, projection) ? await document.get(domainId, document.TYPE_PROBLEM, pid, projection)
: (await document.getMulti(domainId, document.TYPE_PROBLEM, { sort: sortable(pid), pid }) : (await document.getMulti(domainId, document.TYPE_PROBLEM, { sort: sortable(pid, ddoc?.namespaces), pid })
.project(buildProjection(projection)).limit(1).toArray())[0]; .project(buildProjection(projection)).limit(1).toArray())[0];
if (!res) return null; if (!res) return null;
try { try {
@@ -209,11 +213,12 @@ export class ProblemModel {
static async edit(domainId: string, _id: number, $set: Partial<ProblemDoc>): Promise<ProblemDoc> { static async edit(domainId: string, _id: number, $set: Partial<ProblemDoc>): Promise<ProblemDoc> {
const delpid = $set.pid === ''; const delpid = $set.pid === '';
const ddoc = await DomainModel.get(domainId);
if (delpid) { if (delpid) {
delete $set.pid; delete $set.pid;
$set.sort = sortable(`P${_id}`); $set.sort = sortable(`P${_id}`, ddoc.namespaces);
} else if ($set.pid) { } else if ($set.pid) {
$set.sort = sortable($set.pid); $set.sort = sortable($set.pid, ddoc.namespaces);
} }
await bus.parallel('problem/before-edit', $set); await bus.parallel('problem/before-edit', $set);
const result = await document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined); const result = await document.set(domainId, document.TYPE_PROBLEM, _id, $set, delpid ? { pid: '' } : undefined);