Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

heaps of shit

  • Loading branch information...
commit 8f5d01488c2da2c6a2cbdb543cbb32f71adb8e7f 1 parent 42ba36c
@bjeanes authored
View
1  .gitignore
@@ -3,3 +3,4 @@ log/*
.DS_Store
tmp/**/*
tmp/*
+public/game/*
View
28 app/controllers/application.rb
@@ -4,7 +4,7 @@
class ApplicationController < ActionController::Base
include AuthenticatedSystem
helper :all
- protect_from_forgery :secret => '94a45eddfc4f90ebf9470b5d5271ad35'
+ # protect_from_forgery :secret => '94a45eddfc4f90ebf9470b5d5271ad35'
filter_parameter_logging :password, :password_confirmation
protected
@@ -12,16 +12,32 @@ def in_channel?(channel)
Juggernaut.client_in_channel?(current_user.id, channel)
end
- def send_message
- message = render_to_string(:partial => 'messages/message', :object => @message)
- message = 'receiveMessage("%s");' % message.gsub(/\"/, '\"').gsub(/\n|\r/,'') # escape double quotes and remove new lines
- logger.debug(message)
+ def update_user_list(channels = 'lobby')
+ channels = [*channels].flatten
- Juggernaut.send_to_channel(message, @message.channel)
+ channels.each do |channel|
+ users = User.in(channel)
+ Juggernaut.send_to_channel("...", channel)
+ end
render :nothing => true
end
+
+
+ def send_status_message(msg)
+ Juggernaut.send_to_channel(javascript_chat_message(msg), current_game.id)
+ render :nothing => true
+ end
+
+ def javascript_chat_message(message)
+ 'receiveMessage("%s");' % escape_message(message)
+ end
+
+ def escape_message(message)
+ message.gsub(/\"/, '\"').gsub(/\n|\r/,'')
+ end
+
def session_data_by_id(id)
CGI::Session::ActiveRecordStore::Session.find_by_session_id(id).data
rescue
View
28 app/controllers/game_controller.rb
@@ -1,5 +1,6 @@
class GameController < ApplicationController
before_filter :login_required
+ before_filter :check_current_game, :except => :join
def index
@messages = Message.history.for_channel(current_game).reverse
@@ -19,6 +20,25 @@ def join
redirect_to lobby_path
end
+ def status_update
+ # update_user_list(current_game.id)
+ render :nothing => true
+ end
+
+ def attack
+ monster = params[:monster]
+ Juggernaut.send_to_client_on_channel("js_isAttacked('#{monster}');", current_user.next.id, current_game.id )
+ end
+
+ def life_lost
+ current_user.lives -= 1
+ current_user.next.lives += 1
+
+ message = render_to_string :partial => 'messages/gained_a_life',
+ :locals => {:user_1 => current_user.next, :user_2 => current_user}
+ send_status_message(message)
+ end
+
def start
if request.xhr? && current_user.owns_game?(current_game)
current_game.started_at = Time.now
@@ -27,4 +47,12 @@ def start
redirect_to game_path
end
end
+
+ protected
+ def check_current_game
+ if current_game.nil?
+ flash[:error] = "You must be in a game to do that..."
+ redirect_to lobby_path
+ end
+ end
end
View
74 app/controllers/messages_controller.rb
@@ -1,78 +1,20 @@
class MessagesController < ApplicationController
- # GET /messages
- # GET /messages.xml
- def index
- @messages = Message.find(:all)
-
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @messages }
- end
- end
-
- # GET /messages/1
- # GET /messages/1.xml
- def show
- @message = Message.find(params[:id])
-
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render :xml => @message }
- end
- end
-
- # GET /messages/new
- # GET /messages/new.xml
- def new
- @message = Message.new
- end
-
- # GET /messages/1/edit
- def edit
- @message = Message.find(params[:id])
- end
-
- # POST /messages
- # POST /messages.xml
def create
@message = Message.new(params[:message])
@message.sender = current_user
respond_to do |format|
if in_channel?(@message.channel) && @message.save
- flash[:notice] = 'Message was successfully created.'
- format.html { redirect_to(@message) }
- format.js { send_message }
- else
- format.html { render :action => "new" }
- # format.js { send_to_user_in_lobby(@message, :error => true) }
- end
- end
- end
-
- # PUT /messages/1
- # PUT /messages/1.xml
- def update
- @message = Message.find(params[:id])
-
- respond_to do |format|
- if @message.update_attributes(params[:message])
- flash[:notice] = 'Message was successfully updated.'
- format.html { redirect_to(@message) }
- else
- format.html { render :action => "edit" }
+ format.js { send_message }
end
end
end
-
- # DELETE /messages/1
- # DELETE /messages/1.xml
- def destroy
- @message = Message.find(params[:id])
- @message.destroy
-
- respond_to do |format|
- format.html { redirect_to(messages_url) }
- end
+
+ protected
+ def send_message
+ message = render_to_string(:partial => 'messages/message', :object => @message)
+ Juggernaut.send_to_channel(javascript_chat_message(message), @message.channel)
+
+ render :nothing => true
end
end
View
28 app/controllers/stream_controller.rb
@@ -3,47 +3,19 @@ class StreamController < ApplicationController
def part
if request_valid?
- # This should remove us from a game/channel
request_user.offline!
-
- # should do a render juggernaut and tell all user lists in parted channels to refresh
- message = "<div class=\"message\">-&gt; #{request_user} has disconnected</div>"
- render :juggernaut => {:type => :send_to_channels, :channels => params[:channels]} do |page|
- # have to use @request_user here else get method_missing (block scope is in ActionView)
- page.insert_html :bottom, 'messages', message
- page.replace_html 'users', :partial => 'shared/user_list', :locals => {:users => User.online}
- end
end
render :nothing => true
end
def disconnect
request_user.offline! if request_valid?
-
- message = "<div class=\"message\">-&gt; #{request_user} has quit</div>"
- render :juggernaut => {:type => :send_to_all} do |page|
- # have to use @request_user here else get method_missing (block scope is in ActionView)
- page.insert_html :bottom, 'messages', message
- page.replace_html 'users', :partial => 'shared/user_list', :locals => {:users => User.online}
- end
render :nothing => true
end
def join
if request_valid?
request_user.online!
-
- message = "<div class=\"message\">-&gt; #{request_user} has connected</div>"
-
- # This should add the request_user to a game/lobby
-
- # this should push current user name onto all channel user lists
- render :juggernaut => {:type => :send_to_channels, :channels => params[:channels]} do |page|
- # have to use @request_user here else get method_missing (block scope is in ActionView)
- page.insert_html :bottom, 'messages', message
- page.replace_html 'users', :partial => 'shared/user_list', :locals => {:users => User.online}
- end
-
render :nothing => true
else
render :text => "403", :status => 403
View
6 app/models/message.rb
@@ -35,11 +35,7 @@ def channel
channel_id
end
-
- def timestamp
- created_at.strftime("%d %b %y @ %H:%M")
- end
-
+
def sent!
sent = true
save!
View
6 app/models/user.rb
@@ -42,6 +42,12 @@ class User < ActiveRecord::Base
end
end
+ def lives_changed(new_lives)
+ old_lives = self.lives
+ self.lives = new_lives.to_i
+ (old_lives + new_lives).to_i
+ end
+
def self.authenticate(login, password)
return nil if login.blank? || password.blank?
u = find_by_login(login) # need to get the salt
View
26 app/views/game/index.html.erb
@@ -14,31 +14,7 @@
Flash is required to play this game
</div>
</div>
- <div id="chat">
- <div id="messages">
- <div class="history">
- <%= render :partial => 'messages/message', :collection => @messages %>
- <div class="marker">
- &#x21E7; HISTORY &#x21E7;
- </div>
- </div>
- </div>
- <div id="text_input">
- <form method="post" action="<%= messages_path %>" id="send_message_form">
- <%= text_field_tag 'message[content]', '',
- { :id => 'message_content',
- :disabled => true} %>
- <%= hidden_field_tag 'message[kind]', 'channel', {:id => 'message_kind'}%>
- <%= hidden_field_tag 'message[channel_id]', current_game.id, { :id => 'message_channel' } %>
- <%= hidden_field_tag 'authenticity_token', form_authenticity_token %>
- <%= submit_tag "Send", {:id => "message_send", :disabled => true} %>
- </form>
-
- <div id="chat_status">
- Disconnected.
- </div>
- </div>
- </div>
+ <%= render :partial => 'shared/chat', :locals => {:channel => current_game.id} %>
<div id="users">
<%= render :partial => 'shared/user_list', :locals => {:users => User.online} %>
</div>
View
26 app/views/lobby/index.html.erb
@@ -19,30 +19,8 @@
</div>
<h3>Lobby Chat</h3>
- <div id="chat">
- <div id="messages">
- <div class="history">
- <%= render :partial => 'messages/message', :collection => @messages %>
- <div class="marker">
- &#x21E7; HISTORY &#x21E7;
- </div>
- </div>
- </div>
- <div id="text_input">
- <form method="post" action="<%= messages_path %>" id="send_message_form">
- <%= text_field_tag 'message[content]', '',
- { :id => 'message_content',
- :disabled => true} %>
- <%= hidden_field_tag 'message[kind]', 'lobby', {:id => 'message_kind'}%>
- <%= hidden_field_tag 'authenticity_token', form_authenticity_token %>
- <%= submit_tag "Send", {:id => "message_send", :disabled => true} %>
- </form>
-
- <div id="chat_status">
- Disconnected.
- </div>
- </div>
- </div>
+ <%= render :partial => 'shared/chat', :locals => {:channel => 'lobby'} %>
+
<div id="users">
<%= render :partial => 'shared/user_list', :locals => {:users => User.online} %>
</div>
View
6 app/views/messages/_message.html.erb
@@ -1,8 +1,6 @@
<div class="message">
- [<%= message.timestamp %>]
- <span class="sender" style="color:#<%= message.sender.colour %>">
- <%= h(message.sender) %>
- </span>:
+ <%= timestamp(message.created_at) %>
+ <%= user(message.sender) %>:
<%= h(message.content) %>
</div>
View
36 app/views/messages/edit.html.erb
@@ -1,36 +0,0 @@
-<h1>Editing message</h1>
-
-<% form_for(@message) do |f| %>
- <%= f.error_messages %>
-
- <p>
- <%= f.label :sender_id %><br />
- <%= f.text_field :sender_id %>
- </p>
- <p>
- <%= f.label :recipient_id %><br />
- <%= f.text_field :recipient_id %>
- </p>
- <p>
- <%= f.label :channel_id %><br />
- <%= f.text_field :channel_id %>
- </p>
- <p>
- <%= f.label :content %><br />
- <%= f.text_area :content %>
- </p>
- <p>
- <%= f.label :sent %><br />
- <%= f.check_box :sent %>
- </p>
- <p>
- <%= f.label :kind %><br />
- <%= f.text_field :kind %>
- </p>
- <p>
- <%= f.submit "Update" %>
- </p>
-<% end %>
-
-<%= link_to 'Show', @message %> |
-<%= link_to 'Back', messages_path %>
View
30 app/views/messages/index.html.erb
@@ -1,30 +0,0 @@
-<h1>Listing messages</h1>
-
-<table>
- <tr>
- <th>Sender</th>
- <th>Recipient</th>
- <th>Channel</th>
- <th>Content</th>
- <th>Sent</th>
- <th>Kind</th>
- </tr>
-
-<% for message in @messages %>
- <tr>
- <td><%=h message.sender_id %></td>
- <td><%=h message.recipient_id %></td>
- <td><%=h message.channel_id %></td>
- <td><%=h message.content %></td>
- <td><%=h message.sent %></td>
- <td><%=h message.kind %></td>
- <td><%= link_to 'Show', message %></td>
- <td><%= link_to 'Edit', edit_message_path(message) %></td>
- <td><%= link_to 'Destroy', message, :confirm => 'Are you sure?', :method => :delete %></td>
- </tr>
-<% end %>
-</table>
-
-<br />
-
-<%= link_to 'New message', new_message_path %>
View
35 app/views/messages/new.html.erb
@@ -1,35 +0,0 @@
-<h1>New message</h1>
-
-<% form_for(@message) do |f| %>
- <%= f.error_messages %>
-
- <p>
- <%= f.label :sender_id %><br />
- <%= f.text_field :sender_id %>
- </p>
- <p>
- <%= f.label :recipient_id %><br />
- <%= f.text_field :recipient_id %>
- </p>
- <p>
- <%= f.label :channel_id %><br />
- <%= f.text_field :channel_id %>
- </p>
- <p>
- <%= f.label :content %><br />
- <%= f.text_area :content %>
- </p>
- <p>
- <%= f.label :sent %><br />
- <%= f.check_box :sent %>
- </p>
- <p>
- <%= f.label :kind %><br />
- <%= f.text_field :kind %>
- </p>
- <p>
- <%= f.submit "Create" %>
- </p>
-<% end %>
-
-<%= link_to 'Back', messages_path %>
View
33 app/views/messages/show.html.erb
@@ -1,33 +0,0 @@
-<p>
- <b>Sender:</b>
- <%=h @message.sender_id %>
-</p>
-
-<p>
- <b>Recipient:</b>
- <%=h @message.recipient_id %>
-</p>
-
-<p>
- <b>Channel:</b>
- <%=h @message.channel_id %>
-</p>
-
-<p>
- <b>Content:</b>
- <%=h @message.content %>
-</p>
-
-<p>
- <b>Sent:</b>
- <%=h @message.sent %>
-</p>
-
-<p>
- <b>Kind:</b>
- <%=h @message.kind %>
-</p>
-
-
-<%= link_to 'Edit', edit_message_path(@message) %> |
-<%= link_to 'Back', messages_path %>
View
5 config/routes.rb
@@ -18,8 +18,11 @@
# Game routes
map.with_options :controller => "game" do |m|
m.game 'game'
- m.connect 'game/start', :action => "start"
m.join_game 'game/join/:id', :action => "join"
+
+ %w{start status_update life_lost attack}.each do |action|
+ m.connect "game/#{action}", :action => action
+ end
end
map.root :controller => "lobby", :action => "redirect_to_lobby"
View
18 public/javascripts/application.js
@@ -70,16 +70,20 @@ function sendMessage() {
function js_statusUpdate(lives, gold, income)
{
- if(lives <= 0) alert('You are dead!');
- // update player info
- console.log("Lives: "+ lives + ", Gold: " + gold + ", Income: " + income);
+ // new Ajax.Request('/game/status_update', {
+ // method: 'post',
+ // parameters: {lives: lives, gold: gold, income: income}
+ // });
}
// Current user is attacking the next player
function js_attack(monster)
{
- js_isAttacked(monster);
- console.log("Monster: "+ monster);
+ // js_isAttacked(monster);
+ new Ajax.Request('/game/attack', {
+ method: 'post',
+ parameters: {monster: monster}
+ });
}
function js_isAttacked(monster)
@@ -90,9 +94,7 @@ function js_isAttacked(monster)
// flash calls this function
function js_lifeLost()
{
- // Server request will be sent here. the server should then
- // decrease
- console.log('Life lost... TODO: send this to server');
+ new Ajax.Request('/game/life_lost', {method: 'post'});
}
function js_lifeGained()
View
6 public/stylesheets/application.css
@@ -60,10 +60,14 @@ div#chat div#messages {
overflow-x:auto;
}
-div#chat div#messages div.message span {
+div#chat div#messages div.message span.user {
font-weight: bold;
}
+div#chat div#messages div.status {
+font-style:italic;
+}
+
div#chat div#messages div.history {
background-color: #eee;
opacity: .5;
2  vendor/plugins/juggernaut
@@ -1 +1 @@
-Subproject commit 7657bc319d4b9322f43575ab33c23283ce74c51b
+Subproject commit 3b686e839880d3a83d2c7a2700b30aaeaa681cd7
Please sign in to comment.
Something went wrong with that request. Please try again.