Class: TacScribe::Cache

Inherits:
Object
  • Object
show all
Includes:
GeoRuby::SimpleFeatures, Singleton
Defined in:
lib/tac_scribe/cache.rb

Overview

The in-memory cache that is updated by events in real-time before the data is synced to the DB

Constant Summary collapse

@@cache =
Concurrent::Hash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reference_latitudeObject

Returns the value of attribute reference_latitude.



19
20
21
# File 'lib/tac_scribe/cache.rb', line 19

def reference_latitude
  @reference_latitude
end

#reference_longitudeObject

Returns the value of attribute reference_longitude.



19
20
21
# File 'lib/tac_scribe/cache.rb', line 19

def reference_longitude
  @reference_longitude
end

Instance Method Details

#clearObject



96
97
98
99
100
# File 'lib/tac_scribe/cache.rb', line 96

def clear
  @@cache.clear
  self.reference_latitude = nil
  self.reference_longitude = nil
end

#dataObject



21
22
23
# File 'lib/tac_scribe/cache.rb', line 21

def data
  @@cache
end

#delete_object(id) ⇒ Object



92
93
94
# File 'lib/tac_scribe/cache.rb', line 92

def delete_object(id)
  @@cache[id][:deleted] = true if @@cache[id]
end

#write_object(object) ⇒ Object



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
# File 'lib/tac_scribe/cache.rb', line 25

def write_object(object)
  if (reference_latitude != 0 || reference_longitude != 0) && object[:type] != "Ground+Static+Aerodrome"
    localize_position(object)
  end

  id = object[:object_id]
  id ||= object[:id].to_s
  object[:id] = id

  cache_object = @@cache[id]

  object[:position] = set_position(object, cache_object)

  %i[object_id latitude longitude color country].each do |key|
    object.delete(key)
  end

  # https://wiki.hoggitworld.com/view/DCS_singleton_coalition
  # Tacview returns a string, CinC returns an integer so we
  # only do the conversion if there is a string.
  if object[:coalition] && object[:coalition].is_a?(String)
    object[:coalition] = case object[:coalition]
                         when 'Enemies' # Enemies is Bluefor
                           2
                         when 'Allies' # Allies is Redfor
                           1
                         else # Neutral
                           0
                         end
  end

  if object[:type] = "Ground+Static+Aerodrome"
    object[:heading] = object[:wind_heading] ? object[:wind_heading] : 0
    object[:speed] = object[:wind_speed] ? object[:wind_speec] : 0
    object.delete(:wind_heading)
    object.delete(:wind_speed)
    object.delete(:category)
    cache_object = object
    # No-op
  elsif cache_object
    object[:heading] = calculate_heading(cache_object, object)
    object[:speed] = calculate_speed(cache_object, object)
    cache_object.merge!(object)
  else
    object[:heading] = -1
    object[:speed] = 0
    cache_object = object
  end

  if !cache_object.key?(:altitude) || !cache_object[:altitude]
    cache_object[:altitude] = 0
  end
  if !cache_object.key?(:coalition) || !cache_object[:coalition]
    cache_object[:coalition] = 2
  end

  cache_object[:pilot] = nil unless cache_object.key?(:pilot)

  cache_object[:group] = nil unless cache_object.key?(:group)

  cache_object[:deleted] = false unless cache_object.key?(:deleted)

  cache_object[:updated_at] = Time.now

  @@cache[id] = cache_object
end