forked from dsandler/davesgalaxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patrol.py
executable file
·89 lines (74 loc) · 2.52 KB
/
patrol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# vim: set ts=2 sw=2 expandtab:
import game
from optparse import OptionParser
import sys
import shape
def main():
parser = OptionParser()
parser.add_option("-U", "--username", dest="username",
help="username of login")
parser.add_option("-P", "--password", dest="password",
help="password for login")
parser.add_option("-n", "--noop", dest="noop",
action="store_true", default=False, help="dry run")
parser.add_option("-m", "--maximum", dest="maximum",
type="int", default=100, help="maximum manifest multiplier")
parser.add_option("-r", "--patrol_route", dest="patrol",
type="string", help="route that the patrol should join")
(options, args) = parser.parse_args()
planetid = args[0]
tobuild = args[1:]
g=game.Galaxy()
if options.username and options.password:
# explicit login
g.login(options.username, options.password, force=True)
else:
# try to pick up stored credentials
g.login()
patrol_route = g.find_route(options.patrol)
source = None
try:
source = g.get_planet(int(planetid))
except ValueError:
source = g.find_planet(planetid)
source.load()
manifest = {}
for pr in tobuild:
ship, qty = pr.split("=")
if ship in game.ALL_SHIPS.keys():
if ship in manifest:
manifest[ship] += float(qty)
else:
manifest[ship] = float(qty)
else:
print "error: unknown ship type: " + ship
sys.exit(1)
BuildPatrol(g, source, patrol_route, manifest, options.maximum, options.noop)
def BuildPatrol(g, source, route, manifest, maximum, noop=False):
count = source.how_many_can_build(manifest)
canbuild = manifest.copy()
for key in canbuild.keys():
canbuild[key] = int(count * canbuild[key])
print "planet " + str(source) + " can build " + str(canbuild) + " ships"
count = min(maximum, count)
for key in manifest.keys():
manifest[key] = int(count * manifest[key])
print "planet " + str(source) + " will build " + str(manifest) + " ships"
if not noop:
fleet = source.build_fleet(manifest)
if fleet:
fleet.move_to_route(route)
print " build fleed %d and dispatched to %s" % (fleet.fleetid, route.name)
else:
print " failed to build fleet"
count = 0
if count > 0:
if not noop:
print "built %s" % str(manifest)
else:
print "would have built %s" % str(manifest)
g.write_planet_cache()
g.write_fleet_cache()
if __name__ == "__main__":
main()