From ede07e6e38a346b3ff0bbccefb655f3ae0a32504 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 29 Nov 2023 23:01:44 -0500 Subject: Rewrite web component with Sinatra --- web/Gemfile | 9 +++++++++ web/Gemfile.lock | 48 +++++++++++++++++++++++++++++++++++++++++++ web/config.ru | 2 ++ web/public/header.png | Bin 0 -> 1213722 bytes web/views/index.haml | 24 ++++++++++++++++++++++ web/views/style.scss | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ web/web.rb | 44 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 182 insertions(+) create mode 100644 web/Gemfile create mode 100644 web/Gemfile.lock create mode 100644 web/config.ru create mode 100644 web/public/header.png create mode 100644 web/views/index.haml create mode 100644 web/views/style.scss create mode 100644 web/web.rb diff --git a/web/Gemfile b/web/Gemfile new file mode 100644 index 0000000..c56bd91 --- /dev/null +++ b/web/Gemfile @@ -0,0 +1,9 @@ +source "https://rubygems.org" + +gem "haml" +gem "puma" +gem "rack-cache" +gem "sassc" +gem "sequel" +gem "sinatra" +gem "sqlite3" diff --git a/web/Gemfile.lock b/web/Gemfile.lock new file mode 100644 index 0000000..9869d49 --- /dev/null +++ b/web/Gemfile.lock @@ -0,0 +1,48 @@ +GEM + remote: https://rubygems.org/ + specs: + bigdecimal (3.1.4) + ffi (1.16.3) + haml (6.2.3) + temple (>= 0.8.2) + thor + tilt + mustermann (3.0.0) + ruby2_keywords (~> 0.0.1) + nio4r (2.6.1) + puma (6.4.0) + nio4r (~> 2.0) + rack (2.2.8) + rack-cache (1.14.0) + rack (>= 0.4) + rack-protection (3.1.0) + rack (~> 2.2, >= 2.2.4) + ruby2_keywords (0.0.5) + sassc (2.4.0) + ffi (~> 1.9) + sequel (5.74.0) + bigdecimal + sinatra (3.1.0) + mustermann (~> 3.0) + rack (~> 2.2, >= 2.2.4) + rack-protection (= 3.1.0) + tilt (~> 2.0) + sqlite3 (1.6.9-x86_64-darwin) + temple (0.10.3) + thor (1.3.0) + tilt (2.3.0) + +PLATFORMS + x86_64-darwin-22 + +DEPENDENCIES + haml + puma + rack-cache + sassc + sequel + sinatra + sqlite3 + +BUNDLED WITH + 2.4.21 diff --git a/web/config.ru b/web/config.ru new file mode 100644 index 0000000..9dd12d3 --- /dev/null +++ b/web/config.ru @@ -0,0 +1,2 @@ +require './web' +run Sinatra::Application diff --git a/web/public/header.png b/web/public/header.png new file mode 100644 index 0000000..9384865 Binary files /dev/null and b/web/public/header.png differ diff --git a/web/views/index.haml b/web/views/index.haml new file mode 100644 index 0000000..186dd40 --- /dev/null +++ b/web/views/index.haml @@ -0,0 +1,24 @@ +!!! 5 +%html + %head + %title LINGO Bot Scoreboard + %link{ rel: "stylesheet", href: "style.css", type: "text/css"} + %body + #header + %img{ src: "header.png" } + #content + %h2 Bot Puzzles Scoreboard + %table#scores + %tr.scores-header + %th + %th{colspan: 2} Player + %th Score + - row_cycle = ["even", "odd"].cycle + - @scores.each_with_index do |score,index| + %tr{class: row_cycle.next} + %td.score-index #{index+1}. + %td.score-pfp + - if !score.avatar_url.nil? + %img{ src: score.avatar_url } + %td.score-name= score.username + %td.score-value= score.score diff --git a/web/views/style.scss b/web/views/style.scss new file mode 100644 index 0000000..a65cd94 --- /dev/null +++ b/web/views/style.scss @@ -0,0 +1,55 @@ +body { + background-color: black; + color: white; + font-family: sans-serif; +} + +#header { + width: 100%; + + img { + max-width: 80%; + margin: 0 auto; + display: block; + } +} + +h2 { + text-align: center; +} + +#scores { + 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/web/web.rb b/web/web.rb new file mode 100644 index 0000000..15c088e --- /dev/null +++ b/web/web.rb @@ -0,0 +1,44 @@ +require 'rack/cache' +require 'yaml' + +require 'rubygems' +require 'bundler/setup' +Bundler.require :default + +use Rack::Cache + +config = YAML.load(open("config.yml")) +db = Sequel.connect("sqlite://#{config["database"]}") + +class LingoScore < Sequel::Model +end + +get '/' do + @scores = LingoScore.reverse_order(:score) + + haml :index +end + +post '/update' do + if params[:secret_code] != config["secret_code"] then + 403 + else + if LingoScore.where(user_id: params[:user_id]).count == 0 + score = LingoScore.new(score: 0) + else + score = LingoScore.first(user_id: params[:user_id]) + end + score.username = params[:username] + score.avatar_url = CGI.unescape(params[:avatar_url]) + score.score += 1 + score.save + + 201 + end +end + +get '/style.css' do + cache_control :public, :max_age => 36000 + + scss :style +end -- cgit 1.4.1