Skip to content

Commit

Permalink
Merge pull request #16 from encryptedge/feat/discord-blood
Browse files Browse the repository at this point in the history
feat: added first blood notifications to discord
  • Loading branch information
BRAVO68WEB authored Jan 22, 2024
2 parents 534d3f4 + ef9bb4b commit 9fa0c16
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/api/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ZodEnvironmentVariables = z.object({
MAIL_LOGGER: z.string(),
MAIL_FROM_EMAIL: z.string(),
MAIL_FROM_NAME: z.string(),
DISCORD_BLOOD_HOOK: z.string(),
UPSTASH_REDIS_REST_URL: z.string(),
UPSTASH_REDIS_REST_TOKEN: z.string(),
});
Expand Down
43 changes: 43 additions & 0 deletions packages/api/helpers/notifyBlood.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import axios from "axios";

export const notifyForBlood = async (
{
teamName,
challengeName,
}: {
teamName: string;
challengeName: string;
}
) => {
try {
const res = await axios.post(process.env.DISCORD_BLOOD_HOOK, {
content: "@here",
tts: false,
embeds: [
{
id: Date.now().toString(),
description: "",
fields: [],
title: `🎉 ${teamName} owned first blood 🩸 for ${challengeName}`,
color: 7_782_594,
footer: {
icon_url: "https://rcs.encryptedge.in/logo.png",
text: "eeCTF"
},
author: {
name: "RCS CTF 2024"
}
}
],
components: [],
actions: {},
username: "eeCTF Blood Notify"
}
);
return res.data;
} catch (error) {
if(error instanceof Error) {
throw new TypeError(error.message);
}
}
};
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@graphql-codegen/typescript-operations": "^4.0.1",
"@hono/node-server": "^1.3.2",
"@upstash/redis": "^1.28.1",
"axios": "^1.6.2",
"dotenv": "^16.3.1",
"eslint": "^8.53.0",
"graphql-request": "^6.1.0",
Expand Down
43 changes: 38 additions & 5 deletions packages/api/services/challenges.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ import { SnowflakeId } from "hyperflake";

import { Challenges_Insert_Input, Machines_Insert_Input, Mutation_Root, Query_Root } from "../graphql/types";
import { client } from "../helpers/gqlClient";
import { notifyForBlood } from "../helpers/notifyBlood";

const snowflake = SnowflakeId();

interface IBloodCheck extends Query_Root {
blood_check: {
challenge_id: string;
submission: {
submited_flag: string;
};
}[];
}

export class ChallengeService {
public createMachineS = async (reqBody: IMachineCreateInput) => {
try {
Expand Down Expand Up @@ -164,6 +174,7 @@ export class ChallengeService {

public submitFlagS = async (reqBody: IMachineSubmitFlagInput) => {
try {
let is_blood_aquired = false;
const { challenge_id, submited_flag, team_id, user_id } = reqBody;

if (!challenge_id || !submited_flag || !team_id || !user_id) {
Expand All @@ -175,14 +186,25 @@ export class ChallengeService {
scores(where: {challenge_id: {_eq: $challenge_id}, team_id: { _eq: $team_id }}) {
id
}
blood_check: scores(where: {challenge_id: {_eq: $challenge_id}}) {
challenge_id
submission {
submited_flag
}
}
}
`;

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

if(blood_check.length > 0){
is_blood_aquired = true;
}

if(scores.length > 0){
throw new Error("Already solved");
}
Expand Down Expand Up @@ -236,6 +258,12 @@ export class ChallengeService {
id
team_id
user_id
team {
name
}
challenge {
name
}
}
}
`;
Expand All @@ -252,6 +280,13 @@ export class ChallengeService {
throw new Error("Failed to submit score");
}

if(!is_blood_aquired){
notifyForBlood({
teamName: insert_scores_one.team.name,
challengeName: insert_scores_one.challenge.name,
});
}

return {
message: "GG!"
};
Expand Down Expand Up @@ -369,10 +404,8 @@ export class ChallengeService {
for (const machine of challengeCollection) {
if(machine.depends_on) {
const depends_on_machine = challengeCollection.find(challenge => challenge.id === machine.depends_on);
if(depends_on_machine) {
if(depends_on_machine.challenges?.length !== depends_on_machine.total_challenges) {
challengeCollection.splice(challengeCollection.indexOf(machine), 1);
}
if(depends_on_machine && depends_on_machine.challenges?.length !== depends_on_machine.total_challenges) {
challengeCollection.splice(challengeCollection.indexOf(machine), 1);
}
}
}
Expand Down
45 changes: 24 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9fa0c16

Please sign in to comment.