Class: ActiveGenie::Ranker::Entities::Player
- Inherits:
-
Object
- Object
- ActiveGenie::Ranker::Entities::Player
show all
- Defined in:
- lib/active_genie/entities/player.rb
Constant Summary
collapse
- BASE_ELO =
1000
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params) ⇒ Player
Returns a new instance of Player.
9
10
11
12
13
14
15
16
17
|
# File 'lib/active_genie/entities/player.rb', line 9
def initialize(params)
@params = if params.is_a?(String)
{ content: params }
elsif params.respond_to?(:transform_keys)
params.transform_keys(&:to_sym)
else
params.dup
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/active_genie/entities/player.rb', line 104
def method_missing(method_name, *args, &)
if method_name == :[] && args.size == 1
attr_name = args.first.to_sym
return send(attr_name) if respond_to?(attr_name)
return nil
end
super
end
|
Instance Attribute Details
#eliminated ⇒ Object
51
52
53
|
# File 'lib/active_genie/entities/player.rb', line 51
def eliminated
@eliminated ||= @params[:eliminated]
end
|
Instance Method Details
#content ⇒ Object
19
20
21
|
# File 'lib/active_genie/entities/player.rb', line 19
def content
@content ||= @params[:content] || @params
end
|
#draw! ⇒ Object
66
67
68
|
# File 'lib/active_genie/entities/player.rb', line 66
def draw!
@ffa_draw_count = ffa_draw_count + 1
end
|
#elo ⇒ Object
35
36
37
|
# File 'lib/active_genie/entities/player.rb', line 35
def elo
@elo = @elo || @params[:elo] || generate_elo_by_score
end
|
#elo=(value) ⇒ Object
60
61
62
|
# File 'lib/active_genie/entities/player.rb', line 60
def elo=(value)
@elo = value || BASE_ELO
end
|
#ffa_draw_count ⇒ Object
47
48
49
|
# File 'lib/active_genie/entities/player.rb', line 47
def ffa_draw_count
@ffa_draw_count ||= @params[:ffa_draw_count] || 0
end
|
#ffa_lose_count ⇒ Object
43
44
45
|
# File 'lib/active_genie/entities/player.rb', line 43
def ffa_lose_count
@ffa_lose_count ||= @params[:ffa_lose_count] || 0
end
|
#ffa_score ⇒ Object
78
79
80
|
# File 'lib/active_genie/entities/player.rb', line 78
def ffa_score
(ffa_win_count * 3) + ffa_draw_count
end
|
#ffa_win_count ⇒ Object
39
40
41
|
# File 'lib/active_genie/entities/player.rb', line 39
def ffa_win_count
@ffa_win_count ||= @params[:ffa_win_count] || 0
end
|
#id ⇒ Object
27
28
29
|
# File 'lib/active_genie/entities/player.rb', line 27
def id
@id ||= @params[:id] || Digest::MD5.hexdigest(content.to_s)
end
|
#lose! ⇒ Object
74
75
76
|
# File 'lib/active_genie/entities/player.rb', line 74
def lose!
@ffa_lose_count = ffa_lose_count + 1
end
|
#name ⇒ Object
23
24
25
|
# File 'lib/active_genie/entities/player.rb', line 23
def name
@name ||= @params[:name] || (content.is_a?(Hash) ? (content[:name] || content.to_s[0..10]) : content[0..10])
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
117
118
119
|
# File 'lib/active_genie/entities/player.rb', line 117
def respond_to_missing?(method_name, include_private = false)
method_name == :[] || super
end
|
#score ⇒ Object
31
32
33
|
# File 'lib/active_genie/entities/player.rb', line 31
def score
@score ||= @params[:score]
end
|
#score=(value) ⇒ Object
55
56
57
58
|
# File 'lib/active_genie/entities/player.rb', line 55
def score=(value)
@score = value
@elo = generate_elo_by_score
end
|
#sort_value ⇒ Object
82
83
84
|
# File 'lib/active_genie/entities/player.rb', line 82
def sort_value
(ffa_score * 1_000_000) + ((elo || 0) * 100) + (score || 0)
end
|
#to_h ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/active_genie/entities/player.rb', line 90
def to_h
{
id:, name:, content:,
score:, elo:,
ffa_win_count:, ffa_lose_count:, ffa_draw_count:,
eliminated:, ffa_score:, sort_value:
}
end
|
#to_json(*_args) ⇒ Object
86
87
88
|
# File 'lib/active_genie/entities/player.rb', line 86
def to_json(*_args)
to_h.to_json
end
|
#to_s ⇒ Object
100
101
102
|
# File 'lib/active_genie/entities/player.rb', line 100
def to_s
content.to_s
end
|
#win! ⇒ Object
70
71
72
|
# File 'lib/active_genie/entities/player.rb', line 70
def win!
@ffa_win_count = ffa_win_count + 1
end
|