Class: TFClient::Models::Local::Database
- Inherits:
-
Object
- Object
- TFClient::Models::Local::Database
- Defined in:
- lib/textflight-client/models/local.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #create_system(id:, nav:) ⇒ Object
-
#initialize(path:) ⇒ Database
constructor
A new instance of Database.
- #system_for_id(id:) ⇒ Object
Constructor Details
#initialize(path:) ⇒ Database
Returns a new instance of Database.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/textflight-client/models/local.rb', line 19 def initialize(path:) @connection = Sequel.connect("sqlite://#{path}") if !@connection.table_exists?(:systems) @connection.create_table(:systems) do primary_key :id column :x, Integer column :y, Integer column :claimed_by, String column :brightness, Integer column :asteroid_ore, String column :asteroid_density, Integer column :links, String column :planets, String end end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
17 18 19 |
# File 'lib/textflight-client/models/local.rb', line 17 def connection @connection end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/textflight-client/models/local.rb', line 17 def path @path end |
Instance Method Details
#create_system(id:, nav:) ⇒ Object
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 |
# File 'lib/textflight-client/models/local.rb', line 42 def create_system(id:, nav:) TFClient.info("creating a new system with id: #{id}") links = nav.links.items.map do |link| [link[:index], link[:direction], link[:drag], link[:faction]] end planets = nav.planets.items.map do |planet| [planet[:index], planet[:type], planet[:name], planet[:faction]] end table = @connection[:systems] table.insert( { id: id, x: nav.coordinates.x, y: nav.coordinates.y, claimed_by: nav.claimed_by ? nav.claimed_by.faction : "", brightness: nav.brightness.value, asteroid_ore: nav.asteroids.ore, asteroid_density: nav.asteroids.density, links: JSON.generate(links), planets: JSON.generate(planets) } ) end |
#system_for_id(id:) ⇒ Object
37 38 39 40 |
# File 'lib/textflight-client/models/local.rb', line 37 def system_for_id(id:) table = @connection[:systems] table.where(id: id) end |