[web] Run java files

This commit is contained in:
orosmatthew 2023-05-07 16:30:56 -04:00
parent 631fb2b040
commit 1e84b54176
8 changed files with 78 additions and 18 deletions

View File

@ -43,6 +43,7 @@ model Submission {
model Problem {
id Int @id @default(autoincrement())
friendlyName String @unique
pascalName String @unique
sampleInput String
sampleOutput String
realInput String

View File

@ -1,8 +1,9 @@
<script lang="ts">
import { enhance } from '$app/forms';
import type { PageData } from './$types';
import type { Actions, PageData } from './$types';
export let data: PageData;
export let form: Actions;
</script>
<svelte:head>
@ -11,6 +12,10 @@
<h1 style="text-align:center" class="mb-4">{data.name}</h1>
{#if form && !form.success}
<div class="alert alert-danger">An error occured</div>
{/if}
<div class="row">
<div class="col-6">
<a href="/admin/contests" class="btn btn-outline-primary">All Contests</a>

View File

@ -34,7 +34,7 @@ function copyFolderSync(source: string, target: string) {
});
}
export const actions = {
create: async ({ request, params }) => {
create: async ({ request }) => {
const data = await request.formData();
const name = data.get('name');
const problems = (await db.problem.findMany()).filter((problem) => {
@ -75,9 +75,14 @@ export const actions = {
await git.init();
await git.checkoutLocalBranch('master');
createdContest.problems.forEach((problem) => {
copyFolderSync(
'templates/java/problem',
join('temp', team.id.toString(), problem.friendlyName)
fs.mkdirSync(join('temp', team.id.toString(), problem.pascalName));
fs.writeFileSync(
join('temp', team.id.toString(), problem.pascalName, problem.pascalName + '.java'),
`public class ${problem.pascalName} {
public static void main(String[] args) {
System.out.println("Hello ${problem.pascalName}!");
}
}`
);
});
await git.add('.');

View File

@ -22,17 +22,19 @@ export const actions = {
}
const data = await request.formData();
const name = data.get('name');
const pascalName = data.get('pascalName');
const sampleInput = data.get('sampleInput');
const sampleOutput = data.get('sampleOutput');
const realInput = data.get('realInput');
const realOutput = data.get('realOutput');
if (!name || !sampleInput || !sampleOutput || !realInput || !realOutput) {
if (!name || !pascalName || !sampleInput || !sampleOutput || !realInput || !realOutput) {
return { success: false };
}
await db.problem.update({
where: { id: problemId },
data: {
pascalName: pascalName.toString(),
friendlyName: name.toString(),
sampleInput: sampleInput.toString(),
sampleOutput: sampleOutput.toString(),

View File

@ -61,8 +61,8 @@
{/if}
<form method="POST" action="?/edit">
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="row justify-content-center">
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="col-md-auto">
<textarea
name="name"
@ -71,6 +71,15 @@
disabled={!editing}
use:stretchTextarea>{data.problemData.friendlyName}</textarea
>
<h4 style="text-align:center" class="mt-3">PascalCase Name (for filenames)</h4>
<div class="col-md-auto">
<input
value={data.problemData.pascalName}
disabled={!editing}
name="pascalName"
class="form-control"
/>
</div>
</div>
</div>
<h4 style="text-align:center" class="mt-5">Sample Data</h4>

View File

@ -5,23 +5,29 @@ export const actions = {
create: async ({ request }) => {
const data = await request.formData();
const name = data.get('name');
const pascalName = data.get('pascalName');
const sampleInput = data.get('sampleInput');
const sampleOutput = data.get('sampleOutput');
const realInput = data.get('realInput');
const realOutput = data.get('realOutput');
if (!name || !sampleInput || !sampleOutput || !realInput || !realOutput) {
if (!name || !pascalName || !sampleInput || !sampleOutput || !realInput || !realOutput) {
return { success: false };
}
await db.problem.create({
data: {
friendlyName: name.toString(),
sampleInput: sampleInput.toString(),
sampleOutput: sampleOutput.toString(),
realInput: realInput.toString(),
realOutput: realOutput.toString()
}
});
try {
await db.problem.create({
data: {
pascalName: pascalName.toString(),
friendlyName: name.toString(),
sampleInput: sampleInput.toString(),
sampleOutput: sampleOutput.toString(),
realInput: realInput.toString(),
realOutput: realOutput.toString()
}
});
} catch {
return { success: false };
}
return { success: true };
}

View File

@ -27,11 +27,15 @@
{/if}
<form method="POST" action="?/create" use:enhance>
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="row justify-content-center">
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="col-md-auto">
<textarea name="name" class="form-control" />
</div>
<h4 style="text-align:center" class="mt-3">PascalCase Name (for filenames)</h4>
<div class="col-md-auto">
<input name="pascalName" class="form-control" />
</div>
</div>
<h4 style="text-align:center" class="mt-5">Sample Data</h4>
<div class="row">

View File

@ -0,0 +1,28 @@
import { db } from '$lib/server/prisma';
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const GET = (async ({ params }) => {
const session = params.session;
const activeTeam = await db.activeTeam.findUnique({
where: { sessionToken: session },
include: { contest: { include: { problems: true } } }
});
if (!activeTeam) {
return json({ success: false });
}
return json({
success: true,
contestId: activeTeam.contestId,
teamId: activeTeam.teamId,
problems: activeTeam.contest.problems.map((problem) => {
return {
id: problem.id,
name: problem.friendlyName,
pascalName: problem.pascalName,
sampleInput: problem.sampleInput,
sampleOutput: problem.sampleOutput
};
})
});
}) satisfies RequestHandler;