diff --git a/.gitignore b/.gitignore index 7503717..0cc6ae0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ build # token config.json -/src/config.json \ No newline at end of file +/src/config.json +src/config.json \ No newline at end of file diff --git a/src/commands/project_cmd.cpp b/src/commands/project_cmd.cpp index 8a08412..bfe080a 100644 --- a/src/commands/project_cmd.cpp +++ b/src/commands/project_cmd.cpp @@ -1,15 +1,100 @@ #include "commands.h" #include "../globals/globals.h" +using json = nlohmann::json; + void cmd::projectCommand(dpp::cluster& bot, const dpp::slashcommand_t& event) { - static int index; - const std::string project = cmd::utils::readFileLine("res/project.txt", index); + static int index = -1; + index++; + + std::ifstream projectFile("res/project.json"); + if (!projectFile.is_open()) + { + event.reply("Failed to open project file."); + return; + } + + json data; + try + { + projectFile >> data; + } + catch (const json::parse_error& e) + { + event.reply("Failed to parse project file."); + return; + } + + if (!data.contains("projects") || !data["projects"].is_array() || index >= data["projects"].size()) + { + event.reply("Invalid project data or index out of bounds."); + return; + } + + const auto& project = data["projects"][index]; + if (!project.contains("title") || !project.contains("description")) + { + event.reply("Project data is missing required fields."); + return; + } + + const std::string projectTitle = project["title"]; + const std::string projectDescription = project["description"]; + const std::string projectHint = project.contains("hint") ? project["hint"] : "No hint available."; + dpp::embed embed = dpp::embed() .set_color(globals::color::defaultColor) - .add_field(project, ""); + .add_field("Project Idea", projectTitle) + .add_field("Description", projectDescription); dpp::message message(event.command.channel_id, embed); + + // Add hint button with index as custom_id + message.add_component( + dpp::component().add_component( + dpp::component() + .set_label("Hint") + .set_type(dpp::cot_button) + .set_style(dpp::cos_primary) + .set_id("hint_button_" + std::to_string(index)) + ) + ); + event.reply(message); + + bot.on_button_click([&bot, &data](const dpp::button_click_t& event) { + // Ignore if button id does not start with hint_button_ + if (event.custom_id.rfind("hint_button_", 0) != 0) + return; + + // Remove button + dpp::message updatedMsg = event.command.get_context_message(); + updatedMsg.components.clear(); + bot.message_edit(updatedMsg); + + json data; + try + { + std::ifstream projectFile("res/project.json"); + projectFile >> data; + } + catch (const json::parse_error& e) + { + event.reply("Failed to parse project file."); + return; + } + + const int hintButtonIndex = std::stoi(event.custom_id.substr(event.custom_id.rfind('_') + 1)); + const auto& project = data["projects"][hintButtonIndex]; + const std::string hint = project.contains("hint") ? project["hint"] : "No hint available."; + + dpp::embed hintEmbed = dpp::embed() + .set_color(globals::color::defaultColor) + .add_field("Hint", hint); + + dpp::message hintMessage(event.command.channel_id, hintEmbed); + event.reply(hintMessage); + }); } diff --git a/src/res/project.json b/src/res/project.json new file mode 100644 index 0000000..98b33ef --- /dev/null +++ b/src/res/project.json @@ -0,0 +1,166 @@ +{ + "projects": [ + { + "title": "Hello World! Program", + "description": "Create a program that prints `Hello, World!` to the screen.", + "hint": "Use the [std::cout](https://en.cppreference.com/w/cpp/io/cout) stream to print text to the console.", + "level": "Beginner" + }, + { + "title": "Numbers 1 to 10", + "description": "Display the numbers from 1 to 10 on the screen.", + "hint": "Use a `for` loop to iterate from 1 to 10 and print each number. [Learn more about for loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Square Pattern of Stars", + "description": "Print a square made of `*` symbols in a grid format.", + "hint": "Use nested `for` loops to print a square pattern. [Learn more about nested loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Sum of First 10 Numbers", + "description": "Calculate and display the sum of numbers from 1 to 10.", + "hint": "Use a `for` loop to iterate from 1 to 10 and accumulate the sum. [Learn about for loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Simple GUI Application", + "description": "Create a basic graphical user interface with text and buttons.", + "hint": "Consider using a GUI library like Qt or GTK. [Learn more about Qt](https://doc.qt.io/qt-5.15/index.html), [Learn about GTK](https://www.gtk.org/docs)", + "level": "Intermediate" + }, + { + "title": "Print Your Name Multiple Times", + "description": "Print your name 5 times on the screen.", + "hint": "Use a `for` loop to print your name multiple times. [Learn more about loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Even Numbers from 2 to 20", + "description": "Display even numbers starting from 2 up to 20.", + "hint": "Use a `for` loop with a step of 2 to print even numbers. [Learn about steps in loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Countdown from 10 to 1", + "description": "Create a countdown starting from 10 and ending at 1.", + "hint": "Use a `for` loop to iterate from 10 to 1 and print each number. [Learn more about loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Alphabet A to Z", + "description": "Print the alphabet from A to Z.", + "hint": "Use a `for` loop to iterate through ASCII values of letters. [Learn more about ASCII](https://www.asciitable.com/)", + "level": "Beginner" + }, + { + "title": "Triangle of Stars", + "description": "Print a triangle shape using `*` symbols.", + "hint": "Use nested `for` loops to print a triangle pattern. [Learn about nested loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Factorial Calculator", + "description": "Calculate the factorial of a given number.", + "hint": "Use a `for` loop to multiply numbers from 1 to the given number. [Learn about factorials](https://en.wikipedia.org/wiki/Factorial)", + "level": "Beginner" + }, + { + "title": "Even or Odd Number Checker", + "description": "Check if a number is even or odd.", + "hint": "Use the modulus operator `%` to check for even or odd. [Learn about modulus operator](https://www.geeksforgeeks.org/modulo-operator-in-c-cpp-with-examples/)", + "level": "Beginner" + }, + { + "title": "Multiplication Table", + "description": "Print the multiplication table for a given number.", + "hint": "Use nested `for` loops to print the multiplication table. [Learn about nested loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Greatest Common Divisor (GCD)", + "description": "Find the greatest common divisor of two numbers.", + "hint": "Use the Euclidean algorithm to find the GCD. [Learn more about GCD](https://en.wikipedia.org/wiki/Greatest_common_divisor)", + "level": "Intermediate" + }, + { + "title": "Palindrome Checker", + "description": "Check if a number is a palindrome.", + "hint": "Convert the number to a string and check if it reads the same backward. [Learn about palindromes](https://en.wikipedia.org/wiki/Palindrome)", + "level": "Intermediate" + }, + { + "title": "Fibonacci Sequence", + "description": "Generate the nth Fibonacci number.", + "hint": "Use a loop or recursion to generate Fibonacci numbers. [Learn about Fibonacci](https://en.wikipedia.org/wiki/Fibonacci_sequence)", + "level": "Intermediate" + }, + { + "title": "Celsius to Fahrenheit Converter", + "description": "Convert a temperature from Celsius to Fahrenheit.", + "hint": "Use the formula `F = C * 9/5 + 32` for conversion. [Learn more about temperature conversion](https://www.rapidtables.com/convert/temperature/celsius-to-fahrenheit.html)", + "level": "Beginner" + }, + { + "title": "Vowel Counter", + "description": "Count the number of vowels in a string.", + "hint": "Use a loop to iterate through the string and count vowels. [Learn more about string manipulation](https://en.cppreference.com/w/cpp/string/basic_string)", + "level": "Beginner" + }, + { + "title": "String Reversal", + "description": "Reverse a given string.", + "hint": "Use the [std::reverse](https://en.cppreference.com/w/cpp/algorithm/reverse) function or a loop to reverse the string.", + "level": "Beginner" + }, + { + "title": "Largest Number in an Array", + "description": "Find the largest number in an array.", + "hint": "Use a loop to iterate through the array and find the maximum value. [Learn about arrays](https://en.cppreference.com/w/cpp/container/array)", + "level": "Intermediate" + }, + { + "title": "Matrix Addition", + "description": "Add two matrices together.", + "hint": "Use nested loops to iterate through matrices and add corresponding elements. [Learn more about matrices](https://www.wikiwand.com/en/articles/Matrix)", + "level": "Intermediate" + }, + { + "title": "Bubble Sort", + "description": "Sort an array using the bubble sort algorithm.", + "hint": "Implement the bubble sort algorithm to sort the array. [Learn more about bubble sort](https://en.wikipedia.org/wiki/Bubble_sort)", + "level": "Intermediate" + }, + { + "title": "Prime Number Checker", + "description": "Check if a given number is prime.", + "hint": "Use a loop to check if the number has any divisors other than 1 and itself. [Learn about prime numbers](https://en.wikipedia.org/wiki/Prime_number)", + "level": "Intermediate" + }, + { + "title": "Basic Calculator", + "description": "Create a basic calculator that can add, subtract, multiply, and divide.", + "hint": "Use `switch` statements to perform different arithmetic operations. [Learn about switch statements](https://en.cppreference.com/w/cpp/language/switch)", + "level": "Intermediate" + }, + { + "title": "Random Number Generator", + "description": "Generate a random number between 1 and 100.", + "hint": "Use the `rand()` function and `srand()` to generate random numbers. [Learn more about random numbers](https://en.cppreference.com/w/cpp/numeric/random/rand)", + "level": "Beginner" + }, + { + "title": "Anagram Checker", + "description": "Check if a word is an anagram of another word.", + "hint": "Sort both words and compare if they are equal. [Learn more about anagrams](https://en.wikipedia.org/wiki/Anagram)", + "level": "Intermediate" + }, + { + "title": "Dice Rolling Simulator", + "description": "Simulate rolling a pair of dice.", + "hint": "Use the `rand()` function to generate random numbers between 1 and 6. [Learn about random numbers](https://en.cppreference.com/w/cpp/numeric/random/rand)", + "level": "Beginner" + } + ] +} diff --git a/src/res/project.txt b/src/res/project.txt deleted file mode 100644 index b9d82d0..0000000 --- a/src/res/project.txt +++ /dev/null @@ -1,74 +0,0 @@ -Print "Hello, World!" to the screen. -Display numbers 1 to 10. -Print a square of * symbols. -Calculate the sum of numbers 1 to 10. -Create a simple GUI (e.g. with text and buttons). -Print your name 5 times. -Print even numbers from 2 to 20. -Create a simple countdown from 10 to 1. -Print the alphabet (A to Z). -Print a triangle of * symbols. -Calculate the factorial of a number. -Check if a number is even or odd. -Display the multiplication table for a number. -Find the greatest common divisor (GCD) of two numbers. -Check if a number is a palindrome. -Calculate the nth Fibonacci number. -Convert Celsius to Fahrenheit. -Count the number of vowels in a string. -Reverse a string. -Find the largest number in an array. -Create a program that adds two matrices. -Sort an array using bubble sort. -Check if a number is a prime number. -Create a basic calculator (add, subtract, multiply, divide). -Generate a random number between 1 and 100. -Check if a word is an anagram of another word. -Simulate rolling a pair of dice. -Print the first n prime numbers. -Draw a rectangle of * symbols. -Find the sum of digits in a number. -Calculate the area of a circle given its radius. -Create a program that generates a username from a name. -Implement a basic guessing game. -Print the elements of a 2D array. -Create a stopwatch timer. -Count the occurrences of a character in a string. -Print Pascal’s triangle. -Calculate the power of a number (a^b). -Create a program to simulate a coin toss. -Sort an array using insertion sort. -Convert a binary number to decimal. -Implement a simple to-do list. -Check if a year is a leap year. -Print the sum of a series, e.g., 1 + 1/2 + 1/3 + ... + 1/n. -Implement a simple login system. -Convert a decimal number to binary. -Display a pattern like a diamond using *. -Find the second largest number in an array. -Check if a string is a palindrome. -Implement a Caesar cipher for text encryption. -Find the sum of an array. -Create a program to simulate a simple game of Rock, Paper, Scissors. -Calculate compound interest. -Create a simple inventory management system. -Print the border of a square with *. -Display the current system date and time. -Print a spiral pattern of numbers. -Calculate the average of numbers in an array. -Find the maximum and minimum of numbers in an array. -Implement a program to check for armstrong numbers. -Create a Tic-Tac-Toe game for two players. -Count the words in a sentence. -Implement a simple stopwatch. -Print the ASCII values of characters in a string. -Create a program to find all divisors of a number. -Convert a number from hexadecimal to decimal. -Generate the multiplication table for 1 to 10. -Find the LCM of two numbers. -Check if a string contains only digits. -Implement a number guessing game. -Create a hangman game with a predefined word. -Count the occurrences of each word in a sentence. -Generate all permutations of a string. -Implement a basic text-based menu system.