Skip to content
This repository has been archived by the owner on Jul 10, 2018. It is now read-only.

Commit

Permalink
heaps of shit
Browse files Browse the repository at this point in the history
  • Loading branch information
bjeanes committed Oct 22, 2008
1 parent 42ba36c commit 8f5d014
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 303 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ log/*
.DS_Store
tmp/**/*
tmp/*
public/game/*
28 changes: 22 additions & 6 deletions app/controllers/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,40 @@
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
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
Expand Down
28 changes: 28 additions & 0 deletions app/controllers/game_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
74 changes: 8 additions & 66 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 0 additions & 28 deletions app/controllers/stream_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 1 addition & 25 deletions app/views/game/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
26 changes: 2 additions & 24 deletions app/views/lobby/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 2 additions & 4 deletions app/views/messages/_message.html.erb
Original file line number Diff line number Diff line change
@@ -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>
36 changes: 0 additions & 36 deletions app/views/messages/edit.html.erb

This file was deleted.

Loading

0 comments on commit 8f5d014

Please sign in to comment.