/
result.rb
76 lines (62 loc) · 1.76 KB
/
result.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Result < ActiveRecord::Base
belongs_to :game
delegate :team1, :to => :game
delegate :team2, :to => :game
delegate :winner, :to => :game
include Confirmable
serialize :result_sets
GameSet = Struct.new :number, :team1_score, :team2_score
def sets
result_sets.keys.collect do |set_number|
scores = result_sets[set_number]
GameSet.new(set_number, scores[:team1], scores[:team2])
end
end
def winner
scores = {:team1=> 0, :team2 => 0}
sets.each do |set|
if set.team1_score > set.team2_score
scores[:team1]+=1
else
scores[:team2]+=1
end
end
return scores[:team1] > scores[:team2] ? game.team1 : game.team2
end
def on_confirm
game.update_attribute :winner_team_id, winner.id
Stat.update_all_for_game(game)
notification= NotificationType.RESULT_CONFIRMED
notification[:params][:name] = self.name
notifications.create notification
end
def as_json(options={})
super(:include => {
:game => {
:include => {
:team1 => {:methods => [:players]},
:team2 => {:methods => [:players]}
}
}
},
:methods => [:winner])
end
#messages to show in confirmations
def confirmation_message
"confirmed the result of the game #{game.description}"
end
def rejection_message
"rejected the result of the game #{game.description}"
end
#messages to show in confirmations
def confirmation_ask_message
"confirm the result of the game #{game.description}"
end
def rejection_ask_message
"reject the result of the game #{game.description}"
end
private
def confirmating_player_groups
game.winner == team1 ? team2 : team1
end
end