diff --git a/html/index.html b/html/index.html index ff319bc..2b27778 100644 --- a/html/index.html +++ b/html/index.html @@ -10,10 +10,26 @@ - - + --> + +

Défi :

+ +
+ + + + +
+
+ + + + + + diff --git a/html/monscript.js b/html/monscript.js new file mode 100644 index 0000000..0c91658 --- /dev/null +++ b/html/monscript.js @@ -0,0 +1,25 @@ +document.getElementById(""); + +// src="data:audio/ogg;base64,BASE64CODE" + +function updateAudios(response) { + document.getElementById("one").src = "data:audio/ogg;base64," + response.dataOne; + document.getElementById("two").src = "data:audio/ogg;base64," + response.dataTwo; + document.getElementById("three").src = "data:audio/ogg;base64," + response.dataThree; + document.getElementById("four").src = "data:audio/ogg;base64," + response.dataFour; + document.getElementById("answer").innerText = "Réponse : " + response.answer; + + const select = document.createElement("select"); + response.answers.forEach(element => { + const option = document.createElement("option"); + option.innerText = element; + select.append(option); + }); + document.getElementById("content").append(select); +} + +fetch("/newchallenge", { + method: "POST", +}) +.then((response) => response.json()) +.then((json) => updateAudios(json)); diff --git a/src/main.cpp b/src/main.cpp index f1d2846..13e16d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,22 +5,26 @@ #include #include -static const std::vector links = { - "2gQ--SCEfmI", - "9xBeLSXOvSY", - "gv7QwZqMFYc", - "tFRStkEkhbY", - "OecoHvuxkxY", - "7SVKgjhpTHY", - "KF63Z27NPTE", - "7fKPxTbFt88", - "ZccDikDUHmA", - "PjnHPuEUbjU" -}; +#include +#include +#include + +static const std::map links = { + {"2gQ--SCEfmI", "What I Want"}, + {"9xBeLSXOvSY", "Drunk"}, + {"gv7QwZqMFYc", "Can't Wait"}, + {"tFRStkEkhbY", "Lazy"}, + {"OecoHvuxkxY", "Sunburn"}, + {"7SVKgjhpTHY", "Long Time Friends"}, + {"KF63Z27NPTE", "zero_one"}, + {"7fKPxTbFt88", "Animal"}, + {"ZccDikDUHmA", "Fly Home"}, + {"PjnHPuEUbjU", "Chosen"}}; static const std::string YTDLP_BINARY = "/home/simon/.local/bin/yt-dlp"; -std::string GetAudioPath(const std::string& id) { +std::string GetAudioPath(const std::string &id) +{ return "audio/" + id + ".ogg"; } @@ -36,17 +40,14 @@ void DownloadFile(const std::string &id) ep.run(); } -void DownloadFiles() { - for (const std::string& id : links) { +void DownloadFiles() +{ + for (const auto &[id, title] : links) + { DownloadFile(id); } } -/** - * std::vector args = {"/usr/bin/ffmpeg", "-hide_banner", "-loglevel", "panic", "-i", "pipe:0", "-b:a", "32k", "-map", - "a", "-c:a", "libopus", "-f", "ogg", "-y", "pipe:1"}; - */ - float GetDuration(const std::string &id) { stx::ExecPipe ep; @@ -77,40 +78,93 @@ float GetRandomReal(float min, float max) return distrib(generator); } -void ExtractAudio(const std::string &id, float begin, float end, const std::string& outputName) +std::string ExtractAudio(const std::string &id, float begin, float end) { stx::ExecPipe ep; - std::vector args = {"/usr/bin/ffmpeg", "-y", "-loglevel", "panic", "-i", GetAudioPath(id), "-ss", std::to_string(begin), "-to", std::to_string(end), outputName}; + std::vector args = {"/usr/bin/ffmpeg", "-y", "-loglevel", "panic", "-i", GetAudioPath(id), "-ss", std::to_string(begin), "-to", std::to_string(end), "-f", "ogg", "pipe:1"}; ep.add_exec(&args); + + std::string output; + ep.set_output_string(&output); + ep.run(); + + return output; } -std::string GetRandomLink() { - return links.at(rand() % links.size()); +std::pair GetRandomLink() +{ + auto it = links.begin(); + std::advance(it, rand() % links.size()); + return *it; } -void GenRandomChallenge() { - std::string randomLink = GetRandomLink(); +struct Challenge +{ + std::string one, two, three, four; + std::string answer; +}; + +Challenge GenRandomChallenge() +{ + Challenge challenge; + auto [randomLink, randomTitle] = GetRandomLink(); float duration = GetDuration(randomLink); float randomSeek = GetRandomReal(duration / 4.0f, duration * 3.0f / 4.0f); - ExtractAudio(randomLink, randomSeek, randomSeek + 0.5f, "challenges/1.ogg"); - ExtractAudio(randomLink, randomSeek, randomSeek + 1.0f, "challenges/2.ogg"); - ExtractAudio(randomLink, randomSeek, randomSeek + 1.5f, "challenges/3.ogg"); - ExtractAudio(randomLink, randomSeek, randomSeek + 2.0f, "challenges/4.ogg"); + challenge.answer = randomTitle; + challenge.one = ExtractAudio(randomLink, randomSeek, randomSeek + 0.5f); + challenge.two = ExtractAudio(randomLink, randomSeek, randomSeek + 1.0f); + challenge.three = ExtractAudio(randomLink, randomSeek, randomSeek + 1.5f); + challenge.four = ExtractAudio(randomLink, randomSeek, randomSeek + 2.0f); std::cout << "Done !\n"; + + return challenge; } +std::string toBase64(const std::string &input) +{ + std::string result; + result.resize(tb64enclen(input.length())); + + tb64enc(reinterpret_cast(input.data()), input.size(), reinterpret_cast(result.data())); + + return result; +} + +std::vector GetPossibleAnswers() { + std::vector answers; + for (const auto& [link, title] : links) { + answers.push_back(title); + } + return answers; +} int main(int argc, char **argv) { srand(time(0)); - - GenRandomChallenge(); + + httplib::Server server; + server.Post("/newchallenge", [](const httplib::Request &req, httplib::Response &res){ + Challenge challenge = GenRandomChallenge(); + + nlohmann::json j; + j["answer"] = challenge.answer; + j["dataOne"] = toBase64(challenge.one); + j["dataTwo"] = toBase64(challenge.two); + j["dataThree"] = toBase64(challenge.three); + j["dataFour"] = toBase64(challenge.four); + j["answers"] = GetPossibleAnswers(); + + res.set_content(j.dump(), "application/json"); + }); + + server.set_mount_point("/", "html"); + server.listen("0.0.0.0", 8080); return 0; } diff --git a/xmake.lua b/xmake.lua index eda0c87..cdf4f26 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,78 +1,12 @@ add_rules("mode.debug", "mode.release") -set_languages("c++11") +set_languages("c++17") + +add_requires("cpp-httplib", "nlohmann_json", "turbobase64") target("Songdle") set_kind("binary") add_files("src/*.cpp") set_rundir(".") - --- --- If you want to known more usage about xmake, please see https://xmake.io --- --- ## FAQ --- --- You can enter the project directory firstly before building project. --- --- $ cd projectdir --- --- 1. How to build project? --- --- $ xmake --- --- 2. How to configure project? --- --- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release] --- --- 3. Where is the build output directory? --- --- The default output directory is `./build` and you can configure the output directory. --- --- $ xmake f -o outputdir --- $ xmake --- --- 4. How to run and debug target after building project? --- --- $ xmake run [targetname] --- $ xmake run -d [targetname] --- --- 5. How to install target to the system directory or other output directory? --- --- $ xmake install --- $ xmake install -o installdir --- --- 6. Add some frequently-used compilation flags in xmake.lua --- --- @code --- -- add debug and release modes --- add_rules("mode.debug", "mode.release") --- --- -- add macro definition --- add_defines("NDEBUG", "_GNU_SOURCE=1") --- --- -- set warning all as error --- set_warnings("all", "error") --- --- -- set language: c99, c++11 --- set_languages("c99", "c++11") --- --- -- set optimization: none, faster, fastest, smallest --- set_optimize("fastest") --- --- -- add include search directories --- add_includedirs("/usr/include", "/usr/local/include") --- --- -- add link libraries and search directories --- add_links("tbox") --- add_linkdirs("/usr/local/lib", "/usr/lib") --- --- -- add system link libraries --- add_syslinks("z", "pthread") --- --- -- add compilation and link flags --- add_cxflags("-stdnolib", "-fno-strict-aliasing") --- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true}) --- --- @endcode --- + add_packages("cpp-httplib", "nlohmann_json", "turbobase64")