-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.rb
154 lines (128 loc) · 3.24 KB
/
game.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'json'
class Team
def initialize(number:)
@number = number
begin
@total_number_of_tests = File.read("#{path}/tmp/total_examples.txt").to_i
rescue
@total_number_of_tests = 1000
end
end
attr_reader(:number)
def rubocop_result
if rubocop_number_of_alerts == -1
"NOT RUN YET: #{rubocop_points} points"
else
"#{rubocop_number_of_alerts} offenses: #{rubocop_points} points"
end
end
def rubocop_color
if rubocop_number_of_alerts == -1
:cyan
elsif rubocop_number_of_alerts == 0
:green
else
:yellow
end
end
def rubocop_points
if rubocop_number_of_alerts == -1
0
else
10 - rubocop_number_of_alerts
end
end
def rubocop_number_of_alerts
if tests_percentage > 99 && File.exists?("#{path}/tmp/rubocop_results.txt")
`cat #{path}/tmp/rubocop_results.txt | grep offense`.split(" ")[3].to_i
else
-1
end
end
def time_seconds
begin
start_time = File.new("#{path}/tmp/start_time.txt").ctime.to_i
end_time = File.new("#{path}/tmp/test_results.txt").ctime.to_i
end_time - start_time
rescue
-1
end
end
def time_points
return 0 if time_seconds.nil?
return 0 if number_of_passed_tests < 1
passed_minutes = time_seconds / 60
if passed_minutes < 10
return 10
elsif passed_minutes > 20
return 0
else
return 20 - passed_minutes
end
end
def time_result
Time.at(time_seconds).utc.strftime("%M:%S")+ " #{time_points} points"
end
def rspec_json
begin
JSON.parse(File.read("#{path}/tmp/test_results.txt"))
rescue
{"error": true, "summary" => {"example_count" => 0, "failure_count" => 0, "pending_count" => 0} }
end
end
def rspec_summary
rspec_json["summary"]
end
def number_of_passed_tests
# Minitest outputs plain text
if File.exists?("#{path}/tmp/test_results.txt") && File.readlines("#{path}/tmp/test_results.txt").grep(/assertions/).any?
splitted = `cat #{path}/tmp/test_results.txt | grep assertions,`.split(" ")
splitted[2].to_i - splitted[4].to_i - splitted[6].to_i
# Rspec outputs a JSON file
else
rspec_summary["example_count"] - rspec_summary["failure_count"] - rspec_summary["pending_count"]
end
end
def total_number_of_tests
@total_number_of_tests
end
def tests_percentage
number_of_passed_tests * 100 / total_number_of_tests
end
def tests_points
20*tests_percentage/100
end
def tests_result
if File.exists?("#{path}/tmp/test_results.txt")
"#{number_of_passed_tests}/#{total_number_of_tests} (#{tests_percentage}%): #{tests_points} points"
else
"NOT RUN YET: #{tests_points} points"
end
end
def tests_color
if !File.exists?("#{path}/tmp/test_results.txt")
:cyan
elsif tests_percentage > 99
:green
else
:yellow
end
end
def path
"team#{number}"
end
def total_points
" #{(tests_points + rubocop_points + time_points).to_s.rjust(2, '0')} points "
end
end
class Game
def initialize(teams:)
@teams = []
for number in teams
@teams << Team.new(number: number)
end
end
def teams
@teams
end
end