Permalink
Browse files

Leaner logging and performance, AND discovered the result of prematur…

…e checkmate detection !
  • Loading branch information...
1 parent acfa71a commit c4726ad2c1b90ee8c7515eee043080344d3c4bad Dean Radcliffe committed Nov 2, 2010
@@ -28,16 +28,10 @@ def player_over_http
return u && u.playing_as
end
- # the player this request is being processed for - should be request.player but...
- def current_player
- p = request.player || player_over_http
- request.player = p if p
- end
- helper_method :current_player
-
# descendant controllers call authorize to ensure player is logged in, or redirect them to login
def authorize
- unless current_player
+ request.player ||= player_over_http
+ unless request.player
flash[:notice] = "Login is required in order to take this action."
session[:original_uri] = request.request_uri
redirect_to login_url
@@ -2,7 +2,7 @@ class ChatsController < ApplicationController
before_filter :authorize
def create
- request.match.chats.create( params[:chat].merge(:player_id => current_player.id) )
+ request.match.chats.create( params[:chat].merge(:player_id => request.player.id) )
render :text => 'ok', :layout => false
end
@@ -5,6 +5,8 @@ class MatchController < ApplicationController
def index; end
def show; end
def new; end
+ def update_view; Rails.logger.silence{ render :action => 'update_view' } ; end
+ def viewmodel; Rails.logger.silence{ render :action => 'viewmodel' } ; end
# json for autocomplete
def players
@@ -13,7 +15,7 @@ def players
# give up the ghost
def resign
- request.match.resign( current_player )
+ request.match.resign( request.player )
redirect_to :action => 'index'
end
@@ -11,6 +11,7 @@ def create
flash[:error] = @move.errors.full_messages unless @move.id
# not working ! see checks in prod matches 75/78
+ # HA ! memoization screwed it up !
# [:black, :white].each do |side|
# match.checkmate_by(side.opposite) if match.reload.board.in_checkmate?(side)
# end
@@ -39,7 +39,7 @@ def piece_img_src board, piece, position
end
def my_next_matches
- my_other_matches = current_player.matches.where_turn_of(current_player).reject do |m|
+ my_other_matches = request.player.matches(:include => [:gameplays, :players], :conditions => 'active = true').where_turn_of(request.player).reject do |m|
m == match
end
my_other_matches.map(&:id).map(&:to_s).to_json
View
@@ -6,6 +6,8 @@ class Match < ActiveRecord::Base
has_many :chats
belongs_to :winner, :class_name => 'Player', :foreign_key => 'winning_player'
+ default_scope :include => :gameplays
+
named_scope :active, :conditions => { :active => 1 }
named_scope :completed, :conditions => { :active => 0 }
@@ -93,14 +95,14 @@ def turn_of? player
end
# Returns the symbol :white or :black of the next to move in this match
- def side_to_move
+ def side_to_move( method = :length )
s = initial_board.side_to_move
- moves.count % 2 == 0 ? s : s.opposite
+ moves.send(method) % 2 == 0 ? s : s.opposite
end
def side_of( plyr )
- return :white if plyr == self.white
- return :black if plyr == self.black
+ return :white if plyr.id == gameplays.white.player_id
+ return :black if plyr.id == gameplays.black.player_id
end
def resign( plyr )
View
@@ -3,7 +3,7 @@ class Player < ActiveRecord::Base
has_many :gameplays
has_many :matches, :through => :gameplays do
def where_turn_of(player)
- self.select{ |m| m.active? && (m.side_to_move == m.side_of(player)) }
+ self.select{ |m| m.active? && (m.side_to_move(:count) == m.side_of(player)) }
end
end
@@ -1,14 +1,6 @@
-<h1>Authenticate#index</h1>
+<h1>Login</h1>
-<% if @current_player -%>
- Logged in
-<% else -%>
- You must login below to use this app.
-<% end -%>
-
-<div>
- <a href="http://apps.facebook.com/enpassant/">Login through Facebook</a>.
-</div>
+<%= request.player ? "Logged In" : "You must login below to use this app." %>
<% form_tag(login_path) do %>
<p>
@@ -1,9 +1,9 @@
<h1>Your current duels</h1>
-Welcome, <%= current_player.name %>.
+Welcome, <%= request.player.name %>.
<h3>Active Matches - Your Turn</h3>
<ul>
- <% current_player.matches.where_turn_of(current_player).each do |m| %>
+ <% request.player.matches.where_turn_of(request.player).each do |m| %>
<li>
<%= link_to m.name, match_url(m) %>
</li>
@@ -12,14 +12,14 @@ Welcome, <%= current_player.name %>.
<h3>Active Matches - Their Turn</h3>
<ul>
- <% ( current_player.matches.active.to_a - current_player.matches.where_turn_of(current_player) ).each do |m| %>
+ <% ( request.player.matches.active.to_a - request.player.matches.where_turn_of(request.player) ).each do |m| %>
<li><%= link_to m.name, match_url(m) %></li>
<% end %>
</ul>
<h3>Completed Matches</h3>
<ul>
-<% current_player.matches.completed.each do |m| %>
+<% request.player.matches.completed.each do |m| %>
<li>
<%= link_to m.name, match_url(m) %>
(<%= m.outcome %>)
@@ -1,5 +1,5 @@
<h1 id="match_name"><%= "#{match.name}" %>
- <% if match.side_of(current_player) %>
+ <% if match.side_of(request.player) %>
<span class="active_yours" data-bind="visible:your_turn"
style="<%= 'display:none' unless your_turn %>">Your Move</span>
<span class="active_theirs" data-bind="visible:!your_turn()"
@@ -46,7 +46,7 @@
</div>
- <% if match.side_of(current_player) %>
+ <% if match.side_of(request.player) %>
<div id="chat_container" style="float:left;">
<div id="chat_window" style="overflow:auto; border:solid 1px black; height: 300px; width: 180px;">
<%= render :partial => 'chats/show' %>
@@ -64,12 +64,12 @@
</div>
-<% if match.side_of(current_player) %>
+<% if match.side_of(request.player) %>
<div id="move_creator_parent" style="clear:both;">
<%= render :partial => "move/create" %>
<!-- Your Move Queue:
<div id="this_move_queue">
- <%= match.gameplays.send(match.side_of(current_player)).move_queue if match.side_of(current_player) %>
+ <%= match.gameplays.send(match.side_of(request.player)).move_queue if match.side_of(request.player) %>
</div>
-->
</div>
@@ -27,7 +27,7 @@ var view = {
selected_piece_coord: new ko.observable(null),
my_next_matches: new ko.observableArray( <%= my_next_matches %> ),
- display_board: new ko.observable(<%= match.moves.count %>),
+ display_board: new ko.observable(<%= match.moves.length %>),
chat_msg: new ko.observable(''), //the message the user is entering
all_moves: new ko.observableArray([
View
@@ -2,12 +2,12 @@
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
-| Controllers | 135 | 92 | 5 | 14 | 2 | 4 |
+| Controllers | 132 | 87 | 5 | 15 | 3 | 3 |
| Helpers | 50 | 36 | 0 | 7 | 0 | 3 |
-| Models | 960 | 658 | 18 | 92 | 5 | 5 |
+| Models | 948 | 659 | 18 | 89 | 4 | 5 |
| Libraries | 376 | 265 | 4 | 36 | 9 | 5 |
+----------------------+-------+-------+---------+---------+-----+-------+
-| Total | 1521 | 1051 | 27 | 149 | 5 | 5 |
+| Total | 1506 | 1047 | 27 | 147 | 5 | 5 |
+----------------------+-------+-------+---------+---------+-----+-------+
- Code LOC: 1051 Test LOC: 0 Code to Test Ratio: 1:0.0
+ Code LOC: 1047 Test LOC: 0 Code to Test Ratio: 1:0.0
View
@@ -23,7 +23,7 @@ def player
player_in_session || player_in_cookie
end
def player= p
- session[:player_id] = p.id
+ session[:player_id] = p && p.id
end
def opponent( match = self.match)

0 comments on commit c4726ad

Please sign in to comment.