Class: Store

Inherits:
Object
  • Object
show all
Defined in:
lib/store.rb

Class Method Summary collapse

Class Method Details

.load_configObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/store.rb', line 3

def self.load_config()
  begin
    Marshal.load(File.read(".config"))
  rescue
    puts "Welcome to Interstate! No config was detected. Enter API key to continue:".color:yellow
    settings = {"api" => STDIN.gets, "current_map" => nil}
    open('.config', 'wb') { |f| f.puts Marshal.dump(settings) }
    Marshal.load(File.read(".config"))
  end
end

.load_mapsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/store.rb', line 14

def self.load_maps()
  begin
    Marshal.load(File.read(".maps"))
  rescue
    puts "Caching roadmaps...".color:green
    apps = API.roadmaps
    newData = {} 
    apps.each_pair do |key, app|
      app.each do |roadmap|  
        shortcode = "#{Array.new(1) { (rand(122-97) + 97).chr }}#{10 + rand(89)}"
        newData["#{shortcode}"] = { "title" => roadmap["title"], "_id" => roadmap["_id"] , "appId" => roadmap["appId"] }
      end
    end
    open('.maps', 'wb') { |f| f.puts Marshal.dump(newData) }
    Marshal.load(File.read('.maps'))    
  end
end

.load_roadsObject



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
# File 'lib/store.rb', line 32

def self.load_roads()
  begin
    Marshal.load(File.read(".roads"))
  rescue
    puts "Caching roads...".color:green
    roadmapHash = {}
    maps = self.load_maps
    maps.each_pair do |key, roadmap|
      roads = API.roads(roadmap["_id"])
        roadHash = {}
        roads.each do |road|
          if road["isRoute"]
            road["roads"].each do |subRoad|
              shortcode = "#{Array.new(1) { (rand(122-97) + 97).chr }}#{10 + rand(89)}"
              roadHash["#{shortcode}"] = {"title" => subRoad["title"], "_id" => subRoad["_id"], "description" => subRoad["description"], "route" => road["title"]}
            end
          else
            shortcode = "#{Array.new(1) { (rand(122-97) + 97).chr }}#{10 + rand(89)}"
            roadHash["#{shortcode}"] = {"title" => road["title"], "_id" => road["_id"], "description" => road["description"], "route" => ""}
          end
        end
        roadmapHash[key] = roadHash
      end
      data = Marshal.dump(roadmapHash)
      open('.roads', 'wb') { |f| f.puts data }
      data = File.read('.roads')
      Marshal.load(data)
    end
end

.load_staffObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/store.rb', line 101

def self.load_staff()
  begin
    Marshal.load(File.read('.staff'))
  rescue
    puts "Caching staff...".color(:green)
    roadmapHash = {}
    maps = self.load_maps
    maps.each_pair do |key, roadmap|
      roadmap = API.staff(roadmap["_id"])
      staffHash = {}
      roadmap.each do |staff|
        staffHash[staff["_id"]] = staff["fullName"]
      end
      roadmapHash[key] = staffHash
    end
    open('.staff', 'wb') { |f| f.puts Marshal.dump(roadmapHash) }
    Marshal.load(File.read('.staff'))
  end
end

.overwrite_roads(roads, current_map) ⇒ Object



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
91
92
93
94
95
96
97
98
99
# File 'lib/store.rb', line 62

def self.overwrite_roads(roads, current_map)
  cachedRoads = self.load_roads
  currentRoads = cachedRoads[current_map]
  cachedIDs = []
  toRemove = {}
  currentRoads.each_pair do |k, v|
    cachedIDs << v["_id"]
    toRemove[v["_id"]] = k
  end
  
  roads.each do |road|
    if road["isRoute"] 
      road["roads"].each do |subRoad|
        if cachedIDs.include?(subRoad["_id"])
          cachedIDs.delete(subRoad["_id"])
        else
          shortcode = "#{Array.new(1) { (rand(122-97) + 97).chr }}#{10 + rand(89)}"
          currentRoads["#{shortcode}"] = {"title" => subRoad["title"], "_id" => subRoad["_id"], "description" => subRoad["description"], "route" => road["title"]}
        end
      end
    else
      if cachedIDs.include?(road["_id"])
        cachedIDs.delete(road["_id"])
      else
        shortcode = "#{Array.new(1) { (rand(122-97) + 97).chr }}#{10 + rand(89)}"
        currentRoads["#{shortcode}"] = {"title" => road["title"], "_id" => road["_id"], "description" => road["description"], "route" => ""}
      end
    end
  end
  
  cachedIDs.each do |_id|
    currentRoads.delete(toRemove[_id])
  end
  
  cachedRoads[current_map] = currentRoads
  open('.roads', 'wb') { |f| f.puts Marshal.dump(cachedRoads) } 
  return self.load_roads 
end

.reset_configObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/store.rb', line 127

def self.reset_config()
  File.delete(".config")
  File.delete(".maps")
  File.delete(".roads")
  File.delete(".staff")
  self.load_config
  self.load_maps
  self.load_roads
  self.load_staff
end

.set_current_map(id) ⇒ Object



121
122
123
124
125
# File 'lib/store.rb', line 121

def self.set_current_map(id)
  config = Marshal.load(File.read(".config"))
  data = Marshal.dump({"api" => config["api"], "current_map" => id})
  open('.config', 'wb') { |f| f.puts data }
end

.statusesObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/store.rb', line 138

def self.statuses()
  statuses =  { "0"  => "Stopped",
                "1"  => "Started",
                "2"  => "10%",
                "3"  => "20%",
                "4"  => "30%",
                "5"  => "40%",
                "6"  => "50%",
                "7"  => "60%",
                "8"  => "70%",
                "9"  => "80%",
                "10" => "90%",
                "11" => "Ready",
                "12" => "Launched",
                "13" => "Proposed",
                "14" => "Postponed"  }
end