2014-03-23 00:22:25 +00:00
|
|
|
class GathersController < ApplicationController
|
2014-03-31 21:33:16 +00:00
|
|
|
before_filter :get_gather, except: [:latest, :index, :create]
|
2014-03-23 00:22:25 +00:00
|
|
|
respond_to :html, :js
|
|
|
|
|
|
|
|
def index
|
|
|
|
@gathers = Gather.ordered.limit(50).all
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2014-04-15 09:59:52 +00:00
|
|
|
render layout: 'full'
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def latest
|
|
|
|
@gather = Gather.last(params[:game])
|
|
|
|
redirect_to @gather
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@gather.admin = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@gather = Gather.new
|
|
|
|
@gather.category_id = params[:gather][:category_id]
|
|
|
|
raise AccessError unless @gather.can_create? cuser
|
|
|
|
|
|
|
|
if @gather.save
|
|
|
|
flash[:notice] = t(:gather_create)
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to_back
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@gather = Gather.basic.find(params[:id])
|
|
|
|
raise AccessError unless @gather.can_update? cuser
|
|
|
|
|
|
|
|
Gatherer.transaction do
|
|
|
|
Gather.transaction do
|
|
|
|
if @gather.update_attributes params[:gather]
|
|
|
|
flash[:notice] = 'Gather was successfully updated.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to @gather
|
|
|
|
end
|
|
|
|
|
|
|
|
def pick
|
|
|
|
@gatherer = @gather.gatherers.find(params[:player])
|
|
|
|
raise AccessError unless @gatherer.can_update? cuser, params
|
|
|
|
|
|
|
|
Gatherer.transaction do
|
|
|
|
Gather.transaction do
|
|
|
|
if @gatherer.update_attribute :team, @gatherer.gather.turn
|
|
|
|
flash[:notice] = t(:gathers_user_pick)
|
|
|
|
else
|
|
|
|
flash[:error] = @gatherer.errors.full_messages.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to @gather
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_gather
|
|
|
|
Gather.transaction do
|
|
|
|
@gather = Gather.basic.find(params[:id], :lock => true)
|
|
|
|
@gather.refresh cuser
|
|
|
|
end
|
|
|
|
|
|
|
|
@gatherer = @gather.gatherers.of_user(cuser).first if cuser
|
2014-10-11 00:50:17 +00:00
|
|
|
update_gatherers
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_gatherers
|
|
|
|
# Update user that has left and came back
|
|
|
|
if @gatherer and @gatherer.status == Gatherer::STATE_LEAVING
|
|
|
|
@gatherer.update_attribute(:status, Gatherer::STATE_ACTIVE)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove any users that left over 30 seconds ago
|
|
|
|
removed_users = false
|
|
|
|
@gather.gatherers.each do |gatherer|
|
|
|
|
if gatherer.status == Gatherer::STATE_LEAVING and gatherer.updated_at < Time.now - 30
|
|
|
|
removed_users = true
|
|
|
|
gatherer.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@gather.reload if removed_users
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
|
|
|
end
|