Join and Start games front-end

This commit is contained in:
Jesse C. Fisher 2015-11-06 17:11:19 -08:00
parent 1317aa1a6d
commit 9d9e4a55c5
6 changed files with 73 additions and 26 deletions

View File

@ -1,30 +1,30 @@
var Game = React.createClass({ var Game = React.createClass({
mixins: [SetIntervalMixin], mixins: [SetIntervalMixin],
// TODO: Declare all propTypes
propTypes: { propTypes: {
title: React.PropTypes.string, title: React.PropTypes.string,
players: React.PropTypes.array, players: React.PropTypes.array,
id: React.PropTypes.number, id: React.PropTypes.number,
source: React.PropTypes.string, url: React.PropTypes.string,
play_to_beat_string: React.PropTypes.string play_to_beat_string: React.PropTypes.string
}, },
getInitialState: function() { getInitialState: function() {
return { return {
data: null,
players: [], players: [],
title: "", title: "",
play_to_beat_string: "" play_to_beat_string: "",
is_started: false,
is_joinable: false,
is_startable: false
}; };
}, },
loadGameFromServer: function() { loadGameFromServer: function() {
$.get(this.props.source, function(result) { $.getJSON(this.props.url, function(result) {
if (this.isMounted()) { if (this.isMounted()) {
this.setState({ this.setState({ data: result });
title: result.title,
players: result.players,
current_player_id: result.current_player_id,
play_to_beat_string: result.play_to_beat_string
});
} }
}.bind(this)); }.bind(this));
}, },
@ -37,36 +37,79 @@ var Game = React.createClass({
handlePlayHandSubmit: function(card_ids) { handlePlayHandSubmit: function(card_ids) {
$.ajax({ $.ajax({
url: this.props.url + '/play_hand', url: this.props.url + '/play_hand',
dataType: 'json',
type: 'PATCH', type: 'PATCH',
dataType: 'json',
data: card_ids, data: card_ids,
success: function(data) { success: function(data) {
// TODO: Set state instead of this custom method? // TODO: Set state instead of this custom method?
this.loadGameFromServer(); this.loadGameFromServer();
//this.setState({data: data});
}.bind(this), }.bind(this),
error: function(xhr, status, err) { error: function(xhr, status, err) {
//console.log("handlePlay error");
console.error(this.props.url, status, err.toString()); console.error(this.props.url, status, err.toString());
}.bind(this) }.bind(this)
}); });
}, },
handleStart: function() {
$.ajax({
url: this.props.url + '/start',
type: 'POST',
dataType: 'json',
complete: function (jqXHR, textStatus) {
// callback
},
success: function (data, textStatus, jqXHR) {
// success callback
},
error: function (jqXHR, textStatus, errorThrown) {
// error callback
}
});
},
handleJoin: function() {
$.ajax({
url: this.props.url + '/join',
type: 'POST',
dataType: 'json',
complete: function (jqXHR, textStatus) {
// callback
},
success: function (data, textStatus, jqXHR) {
// success callback
},
error: function (jqXHR, textStatus, errorThrown) {
// error callback
}
});
},
render: function() { render: function() {
if (!this.state.data) {
return <div>Loading...</div>;
}
return ( return (
<div> <div>
<div>Title: {this.state.title}</div> <div>Title: {this.state.data.title}</div>
<div>Id: {this.props.id}</div> <div>Id: {this.props.id}</div>
<div>Current Player: {this.state.current_player_id}</div> <div>Current Player: {this.state.data.current_player_id}</div>
<div>Hand to beat: {this.state.play_to_beat_string}</div> <div>Hand to beat: {this.state.data.play_to_beat_string}</div>
<div className='player-list'> <div className='player-list'>
<PlayerList <PlayerList
game_id={this.props.id} game_id={this.props.id}
players={this.state.players} players={this.state.data.players}
onPlayHandSubmit={this.handlePlayHandSubmit} onPlayHandSubmit={this.handlePlayHandSubmit}
current_player_id={this.state.current_player_id} /> current_player_id={this.state.data.current_player_id} />
</div> </div>
<div> <div>
{/* TODO: If not joinable, show why. i.e. game full*/ }
{this.state.data.is_joinable ? <button onClick={this.handleJoin}>Join</button> : null }
{/* TODO: If not startable, show why. i.e. need more players */}
{this.state.data.is_started ? null : <button onClick={this.handleStart} disabled={this.state.data.is_startable ? null : "disabled" }>Start</button>}
</div>
<div>
{console.log('is startable?')}
{console.log(this.state.data.is_startable)}
</div> </div>
</div> </div>
); );

View File

@ -1,5 +1,5 @@
class GamesController < ApplicationController class GamesController < ApplicationController
before_action :set_game, only: [:show, :edit, :update, :destroy, :play_hand] before_action :set_game, only: [:show, :edit, :update, :destroy, :play_hand, :join, :start, :my_inventory]
respond_to :html, :json respond_to :html, :json
@ -96,15 +96,12 @@ class GamesController < ApplicationController
end end
def join def join
@game = Game.find(params[:game_id])
@game.add_player_from_user(current_user) @game.add_player_from_user(current_user)
flash[:notice] = 'Successfully joined game...' flash[:notice] = 'Successfully joined game...'
redirect_to(@game) redirect_to(@game)
end end
def start def start
@game = Game.find(params[:game_id])
begin begin
@game.start @game.start
rescue StandardError => e rescue StandardError => e
@ -120,7 +117,6 @@ class GamesController < ApplicationController
end end
def my_inventory def my_inventory
@game = Game.find(params[:game_id])
@player = @game.players.find_by(user_id: current_user.id) @player = @game.players.find_by(user_id: current_user.id)
end end

View File

@ -94,6 +94,10 @@ class Game < ActiveRecord::Base
end end
end end
def joinable? user
!self.users.include?(user) && !self.full?
end
def next_player_id def next_player_id
player_ids[(player_ids.index(current_player_id) + 1 ) % player_ids.length] player_ids[(player_ids.index(current_player_id) + 1 ) % player_ids.length]
end end

View File

@ -1,4 +1,4 @@
= react_component 'Game', id: @game.id, current_player: @game.current_player.try(:id), url: "#{game_path @game}", source: "#{game_path @game}.json", hand_to_beat: @game.play_to_beat.try(:play).try(:to_s) = react_component 'Game', id: @game.id, url: "#{game_path @game}"
-# TODO: Remove deprecated commented out code -# TODO: Remove deprecated commented out code
-#%p -#%p

View File

@ -5,6 +5,10 @@ else
json.play_to_beat_string @game.play_to_beat.try(:play).try(:to_s) json.play_to_beat_string @game.play_to_beat.try(:play).try(:to_s)
end end
json.is_joinable @game.joinable?(current_user)
json.is_started !@game.status.nil?
json.is_startable @game.startable?
json.players @game.players do |player| json.players @game.players do |player|
json.id player.id json.id player.id
json.email player.user.email json.email player.user.email

View File

@ -2,10 +2,10 @@ Rails.application.routes.draw do
resources :players resources :players
resources :games do resources :games do
get 'start'
get 'join'
get 'my-inventory'
member do member do
get 'my-inventory'
post 'start'
post 'join'
patch 'play_hand' patch 'play_hand'
end end
end end