Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- add chat to games

   - create channel for a user when showing a game
   - broadcast move to players when creating a move
   - add javascript to game view for lame chat client
  • Loading branch information...
commit 0f78557b169ebb62fe56b3efe9c1c211aeabc621 1 parent 1bca557
@jberkman authored
View
2  web/app/controllers/games_controller.rb
@@ -22,6 +22,8 @@ def show
@game = Game.find(params[:id])
raise Forbidden unless @game.players.include? @current_player.key
+ @channel = Broadcaster.instance.channel @current_player
+
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @game }
View
2  web/app/controllers/moves_controller.rb
@@ -34,10 +34,12 @@ def show
# POST /game/1/moves.xml
def create
@move = Move.new(params[:move])
+ @move.player = @current_player.key
@move.parent_key = @game.key
respond_to do |format|
if @move.save
+ Broadcaster.instance.broadcast @move
format.html { redirect_to(game_move_path(@move.game, @move), :notice => 'Move was successfully created.') }
format.xml { render :xml => @move, :status => :created, :location => @move }
else
View
54 web/app/views/games/show.html.erb
@@ -3,4 +3,58 @@
<p>
<b>Engine:</b>
<%= @game.engine %>
+ <div>
+ <div id="chat-view" class="chat-view"></div>
+ <%= form_tag '/', :id => 'chat-form' do -%>
+ <%= text_field_tag 'chat-input', '', :size => 80 %>
+ <% end -%>
+ </div>
</p>
+<%=raw javascript_include_tag('/_ah/channel/jsapi').sub('.js', '') %>
+<script>
+($(function () {
+ function appendChat(text) {
+ $('#chat-view').append($('<div></div>').text(text));
+ $('#chat-view').scrollTo('100%');
+ }
+
+ $('#chat-form').submit(function () {
+ appendChat($('#chat-input').val());
+ $.ajax({
+ type: 'POST',
+ url: '<%= game_moves_path @game %>',
+ data: {
+ 'move[command]': 'say',
+ 'move[args(1)]': $('#chat-input').val()
+ }
+ });
+ $('#chat-input').val('');
+ return false;
+ });
+
+ channel = new goog.appengine.Channel('<%= @channel %>');
+ channel.open({
+ onopen: function() {
+ console.log("channel is opened.");
+ },
+
+ onmessage: function(msg) {
+ console.log("channel got message: " + msg.data);
+ move = $.parseJSON(msg.data).move;
+ switch (move.command) {
+ case 'say':
+ appendChat(move.args[0]);
+ break;
+ }
+ },
+
+ onerror: function(err) {
+ console.log("channel got an error: " + err);
+ },
+
+ onclose: function() {
+ console.log("channel is closed.");
+ }
+ });
+}));
+</script>
View
5 web/public/stylesheets/epoch.css
@@ -0,0 +1,5 @@
+.chat-view {
+ height: 10ex;
+ overflow: auto;
+ background-color: light-gray;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.