Class: CEML::Player

Inherits:
Struct
  • Object
show all
Includes:
Redis::Objects
Defined in:
lib/ceml/models/player.rb

Constant Summary collapse

MSG_PARAMS =
[:received, :recognized, :situation]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



2
3
4
# File 'lib/ceml/models/player.rb', line 2

def id
  @id
end

Class Method Details

.update(bundle_id, player, cb_obj, &blk) ⇒ Object

data =



42
43
44
45
# File 'lib/ceml/models/player.rb', line 42

def self.update bundle_id, player, cb_obj, &blk
  player[:bundle_id] = player[:squad_id] = bundle_id
  new(player[:id].to_s).update player, cb_obj, &blk
end

Instance Method Details

#clear_answersObject



47
48
49
50
51
52
53
54
# File 'lib/ceml/models/player.rb', line 47

def clear_answers
  updating_lock.lock do
    value = data.value || {}
    value.delete(:qs_answers)
    value.delete(:received)
    data.value = value
  end
end

#clear_incident(id) ⇒ Object



33
34
35
# File 'lib/ceml/models/player.rb', line 33

def clear_incident(id)
  current_incidents.delete(id)
end

#merge_new_player_data(player) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ceml/models/player.rb', line 64

def merge_new_player_data player
  updating_lock.lock do
    old_value = data.value || {}
    new_value = old_value.merge player
    new_value[:qs_answers] = (old_value[:qs_answers]||{}).merge(player[:qs_answers] || {})
    # puts "SAVING DATA: #{new_value.inspect}"
    data.value = new_value
  end
end

#resetObject



9
10
11
12
13
# File 'lib/ceml/models/player.rb', line 9

def reset
  data.clear
  message.clear
  current_incidents.clear
end

#split(player) ⇒ Object



58
59
60
61
62
# File 'lib/ceml/models/player.rb', line 58

def split player
  new_message = player.like(*MSG_PARAMS)
  MSG_PARAMS.each{ |p| player.delete(p) }
  return new_message, player
end

#top_incidentObject



27
28
29
30
31
# File 'lib/ceml/models/player.rb', line 27

def top_incident
  if iid = top_incident_id
    IncidentModel.new(iid)
  end
end

#top_incident_idObject



23
24
25
# File 'lib/ceml/models/player.rb', line 23

def top_incident_id
  current_incidents.last
end

#touch(incident_id) ⇒ Object

incidents =



19
20
21
# File 'lib/ceml/models/player.rb', line 19

def touch(incident_id)
  current_incidents[incident_id] = Time.now.to_i
end

#update(player, cb_obj) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ceml/models/player.rb', line 74

def update player, cb_obj
  player = player.dup
  # puts "UPDATING player id #{id} with #{player.inspect}"
  new_message, player = split(player)
  merge_new_player_data(player)
  cmd = new_message[:recognized]
  if cmd and cb_obj and cb_obj.recognize_override(cmd, new_message, player, self)
    message.delete
  else
    message.value = new_message
    yield player if block_given?
  end
end