summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lingo.cpp59
-rw-r--r--rails/app/assets/stylesheets/lingo/main.css.scss37
-rw-r--r--rails/app/controllers/lingo/scores_controller.rb10
-rw-r--r--rails/app/views/lingo/scores/index.html.haml9
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 @@
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)
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 @@
1body { 1body {
2 background-color: black; 2 background-color: black;
3 color: white; 3 color: white;
4 font-family: sans-serif;
4} 5}
5 6
6#header { 7#header {
7 width: 100%; 8 width: 100%;
8 9
9 img { 10 img {
10 max-width: 80%; 11 max-width: 80%;
11 margin: 0 auto; 12 margin: 0 auto;
@@ -15,10 +16,40 @@ body {
15 16
16h2 { 17h2 {
17 text-align: center; 18 text-align: center;
18 font-family: sans-serif;
19} 19}
20 20
21#scores { 21#scores {
22 width: 50%;
23 margin: 0 auto; 22 margin: 0 auto;
23 border-spacing: 0px;
24 tr {
25 &.even {
26 background-color: gray;
27 }
28 &.odd {
29 background-color: purple;
30 }
31 th {
32 text-align: left;
33 padding-left: 0.5em;
34 padding-bottom: 0.5em;
35 }
36 td {
37 padding-right: 1em;
38 padding-top: 0.5em;
39 padding-bottom: 0.5em;
40 border-collapse: collapse;
41 &:first-child {
42 padding-left: 1em;
43 }
44 img {
45 width: 2em;
46 }
47 &.score-pfp {
48 width: 2em;
49 }
50 &.score-value {
51 text-align: center;
52 }
53 }
54 }
24} \ No newline at end of file 55} \ 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 @@
1module Lingo 1module Lingo
2 class ScoresController < ApplicationController 2 class ScoresController < ApplicationController
3 skip_before_action :verify_authenticity_token, only: [:update]
4
3 def index 5 def index
4 @scores = Score.order(score: :desc) 6 @scores = Score.order(score: :desc)
5 end 7 end
@@ -8,13 +10,15 @@ module Lingo
8 if params[:secret_code] != Lingo.secret_code then 10 if params[:secret_code] != Lingo.secret_code then
9 head :unauthorized 11 head :unauthorized
10 else 12 else
11 score = Score.find_or_create_by(user_id: params[:user_id]) 13 score = Score.find_or_create_by(user_id: params[:user_id]) do |score|
14 score.score = 0
15 end
12 score.username = params[:username] 16 score.username = params[:username]
13 score.avatar_url = params[:avatar_url] 17 score.avatar_url = CGI.unescape(params[:avatar_url])
14 score.score += 1 18 score.score += 1
15 score.save! 19 score.save!
16 20
17 render :blank 21 head :created
18 end 22 end
19 end 23 end
20 end 24 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 @@
1%h2 Bot Puzzles Scoreboard 1%h2 Bot Puzzles Scoreboard
2%table#scores 2%table#scores
3 - @scores.each do |score| 3 %tr.scores-header
4 %tr 4 %th
5 %th{colspan: 2} Player
6 %th Score
7 - @scores.each_with_index do |score,index|
8 %tr{class: cycle("even", "odd")}
9 %td.score-index #{index+1}.
5 %td.score-pfp 10 %td.score-pfp
6 - if !score.avatar_url.nil? 11 - if !score.avatar_url.nil?
7 = image_tag score.avatar_url 12 = image_tag score.avatar_url