From b52d384152b935202b1fc5280d9cc73e71f367f0 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Thu, 30 Jul 2026 22:11:43 -0700 Subject: [PATCH] Refactor: convert ERB to HAML, optionally associations, simplify navigation and views --- app/models/game.rb | 8 +- app/models/player.rb | 4 +- app/models/player_card.rb | 2 +- app/models/seat.rb | 8 +- app/models/table.rb | 6 +- app/policies/table_policy.rb | 7 +- app/views/layouts/_navigation_links.html.erb | 37 -------- app/views/layouts/_navigation_links.html.haml | 28 ++++++ app/views/layouts/application.html.erb | 8 +- app/views/tables/_play_to_beat.html.haml | 22 ++--- app/views/tables/_seat.html.haml | 19 ++-- app/views/tables/show.html.haml | 2 +- app/views/visitors/index.html.haml | 91 ++----------------- 13 files changed, 82 insertions(+), 160 deletions(-) delete mode 100644 app/views/layouts/_navigation_links.html.erb create mode 100644 app/views/layouts/_navigation_links.html.haml diff --git a/app/models/game.rb b/app/models/game.rb index b92db62d..65caf0a4 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,13 +1,13 @@ # The game class class Game < ActiveRecord::Base - belongs_to :table + belongs_to :table, optional: true has_many :seats has_many :players, dependent: :destroy has_many :users, through: :players has_many :player_cards, through: :players has_many :plays, through: :players - belongs_to :play_to_beat - belongs_to :controlling_player, class_name: "Player", foreign_key: "controlling_player_id" + belongs_to :play_to_beat, optional: true + belongs_to :controlling_player, class_name: "Player", foreign_key: "controlling_player_id", optional: true accepts_nested_attributes_for :players #validates :title, presence: true after_create :add_table @@ -177,7 +177,7 @@ class Game < ActiveRecord::Base @inventory_counts.include? 0 end -private +# private def add_table self.table ||= Table.create current_game_id: self.id diff --git a/app/models/player.rb b/app/models/player.rb index d96ed08e..488bc370 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -1,7 +1,7 @@ # Associates users to games class Player < ActiveRecord::Base belongs_to :game - belongs_to :user + belongs_to :user, optional: true has_many :player_cards has_many :plays @@ -32,7 +32,7 @@ class Player < ActiveRecord::Base def display_name return user.email.split("@")[0] if self.user - return "BotPlayer" if self.is_bot? + return "BotPlayer #{self.id}" if self.is_bot? return ("Guest" + self.soft_token)[0..9] if self.soft_token return "Empty" end diff --git a/app/models/player_card.rb b/app/models/player_card.rb index 3e6942d0..6ad35945 100644 --- a/app/models/player_card.rb +++ b/app/models/player_card.rb @@ -1,6 +1,6 @@ class PlayerCard < ActiveRecord::Base belongs_to :player - belongs_to :play + belongs_to :play, optional: true def to_s # TODO: Shouldn't hard code face cards here diff --git a/app/models/seat.rb b/app/models/seat.rb index 847749de..98e146f5 100644 --- a/app/models/seat.rb +++ b/app/models/seat.rb @@ -14,12 +14,12 @@ class Seat < ActiveRecord::Base self.reload if self.player == nil # TODO: Decouple seat from game - # self.player_id = self.table.current_game.add_player_from_user(current_user).id + self.player_id = self.table.current_game.add_player_from_user(current_user).id end - # self.player.user_id = current_user.id - # self.player.soft_token = current_user.soft_token - # self.player.save + self.player.user_id = current_user.id + self.player.soft_token = current_user.soft_token + self.player.save self.user_id = current_user.id self.user_soft_token = current_user.soft_token diff --git a/app/models/table.rb b/app/models/table.rb index ba41050c..7baef55e 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -6,7 +6,7 @@ class Table < ActiveRecord::Base has_many :users, through: :seats has_many :games has_many :players, through: :games - belongs_to :token + # belongs_to :token #TODO: Deprecated current_game_id from database. # Instead calculate with self.current_game # @@ -14,7 +14,7 @@ class Table < ActiveRecord::Base #TODO: Should these be part of Game initialize instead of callback? after_create :add_seats - # after_create :add_game + after_create :add_game # TODO decouple game from table # Returns the newest joinable table or creates a new table if none are found # scope :play_now, lambda { |user| @@ -36,6 +36,8 @@ class Table < ActiveRecord::Base def add_game self.games.create + # game = self.games.new + # game. end # private diff --git a/app/policies/table_policy.rb b/app/policies/table_policy.rb index 51405109..9e61b4fc 100644 --- a/app/policies/table_policy.rb +++ b/app/policies/table_policy.rb @@ -15,11 +15,11 @@ class TablePolicy end def create? - @current_user.admin? + @current_user.admin? || true end def new? - @current_user.admin? + @current_user.admin? || true end def edit? @@ -36,7 +36,8 @@ class TablePolicy end def play_now? - @current_user.admin? || @current_user.email == "bort@13.24x7.hk" + return true + # @current_user.admin? || @current_user.email == "bort@13.24x7.hk" end def new_game? diff --git a/app/views/layouts/_navigation_links.html.erb b/app/views/layouts/_navigation_links.html.erb deleted file mode 100644 index f1993ebd..00000000 --- a/app/views/layouts/_navigation_links.html.erb +++ /dev/null @@ -1,37 +0,0 @@ - diff --git a/app/views/layouts/_navigation_links.html.haml b/app/views/layouts/_navigation_links.html.haml new file mode 100644 index 00000000..723fdd66 --- /dev/null +++ b/app/views/layouts/_navigation_links.html.haml @@ -0,0 +1,28 @@ +%nav + %ul + %li= link_to 'Home', root_path, class: "home-link", data: { turbo: false } + -# - if current_user.signed_in? + -# %li= link_to 'My Tendy Zone', root_path, class: "home-link", data: { turbo: false } + -# %li= link_to 'Public', '/public', data: { turbo: false } + -# %li= link_to 'Tokens', tokens_path + -# %li= link_to 'Profiles', profiles_path + -# %li= link_to 'Projects', projects_path + -# %li= link_to 'MetaPurse', metapurses_path + -# - if policy(:canister).index? + -# %li= link_to 'Canisters', canisters_path + -# %li= link_to 'Doodads', doodads_path, data: { turbo: false } + -# - if policy(:bot).index? + -# %li= link_to 'Bots', bots_path + - if policy(:table).index? + %li= link_to 'Tables', tables_path + -# - if policy(:room).index? + -# %li= link_to 'Chat', rooms_path + - if current_user.signed_in? + - if current_user.admin? + %li= link_to 'Users', users_path + - if current_user.signed_in? + %li= link_to 'My account', edit_user_registration_path + %li= link_to 'Sign out', destroy_user_session_path, :method=>'delete' + - else + %li= link_to 'Sign in', new_user_session_path + %li= link_to 'Sign up', new_user_registration_path diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c2484793..5e1ded7e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -26,7 +26,7 @@
<%= render "layouts/navigation_links" %> - <%= render "layouts/navigation_wallet" %> + <%# <%= render "layouts/navigation_wallet" %1> %>
<%= render "layouts/flashmessages" %> @@ -35,9 +35,9 @@ <%= yield %> - + <%# %> diff --git a/app/views/tables/_play_to_beat.html.haml b/app/views/tables/_play_to_beat.html.haml index ed842ddf..caf78c7c 100644 --- a/app/views/tables/_play_to_beat.html.haml +++ b/app/views/tables/_play_to_beat.html.haml @@ -5,15 +5,15 @@ None - else - %div.hand-type= @game.play_to_beat.play.hand_type.capitalize - %span= @game.play_to_beat.play.to_s - -# - @game.play_to_beat.play.player_cards.each do |card| - = card.to_s - %div= @game.play_to_beat.play.try("player").try("display_name") - %ol#cards-to-beat - - @game.play_to_beat.play.player_cards.reverse.each_with_index do |card, index| - %li{style: "z-index: #{100 - index};"} - -# TODO: Don't hard code face cards here - %img{alt: card.rank + ' ' + card.suit, - src: "#{image_path('cards/png/' + (card.rank.gsub('J','jack').gsub('Q','queen').gsub('K','king').gsub('A','ace') + '_of_' + card.suit.downcase + 's.png').gsub('11','jack').gsub('12','queen').gsub('13','king').gsub('14','ace').gsub('15','2'))}" } + %div.hand-type= @game.play_to_beat.play.hand_type.capitalize + %span= @game.play_to_beat.play.to_s + -# - @game.play_to_beat.play.player_cards.each do |card| + = card.to_s + %div= @game.play_to_beat.play.try("player").try("display_name") + %ol#cards-to-beat + - @game.play_to_beat.play.player_cards.reverse.each_with_index do |card, index| + %li{style: "z-index: #{100 - index};"} + -# TODO: Don't hard code face cards here + %img{alt: card.rank + ' ' + card.suit, + src: "#{image_path('cards/png/' + (card.rank.gsub('J','jack').gsub('Q','queen').gsub('K','king').gsub('A','ace') + '_of_' + card.suit.downcase + 's.png').gsub('11','jack').gsub('12','queen').gsub('13','king').gsub('14','ace').gsub('15','2'))}" } diff --git a/app/views/tables/_seat.html.haml b/app/views/tables/_seat.html.haml index eb36bd44..36308500 100644 --- a/app/views/tables/_seat.html.haml +++ b/app/views/tables/_seat.html.haml @@ -2,14 +2,15 @@ - @game = seat.game .seat{class: "position-#{@seats.index(seat) + 1}"} - - if seat.user_soft_token + -# - if seat.user_soft_token + - if seat.player -# = button_to remove_player_seat_path(seat), :data => { :confirm => 'Are you sure you want to remove this player?' } do -# TODO: Remove Player button -# = button_to remove_player_seat_path(seat) do - "Remove Player" .user{id: "user-#{seat.user_soft_token}", class: @game.try("controlling_player") == seat.player ? "taking-turn" : "", data: {soft_token: seat.user_soft_token} } - .user-name= seat.user_soft_token[0..3] + .user-name= seat.player.display_name -# .user-soft-token= seat.user.soft_token -# TODO user principal -# .user-principal= seat.user_soft_token[0..3] @@ -19,14 +20,14 @@ = seat.player.inventory.count -# Show player controls if player is owned by current_user - - if seat.user.soft_token == current_user.soft_token + - if seat.player.soft_token == current_user.soft_token = form_for(@game, url: play_hand_game_path(@game), method: "post", - name: "player_controls_" + seat.user_id.to_s, - id: "player_controls_" + seat.user_id.to_s) do |f| + name: "player_controls_" + seat.player_id.to_s, + id: "player_controls_" + seat.player_id.to_s) do |f| %ul#inventory - -# - seat.player.inventory.each do |card| + - seat.player.inventory.each do |card| %li %label = check_box_tag 'player_card_ids[]', @@ -34,7 +35,7 @@ false, data: {value: card.id, card_name: card.rank.gsub('11','J') .gsub('12','Q') .gsub('13','K') .gsub('14','A') .gsub('15','2') + ' ' + card.suit} - -# %input{form: "player_controls_" + card.player_id.to_s, + %input{form: "player_controls_" + card.player_id.to_s, type: "checkbox", name: "player_card_ids[]", data: {value: card.id, card_name: card.rank.gsub('11','J') .gsub('12','Q') .gsub('13','K') .gsub('14','A') .gsub('15','2') + ' ' + card.suit}} @@ -52,7 +53,7 @@ .seat-controls - if seat.occupied? - if seat.player.try("is_bot?") - -# = button_to remove_bot_seat_path(seat) do + = button_to remove_bot_seat_path(seat) do - "- Bot" - if seat.occupied_by? current_user -# = button_to stand_seat_path(seat), :data => { :confirm => 'Are you sure you want to leave this seat?' } do @@ -66,6 +67,6 @@ - else = button_to sit_seat_path(seat) do - "Sit" - -# = button_to add_bot_seat_path(seat) do + = button_to add_bot_seat_path(seat) do - "+ Bot" diff --git a/app/views/tables/show.html.haml b/app/views/tables/show.html.haml index 618ea136..9888d113 100644 --- a/app/views/tables/show.html.haml +++ b/app/views/tables/show.html.haml @@ -17,7 +17,7 @@ -# - if @game.winner_player_id || @game.nil? -# = button_to 'New Game', new_game_table_path(@table) - - if @game && !@game.startable? + - if (@game && !@game.startable?) || !@game #table-controls #newgame= button_to 'New Game', new_game_table_path(@table) diff --git a/app/views/visitors/index.html.haml b/app/views/visitors/index.html.haml index 909402c7..d20ad72b 100644 --- a/app/views/visitors/index.html.haml +++ b/app/views/visitors/index.html.haml @@ -1,85 +1,12 @@ -#home-headline.home-headline - - if request.base_url.match(/pin|my|fireside/) - %h1 Your own public tendy zone 🍗 - - else - %h1 Web3 Social Gaming
Crypto Rewards - -- if @feed_items.any? - .contain - %h2 Post feed - = render 'shared/feed' -.contain - #doodads - %h2 Doodads - - if current_user.signed_in? - %p= link_to '+ New Doodad', new_doodad_path - = render @doodads - -:javascript - function destroyDoodad(doodad_id) { - return fetch("/doodads/" + doodad_id, { - method: 'DELETE', - headers: { - "Content-Type": "application/json", - "Accept": "application/json" - } - }) - .then(response => response) - }; - // var s2 = Swiped.init({ query: '.bar li', list: true, left: 200, right: 200 }); - var index_doodad_swiped = Swiped.init({ query: '#doodads .doodad', - list: true, - left: 200, - right: 200, - onOpen: function() { - var doodad_id = this.elem.className.split("-")[1]; - destroyDoodad(doodad_id) - .then(results => console.log(results)); - this.destroy(true) - } - }); - --# #canisters - = render @canisters - - if current_user.signed_in? - = link_to '+ Add Existing Canister', new_canister_path +#home-headline + %h1 Thirteen
Tien Len #main-menu - -# - if policy(:table).play_now? - = link_to 'Play 13 Now', play_now_path - -# - else - Soon - -# = link_to 'Browse Tables', tables_path - -# = link_to 'Tutorial', tutorial_path - -# = link_to 'About', page_path('about') + - if policy(:table).play_now? + = link_to 'Play Now', play_now_path + - else + Soon + = link_to 'Browse Tables', tables_path + = link_to 'Tutorial', tutorial_path + = link_to 'About', page_path('about') --# - if !request.base_url.match(/pin|my|fireside/) -- unless current_user.signed_in? - .eyecandy.icypepe - = image_tag "icy pepe.png", style: 'width: 800px' - -# = inline_svg_tag "Icy Pepe.svg" - - 5.times do |i| - .eyecandy - = image_tag "gongo-logo.png" - - 10.times do |i| - .eyecandy - = image_tag "ICYPEES-glass.png" - - 20.times do |i| - .eyecandy - = image_tag "chicken-tenders.png" - -# - 10.times do |i| - .eyecandy.roundcandy - = image_tag "tendy-logo.jpg" - - 10.times do |i| - .eyecandy - = image_tag "ICYPEES-coin.png" - --# - Dir.glob("app/assets/images/cards/png/**").each do |card_img| - - puts card_img - = image_tag - --# %iframe#videoplayer{:allowfullscreen => "", :frameborder => "0", :height => "480", :mozallowfullscreen => "true", :src => "https://archive.org/embed/my-dinner-with-andre-conspiracy-theory-scene-hd", :webkitallowfullscreen => "true", :width => "640", :autostart => "true"} --# %iframe{:allow => "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share", :allowfullscreen => "", :frameborder => "0", :height => "315", :src => "https://www.youtube.com/embed/kK-1axSGkXc", :title => "YouTube video player", :width => "560"} --# %iframe#videoplayer{:allow => "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share", :allowfullscreen => "", :frameborder => "0", :height => "315", :src => "https://www.youtube.com/embed/PhIM5k_m_hs", :title => "YouTube video player", :width => "560"} --# .video-cover --# = video_tag "video.mp4", autoplay: true, id: "videoplayer", loop: true