Skip to content

Commit

Permalink
in the arena trying stuff (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
imagine-hussain authored Sep 27, 2023
1 parent a1b1264 commit d9b290d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 73 deletions.
74 changes: 32 additions & 42 deletions backend/server/routers/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,46 +133,33 @@ def term_sums_even(data: PlannerData) -> bool:
Check that the sum of the course codes in even terms is even
"""
is_even: Callable[[int], bool] = lambda x: x % 2 == 0
print("Checking even")
for y, year in enumerate(data.plan):
# Exclude summer term + odd terms
for i, term in enumerate(year[2::2], 2):
term_sum = sum(map(get_code, term.keys()))
print(f"{y}T{i} sum: {term_sum}")
if not is_even(term_sum):
print("failed: ", term)
return False

return True
return all(
is_even(sum(map(get_code, term.keys())))
for year in data.plan[::2]
for term in year[1::2]
)

# TODO
def term_sums_odd(data: PlannerData) -> bool:
"""
Check that the sum of the course codes in odd terms is odd
"""
is_odd: Callable[[int], bool] = lambda x: x % 2 == 1
print("Checking odd")
for year in data.plan[::2]:
# Exclude summer term + even terms
for term in year[1::2]:
term_sum = sum(map(get_code, term.keys()))
if not is_odd(term_sum):
print("failed: ", term)
return False
return True
return all(
is_odd(sum(map(get_code, term.keys())))
for year in data.plan[::2]
for term in year[1::2]
)

def comp1511_marks(data: PlannerData) -> bool:
"""
Ollie must achieve a mark of 100 in COMP1511 to keep his scholarship
"""
for year in data.plan:
for term in year:
for course in term:
_, marks = term[course] # type: ignore
if course == "COMP1511":
return marks == 100

return False
return any(
marks == 100 and course == "COMP1511"
for year in data.plan
for term in year
for (course, (_, marks)) in term.items() # type: ignore
)


def gen_ed_sum(data: PlannerData) -> bool:
Expand Down Expand Up @@ -204,12 +191,13 @@ def math_limit(data: PlannerData) -> bool:
In your N-th year, you can only take N + 1 math courses
"""
for i, year in enumerate(data.plan, 1):
num_math = len([
course
# Use sum(1, ...) instead of len to avoid dual allocation
num_math = sum(
1
for term in year
for course in term
if course.startswith("MATH")
])
)
if num_math > i + 1:
return False

Expand All @@ -228,15 +216,16 @@ def comp1531_third_year(data: PlannerData) -> bool:
COMP1531 must be taken in the third year
"""
third_year = data.plan[2]
for term in third_year:
for course in term:
if course == "COMP1531":
return True

return False
return any(
course == "COMP1531"
for term in third_year
for course in term
)

# (validator_func, message, Optional<flag>)
requirements: list[tuple[Callable[[PlannerData], bool], str, Optional[str]]] = [
ValidatorFn = Callable[[PlannerData], bool]
ObjectiveMessage = str
Flag = str
requirements: list[tuple[ValidatorFn, ObjectiveMessage, Optional[Flag]]] = [
# Challenge 1
(hard_requirements, "Before you can submit, you must check that you are in a 3 year CS degree and have a math minor", None),
(summer_course, "Ollie must take one summer COMP course.", None),
Expand All @@ -255,7 +244,7 @@ def comp1531_third_year(data: PlannerData) -> bool:
]

@router.post("/validateCtf/")
def validate_ctf(data : PlannerData):
def validate_ctf(data: PlannerData):
"""
Validates the CTF
"""
Expand All @@ -270,10 +259,11 @@ def validate_ctf(data : PlannerData):
"flags": flags,
"message": msg
}

passed.append(msg)
if flag is not None:
flags.append(flag)
print("Ok: ", req_num)

return {
"valid": True,
"failed": -1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint-disable */
import React from 'react';
import { useSelector } from 'react-redux';
import { Typography } from 'antd';
import axios from 'axios';
import styled from 'styled-components';
import prepareCoursesForValidationPayload from 'utils/prepareCoursesForValidationPayload';
import { RootState } from 'config/store';
import CS from '../common/styles';
import S from './styles';
import { Typography } from 'antd';
import styled from 'styled-components';

type CtfResult = {
valid: boolean;
Expand All @@ -30,30 +29,27 @@ const loadingResult: CtfResult = {
failed: 0,
passed: [],
message: 'Loading...',
flags: [],
flags: []
};

const ModalTitle = styled(Title)`
margin: 0 !important;
color: ${({ theme }) => theme.text} !important;
`;


const ValidateCtfButton = () => {
const planner = useSelector((state: RootState) => state.planner);
const degree = useSelector((state: RootState) => state.degree);
const [open, setOpen] = React.useState(false);
const [result, setResult] = React.useState(loadingResult);

const validateCtf = async () => {
// TODO: Call this async and disaplay output
setOpen(true);
const res = await axios.post<CtfResult>(
'/ctf/validateCtf/',
prepareCoursesForValidationPayload(planner, degree, false)
);
setResult(res.data);
console.log(res.data);
};

return (
Expand All @@ -68,38 +64,33 @@ const ValidateCtfButton = () => {
>
<ModalTitle level={4}>Passed Challenges</ModalTitle>
<TextBlock>
{
<ol>
{result.passed.map((challenge, index) => (
<>
<li>
<TextBlock key={index}>{challenge}</TextBlock>
</li>
</>
))}
</ol>
}
<ol>
{result.passed.map((challenge) => (
<li key={challenge}>
<TextBlock>{challenge}</TextBlock>
</li>
))}
</ol>
</TextBlock>

<ModalTitle level={4}>Unlocked Flags</ModalTitle>
<TextBlock>
<ol>
{(result.flags.length > 0) && result.flags.map((flag, index) => (
<>
<li>
<TextBlock key={index}>{flag}</TextBlock>
{result.flags.length > 0 &&
result.flags.map((flag) => (
<li key={flag}>
<TextBlock>{flag}</TextBlock>
</li>
</>
))}
))}
</ol>
</TextBlock>

<ModalTitle level={4}>
{
result.valid ? (
<Text type="success">{result.message}</Text>
) : (
<Text type="warning">Next: {result.message}</Text>
)
}
{result.valid ? (
<Text type="success">{result.message}</Text>
) : (
<Text type="warning">Next: {result.message}</Text>
)}
</ModalTitle>
</S.Modal>
</>
Expand Down

0 comments on commit d9b290d

Please sign in to comment.