Skip to content

Commit

Permalink
Merge pull request #131 from CyberiaResurrection/WringOutSpecTrade
Browse files Browse the repository at this point in the history
Wring out SpeculativeTrade
  • Loading branch information
CyberiaResurrection authored Dec 8, 2024
2 parents 2644e96 + 523fa7c commit d35f0d0
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 18 deletions.
35 changes: 17 additions & 18 deletions PyRoute/SpeculativeTrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def process_tradegoods(self):
return
for (world, neighbor) in self.stars.edges():
worldstar = self.stars.nodes[world]['star']
neighborstar = self.stars.nodes[world]['star']
neighborstar = self.stars.nodes[neighbor]['star']
distance = worldstar.distance(neighborstar)
source_market = max(self.get_market_price(worldstar, neighborstar) - distance, 0)
target_market = max(self.get_market_price(neighborstar, worldstar) - distance, 0)
Expand Down Expand Up @@ -96,39 +96,38 @@ def get_source_tradegoods(self, star):

def T5_calculate_tradegoods(self, star):
cost = 3.0
cost -= 1 if star.tradeCode.agricultural else 0
cost -= 1 if star.tradeCode.asteroid else 0
cost += SpeculativeTrade._calc_tradegoods_core(star)
cost += 1 if star.tradeCode.barren else 0
cost += 1 if star.tradeCode.desert else 0
cost += 1 if star.tradeCode.fluid else 0
cost -= 1 if star.tradeCode.high else 0
cost -= 1 if star.tradeCode.industrial else 0
cost += 1 if star.tradeCode.low else 0
cost += 1 if star.tradeCode.nonindustrial else 0
cost -= 1 if star.tradeCode.poor else 0
cost += 1 if star.tradeCode.rich else 0
cost += 1 if star.tradeCode.vacuum else 0
cost += star.tl / 10.0

return cost

def CT_calculate_tradegoods(self, star):
cost = 4.0
cost += SpeculativeTrade._calc_tradegoods_core(star)
cost -= 1 if star.tradeCode else 0
cost += 2 if star.tradeCode.low else 0
cost += star.tl / 10.0
cost -= 1 if 'A' in star.port else 0
cost += 1 if 'C' in star.port else 0
cost += 2 if 'D' in star.port else 0
cost += 3 if 'E' in star.port else 0
cost += 5 if 'X' in star.port else 0
return cost

@staticmethod
def _calc_tradegoods_core(star):
cost = 0
cost -= 1 if star.tradeCode.agricultural else 0
cost -= 1 if star.tradeCode.asteroid else 0
cost += 1 if star.tradeCode.desert else 0
cost += 1 if star.tradeCode.fluid else 0
cost -= 1 if star.tradeCode.high else 0
cost -= 1 if star.tradeCode else 0
cost += 2 if star.tradeCode.low else 0
cost += 1 if star.tradeCode.nonindustrial else 0
cost -= 1 if star.tradeCode.poor else 0
cost += 1 if star.tradeCode.rich else 0
cost += 1 if star.tradeCode.vacuum else 0
cost += star.tl / 10.0
cost -= 1 if 'A' in star.port else 0
cost += 1 if 'C' in star.port else 0
cost += 2 if 'D' in star.port else 0
cost += 3 if 'E' in star.port else 0
cost += 5 if 'X' in star.port else 0

return cost
192 changes: 192 additions & 0 deletions Tests/testSpeculativeTrade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
"""
Created on Nov 28, 2024
@author: CyberiaResurrection
"""

from PyRoute.AreaItems.Galaxy import Galaxy
from PyRoute.AreaItems.Sector import Sector
from PyRoute.Inputs.ParseStarInput import ParseStarInput
from PyRoute.SpeculativeTrade import SpeculativeTrade
from PyRoute.Star import Star
from PyRoute.SystemData.UWP import UWP
from PyRoute.TradeCodes import TradeCodes
from Tests.baseTest import baseTest
import unittest


class testSpeculativeTrade(baseTest):
def test_spec_trade_across_TL(self):
s = '0101 000000000000000 {} {} - - A 000 0000D'
t = '0102 000000000000000 {} {} - - A 000 0000D'
pop_code = 'scaled'
ru_calc = 'scaled'

cases = [
('?000000-0', ['As'], '?000000-1', ['As', 'De'], "CT", 1500, 1400),
('?000000-0', ['As'], '?000000-1', ['As', 'De'], "T5", 1500, 1400)
]

