Added delayed status and reworked update permissions for match_proposals

This commit is contained in:
Absurdon 2017-09-01 17:52:16 +02:00
parent fa27e9fae4
commit 781132781a
2 changed files with 49 additions and 29 deletions

View file

@ -362,4 +362,8 @@ class Match < ActiveRecord::Base
def can_make_proposal?(cuser)
cuser && (contester1.team.is_leader?(cuser) || contester2.team.is_leader?(cuser))
end
def user_in_match?(user)
user && (user.team == contester1.team || user.team == contester2.team)
end
end

View file

@ -1,22 +1,31 @@
class MatchProposal < ActiveRecord::Base
STATUS_PENDING = 0
STATUS_REVOKED = 1
STATUS_REJECTED = 2
STATUS_PENDING = 0
STATUS_REVOKED = 1
STATUS_REJECTED = 2
STATUS_CONFIRMED = 3
STATUS_DELAYED = 4
# latest time before a match to be confirmed/rejected (in minutes)
CONFIRMATION_LIMIT = 30
belongs_to :match
belongs_to :team
has_many :confirmed_by, class_name: 'Team', uniq: true
#has_many :confirmed_by, class_name: 'Team', uniq: true
attr_accessible :proposed_time, :status
validates_presence_of :match, :team, :proposed_time
scope :of_match, ->(match) { where('match_id = ?', match.id) }
scope :confirmed_for_match, ->(match) { where('match_id = ? AND status = ?', match.id, STATUS_CONFIRMED) }
scope :confirmed_upcoming, ->{ where('status = ? AND proposed_time > UTC_TIMESTAMP()', STATUS_CONFIRMED) }
def status_strings
{STATUS_PENDING => 'Pending',
STATUS_REVOKED => 'Revoked',
STATUS_REJECTED => 'Rejected',
STATUS_CONFIRMED => 'Confirmed'}
STATUS_CONFIRMED => 'Confirmed',
STATUS_DELAYED => 'Delayed'}
end
def can_create? cuser
@ -26,32 +35,11 @@ class MatchProposal < ActiveRecord::Base
end
def can_update? cuser, params = {}
return false unless cuser && match && match.can_make_proposal?(cuser)
return true if cuser.admin?
return false unless cuser && match && (cuser.admin? || match.can_make_proposal?(cuser))
if params.key?(:status) && (status !=(new_status = params[:status].to_i))
return false if new_status == STATUS_PENDING
case this.status
when STATUS_REVOKED
return false
when STATUS_PENDING
case new_status
when STATUS_CONFIRMED, STATUS_REJECTED
return false unless this.team != cuser.team
# TODO: Use time depending on rules
return false if (STATUS_CONFIRMED.to_s == new_status) && (proposed_time < 20.minutes.from_now)
when STATUS_REVOKED
return false unless (this.team == cuser.team)
else
return false
end
when STATUS_CONFIRMED, STATUS_REJECTED
return true
else
return false
end
if params.key?(:status) && (self.status !=(new_status = params[:status].to_i))
return status_change_allowed?(cuser,new_status)
end
true
end
@ -59,4 +47,32 @@ class MatchProposal < ActiveRecord::Base
cuser && cuser.admin?
end
private
def status_change_allowed?(cuser, new_status)
case new_status
when STATUS_PENDING
# never go back to pending
return false
when STATUS_DELAYED
# only confirmed matches can be set to delayed
# only admins can set matches to delayed and only if they are not playing in that match
# matches can only be delayed if they are not to far in the future
return false unless self.status == STATUS_CONFIRMED && cuser.admin? &&
!self.match.user_in_match?(cuser) && self.proposed_time <= CONFIRMATION_LIMIT.minutes.from_now
when STATUS_REVOKED
# unconfirmed can only be revoked by team making the proposal
# confirmed can only be revoked if soon enough before match time
return false unless self.status == STATUS_PENDING && self.team == cuser.team ||
self.status == STATUS_CONFIRMED && self.proposed_time > CONFIRMATION_LIMIT.minutes.from_now
when STATUS_CONFIRMED, STATUS_REJECTED
# only team proposed to can reject or confirm and only if soon enough before match time
return false unless self.status == STATUS_PENDING && self.team != cuser.team &&
self.proposed_time < CONFIRMATION_LIMIT.minutes.from_now
else
# invalid status
return false
end
end
end