Update api.py, commander.py and one other file
This commit is contained in:
		
							parent
							
								
									3f93d863a0
								
							
						
					
					
						commit
						bb93950fa3
					
				@ -141,3 +141,44 @@ class Api:
 | 
			
		||||
    if 'agent' in data:
 | 
			
		||||
      self.agent.update(data['agent'])
 | 
			
		||||
    return contract
 | 
			
		||||
  
 | 
			
		||||
  def sell(self, ship, typ):
 | 
			
		||||
    units = ship.get_cargo(typ)
 | 
			
		||||
    data = {
 | 
			
		||||
      'symbol': typ,
 | 
			
		||||
      'units': units
 | 
			
		||||
    }
 | 
			
		||||
    data = self.request('post', f'my/ships/{ship}/sell', data)
 | 
			
		||||
    if 'cargo' in data:
 | 
			
		||||
      ship.update(data)
 | 
			
		||||
    if 'agent' in data:
 | 
			
		||||
      self.agent.update(data['agent'])
 | 
			
		||||
    return data
 | 
			
		||||
    
 | 
			
		||||
  def buy(self, ship, typ, amt):
 | 
			
		||||
    data = {
 | 
			
		||||
      'symbol': typ,
 | 
			
		||||
      'units': amt
 | 
			
		||||
    }
 | 
			
		||||
    data = self.request('post', f'my/ships/{ship}/purchase', data)
 | 
			
		||||
    if 'cargo' in data:
 | 
			
		||||
      ship.update(data)
 | 
			
		||||
    if 'agent' in data:
 | 
			
		||||
      self.agent.update(data['agent'])
 | 
			
		||||
    return data
 | 
			
		||||
  
 | 
			
		||||
  def jettison(self, ship, typ):
 | 
			
		||||
    units = ship.get_cargo(typ)
 | 
			
		||||
    if units == 0:
 | 
			
		||||
      print('cargo not found')
 | 
			
		||||
      return
 | 
			
		||||
    data = {
 | 
			
		||||
      'symbol': typ,
 | 
			
		||||
      'units': units
 | 
			
		||||
    }
 | 
			
		||||
    data = self.request('post', f'my/ships/{ship.symbol}/jettison', data)
 | 
			
		||||
    if 'cargo' in data:
 | 
			
		||||
      ship.update(data)
 | 
			
		||||
    if 'agent' in data:
 | 
			
		||||
      self.agent.update(data['agent'])
 | 
			
		||||
    return data
 | 
			
		||||
 | 
			
		||||
@ -72,8 +72,11 @@ class Commander(CommandLine):
 | 
			
		||||
  def after_cmd(self):
 | 
			
		||||
    self.store.flush()
 | 
			
		||||
      
 | 
			
		||||
  def do_info(self):
 | 
			
		||||
    pprint(self.api.info(), 100)
 | 
			
		||||
  def do_info(self, arg=''):
 | 
			
		||||
    if arg.startswith('r'):
 | 
			
		||||
      self.api.info()
 | 
			
		||||
    
 | 
			
		||||
    pprint(self.agent, 100)
 | 
			
		||||
    
 | 
			
		||||
  def do_register(self, faction):
 | 
			
		||||
    self.api.register(faction.upper())
 | 
			
		||||
@ -200,3 +203,26 @@ class Commander(CommandLine):
 | 
			
		||||
      waypoint = self.resolve('Waypoint', arg)
 | 
			
		||||
    r =  self.api.marketplace(waypoint)
 | 
			
		||||
    pprint(r)
 | 
			
		||||
  
 | 
			
		||||
  def do_cargo(self):
 | 
			
		||||
    if not self.has_ship(): return
 | 
			
		||||
    for c, units in self.ship.cargo.items():
 | 
			
		||||
      print(f'{units:4d} {c}')
 | 
			
		||||
      
 | 
			
		||||
  def do_buy(self, resource, amt=None):
 | 
			
		||||
    if not self.has_ship(): return
 | 
			
		||||
    if amt is None:
 | 
			
		||||
      amt = self.ship.cargo_capacity - self.ship.cargo_units
 | 
			
		||||
    self.api.buy(self.ship, resource.upper(), amt)
 | 
			
		||||
    self.do_cargo()
 | 
			
		||||
  
 | 
			
		||||
  def do_sell(self, resource):
 | 
			
		||||
    if not self.has_ship(): return
 | 
			
		||||
    self.api.sell(self.ship, resource.upper())
 | 
			
		||||
    self.do_cargo()
 | 
			
		||||
    
 | 
			
		||||
  def do_dump(self, resource):
 | 
			
		||||
    if not self.has_ship(): return
 | 
			
		||||
    self.api.jettison(self.ship, resource.upper())
 | 
			
		||||
    self.do_cargo()
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
@ -17,8 +17,16 @@ class Marketplace(SystemMember):
 | 
			
		||||
    self.setlst('exchange', d, 'exchange', 'symbol')
 | 
			
		||||
    if 'tradeGoods' in d:
 | 
			
		||||
      self.last_prices = time()
 | 
			
		||||
      prices = {}
 | 
			
		||||
      for g in mg(d, 'tradeGoods'):
 | 
			
		||||
        pass
 | 
			
		||||
        price = {}
 | 
			
		||||
        symbol= mg(g, 'symbol')
 | 
			
		||||
        price['symbol'] = symbol
 | 
			
		||||
        price['buy'] = mg(g, 'purchasePrice')
 | 
			
		||||
        price['sell'] = mg(g, 'sellPrice')
 | 
			
		||||
        prices[symbol] = price
 | 
			
		||||
      self.prices = prices
 | 
			
		||||
      
 | 
			
		||||
  @classmethod
 | 
			
		||||
  def ext(self):
 | 
			
		||||
    return 'mkt'
 | 
			
		||||
@ -26,3 +34,11 @@ class Marketplace(SystemMember):
 | 
			
		||||
  def path(self):
 | 
			
		||||
    sector, system, _ = self.symbol.split('-')
 | 
			
		||||
    return f'atlas/{sector}/{system[0:1]}/{system}/{self.symbol}.{self.ext()}'
 | 
			
		||||
  
 | 
			
		||||
  def f(self, detail=1):
 | 
			
		||||
    r = self.symbol
 | 
			
		||||
    if detail > 1:
 | 
			
		||||
      r += '\n'
 | 
			
		||||
      for p in self.prices.values():
 | 
			
		||||
        r += f'{p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n'
 | 
			
		||||
    return r
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user