for case in cases:
with self.subTest(msg=case[4]):
s_actual = s.format(case[0], " ".join(case[1]).ljust(38))
t_actual = t.format(case[2], " ".join(case[3]).ljust(38))
trade_type = case[4]
expSrcPrice = case[5]
expTrgPrice = case[6]
sector = Sector('# Core', '# 0, 0')

foo = Star.parse_line_into_star(s_actual, sector, pop_code, ru_calc)
foo.index = 1

bar = Star.parse_line_into_star(t_actual, sector, pop_code, ru_calc)
bar.index = 2

galaxy = Galaxy(min_btn=15, max_jump=4)
galaxy.sectors[sector.name] = sector
galaxy.star_mapping[1] = foo
galaxy.star_mapping[2] = bar
galaxy.stars.add_node(foo.index, star=foo)
galaxy.stars.add_node(bar.index, star=bar)
galaxy.stars.add_edge(foo.index, bar.index)
sector.worlds.append(foo)
sector.worlds.append(bar)

spec = SpeculativeTrade(trade_type, galaxy.stars)
spec.process_tradegoods()

edge = galaxy.stars.get_edge_data(foo.index, bar.index)
self.assertEqual(expSrcPrice, edge['SourceMarketPrice'])
self.assertEqual(expTrgPrice, edge['TargetMarketPrice'])

def test_spec_trade_same_TL_no_codes(self):
s = '0101 000000000000000 {} {} - - A 000 0000D'
t = '0102 000000000000000 {} {} - - A 000 0000D'
pop_code = 'scaled'
ru_calc = 'scaled'

cases = [
('?000000-0', [], '?000000-0', [], "CT", 1000, 1000),
('?000000-0', [], '?000000-0', [], "T5", 1000, 1000),
('A000000-0', [], 'X000000-0', [], "CT", 2000, 0),
('A000000-0', [], 'X000000-0', [], "T5", 1000, 1000),
('B000000-0', [], 'E000000-0', [], "CT", 1000, 0),
('B000000-0', [], 'E000000-0', [], "T5", 1000, 1000),
('C000000-0', [], 'D000000-0', [], "CT", 0, 0),
('C000000-0', [], 'D000000-0', [], "T5", 1000, 1000),
('D000000-0', [], 'C000000-0', [], "CT", 0, 0),
('D000000-0', [], 'C000000-0', [], "T5", 1000, 1000),
('E000000-0', [], 'B000000-0', [], "CT", 0, 1000),
('E000000-0', [], 'B000000-0', [], "T5", 1000, 1000),
('X000000-0', [], 'A000000-0', [], "CT", 0, 2000),
('X000000-0', [], 'A000000-0', [], "T5", 1000, 1000)
]

for case in cases:
with self.subTest(msg=case[4] + " " + case[0]):
s_actual = s.format(case[0], " ".join(case[1]).ljust(38))
t_actual = t.format(case[2], " ".join(case[3]).ljust(38))
trade_type = case[4]
expSrcPrice = case[5]
expTrgPrice = case[6]
sector = Sector('# Core', '# 0, 0')

foo = Star.parse_line_into_star(s_actual, sector, pop_code, ru_calc)
foo.index = 1

bar = Star.parse_line_into_star(t_actual, sector, pop_code, ru_calc)
bar.index = 2

galaxy = Galaxy(min_btn=15, max_jump=4)
galaxy.sectors[sector.name] = sector
galaxy.star_mapping[1] = foo
galaxy.star_mapping[2] = bar
galaxy.stars.add_node(foo.index, star=foo)
galaxy.stars.add_node(bar.index, star=bar)
galaxy.stars.add_edge(foo.index, bar.index)
sector.worlds.append(foo)
sector.worlds.append(bar)

spec = SpeculativeTrade(trade_type, galaxy.stars)
spec.process_tradegoods()

edge = galaxy.stars.get_edge_data(foo.index, bar.index)
self.assertEqual(expSrcPrice, edge['SourceMarketPrice'])
self.assertEqual(expTrgPrice, edge['TargetMarketPrice'])

def test_spec_trade_same_TL_with_codes(self):
s = '0101 000000000000000 {} {} - - A 000 0000D'
t = '0102 000000000000000 {} {} - - A 000 0000D'
pop_code = 'scaled'
ru_calc = 'scaled'

