Refactor: convert ERB to HAML, optionally associations, simplify navigation and views

This commit is contained in:
Jesse C. Fisher 2026-07-30 22:11:43 -07:00 committed by The Needful DO
parent ef3cffb107
commit b52d384152
13 changed files with 82 additions and 160 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -1,37 +0,0 @@
<nav>
<ul>
<% if current_user.signed_in? %>
<li><%= link_to 'My Tendy Zone', root_path, class: "home-link", data: { turbo: false } %></li>
<% end %>
<li><%= link_to 'Public', '/public', data: { turbo: false } %></li>
<li><%= link_to 'Tokens', tokens_path %></li>
<li><%= link_to 'Profiles', profiles_path %></li>
<li><%= link_to 'Projects', projects_path %></li>
<li><%= link_to 'MetaPurse', metapurses_path %></li>
<% if policy(:canister).index? %>
<li><%= link_to 'Canisters', canisters_path %></li>
<% end %>
<li><%= link_to 'Doodads', doodads_path, data: { turbo: false } %></li>
<% if policy(:bot).index? %>
<li><%= link_to 'Bots', bots_path %></li>
<% end %>
<% if policy(:table).index? %>
<li><%= link_to 'Tables', tables_path %></li>
<% end %>
<% if policy(:room).index? %>
<li><%= link_to 'Chat', rooms_path %></li>
<% end %>
<% if current_user.signed_in? %>
<% if current_user.admin? %>
<li><%= link_to 'Users', users_path %></li>
<% end %>
<% end %>
<% if current_user.signed_in? %>
<li><%= link_to 'My account', edit_user_registration_path %></li>
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
<li><%= link_to 'Sign in', new_user_session_path %></li>
<li><%= link_to 'Sign up', new_user_registration_path %></li>
<% end %>
</ul>
</nav>

View File

@ -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

View File

@ -26,7 +26,7 @@
<header>
<%= render "layouts/navigation_links" %>
<%= render "layouts/navigation_wallet" %>
<%# <%= render "layouts/navigation_wallet" %1> %>
</header>
<%= render "layouts/flashmessages" %>
@ -35,9 +35,9 @@
<%= yield %>
</div>
<footer>
<%= render "layouts/footer" %>
</footer>
<%# <footer> %>
<%# <%= render "layouts/footer" %1> %>
<%# </footer> %>
</body>
</html>

View File

@ -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'))}" }

View File

@ -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"

View File

@ -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)

View File

@ -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 <br/>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 <br/>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