summary refs log tree commit diff stats
path: root/lingo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lingo.cpp')
-rw-r--r--lingo.cpp59
1 files changed, 56 insertions, 3 deletions
diff --git a/lingo.cpp b/lingo.cpp index 0e8720e..e177b73 100644 --- a/lingo.cpp +++ b/lingo.cpp
@@ -1,6 +1,10 @@
1#include <dpp/dpp.h> 1#include <dpp/dpp.h>
2#include <random> 2#include <random>
3#include <yaml-cpp/yaml.h> 3#include <yaml-cpp/yaml.h>
4#include <curl_easy.h>
5#include <curl_pair.h>
6#include <curl_form.h>
7#include <curl_exception.h>
4#include <iostream> 8#include <iostream>
5#include <memory> 9#include <memory>
6#include <thread> 10#include <thread>
@@ -278,9 +282,10 @@ public:
278 282
279 bot_->on_message_create([this](const dpp::message_create_t& event) { 283 bot_->on_message_create([this](const dpp::message_create_t& event) {
280 std::lock_guard answer_lock(answers_mutex_); 284 std::lock_guard answer_lock(answers_mutex_);
281 if (answer_by_message_.count(static_cast<uint64_t>(event.msg.message_reference.message_id))) 285 uint64_t puzzle_id = static_cast<uint64_t>(event.msg.message_reference.message_id);
286 if (answer_by_message_.count(puzzle_id))
282 { 287 {
283 std::string canonical_answer = hatkirby::lowercase(answer_by_message_[event.msg.message_reference.message_id]); 288 std::string canonical_answer = hatkirby::lowercase(answer_by_message_[puzzle_id]);
284 std::string canonical_attempt = hatkirby::lowercase(event.msg.content); 289 std::string canonical_attempt = hatkirby::lowercase(event.msg.content);
285 while (canonical_attempt.find("||") != std::string::npos) 290 while (canonical_attempt.find("||") != std::string::npos)
286 { 291 {
@@ -290,7 +295,49 @@ public:
290 std::cout << "\"" << canonical_attempt << "\"" << std::endl; 295 std::cout << "\"" << canonical_attempt << "\"" << std::endl;
291 if (canonical_attempt == canonical_answer) 296 if (canonical_attempt == canonical_answer)
292 { 297 {
293 bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "✅"); 298 if (solved_puzzles_.count(puzzle_id))
299 {
300 bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "✅");
301 } else {
302 bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "🎉");
303 solved_puzzles_.insert(puzzle_id);
304
305 // Submit the score to the scoreboard.
306 curl::curl_form form;
307 curl::curl_easy easy;
308
309 std::string avatar_url = event.msg.author.get_avatar_url();
310 easy.escape(avatar_url);
311
312 // Forms creation
313 curl::curl_pair<CURLformoption,std::string> username_form(CURLFORM_COPYNAME,"username");
314 curl::curl_pair<CURLformoption,std::string> username_cont(CURLFORM_COPYCONTENTS,event.msg.author.username);
315 curl::curl_pair<CURLformoption,std::string> pass_form(CURLFORM_COPYNAME,"user_id");
316 curl::curl_pair<CURLformoption,std::string> pass_cont(CURLFORM_COPYCONTENTS,std::to_string(static_cast<uint64_t>(event.msg.author.id)));
317 curl::curl_pair<CURLformoption,std::string> av_form(CURLFORM_COPYNAME,"avatar_url");
318 curl::curl_pair<CURLformoption,std::string> av_cont(CURLFORM_COPYCONTENTS,avatar_url);
319 curl::curl_pair<CURLformoption,std::string> code_form(CURLFORM_COPYNAME,"secret_code");
320 curl::curl_pair<CURLformoption,std::string> code_cont(CURLFORM_COPYCONTENTS,scoreboard_secret_code_);
321
322 try {
323 // Form adding
324 form.add(username_form,username_cont);
325 form.add(pass_form,pass_cont);
326 form.add(av_form,av_cont);
327 form.add(code_form,code_cont);
328
329 // Add some options to our request
330 easy.add<CURLOPT_URL>(scoreboard_endpoint_.c_str());
331 easy.add<CURLOPT_SSL_VERIFYPEER>(false);
332 easy.add<CURLOPT_HTTPPOST>(form.get());
333 // Execute the request.
334 easy.perform();
335
336 } catch (curl::curl_easy_exception &error) {
337 // Otherwise we could print the stack like this:
338 error.print_traceback();
339 }
340 }
294 } else { 341 } else {
295 bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "❌"); 342 bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "❌");
296 } 343 }
@@ -305,6 +352,9 @@ public:
305 database_ = std::make_unique<verbly::database>(config["verbly_datafile"].as<std::string>()); 352 database_ = std::make_unique<verbly::database>(config["verbly_datafile"].as<std::string>());
306 imagenet_ = std::make_unique<imagenet>(config["imagenet"].as<std::string>()); 353 imagenet_ = std::make_unique<imagenet>(config["imagenet"].as<std::string>());
307 354
355 scoreboard_endpoint_ = config["scoreboard_endpoint"].as<std::string>();
356 scoreboard_secret_code_ = config["scoreboard_secret_code"].as<std::string>();
357
308 for (;;) 358 for (;;)
309 { 359 {
310 std::thread genpuzz(&lingo::generatePuzzle, this, channel); 360 std::thread genpuzz(&lingo::generatePuzzle, this, channel);
@@ -510,7 +560,10 @@ private:
510 std::unique_ptr<verbly::database> database_; 560 std::unique_ptr<verbly::database> database_;
511 std::unique_ptr<imagenet> imagenet_; 561 std::unique_ptr<imagenet> imagenet_;
512 std::map<uint64_t, std::string> answer_by_message_; 562 std::map<uint64_t, std::string> answer_by_message_;
563 std::set<uint64_t> solved_puzzles_;
513 std::mutex answers_mutex_; 564 std::mutex answers_mutex_;
565 std::string scoreboard_endpoint_;
566 std::string scoreboard_secret_code_;
514}; 567};
515 568
516int main(int argc, char** argv) 569int main(int argc, char** argv)