Example #1
0
    def _reached_other_settlement(self):
        self.log.info('%s reached the other warehouse area (%s)', self,
                      self.settlement.get_component(NamedComponent).name)
        if self.sold_resource is not None:
            sellable_amount = self._get_max_sellable_amount(
                self.ship.get_component(StorageComponent).inventory[
                    self.sold_resource])
            if sellable_amount > 0:
                BuyResource(self.settlement.get_component(TradePostComponent),
                            self.ship, self.sold_resource,
                            sellable_amount).execute(self.owner.session)
                if self.bought_resource is None:
                    self.report_success('Sold %d of resource %d' %
                                        (sellable_amount, self.sold_resource))
                    return
                else:
                    self.log.info('%s sold %d of resource %d', self,
                                  sellable_amount, self.sold_resource)

        buyable_amount = self._get_max_buyable_amount()
        if buyable_amount <= 0:
            self.report_failure('No resources can be bought')
            return

        SellResource(self.settlement.get_component(TradePostComponent),
                     self.ship, self.bought_resource,
                     buyable_amount).execute(self.owner.session)
        self.log.info('%s bought %d of resource %d', self, buyable_amount,
                      self.bought_resource)
        self.state = self.missionStates.returning_to_my_settlement
        self._return_to_my_settlement()
Example #2
0
	def transfer(self, res_id, settlement, selling):
		"""Buy or sell the resources"""
		if self.instance.position.distance(settlement.warehouse.position) <= self.instance.radius:
			is_own = settlement.owner is self.instance.owner
			if selling and not is_own: # ship sells resources to settlement
				self.log.debug('InternationalTrade: %s/%s is selling %d of res %d to %s/%s',
				               self.instance.get_component(NamedComponent).name, self.instance.owner.name,
				               self.exchange, res_id,
				               settlement.get_component(NamedComponent).name, settlement.owner.name)
				# international trading has own error handling, no signal_error
				SellResource(settlement.get_component(TradePostComponent), self.instance,
				             res_id, self.exchange).execute(self.instance.session)
			elif selling and is_own: # transfer from settlement to ship
				self.log.debug('Trade: Transferring %s of res %s from %s/%s to %s/%s',
				               self.exchange, res_id,
				               settlement.get_component(NamedComponent).name, settlement.owner.name,
				               self.instance.get_component(NamedComponent).name, self.instance.owner.name)
				TransferResource(self.exchange, res_id, settlement,
				                 self.instance, signal_errors=True).execute(self.instance.session)

			elif not selling and not is_own: # ship buys resources from settlement
				self.log.debug('InternationalTrade: %s/%s is buying %d of res %d from %s/%s',
				               self.instance.get_component(NamedComponent).name,
				               self.instance.owner.name, self.exchange, res_id,
				               settlement.get_component(NamedComponent).name, settlement.owner.name)
				# international trading has own error handling, no signal_error
				BuyResource(settlement.get_component(TradePostComponent), self.instance, res_id, self.exchange).execute(self.instance.session)
			elif not selling and is_own: # transfer from ship to settlement
				self.log.debug('Trade: Transferring %s of res %s from %s/%s to %s/%s',
				               self.exchange, res_id,
				               self.instance.get_component(NamedComponent).name, self.instance.owner.name,
				               settlement.get_component(NamedComponent).name, settlement.owner.name)
				TransferResource(self.exchange, res_id, self.instance,
				                 settlement, signal_errors=True).execute(self.instance.session)