Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 1951f70

Browse files
committed
The GameExhibit object is actually used
Using this decorator lets us get rid of a smelly instance variable in `GamesController#show` (`@opponent`). which is rather nice. The `games/show.html.erb` view template is now a rather empty shell for the `@game`'s partial, but this could change with the design (shared UI elements could be moved there, for example).
1 parent 0db2baa commit 1951f70

File tree

8 files changed

+150
-156
lines changed

8 files changed

+150
-156
lines changed

app/controllers/games_controller.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ class GamesController < ApplicationController
22
before_action :set_game, only: [:show]
33

44
def show
5-
@opponent ||= @game.players.where.not(id: current_player&.id).first || NilPlayer.new
6-
render game_template || :show
5+
@game = GameExhibit.new(@game, current_player)
76
end
87

98
def create
@@ -17,23 +16,6 @@ def game_params
1716
params.require(:game).permit(:name)
1817
end
1918

20-
def game_template
21-
case @game.state
22-
when 'awaiting_opponent'
23-
return if player_in_game?
24-
AwaitingOpponentGame.template_path
25-
when 'running'
26-
return if player_in_game?
27-
FullGame.template_path
28-
when 'finished'
29-
FinishedGame.template_path
30-
end
31-
end
32-
33-
def player_in_game?
34-
current_player && current_player.game == @game
35-
end
36-
3719
def set_game
3820
@game = Game.find_by(slug: params[:id].parameterize)
3921
redirect_to root_path unless @game

app/exhibits/game_exhibit.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ def to_partial_path
1717
end
1818
end
1919

20-
def to_model
21-
__getobj__
20+
def current_player
21+
@current_player
22+
end
23+
24+
def opponent
25+
players.where.not(id: current_player&.id).first || NilPlayer.new
2226
end
2327

28+
# this method must **not** be delegated to the underlying model; otherwise,
29+
# when the object is rendered by `ActionView::PartialRenderer` (eg. when calling `render` from a view),
30+
# `to_partial_path` will be sent to the actual model instead of the exhibit
31+
def to_model
32+
self
33+
end
34+
2435
def class
2536
__getobj__.class
2637
end

app/views/games/awaiting_opponent.html.erb renamed to app/views/games/_awaiting_opponent_game.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</div>
1111
<div class="too_late">
1212
<h1>You've been challenged to a battle!</h1>
13-
<p>Your opponent is waiting for you. Join the room <strong><%= @game.name %></strong> right now!</p>
13+
<p>Your opponent is waiting for you. Join the room <strong><%= awaiting_opponent_game.name %></strong> right now!</p>
1414

15-
<%= form_tag game_opponent_path(@game), method: :post do %>
15+
<%= form_tag game_opponent_path(awaiting_opponent_game), method: :post do %>
1616
<%= submit_tag "Join Game", class: "button green" %>
1717
<% end %>
1818
</div>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<div class="row">
2+
<%= render "games/stars" %>
3+
<div class="wrapper">
4+
<div class="join_us">
5+
<div class="mothership">
6+
<div class="ship">
7+
<%= render "games/ships/svg/mothership" %>
8+
</div>
9+
</div>
10+
</div>
11+
<div class="too_late">
12+
<% case finished_game.current_player
13+
when finished_game.winner %>
14+
<h1>Game Finished, Congratulations! You WON!</h1>
15+
<p>
16+
You're the master of the <strong><%= finished_game.name %></strong> room
17+
where you defeated <strong><%= finished_game.opponent.nickname %></strong>.
18+
</p>
19+
<% when finished_game.loser %>
20+
<h1>Game Finished, You LOSE!</h1>
21+
<p>
22+
Today wasn't a good day for you in the <strong><%= finished_game.name %></strong> room.
23+
Looks like <strong><%= finished_game.opponent.nickname %></strong> ruled this finished_game.
24+
</p>
25+
<% else %>
26+
<h1>Too late! This finished is already finished_game.</h1>
27+
<p>
28+
You're currently in the <strong><%= finished_game.name %></strong> room
29+
where <strong><%= finished_game.players.first.nickname %></strong> and
30+
<strong><%= finished_game.players.last.nickname %></strong> had an epic fight deep in the space.
31+
</p>
32+
<% end %>
33+
34+
<p>In total, the battle involved <strong><%= finished_game.ships.count %> ships</strong> and <strong><%= finished_game.ships.sum(:damage) %> damages</strong>.</p>
35+
36+
<%= render 'components/social_links' %>
37+
</div>
38+
</div>
39+
</div>
File renamed without changes.

