Skip to content

Commit

Permalink
Minor overhaul of how the controller handles parsers
Browse files Browse the repository at this point in the history
Consequently, we now use one 'show' view for all parsers, we
correctly interpret WSplit's OldTime vs. RunTime vs. BestTime, and a
bug causing NaN% split widths has been solved.

Fixes #6, fixes #7, fixes #8, fixes #10. Boom. *drop mic*
  • Loading branch information
glacials committed Nov 24, 2013
1 parent 27eb99b commit 3abb385
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 124 deletions.
31 changes: 15 additions & 16 deletions app/controllers/runs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def show
@run_record.save
@run = parse @run_record
if @run.present?
render @run.parser
render :show
else
if @run_record.hits > 1
render :cant_parse
Expand Down Expand Up @@ -59,23 +59,22 @@ def parse(run)
splits = File.read Rails.root.join "private", "runs", run.nick

begin
result = WsplitParser.new.parse splits
if result.present?
result.parser = :wsplit
return result
result = nil
parsers = [WsplitParser.new, TimesplittrackerParser.new, SplitterzParser.new]
parsers.each do |p|
result = p.parse splits
break if result.present?
end

result = TimesplittrackerParser.new.parse splits
if result.present?
result.parser = :timesplittracker
return result
end

result = SplitterzParser.new.parse splits
if result.present?
result.parser = :splitterz
return result
return nil if result.nil?
# Set a `time` method for splits that converts best_time objects to floats
result.splits.first.class.send(:define_method, :time) { self.best_time.to_s.to_f }
# Set a `time` method for the run that returns the total run time
def result.time
self.splits.inject(0) { |sum, split| sum += split.time }
end
# Set a `finish_time` method for splits that returns the run time when the split finished
result.splits.first.class.send(:define_method, :finish_time) { self.parent.splits.first(self.parent.splits.index(self)+1).inject(0) { |sum, split| sum += split.time.to_s.to_f } }
return result
rescue ArgumentError # comes from non UTF-8 files
return nil
end
Expand Down
17 changes: 11 additions & 6 deletions app/views/runs/wsplit.html.haml → app/views/runs/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
#permalink-indicator
↑ That's your permalink!
%h2 #{@run.title}
- run_time = @run.splits.last.runtime.to_s.to_f
- colors = [:blue, :purple, :green, :red, :orange, :yellow]
- c = 0
- percentage = 0
.size-chart.l-vbox.pure-g{style: "margin-bottom: 30px;"}
- @run.splits.each do |split|
- percentage += split.time.to_s.to_f/run_time*100
- percentage += split.time/@run.time*100
- @run.splits.each do |split|
.size-chart-item.pure-u{class: "size-chart-" + colors[c].to_s, style: "width: #{split.time.to_s.to_f/run_time*100 + (100 - percentage)/@run.splits.length}%;"}
.size-chart-item.pure-u{class: "size-chart-" + colors[c].to_s, style: "width: #{split.time/@run.time*100 + (100 - percentage)/@run.splits.length}%;"}
.size-chart-label{href: "/base/", style: "white-space: nowrap;"}
= split.title
%span.size-chart-size
= Time.at(split.time.to_s.to_f).utc.strftime("%H:%M:%S")
= Time.at(split.time).utc.strftime("%k:%M:%S")
- if @run.time < 20*60
%small>= Time.at(split.time).utc.strftime(".%2N")
- c = (c + 1) % colors.length
.content
%table.pure-table{style: "margin: auto;"}
Expand All @@ -34,9 +35,13 @@
%td
= split.title
%td.align-right
= Time.at(split.time.to_s.to_f).utc.strftime("%H:%M:%S")
= Time.at(split.time).utc.strftime("%k:%M:%S")
- if @run.time < 20*60
%small>= Time.at(split.time).utc.strftime(".%2N")
%td.align-right
= Time.at(split.runtime.to_s.to_f).utc.strftime("%H:%M:%S")
= Time.at(split.finish_time).utc.strftime("%k:%M:%S")
- if @run.time < 20*60
%small>= Time.at(split.finish_time).utc.strftime(".%2N")
- if @run_record.hits < 2
:javascript
$('#permalink-indicator').fadeIn().delay(4000).fadeOut();
46 changes: 0 additions & 46 deletions app/views/runs/splitterz.html.haml

This file was deleted.

46 changes: 0 additions & 46 deletions app/views/runs/timesplittracker.html.haml

This file was deleted.

12 changes: 7 additions & 5 deletions lib/splitterz_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class SplitterzParser < BabelBridge::Parser
rule :splitterz_file, :title_line, many?(:splits)

rule :title_line, :title, ",", :attempts
rule :splits, :title, ",", :runtime, ",", :time, :newline
rule :splits, :title, ",", :run_time, ",", :best_time, :newline

rule :title, /([^,]*)/
rule :runtime, /([\d]*:[\d]*:[\d]*\.[\d]*)/
rule :time, /([\d]*.[\d]*)/
rule :attempts, /(\d*)/
rule :title, /([^,]*)/
rule :attempts, /(\d*)/
rule :run_time, :time
rule :best_time, :time

rule :time, /(\d*:?\d*:?\d*.\d*)/

rule :newline, :windows_newline
rule :newline, :unix_newline
Expand Down
6 changes: 4 additions & 2 deletions lib/timesplittracker_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class TimesplittrackerParser < BabelBridge::Parser

rule :first_line, /([^\t\r\n]*)/, :tab, /([^\t\r\n]*)/, :tab, :newline
rule :title_line, /([^\t\r\n]*)/, :tab, /([^\t\r\n]*)/, :newline
rule :splits, :title, :tab, :time, :newline, :image_path, :tab, :newline
rule :splits, :title, :tab, :best_time, :newline, :image_path, :tab, :newline

rule :title, /([^\t]*)/
rule :time, /(\d*\.\d*)/
rule :best_time, :time
rule :image_path, /([^\t\r\n]*)/

rule :time, /(\d*\.\d*)/

rule :newline, :windows_newline
rule :newline, :unix_newline
rule :tab, "\t"
Expand Down
8 changes: 5 additions & 3 deletions lib/wsplit_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ class WsplitParser < BabelBridge::Parser
rule :attempts_line, "Attempts=", :attempts, :newline
rule :offset_line, "Offset=", :offset, :newline
rule :size_line, "Size=", :size, :newline
rule :splits, :title, ",", :zero, ",", :runtime, ",", :time, :newline
rule :splits, :title, ",", :old_time, ",", :run_time, ",", :best_time, :newline
rule :icons_line, "Icons=", /(.*)/, :newline?

rule :title, /([^,\r\n]*)/
rule :attempts, /(\d+)/
rule :offset, /(\d+)/
rule :size, /([^\r\n]*)/

rule :zero, "0"
rule :runtime, /([\d\.]+)/
rule :old_time, :time
rule :run_time, :time
rule :best_time, :time

rule :time, /([\d\.]+)/

rule :newline, :windows_newline
Expand Down

0 comments on commit 3abb385

Please sign in to comment.