cases = [
('?000000-0', ['Ag'], '?000000-0', ['In'], "CT", 3000, 2000),
('?000000-0', ['Ag'], '?000000-0', ['In'], "T5", 3000, 3000),
('?000000-0', ['As'], '?000000-0', ['Oc'], "CT", 1000, 0),
('?000000-0', ['As'], '?000000-0', ['Oc'], "T5", 1000, 0),
('?000000-0', ['Ba'], '?000000-0', ['Wa'], "CT", 1000, 0),
('?000000-0', ['Ba'], '?000000-0', ['Wa'], "T5", 0, 0),
('?000000-0', ['De'], '?000000-0', ['Va'], "CT", 1000, 0),
('?000000-0', ['De'], '?000000-0', ['Va'], "T5", 1000, 0),
('?000000-0', ['Fl'], '?000000-0', ['Ri'], "CT", 0, 0),
('?000000-0', ['Fl'], '?000000-0', ['Ri'], "T5", 0, 0),
('?000000-0', ['Hi'], '?000000-0', ['Po'], "CT", 2000, 2000),
('?000000-0', ['Hi'], '?000000-0', ['Po'], "T5", 2000, 1000),
('?000000-0', ['Ic'], '?000000-0', ['Ni'], "CT", 1000, 0),
('?000000-0', ['Ic'], '?000000-0', ['Ni'], "T5", 1000, 0),
('?000000-0', ['In'], '?000000-0', ['Na'], "CT", 1000, 1000),
('?000000-0', ['In'], '?000000-0', ['Na'], "T5", 2000, 1000),
('?000000-0', ['Lo'], '?000000-0', ['Lo'], "CT", 0, 0),
('?000000-0', ['Lo'], '?000000-0', ['Lo'], "T5", 0, 0),
('?000000-5', ['Na'], '?000000-5', ['In'], "CT", 500, 500),
('?000000-5', ['Na'], '?000000-5', ['In'], "T5", 500, 1500),
('?000000-5', ['Ni'], '?000000-5', ['Ic'], "CT", 0, 500),
('?000000-5', ['Ni'], '?000000-5', ['Ic'], "T5", 0, 500),
('?000000-5', ['Po'], '?000000-5', ['Hi'], "CT", 1500, 1500),
('?000000-5', ['Po'], '?000000-5', ['Hi'], "T5", 500, 1500),
('?000000-5', ['Ri'], '?000000-5', ['Fl'], "CT", 0, 0),
('?000000-5', ['Ri'], '?000000-5', ['Fl'], "T5", 0, 0),
('?000000-5', ['Va'], '?000000-5', ['De'], "CT", 0, 500),
('?000000-5', ['Va'], '?000000-5', ['De'], "T5", 0, 500),
('?000000-5', ['Wa'], '?000000-5', ['Ba'], "CT", 0, 500),
('?000000-5', ['Wa'], '?000000-5', ['Ba'], "T5", 0, 0),
('?000000-5', ['Oc'], '?000000-5', ['As'], "CT", 0, 500),
('?000000-5', ['Oc'], '?000000-5', ['As'], "T5", 0, 500),
]

for case in cases:
with self.subTest(msg=case[4] + " " + case[0] + " " + " ".join(case[1])):
s_actual = s.format(case[0], " ".join(case[1]).ljust(38))
t_actual = t.format(case[2], " ".join(case[3]).ljust(38))
trade_type = case[4]
expSrcPrice = case[5]
expTrgPrice = case[6]
sector = Sector('# Core', '# 0, 0')

foo = Star.parse_line_into_star(s_actual, sector, pop_code, ru_calc)
foo.index = 1

bar = Star.parse_line_into_star(t_actual, sector, pop_code, ru_calc)
bar.index = 2

galaxy = Galaxy(min_btn=15, max_jump=4)
galaxy.sectors[sector.name] = sector
galaxy.star_mapping[1] = foo
galaxy.star_mapping[2] = bar
galaxy.stars.add_node(foo.index, star=foo)
galaxy.stars.add_node(bar.index, star=bar)
galaxy.stars.add_edge(foo.index, bar.index)
sector.worlds.append(foo)
sector.worlds.append(bar)

spec = SpeculativeTrade(trade_type, galaxy.stars)
spec.process_tradegoods()

edge = galaxy.stars.get_edge_data(foo.index, bar.index)
self.assertEqual(expSrcPrice, edge['SourceMarketPrice'])
self.assertEqual(expTrgPrice, edge['TargetMarketPrice'])


if __name__ == '__main__':
unittest.main()

0 comments on commit d35f0d0

Please sign in to comment.