Skip to content

TradeNetwork write up

bonzaiferroni edited this page Jan 25, 2017 · 1 revision

there are 3 files

  1. Diplomat.ts - Just keeps track of who are your allies, trade partners, etc. gist: https://gist.github.com/bonzaiferroni/8d9c5f6f094f53eaaac4a09e5b0c86ca
  2. WorldMap.ts - Scans your world map, keeping track of which rooms are yours and collecting a map of storages/terminals. Also uses observers in your rooms to scan all rooms in range for ally rooms (previously done by RadarMission) gist: https://gist.github.com/bonzaiferroni/86b9de3261dbeaa3f0fc9de6bcf61029
  3. TradeNetwork.ts - This is where all the magic happens, looks for shortages/surpluses and matches them up. gist: https://gist.github.com/bonzaiferroni/ab73313109bdd1360344312e933bbbe9

Very cpu/resource efficient, pretty clean code that is easy to modify to fit your needs. The main thing that would have to remain the same is the conditions for trade between ally terminals, so that we are all working with the same system. We could also implement a quota system, that would be really easy to do, if you wanted to opt-in / opt-out of the network (maybe you are saving up your resources to buy a token, etc.).

bonus: MarketTrader.ts is also decoupled and only depends on these files, so you can have a nice API for buying/selling shortages on the market if you want it. gist: https://gist.github.com/bonzaiferroni/6acb84386fc0324df9a61c45735a1988

here is an example of how you'd run the code, this would just need to run each tick:

// initialize
let diplomat = new Diplomat();
let map = new WorldMap(diplomat);
map.init();
let network = new TradeNetwork(map, diplomat);
network.init();
let market = new MarketTrader(network);

// actions
map.actions();
network.actions();
market.actions();