diff --git a/.idea/gambosite.iml b/.idea/gambosite.iml index 8e441d5..9b6b960 100644 --- a/.idea/gambosite.iml +++ b/.idea/gambosite.iml @@ -154,76 +154,78 @@ diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index ff2b71c..b76295b 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -105,11 +105,33 @@ body { } h1 { - color: #fe8019; margin: auto; text-align: center; padding-top: 10px; padding-bottom: 1em; + color: #fe8019; +} + +.left-just { + text-align: left; + margin: 0 auto; + max-width: 800px; +} + +h2 { + color: #fabd2f; +} + +h3 { + color: #689d6a; +} + +p, label { + color: #ebdbb2; +} + +label { + font-weight: bold; } a { @@ -190,4 +212,13 @@ a:visited { 100% { transform: translateY(0) rotate(-5deg); } +} + +select { + margin-bottom: 20px; + margin-right: 20px; +} + +#set-alt-button { + margin-bottom: 20px; } \ No newline at end of file diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb new file mode 100644 index 0000000..df1ebe0 --- /dev/null +++ b/app/controllers/admin_controller.rb @@ -0,0 +1,55 @@ +class AdminController < ApplicationController + PASSWORD = "butts" + before_action :authenticate_admin, only: [:index, :set_alt, :clear_alt ] + + def index + @mains = Player.joins(:alternate_players).distinct + @player_names = Player.all.map(&:name) + end + + def set_alt + main = Player.find_by(name: params[:main_selected]) + alt = Player.find_by(name: params[:alt_selected]) + + if main && alt && main.id != alt.id + main.alternate_players << alt + alt.main_player_id = main.id + + main.save + alt.save + end + + refresh_or_redirect_to admin_path + end + + def clear_alt + main = Player.find_by(id: params[:main_id]) + alt = Player.find_by(id: params[:alt_id]) + + if main && alt && main.alternate_players.include?(alt) + main.alternate_players.delete(alt) + main.save + end + + refresh_or_redirect_to admin_path + end + + def login + + end + + def login_submit + pw = params[:password] + if pw == PASSWORD + session[:user_id] = 1 + redirect_to admin_path, notice: "Logged in!" + else + refresh_or_redirect_to admin_login_path, notice: "Login failed!" + end + end + + def destroy + session.delete(:user_id) + redirect_to root_path, notice: "Logged out!" + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 95ff55b..3115c30 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -20,4 +20,10 @@ class ApplicationController < ActionController::Base def unauthorized_response render json: { error: "API key is missing or empty" }, status: :unauthorized end + + def authenticate_admin + unless session[:user_id] + redirect_to admin_login_path, alert: "Login first" + end + end end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb new file mode 100644 index 0000000..d5c6d35 --- /dev/null +++ b/app/helpers/admin_helper.rb @@ -0,0 +1,2 @@ +module AdminHelper +end diff --git a/app/models/player.rb b/app/models/player.rb index 04f1986..86c1059 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -35,6 +35,10 @@ class Player < ApplicationRecord self.main_player end + def alts_concat + self.alternate_players.map(&:name).join(", ") + end + def no_circular_references if main_player_id.present? && (main_player.main_player== self) errors.add(:main_account, "circular reference") diff --git a/app/views/admin/index.html.erb b/app/views/admin/index.html.erb new file mode 100644 index 0000000..2752f22 --- /dev/null +++ b/app/views/admin/index.html.erb @@ -0,0 +1,45 @@ +

Admin

+ +

Alt List

+
+
+
+
Main
+
Alts
+
Remove
+
+ <% @mains.each do |main| %> + <% main.alternate_players.each do |alt| %> +
+
<%= main.name %>
+
<%= alt.name %>
+
<%= button_to "Clear Alt", + "/admin/clear_alt", + method: :post, + params: { main_id: main.id, alt_id: alt.id } %> +
+
+ <% end %> + <% end %> +
+ + <%= form_with(url: "/admin/set_alt", method: :post, local: true) do %> +

Set Alt

+ + + + + + <%= submit_tag "Set Alt", id: "set-alt-button" %> + <% end %> + Leaderboard + Logout +
diff --git a/app/views/admin/login.html.erb b/app/views/admin/login.html.erb new file mode 100644 index 0000000..e5f22fa --- /dev/null +++ b/app/views/admin/login.html.erb @@ -0,0 +1,8 @@ +

Admin Login

+
+ <%= form_with(url: "/admin/login_submit", method: :post, local: true) do %> + + + + <% end %> +
\ No newline at end of file diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb new file mode 100644 index 0000000..b01a63f --- /dev/null +++ b/config/initializers/session_store.rb @@ -0,0 +1 @@ +Rails.application.config.session_store :cookie_store, key: "forcek.in", expire_after: 30.minutes diff --git a/config/routes.rb b/config/routes.rb index 57f4b1f..3460f26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,4 +27,12 @@ Rails.application.routes.draw do get "bot/players" => "bot#players" get "bot/player/:id" => "bot#player_name" get "bot/summary" => "bot#summary" + + # Admin + get "admin", to: "admin#index" + post "admin/set_alt", to: "admin#set_alt" + post "admin/clear_alt", to: "admin#clear_alt" + get "admin/login", to: "admin#login" + post "admin/login_submit", to: "admin#login_submit" + get "admin/destroy", to: "admin#destroy" end diff --git a/test/controllers/admin_controller_test.rb b/test/controllers/admin_controller_test.rb new file mode 100644 index 0000000..f891499 --- /dev/null +++ b/test/controllers/admin_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class AdminControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end