Class: VideojsUserTrack

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/videojs_user_track.rb

Instance Method Summary collapse

Instance Method Details

#dataObject



51
52
53
54
55
56
57
# File 'app/models/videojs_user_track.rb', line 51

def data
  @data ||= (begin
    a = JSON.parse(ut.status.to_s) rescue []
    (a[ut.video_second_length - 1] ||= 0) if not ut.video_second_length.zero?
    a.map(&:to_i)
  end)
end

#finished?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/videojs_user_track.rb', line 37

def finished?
  # 避免视频没有初始化
  (video_second_length > 10) &&
  # 总时长误差不超过5秒
  ((data.length - video_second_length).abs <= 5) &&
  (
    # 判断中断次数是否超过总秒数的二十分之一,否则就算看完。
    # 比如60秒的视频,里面没看的只有3秒,不管连续还是不连续,就算看完了。
    (played_percents >= 0.95) ||
    # 或者强制标记看完
    self.mark_watched
  )
end

#inc_seconds(seconds, last_played_at = nil) ⇒ Object

like array index in Ruby, idx begins with 0



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/videojs_user_track.rb', line 7

def inc_seconds seconds, last_played_at = nil
  return false if seconds.blank?
  seconds = seconds.map(&:to_i).uniq
  return false if seconds.detect {|second| (second > ut.video_second_length) || (second < 0)} # 大于video_second_length的秒一定不合法

  seconds.each do |second|
    data[second] ||= 0
    data[second] +=  1
    data[second] = 9 if data[second] >= 10 # 最多只记录9次
  end
  self.last_played_at = last_played_at if not last_played_at.to_i.zero?

  resave!
end

#max_unwatched_range_begin_atObject

返回未看的 最大连续区域的首个索引



23
24
25
26
27
28
29
30
# File 'app/models/videojs_user_track.rb', line 23

def max_unwatched_range_begin_at
  data_str = data.join
  # 区分零和非零区域
  flags = data_str.scan(/[1-9]+|0+/)
  flag_zero_max = flags.select {|i| i.to_i.zero? }.max {|i| i.length }
  return 0 if flag_zero_max.nil?
  data_str.index(flag_zero_max) + 1
end

#played_percentsObject



32
33
34
35
# File 'app/models/videojs_user_track.rb', line 32

def played_percents
  return 0.0 if data.size.zero?
  data.reject {|i| i.to_i.zero? }.size / data.size.to_f
end

#resave!Object



61
62
63
64
# File 'app/models/videojs_user_track.rb', line 61

def resave!
  ut.status = data.to_json
  ut.save!
end

#utObject

alias user_track



60
# File 'app/models/videojs_user_track.rb', line 60

def ut; self end