]> git.scottworley.com Git - planeteer/blob - planeteer.go
87c74486583e3b835854bfe61b2ae84e4eaacecd
[planeteer] / planeteer.go
1 /* Planeteer: Give trade route advice for Planets: The Exploration of Space
2 * Copyright (C) 2011 Scott Worley <sworley@chkno.net>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import "flag"
21 import "json"
22 import "os"
23 import "fmt"
24
25 var datafile = flag.String("planet_data_file", "planet-data",
26 "The file to read planet data from")
27
28 type Commodity struct {
29 Name string
30 BasePrice int
31 CanSell bool
32 Limit int
33 }
34
35 type planet_data struct {
36 Commodities []Commodity
37 Planets []struct {
38 Name string
39 BeaconOn bool
40 /* Use relative prices rather than absolute prices because you
41 can get relative prices without traveling to each planet. */
42 RelativePrices map [string] int
43 }
44 }
45
46 func ReadData() (data planet_data) {
47 f, err := os.Open(*datafile)
48 if err != nil {
49 panic(err)
50 }
51 defer f.Close()
52 err = json.NewDecoder(f).Decode(&data)
53 if err != nil {
54 panic(err)
55 }
56 return
57 }
58
59 func TradeValue(data planet_data,
60 from_index, to_index, commodity_index, quantity int) int {
61
62 commodity := &data.Commodities[commodity_index]
63 if !commodity.CanSell {
64 return 0
65 }
66
67 from_planet := &data.Planets[from_index]
68 from_relative_price, from_available := from_planet.RelativePrices[commodity.Name]
69 if !from_available {
70 return 0
71 }
72
73 to_planet := &data.Planets[to_index]
74 to_relative_price, to_available := to_planet.RelativePrices[commodity.Name]
75 if !to_available {
76 return 0
77 }
78
79 from_absolute_price := from_relative_price * commodity.BasePrice
80 to_absolute_price := to_relative_price * commodity.BasePrice
81 buy_price := from_absolute_price
82 sell_price := int(float64(to_absolute_price) * 0.9)
83 return (sell_price - buy_price) * quantity
84
85 }
86
87 func FindBestTrades(data planet_data) [][]*Commodity {
88 best := make([][]*Commodity, len(data.Planets))
89 for from_index := range data.Planets {
90 best[from_index] = make([]*Commodity, len(data.Planets))
91 for to_index := range data.Planets {
92 best_gain := 0
93 for commodity_index := range data.Commodities {
94 gain := TradeValue(data, from_index, to_index, commodity_index, 1)
95 if gain > best_gain {
96 best[from_index][to_index] = &data.Commodities[commodity_index]
97 gain = best_gain
98 }
99 }
100 }
101 }
102 return best
103 }
104
105 func main() {
106 flag.Parse()
107 data := ReadData()
108 best_trades := FindBestTrades(data)
109 for from_index, from_planet := range data.Planets {
110 for to_index, to_planet := range data.Planets {
111 best_trade := "(nothing)"
112 if best_trades[from_index][to_index] != nil {
113 best_trade = best_trades[from_index][to_index].Name
114 }
115 fmt.Printf("%s to %s: %s\n", from_planet.Name, to_planet.Name, best_trade)
116 }
117 }
118 }