app/views/games/_game.html.erb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<% if game.players.count < 2 %>
2+
<% content_for :log, 'Waiting for player to join..' %>
3+
<% end %>
4+
5+
<div class="wrapper">
6+
<div class="combat_zone attack_mode" id="combat_zone">
7+
<%= render "stars" %>
8+
<div class="hud">
9+
<%= content_tag :h1, "ROOM: #{@game.name}", class: "room", id: "game_title", data: { game_id: @game.id } %>
10+
<ul id="life_player">
11+
<li class="life_bar"><%= render "games/hud/life" %></li>
12+
<li class="life_bar"><%= render "games/hud/life" %></li>
13+
<li class="life_bar"><%= render "games/hud/life" %></li>
14+
<li class="life_bar"><%= render "games/hud/life" %></li>
15+
<li class="life_bar"><%= render "games/hud/life" %></li>
16+
<li class="life_bar"><%= render "games/hud/life" %></li>
17+
<li class="life_bar"><%= render "games/hud/life" %></li>
18+
<li class="life_bar"><%= render "games/hud/life" %></li>
19+
<li class="life_bar"><%= render "games/hud/life" %></li>
20+
<li class="life_bar"><%= render "games/hud/life" %></li>
21+
</ul>
22+
<div id="current_player" data-player-id="<%= game.current_player.id %>" data-player-nickname="<%= game.current_player.nickname %>"></div>
23+
<%= form_for(game.current_player, url: player_path(game.current_player), data: { remote: true }, class: "player") do |f| %>
24+
<%= f.text_field :nickname, id: 'current_player_nickname' %>
25+
<% end %>
26+
<ul id="life_opponent">
27+
<li class="life_bar"><%= render "games/hud/life" %></li>
28+
<li class="life_bar"><%= render "games/hud/life" %></li>
29+
<li class="life_bar"><%= render "games/hud/life" %></li>
30+
<li class="life_bar"><%= render "games/hud/life" %></li>
31+
<li class="life_bar"><%= render "games/hud/life" %></li>
32+
<li class="life_bar"><%= render "games/hud/life" %></li>
33+
<li class="life_bar"><%= render "games/hud/life" %></li>
34+
<li class="life_bar"><%= render "games/hud/life" %></li>
35+
<li class="life_bar"><%= render "games/hud/life" %></li>
36+
<li class="life_bar"><%= render "games/hud/life" %></li>
37+
</ul>
38+
39+
<h1 class="opponent" id="opponent_nickname"><%= game.opponent.nickname.presence || 'waiting player' %></h1>
40+
41+
<div class="bonus_bar">
42+
<div class="bonus_percentage"></div>
43+
</div>
44+
45+
<%= text_field_tag :stdin, "", class: "attack" %>
46+
47+
<%= form_tag(attacks_path, id: "attack", data: { remote: true }) do %>
48+
<%= hidden_field_tag :word %>
49+
<%= hidden_field_tag :game_id, @game.id %>
50+
<% end %>
51+
<%= form_tag(defenses_path, id: "defense", data: { remote: true }) do %>
52+
<%= hidden_field_tag :word %>
53+
<%= hidden_field_tag :perfect_typing %>
54+
<%= hidden_field_tag :game_id, @game.id %>
55+
<% end %>
56+
<%= form_tag bombings_path, id: "bombing_frequency", data: { remote: true } do %>
57+
<%= hidden_field_tag :word %>
58+
<%= hidden_field_tag :game_id, @game.id %>
59+
<% end %>
60+
</div>
61+
</div>
62+
</div>
63+
64+
<div id="dockyard">
65+
<div id="mothership_template" class="mothership">
66+
<p class="word"></p>
67+
<div class="ship">
68+
<%= render "games/ships/svg/mothership" %>
69+
</div>
70+
</div>
71+
<div id="small_ship_template" class="ship small_ship">
72+
<p class="word"></p>
73+
<div class="ship">
74+
<%= render "games/ships/svg/small_ship" %>
75+
</div>
76+
</div>
77+
<div id="medium_ship_template" class="ship medium_ship">
78+
<p class="word"></p>
79+
<div class="ship">
80+
<%= render "games/ships/svg/medium_ship" %>
81+
</div>
82+
</div>
83+
<div id="large_ship_template" class="ship large_ship">
84+
<p class="word"></p>
85+
<div class="ship">
86+
<%= render "games/ships/svg/large_ship" %>
87+
</div>
88+
</div>
89+
<div id="rocket_template" class="rocket">
90+
<div class="ship">
91+
<%= render "games/ships/svg/rocket" %>
92+
</div>
93+
</div>
94+
</div>

app/views/games/finished.html.erb

Lines changed: 0 additions & 39 deletions
This file was deleted.

