class Trader::GameBackend::State

Attributes

state[R]

Public Class Methods

new() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 6
def initialize
  reset
end

Public Instance Methods

account_for(_account_name, create: true) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 30
def account_for(_account_name, create: true)
  account = state['accounts'][_account_name]
  account = state['accounts'][_account_name] = {} if account.nil? && create
  account
end
all_markets() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 22
def all_markets
  state['markets'].values.map { |m| CurrencyPair.new(m['base'], m['quote']) }
end
balance_for(_account_name, _currency, create: true) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 36
def balance_for(_account_name, _currency, create: true)
  account = account_for(_account_name)
  balance = account[_currency.to_s]
  balance = account[_currency.to_s] = new_balance if balance.nil? && create
  balance
end
find_order_by_id(_id) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 49
def find_order_by_id(_id)
  state['markets'].values.each do |market|
    order = market['open'].find { |o| o['id'] == _id } ||
      market['closed'].find { |o| o['id'] == _id }
    return { market: market, order: order } unless order.nil?
  end
  raise ArgumentError, 'order id not found'
end
load_from_file(_path) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 10
def load_from_file(_path)
  if File.exists? _path
    @state = YAML.load File.read(_path)
  else
    reset
  end
end
market_for(_pair, create: true) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 43
def market_for(_pair, create: true)
  market = state['markets'][_pair.to_s]
  market = state['markets'][_pair.to_s] = new_market_for(_pair) if market.nil? && create
  market
end
store_to_file(_path) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 18
def store_to_file(_path)
  File.write _path, YAML.dump(@state)
end
unique_id() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 26
def unique_id
  state['next_id'] += 1
end

Private Instance Methods

new_balance() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 71
def new_balance
  { 'available' => 0, 'frozen' => 0 }
end
new_market_for(_pair) click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 62
def new_market_for(_pair)
  {
    'base' => _pair.base.to_s,
    'quote' => _pair.quote.to_s,
    'open' => [],
    'closed' => []
  }
end
reset() click to toggle source
# File lib/trade-o-matic/adapters/game_backend/state.rb, line 75
def reset
  @state = {
    'next_id' => 0,
    'accounts' => {},
    'markets' => {}
  }
end