Guest User

Untitled

a guest
Sep 10th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Session
  2.     @DNF: -1
  3.    
  4.     constructor: -> @solves = []
  5.  
  6.     length: -> @solves.length
  7.     reset: -> @solves = []
  8.  
  9.     add: (solve) -> @solves[@solves.length] = solve
  10.     delete: (index) -> @solves.splice index, 1
  11.  
  12.     current_average: (n) -> @average(@solves.length - n, n)
  13.  
  14.     session_average: -> @average(0, @solves.length)
  15.  
  16.     toggle_dnf: (index) ->
  17.         index = @solves.length - 1 if index == null
  18.         @solves[index]['DNF'] = !@solves[index]['DNF']
  19.  
  20.     toggle_plus_two: (index) ->
  21.         index = @solves.length - 1 if index == null
  22.         @solves[index]['plus_two'] = !@solves[index]['plus_two']
  23.         @solves[index]['time'] += `this.solves[index]['plus_two'] ? 2000 : -2000`
  24.  
  25.     trim_count = (n) -> Math.ceil n / 20.0
  26.  
  27.     average: (index, length) ->
  28.         return Session.DNF if (length - index) < 3
  29.  
  30.         trim = trim_count length
  31.         copy = @solves.slice index, index + length
  32.         copy.sort session_sort
  33.         copy.splice 0, trim
  34.         copy.splice copy.length - trim, trim
  35.  
  36.         return Session.DNF if copy[copy.length - 1]['DNF']
  37.  
  38.         sum = copy.reduce ((a, b) -> a + b['time']), 0
  39.         sum / (length - (2 * trim))
  40.  
  41.     session_mean : ->
  42.         return Session.DNF if @solves.length < 1
  43.  
  44.         dnfs = 0
  45.         sum = @solves.reduce ((t, s) ->
  46.             t + `s['DNF'] ? (++dnfs, 0) : s['time']`), 0
  47.  
  48.         return Session.DNF if @solves.length == dnfs
  49.         sum / (@solves.length - dnfs)
  50.  
  51.     session_sort = (a, b) ->
  52.         return 1  if a['DNF']
  53.         return -1 if b['DNF']
  54.         return a['time'] - b['time']
  55.  
  56.     best_average : (length, find_best_singles) ->
  57.         best_avg = Infinity
  58.         best_avg_index = -1
  59.         best_single_index = -1
  60.         worst_single_index = -1
  61.         i = 0
  62.         end = @solves.length - (length - 1)
  63.         while i < end
  64.             a = @average i, length
  65.             if a < best_avg
  66.                 best_avg = a
  67.                 best_avg_index = i
  68.             ++i
  69.  
  70.         if find_best_singles && best_avg_index != -1
  71.             best_single = Infinity
  72.             worst_single = -Infinity
  73.            
  74.             i = best_avg_index
  75.             end = best_avg_index + length
  76.             while i < end
  77.                 if @solves[i]['time'] < best_single
  78.                     best_single = @solves[i]['time']
  79.                     best_single_index = i
  80.                
  81.                 if @solves[i]['time'] > worst_single
  82.                     worst_single = @solves[i]['time']
  83.                     worst_single_index = i
  84.  
  85.         'avg': best_avg, 'index': best_avg_index,
  86.         'best_single_index': best_single_index,
  87.         'worst_single_index': worst_single_index
  88.  
  89.     load: ->
  90.         if localStorage
  91.             local_solves = localStorage.getItem 'session.solves'
  92.             if local_solves != null
  93.                 @solves = JSON.parse local_solves
  94.  
  95.     save: ->
  96.         if localStorage
  97.             localStorage.setItem 'session.solves', JSON.stringify @solves
Add Comment
Please, Sign In to add comment