From 0b4d3fbc200e646eed48a1a919dde9ee03678688 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 10 Dec 2022 11:38:57 -0500 Subject: Bot can submit to scoreboard now Also updated the scoreboard styling refs #13 --- lingo.cpp | 59 ++++++++++++++++++++++-- rails/app/assets/stylesheets/lingo/main.css.scss | 37 +++++++++++++-- rails/app/controllers/lingo/scores_controller.rb | 10 ++-- rails/app/views/lingo/scores/index.html.haml | 9 +++- 4 files changed, 104 insertions(+), 11 deletions(-) diff --git a/lingo.cpp b/lingo.cpp index 0e8720e..e177b73 100644 --- a/lingo.cpp +++ b/lingo.cpp @@ -1,6 +1,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include @@ -278,9 +282,10 @@ public: bot_->on_message_create([this](const dpp::message_create_t& event) { std::lock_guard answer_lock(answers_mutex_); - if (answer_by_message_.count(static_cast(event.msg.message_reference.message_id))) + uint64_t puzzle_id = static_cast(event.msg.message_reference.message_id); + if (answer_by_message_.count(puzzle_id)) { - std::string canonical_answer = hatkirby::lowercase(answer_by_message_[event.msg.message_reference.message_id]); + std::string canonical_answer = hatkirby::lowercase(answer_by_message_[puzzle_id]); std::string canonical_attempt = hatkirby::lowercase(event.msg.content); while (canonical_attempt.find("||") != std::string::npos) { @@ -290,7 +295,49 @@ public: std::cout << "\"" << canonical_attempt << "\"" << std::endl; if (canonical_attempt == canonical_answer) { - bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "✅"); + if (solved_puzzles_.count(puzzle_id)) + { + bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "✅"); + } else { + bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "🎉"); + solved_puzzles_.insert(puzzle_id); + + // Submit the score to the scoreboard. + curl::curl_form form; + curl::curl_easy easy; + + std::string avatar_url = event.msg.author.get_avatar_url(); + easy.escape(avatar_url); + + // Forms creation + curl::curl_pair username_form(CURLFORM_COPYNAME,"username"); + curl::curl_pair username_cont(CURLFORM_COPYCONTENTS,event.msg.author.username); + curl::curl_pair pass_form(CURLFORM_COPYNAME,"user_id"); + curl::curl_pair pass_cont(CURLFORM_COPYCONTENTS,std::to_string(static_cast(event.msg.author.id))); + curl::curl_pair av_form(CURLFORM_COPYNAME,"avatar_url"); + curl::curl_pair av_cont(CURLFORM_COPYCONTENTS,avatar_url); + curl::curl_pair code_form(CURLFORM_COPYNAME,"secret_code"); + curl::curl_pair code_cont(CURLFORM_COPYCONTENTS,scoreboard_secret_code_); + + try { + // Form adding + form.add(username_form,username_cont); + form.add(pass_form,pass_cont); + form.add(av_form,av_cont); + form.add(code_form,code_cont); + + // Add some options to our request + easy.add(scoreboard_endpoint_.c_str()); + easy.add(false); + easy.add(form.get()); + // Execute the request. + easy.perform(); + + } catch (curl::curl_easy_exception &error) { + // Otherwise we could print the stack like this: + error.print_traceback(); + } + } } else { bot_->message_add_reaction(event.msg.id, event.msg.channel_id, "❌"); } @@ -305,6 +352,9 @@ public: database_ = std::make_unique(config["verbly_datafile"].as()); imagenet_ = std::make_unique(config["imagenet"].as()); + scoreboard_endpoint_ = config["scoreboard_endpoint"].as(); + scoreboard_secret_code_ = config["scoreboard_secret_code"].as(); + for (;;) { std::thread genpuzz(&lingo::generatePuzzle, this, channel); @@ -510,7 +560,10 @@ private: std::unique_ptr database_; std::unique_ptr imagenet_; std::map answer_by_message_; + std::set solved_puzzles_; std::mutex answers_mutex_; + std::string scoreboard_endpoint_; + std::string scoreboard_secret_code_; }; int main(int argc, char** argv) diff --git a/rails/app/assets/stylesheets/lingo/main.css.scss b/rails/app/assets/stylesheets/lingo/main.css.scss index c5bdad0..a65cd94 100644 --- a/rails/app/assets/stylesheets/lingo/main.css.scss +++ b/rails/app/assets/stylesheets/lingo/main.css.scss @@ -1,11 +1,12 @@ body { background-color: black; color: white; + font-family: sans-serif; } #header { width: 100%; - + img { max-width: 80%; margin: 0 auto; @@ -15,10 +16,40 @@ body { h2 { text-align: center; - font-family: sans-serif; } #scores { - width: 50%; margin: 0 auto; + border-spacing: 0px; + tr { + &.even { + background-color: gray; + } + &.odd { + background-color: purple; + } + th { + text-align: left; + padding-left: 0.5em; + padding-bottom: 0.5em; + } + td { + padding-right: 1em; + padding-top: 0.5em; + padding-bottom: 0.5em; + border-collapse: collapse; + &:first-child { + padding-left: 1em; + } + img { + width: 2em; + } + &.score-pfp { + width: 2em; + } + &.score-value { + text-align: center; + } + } + } } \ No newline at end of file diff --git a/rails/app/controllers/lingo/scores_controller.rb b/rails/app/controllers/lingo/scores_controller.rb index 63fd0f9..59bbd9d 100644 --- a/rails/app/controllers/lingo/scores_controller.rb +++ b/rails/app/controllers/lingo/scores_controller.rb @@ -1,5 +1,7 @@ module Lingo class ScoresController < ApplicationController + skip_before_action :verify_authenticity_token, only: [:update] + def index @scores = Score.order(score: :desc) end @@ -8,13 +10,15 @@ module Lingo if params[:secret_code] != Lingo.secret_code then head :unauthorized else - score = Score.find_or_create_by(user_id: params[:user_id]) + score = Score.find_or_create_by(user_id: params[:user_id]) do |score| + score.score = 0 + end score.username = params[:username] - score.avatar_url = params[:avatar_url] + score.avatar_url = CGI.unescape(params[:avatar_url]) score.score += 1 score.save! - render :blank + head :created end end end diff --git a/rails/app/views/lingo/scores/index.html.haml b/rails/app/views/lingo/scores/index.html.haml index f0f681d..afcd0c7 100644 --- a/rails/app/views/lingo/scores/index.html.haml +++ b/rails/app/views/lingo/scores/index.html.haml @@ -1,7 +1,12 @@ %h2 Bot Puzzles Scoreboard %table#scores - - @scores.each do |score| - %tr + %tr.scores-header + %th + %th{colspan: 2} Player + %th Score + - @scores.each_with_index do |score,index| + %tr{class: cycle("even", "odd")} + %td.score-index #{index+1}. %td.score-pfp - if !score.avatar_url.nil? = image_tag score.avatar_url -- cgit 1.4.1