app/views/games/show.html.erb

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1 @@
1-
<% if @game.players.count < 2 %>
2-
<% content_for :log, 'Waiting for player to join..' %>
3-
<% end %>
4-
5-
<div class="wrapper">
6-
<div class="combat_zone attack_mode" id="combat_zone">
7-
<%= render "stars" %>
8-
<div class="hud">
9-
<%= content_tag :h1, "ROOM: #{@game.name}", class: "room", id: "game_title", data: { game_id: @game.id } %>
10-
<ul id="life_player">
11-
<li class="life_bar"><%= render "games/hud/life" %></li>
12-
<li class="life_bar"><%= render "games/hud/life" %></li>
13-
<li class="life_bar"><%= render "games/hud/life" %></li>
14-
<li class="life_bar"><%= render "games/hud/life" %></li>
15-
<li class="life_bar"><%= render "games/hud/life" %></li>
16-
<li class="life_bar"><%= render "games/hud/life" %></li>
17-
<li class="life_bar"><%= render "games/hud/life" %></li>
18-
<li class="life_bar"><%= render "games/hud/life" %></li>
19-
<li class="life_bar"><%= render "games/hud/life" %></li>
20-
<li class="life_bar"><%= render "games/hud/life" %></li>
21-
</ul>
22-
<div id="current_player" data-player-id="<%= current_player.id %>" data-player-nickname="<%= current_player.nickname %>"></div>
23-
<%= form_for(current_player, url: player_path(current_player), data: { remote: true }, class: "player") do |f| %>
24-
<%= f.text_field :nickname, id: 'current_player_nickname' %>
25-
<% end %>
26-
<ul id="life_opponent">
27-
<li class="life_bar"><%= render "games/hud/life" %></li>
28-
<li class="life_bar"><%= render "games/hud/life" %></li>
29-
<li class="life_bar"><%= render "games/hud/life" %></li>
30-
<li class="life_bar"><%= render "games/hud/life" %></li>
31-
<li class="life_bar"><%= render "games/hud/life" %></li>
32-
<li class="life_bar"><%= render "games/hud/life" %></li>
33-
<li class="life_bar"><%= render "games/hud/life" %></li>
34-
<li class="life_bar"><%= render "games/hud/life" %></li>
35-
<li class="life_bar"><%= render "games/hud/life" %></li>
36-
<li class="life_bar"><%= render "games/hud/life" %></li>
37-
</ul>
38-
39-
<h1 class="opponent" id="opponent_nickname"><%= @opponent.nickname.presence || 'waiting player' %></h1>
40-
41-
<div class="bonus_bar">
42-
<div class="bonus_percentage"></div>
43-
</div>
44-
45-
<%= text_field_tag :stdin, "", class: "attack" %>
46-
47-
<%= form_tag(attacks_path, id: "attack", data: { remote: true }) do %>
48-
<%= hidden_field_tag :word %>
49-
<%= hidden_field_tag :game_id, @game.id %>
50-
<% end %>
51-
<%= form_tag(defenses_path, id: "defense", data: { remote: true }) do %>
52-
<%= hidden_field_tag :word %>
53-
<%= hidden_field_tag :perfect_typing %>
54-
<%= hidden_field_tag :game_id, @game.id %>
55-
<% end %>
56-
<%= form_tag bombings_path, id: "bombing_frequency", data: { remote: true } do %>
57-
<%= hidden_field_tag :word %>
58-
<%= hidden_field_tag :game_id, @game.id %>
59-
<% end %>
60-
</div>
61-
</div>
62-
</div>
63-
64-
<div id="dockyard">
65-
<div id="mothership_template" class="mothership">
66-
<p class="word"></p>
67-
<div class="ship">
68-
<%= render "games/ships/svg/mothership" %>
69-
</div>
70-
</div>
71-
<div id="small_ship_template" class="ship small_ship">
72-
<p class="word"></p>
73-
<div class="ship">
74-
<%= render "games/ships/svg/small_ship" %>
75-
</div>
76-
</div>
77-
<div id="medium_ship_template" class="ship medium_ship">
78-
<p class="word"></p>
79-
<div class="ship">
80-
<%= render "games/ships/svg/medium_ship" %>
81-
</div>
82-
</div>
83-
<div id="large_ship_template" class="ship large_ship">
84-
<p class="word"></p>
85-
<div class="ship">
86-
<%= render "games/ships/svg/large_ship" %>
87-
</div>
88-
</div>
89-
<div id="rocket_template" class="rocket">
90-
<div class="ship">
91-
<%= render "games/ships/svg/rocket" %>
92-
</div>
93-
</div>
94-
</div>
1+
<%= render @game %>

0 commit comments

Comments
 (0)