Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/api/controllers/challenges.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ export class ChallengeController extends ChallengeService {
}
}

public getProgress = async (ctx: Context) => {
try {
const userTeamID = await ctx.get("team_id");
const data = await this.getTeamProgressS(userTeamID);
return ctx.json(data);
}
catch (error: any) {
return ctx.json({
error: error.message
});
}
}

public sumbitFlag = async (ctx: Context) => {
try {
const reqBody = await ctx.req.json();
Expand Down
30 changes: 30 additions & 0 deletions packages/api/controllers/stats.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Context } from "hono";

import { StatsService } from "../services/stats.service";

export class StatsControtoller extends StatsService {
public getTeamScore = async (ctx: Context) => {
try {
const teamId = ctx.get("team_id");
const data = await this.getTeamScoreS(teamId);
return ctx.json(data);
}
catch (error: any) {
return ctx.json({
error: error.message
});
}
};

public getAllTeamsScore = async (ctx: Context) => {
try {
const data = await this.getAllTeamScoreS();
return ctx.json(data);
}
catch (error: any) {
return ctx.json({
error: error.message
});
}
}
}
24 changes: 22 additions & 2 deletions packages/api/controllers/teams.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,30 @@ export class TeamController extends TeamService {
}
}

public getProgress = async (ctx: Context) => {
public leaveTeam = async (ctx: Context) => {
try {
const user = ctx.get("user");
const data = await this.leaveTeamS({
user_id: user.id
});
return ctx.json(data);
}
catch (error: any) {
return ctx.json({
error: error.message
});
}
}

public editTeam = async (ctx: Context) => {
try {
const userTeamID = await ctx.get("team_id");
const data = await this.getTeamProgressS(userTeamID);
const reqBody = await ctx.req.json();
const data = await this.EditTeamInfo({
id: userTeamID,
team_name: reqBody.team_name,
join_code: reqBody.join_code
});
return ctx.json(data);
}
catch (error: any) {
Expand Down
2 changes: 2 additions & 0 deletions packages/api/routes/challenges.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ const challengeController = new ChallengeController();
challengeRouter.post("/manage/machine", authMiddleware.authenticate(AUTH_PERMS.ADMIN), challengeController.createMachine);
challengeRouter.get("/manage/machine/:machineId", authMiddleware.authenticate(AUTH_PERMS.ADMIN), challengeController.getMachineById);
challengeRouter.get("/manage/machines", authMiddleware.authenticate(AUTH_PERMS.ADMIN), challengeController.getMachines);

challengeRouter.get("/progress", authMiddleware.authenticate(AUTH_PERMS.TEAM_MEMBER), challengeController.getProgress);
challengeRouter.put("/submit/:challengeId", authMiddleware.authenticate(AUTH_PERMS.TEAM_MEMBER), challengeController.sumbitFlag);
12 changes: 9 additions & 3 deletions packages/api/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Hono } from "hono";

import { challengeRouter } from "./challenges.routes";
import { userRoutes } from "./users.routes";
import { statsRouter } from "./stats.routes";
import { teamRouter } from "./teams.routes";
import { userRoutes } from "./users.routes";

export const routes = new Hono();

routes.get("/", ctx => ctx.text("Shushhhhhh! Dont look here. ;)"));

routes.route("/user", userRoutes);
routes.route("/challenge", challengeRouter);
routes.route("/stats", statsRouter);
routes.route("/team", teamRouter);
routes.route("/challenge", challengeRouter);
routes.route("/user", userRoutes);

routes.get("/health", ctx => ctx.text("OK"));
routes.get("/flag.txt", ctx => ctx.text("flag{th1s_1s_n0t_th3_fl4g}"));
routes.get("*", ctx => ctx.text("Are you lost?"))
12 changes: 10 additions & 2 deletions packages/api/routes/stats.routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Hono } from "hono";

const router = new Hono();
import { AuthMiddleware } from "../middlewares/auth.middleware";
import { AUTH_PERMS } from "../extras/permissions";
import { StatsControtoller } from "../controllers/stats.controller";

export default router;
export const statsRouter = new Hono();

const authMiddleware = new AuthMiddleware();
const statsController = new StatsControtoller();

statsRouter.get("/team", authMiddleware.authenticate(AUTH_PERMS.TEAM_MEMBER), statsController.getTeamScore);
statsRouter.get("/leaderboard", authMiddleware.authenticate(AUTH_PERMS.AUTHENTICATED), statsController.getAllTeamsScore);
16 changes: 8 additions & 8 deletions packages/api/routes/teams.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { TeamController } from "../controllers/teams.controller";
import { AUTH_PERMS } from "../extras/permissions";

export const teamRouter = new Hono();
const auth = new AuthMiddleware();
const controller = new TeamController();
const authMiddleware = new AuthMiddleware();
const teamController = new TeamController();

teamRouter.post("/", auth.authenticate(AUTH_PERMS.AUTHENTICATED), controller.createTeam);
teamRouter.get("/", auth.authenticate(AUTH_PERMS.AUTHENTICATED), controller.getTeams);
teamRouter.get("/whoami", auth.authenticate(AUTH_PERMS.TEAM_MEMBER), controller.whoami);
teamRouter.get("/progress", auth.authenticate(AUTH_PERMS.TEAM_MEMBER), controller.getProgress);
teamRouter.get("/:teamId", auth.authenticate(AUTH_PERMS.AUTHENTICATED), controller.getTeamById);
teamRouter.post("/join", auth.authenticate(AUTH_PERMS.AUTHENTICATED), controller.joinTeam);
teamRouter.post("/", authMiddleware.authenticate(AUTH_PERMS.AUTHENTICATED), teamController.createTeam);
teamRouter.get("/", authMiddleware.authenticate(AUTH_PERMS.AUTHENTICATED), teamController.getTeams);
teamRouter.get("/whoami", authMiddleware.authenticate(AUTH_PERMS.TEAM_MEMBER), teamController.whoami);
teamRouter.put("/edit", authMiddleware.authenticate(AUTH_PERMS.TEAM_MEMBER), teamController.editTeam);
teamRouter.get("/i/:teamId", authMiddleware.authenticate(AUTH_PERMS.AUTHENTICATED), teamController.getTeamById);
teamRouter.post("/join", authMiddleware.authenticate(AUTH_PERMS.AUTHENTICATED), teamController.joinTeam);
12 changes: 6 additions & 6 deletions packages/api/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { AuthMiddleware } from "../middlewares/auth.middleware";
import { AUTH_PERMS } from "../extras/permissions";

export const userRoutes = new Hono();
const contoller = new UserController();
const auth = new AuthMiddleware();
const userContoller = new UserController();
const authMiddleware = new AuthMiddleware();

userRoutes.post("/register", contoller.registerUser);
userRoutes.post("/verify", contoller.verifyUser);
userRoutes.post("/login", contoller.loginUser);
userRoutes.get("/whoami", auth.authenticate(AUTH_PERMS.AUTHENTICATED), contoller.whoami);
userRoutes.post("/register", userContoller.registerUser);
userRoutes.post("/verify", userContoller.verifyUser);
userRoutes.post("/login", userContoller.loginUser);
userRoutes.get("/whoami", authMiddleware.authenticate(AUTH_PERMS.AUTHENTICATED), userContoller.whoami);
113 changes: 110 additions & 3 deletions packages/api/services/challenges.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ export class ChallengeService {
}

const queryPreCheck = gql`
query checkScore($challenge_id: String!, $user_id: String!) {
scores(where: {challenge_id: {_eq: $challenge_id}, user_id: {_eq: $user_id}}) {
query checkScore($challenge_id: String!, $team_id: String!) {
scores(where: {challenge_id: {_eq: $challenge_id}, team_id: { _eq: $team_id }}) {
id
}
}
`;

const { scores } : Query_Root = await client.request(queryPreCheck, {
challenge_id,
user_id,
team_id,
});

if(scores.length > 0){
Expand Down Expand Up @@ -264,4 +264,111 @@ export class ChallengeService {
throw new Error(error.message);
}
}

public getTeamProgressS = (team_id: string) => {
return new Promise(async (resolve, reject) => {
try {
const query = gql`
query getTeamProgress($team_id: String!) {
machines {
id
name
description
challenges {
id
name
point
description
}
}

scores(where: {
team_id: {
_eq: $team_id
}
}) {
id
challenge_id
team_id
user_id
challenge {
id
name
point
machine {
id
name
}
}
}
}
`;

const data: Query_Root = await client.request(query, {
team_id
});

if(data.machines && data.scores) {
const challengeCollection : IParedMachineProgress[] = []

for(const machine of data.machines) {
const machineProgress: IParedMachineProgress = {
id: machine.id,
name: machine.name,
description: machine.description,
challenges: []
}

for(const challenge of machine.challenges) {
const challengeProgress : IParsedChallengeProgress = {
id: challenge.id,
name: challenge.name,
point: challenge.point,
description: challenge.description,
solved: false
}

for(const score of data.scores) {
if(score.challenge_id === challenge.id) {
challengeProgress.solved = true;
break;
}
}

machineProgress.challenges?.push(challengeProgress);
}

challengeCollection.push(machineProgress);
}

for (const machine of challengeCollection) {
const solvedChallenges = machine.challenges?.filter(challenge => challenge.solved);
const unsolvedChallenges = machine.challenges?.filter(challenge => !challenge.solved);

if(solvedChallenges?.length) {
machine.challenges = solvedChallenges;
if(unsolvedChallenges?.length) {
machine.challenges.push(unsolvedChallenges[0]);
}
}
else {
machine.challenges = [machine.challenges![0]];
}
}

resolve(challengeCollection);
}
else {
throw new Error("No data found");
}
}
catch (error: any) {
if(error.response) {
reject(error.response.errors[0].message);
}
else
reject(error.message);
}
});
}
}
Loading