[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 { model Problem {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
friendlyName String @unique friendlyName String @unique
pascalName String @unique
sampleInput String sampleInput String
sampleOutput String sampleOutput String
realInput String realInput String

View File

@ -1,8 +1,9 @@
<script lang="ts"> <script lang="ts">
import { enhance } from '$app/forms'; import { enhance } from '$app/forms';
import type { PageData } from './$types'; import type { Actions, PageData } from './$types';
export let data: PageData; export let data: PageData;
export let form: Actions;
</script> </script>
<svelte:head> <svelte:head>
@ -11,6 +12,10 @@
<h1 style="text-align:center" class="mb-4">{data.name}</h1> <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="row">
<div class="col-6"> <div class="col-6">
<a href="/admin/contests" class="btn btn-outline-primary">All Contests</a> <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 = { export const actions = {
create: async ({ request, params }) => { create: async ({ request }) => {
const data = await request.formData(); const data = await request.formData();
const name = data.get('name'); const name = data.get('name');
const problems = (await db.problem.findMany()).filter((problem) => { const problems = (await db.problem.findMany()).filter((problem) => {
@ -75,9 +75,14 @@ export const actions = {
await git.init(); await git.init();
await git.checkoutLocalBranch('master'); await git.checkoutLocalBranch('master');
createdContest.problems.forEach((problem) => { createdContest.problems.forEach((problem) => {
copyFolderSync( fs.mkdirSync(join('temp', team.id.toString(), problem.pascalName));
'templates/java/problem', fs.writeFileSync(
join('temp', team.id.toString(), problem.friendlyName) 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('.'); await git.add('.');

View File

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

View File

@ -61,8 +61,8 @@
{/if} {/if}
<form method="POST" action="?/edit"> <form method="POST" action="?/edit">
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="row justify-content-center"> <div class="row justify-content-center">
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="col-md-auto"> <div class="col-md-auto">
<textarea <textarea
name="name" name="name"
@ -71,6 +71,15 @@
disabled={!editing} disabled={!editing}
use:stretchTextarea>{data.problemData.friendlyName}</textarea 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>
</div> </div>
<h4 style="text-align:center" class="mt-5">Sample Data</h4> <h4 style="text-align:center" class="mt-5">Sample Data</h4>

View File

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

View File

@ -27,11 +27,15 @@
{/if} {/if}
<form method="POST" action="?/create" use:enhance> <form method="POST" action="?/create" use:enhance>
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="row justify-content-center"> <div class="row justify-content-center">
<h4 style="text-align:center" class="mt-3">Name</h4>
<div class="col-md-auto"> <div class="col-md-auto">
<textarea name="name" class="form-control" /> <textarea name="name" class="form-control" />
</div> </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> </div>
<h4 style="text-align:center" class="mt-5">Sample Data</h4> <h4 style="text-align:center" class="mt-5">Sample Data</h4>
<div class="row"> <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;