Fixed practice contest

main
jolatime 5 months ago
parent 60e2e4cc99
commit d339a5d78a

@ -13,8 +13,7 @@ import { User } from '../models/user.model';
let subs: Submission[] = [];
let UserID: number = 0;
export async function loadPage(renderer: Renderer2, el: ElementRef, ts: any): Promise<void> {
const isPractice = window.location.href.includes("Prac");
export async function loadPage(renderer: Renderer2, el: ElementRef, ts: any, prac: Boolean): Promise<void> {
const taskbar: HTMLElement = document.getElementById('Taskbar')!;
// basics for use in multiple methods
@ -27,7 +26,7 @@ export async function loadPage(renderer: Renderer2, el: ElementRef, ts: any): Pr
hintDesc: el.nativeElement.querySelector('#Hint_Desc') as HTMLElement,
HintButts: el.nativeElement.querySelector('#Hint_Butts') as HTMLElement
}
await LoadContest(el, elements, ts);
await LoadContest(el, elements, ts, prac);
}
// get the params to see if admin or not
@ -53,12 +52,14 @@ async function getUser() {
}
}
// load the current active contest
async function LoadContest(el: ElementRef, elements: any, ts: any) {
async function LoadContest(el: ElementRef, elements: any, ts: any, prac: Boolean) {
const contestID = new URLSearchParams(window.location.search).get('contestID');
console.log("Contest ID:", contestID);
let contest: Contest | undefined;
if(contestID){
const data = { contestID: contestID};
if(prac === true){
const selectedContestStr = sessionStorage.getItem('contestID');
const selectedContest = selectedContestStr ? parseInt(selectedContestStr, 10) : null;
console.log("Contest ID:", selectedContest);
const data = { contestID: selectedContest};
const res = await fetch('api/contests/getContestByID', {
method: 'POST',
headers: {

@ -104,7 +104,8 @@ export async function InsertIntoTable(data: Contest[], router: Router): Promise<
body: JSON.stringify({email})
})
let activeContest = await res3.json().catch(() => null);
if(activeContest && activeContest.error === 'NO CONTEST FOUND') activeContest = null;
console.log(activeContest);
if(!activeContest || activeContest.message === 'No active contests found') activeContest = null;
data.forEach((contest: Contest) => {
const row = document.createElement('tr');
const name = document.createElement('td');
@ -136,6 +137,7 @@ export async function InsertIntoTable(data: Contest[], router: Router): Promise<
const JoinButton = document.createElement('button');
JoinButton.textContent = 'Join';
JoinButton.addEventListener('click', function() {
sessionStorage.setItem('Prac', 'false');
router.navigate(['/contest-page']);
});
@ -151,7 +153,9 @@ export async function InsertIntoTable(data: Contest[], router: Router): Promise<
pracButt.textContent = 'Practice';
pracButt.addEventListener('click', function() {
console.log("contestID", contest.ContestID);
router.navigate(['/contest-page'], { queryParams: { email: email, contestID: contest.ContestID, Prac: true } });
sessionStorage.setItem('Prac', 'true');
sessionStorage.setItem('contestID', contest.ContestID.toString());
router.navigate(['/contest-page']);
});
pracButtCell.appendChild(pracButt);
row.appendChild(pracButtCell);

@ -88,8 +88,7 @@ export async function PopulateUserTables(PCData: Contest[]): Promise<void> {
leaderbutton.textContent = 'view';
leaderbutton.setAttribute('id','ViewLeaderboard');
leaderbutton.addEventListener('click', function() {
sessionStorage.setItem('ContestString', contest.Name);
Popup('/leaderboard');
//Popup('leaderboard.html' ,getEmail(), 'ContestName=' + contest.Name); Uncomment when leaderboard is implemented
});
leader.appendChild(leaderbutton);
@ -179,21 +178,4 @@ export async function setNewName(NameInput: string): Promise<void> {
},
body: JSON.stringify(data)
});
}
// get the current username of the user
export async function getUsername(): Promise<any> {
const data = { email: getEmail() };
const res = await fetch('api/users/getUsername', {
method: 'POST',
headers: {
'Content-Type' : 'application/json'
},
body: JSON.stringify(data)
});
if (res.ok) {
const ret = await res.json();
return ret;
}
}

@ -19,8 +19,8 @@ export class AdminContestComponent {
// attach to terminal onload
this.terminalService.reattachTerminal();
loadPage(this.renderer, this.el, this);
}
loadPage(this.renderer, this.el, this, false);
}
confirmSelection(){
const confirmSelection_ = confirm("You will lose your current flag progress, are you sure?");

@ -15,7 +15,7 @@ import { SocketIOService } from '../notifications/socket-io.service';
})
export class ContestPageComponent implements OnInit {
isPractice = window.location.href.includes("Prac");
isPractice: boolean = false;
isAdmin: boolean = this.isItAdmin();
notificationMessage: string | null = null;
@ -26,24 +26,14 @@ export class ContestPageComponent implements OnInit {
private socketService: SocketIOService) {}
ngOnInit(): void {
const isItPractice = sessionStorage.getItem('Prac');
if(isItPractice === 'true'){
this.isPractice = true;
}
// attach to terminal onload
this.terminalService.reattachTerminal();
//this.socketService.connect();
loadPage(this.renderer, this.el, this);
/*this.socketService.listen('submission-notification').subscribe((data: any) => {
console.log('Received notification:', data);
this.notificationMessage = `${data.email} solved ${data.flagName}!`;
console.log('Updated notif:', this.notificationMessage);
setTimeout(() => {
this.notificationMessage = null;
}, 5000);
});*/
this.socketService.listen('test-event').subscribe((data: any) => {
console.log('Test event received:', data);
this.notificationMessage = 'Test event received';
});
this.socketService.emit('test-event', { message: 'Hello from client' });
loadPage(this.renderer, this.el, this, this.isPractice);
}
// clear and set terminal for new flag

@ -29,8 +29,8 @@
<br>
<!-- past contest table -->
<div id="Past_Contests">
<table>
<div>
<table id="Past_Contests">
<thead>
<tr>
<th> Contest Name </th>

@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { gotoPage, logOut, getEmail } from '../Helper/Helpers';
import { Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { getNewName, setNewName, getUsername, loadTable } from '../Helper/User-pfp';
import { getNewName, setNewName, loadTable } from '../Helper/User-pfp';
@Component({
selector: 'app-user-profile',

@ -97,7 +97,7 @@ router.post('/getContests', async (req,res) => {
}
});
router.get('/getContestByID', async (req, res) => {
router.post('/getContestByID', async (req, res) => {
const { contestID } = req.body;
if(!contestID)
return res.status(400).json({error: 'No Contest ID'});

Loading…
Cancel
Save