Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 4 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Aug 25, 2012
@dripton Script to mass-set ai passwords, for stresstesting 3b8ec06
@dripton Merge summon_during/summon_after and reinforce_during/reinforce_after
This eliminates a bunch of duplicated code, at the cost of a few
extra conditionals within those methods.
ec63bee
@dripton Change opts to args, and passwd to passwd_path. 36301bf
@dripton If we have no valid moves, dump all known legions for debugging.
(It's very hard to have no valid moves in Titan, so I suspect
some kind of client/server sync bug.)
0b961e5
Showing with 94 additions and 79 deletions.
  1. +41 −0 bin/set-all-slugathon-ai-passwords
  2. +1 −0 setup.py
  3. +16 −21 slugathon/ai/AIClient.py
  4. +4 −10 slugathon/ai/Bot.py
  5. +24 −40 slugathon/ai/CleverBot.py
  6. +8 −8 slugathon/net/Server.py
View
41 bin/set-all-slugathon-ai-passwords
@@ -0,0 +1,41 @@
+#!/usr/bin/python
+
+__copyright__ = "Copyright (c) 2003-2012 David Ripton"
+__license__ = "GNU GPL v2"
+
+
+"""Set all ai passwords to the passed-in value."""
+
+import argparse
+import tempfile
+import os
+
+from slugathon.util import prefs
+
+
+def set_ai_passwords(passwd_path, password):
+ """Set all passwords for users ai* in file password_path to password."""
+ fil = open(passwd_path)
+ with tempfile.NamedTemporaryFile(prefix="slugathon", suffix="passwd",
+ delete=False) as tmp_file:
+ for line in fil:
+ if line.startswith("ai"):
+ user, passwd = line.strip().split(":", 1)
+ tmp_file.write("%s:%s\n" % (user, password))
+ else:
+ tmp_file.write(line)
+ os.rename(tmp_file.name, passwd_path)
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-a", "--password", action="store", type=str,
+ default="ai", help="password to set for each ai")
+ parser.add_argument("--passwd-path", action="store", type=str,
+ default=prefs.passwd_path(), help="path to passwd file")
+ args = parser.parse_args()
+ set_ai_passwords(args.passwd_path, args.password)
+
+
+if __name__ == "__main__":
+ main()
View
1 setup.py
@@ -91,6 +91,7 @@ def write_version_file():
scripts=[
"bin/slugathon",
"bin/stresstest-slugathon",
+ "bin/set-all-slugathon-ai-passwords",
],
classifiers=[
"Development Status :: 3 - Alpha",
View
37 slugathon/ai/AIClient.py
@@ -461,11 +461,10 @@ def update(self, observed, action, names):
if legion:
if legion.player.name == self.playername:
if legion == game.defender_legion:
- reactor.callLater(self.delay, self.ai.reinforce_during,
- game)
+ reactor.callLater(self.delay, self.ai.reinforce, game)
else:
- reactor.callLater(self.delay,
- self.ai.summon_angel_during, game)
+ reactor.callLater(self.delay, self.ai.summon_angel,
+ game)
else:
logging.info("game.battle_active_legion not found")
@@ -491,8 +490,7 @@ def update(self, observed, action, names):
reactor.callLater(self.delay, self.ai.recruit, game)
elif game.phase == Phase.FIGHT:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.reinforce_during,
- game)
+ reactor.callLater(self.delay, self.ai.reinforce, game)
else:
reactor.callLater(self.delay,
self.ai.choose_engagement, game)
@@ -538,8 +536,7 @@ def update(self, observed, action, names):
self.update_creatures(game)
if action.playername == self.playername:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.summon_angel_during,
- game)
+ reactor.callLater(self.delay, self.ai.summon_angel, game)
else:
reactor.callLater(self.delay, self.ai.choose_engagement,
game)
@@ -555,8 +552,7 @@ def update(self, observed, action, names):
self.update_creatures(game)
if action.playername == self.playername:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.summon_angel_during,
- game)
+ reactor.callLater(self.delay, self.ai.summon_angel, game)
else:
reactor.callLater(self.delay, self.ai.choose_engagement,
game)
@@ -565,8 +561,7 @@ def update(self, observed, action, names):
game = self.name_to_game(action.game_name)
if action.playername == self.playername:
if game.battle_phase == Phase.REINFORCE:
- reactor.callLater(self.delay, self.ai.summon_angel_during,
- game)
+ reactor.callLater(self.delay, self.ai.summon_angel, game)
else:
reactor.callLater(self.delay, self.ai.choose_engagement,
game)
@@ -587,17 +582,17 @@ def update(self, observed, action, names):
legion = game.attacker_legion
if (legion.markerid == action.winner_markerid and
legion.can_summon):
- reactor.callLater(self.delay,
- self.ai.summon_angel_after, game)
+ reactor.callLater(self.delay, self.ai.summon_angel,
+ game)
return
else:
if game.defender_legion:
legion = game.defender_legion
if legion.player.name == self.playername:
if (legion.markerid == action.winner_markerid and
legion.can_recruit):
- reactor.callLater(self.delay,
- self.ai.reinforce_after, game)
+ reactor.callLater(self.delay, self.ai.reinforce,
+ game)
return
if game.active_player.name == self.playername:
reactor.callLater(self.delay, self.ai.choose_engagement, game)
@@ -686,11 +681,11 @@ def add_arguments(parser):
def main():
parser = argparse.ArgumentParser()
add_arguments(parser)
- opts, extras = parser.parse_known_args()
- aiclient = AIClient(opts.playername, opts.password, opts.server, opts.port,
- opts.delay, opts.game_name, opts.log_path, opts.ai_time_limit,
- opts.player_time_limit, opts.form_game, opts.min_players,
- opts.max_players)
+ args, extras = parser.parse_known_args()
+ aiclient = AIClient(args.playername, args.password, args.server, args.port,
+ args.delay, args.game_name, args.log_path, args.ai_time_limit,
+ args.player_time_limit, args.form_game, args.min_players,
+ args.max_players)
reactor.callWhenRunning(aiclient.connect)
reactor.run()
View
14 slugathon/ai/Bot.py
@@ -46,17 +46,11 @@ def carry(game, striker_name, striker_hexlabel, target_name,
def recruit(game):
"""Recruit during the Muster phase."""
- def reinforce_during(game):
- """Muster a reinforcement during battle."""
+ def reinforce(game):
+ """Muster a reinforcement during or after battle."""
- def reinforce_after(game):
- """Muster a reinforcement after battle."""
-
- def summon_angel_during(game):
- """Summon angel, during battle."""
-
- def summon_angel_after(game):
- """Summon angel, after battle."""
+ def summon_angel(game):
+ """Summon an angel, during or after battle."""
def acquire_angels(game, markerid, num_angels, num_archangels):
"""Acquire angels."""
View
64 slugathon/ai/CleverBot.py
@@ -236,48 +236,39 @@ def recruit(self, game):
def1 = self.user.callRemote("done_with_recruits", game.name)
def1.addErrback(self.failure)
- def reinforce_during(self, game):
- """Reinforce, during the REINFORCE battle phase"""
- logging.info("")
- assert game.battle_active_player.name == self.playername
- assert game.battle_phase == Phase.REINFORCE
- legion = game.defender_legion
- assert legion.player.name == self.playername
- if game.battle_turn == 4 and legion.can_recruit:
- recruit, recruiters = self._pick_recruit_and_recruiters(legion)
- if recruit is not None:
- logging.info("CleverBot calling recruit_creature %s", recruit)
- def1 = self.user.callRemote("recruit_creature", game.name,
- legion.markerid, recruit, recruiters)
- def1.addErrback(self.failure)
- return
- logging.info("CleverBot calling done_with_reinforcements")
- def1 = self.user.callRemote("done_with_reinforcements", game.name)
- def1.addErrback(self.failure)
-
- def reinforce_after(self, game):
- """Reinforce, after the battle"""
+ def reinforce(self, game):
+ """Reinforce, during the REINFORCE battle phase or after the battle."""
logging.info("")
+ during_battle = (game.battle_phase == Phase.REINFORCE)
+ if during_battle:
+ assert game.battle_active_player.name == self.playername
legion = game.defender_legion
assert legion.player.name == self.playername
- if legion.can_recruit:
+ if (not during_battle or game.battle_turn == 4) and legion.can_recruit:
recruit, recruiters = self._pick_recruit_and_recruiters(legion)
if recruit is not None:
logging.info("CleverBot calling recruit_creature %s", recruit)
def1 = self.user.callRemote("recruit_creature", game.name,
legion.markerid, recruit, recruiters)
def1.addErrback(self.failure)
return
- logging.info("CleverBot calling do_not_reinforce %s", recruit)
- def1 = self.user.callRemote("do_not_reinforce", game.name,
- legion.markerid)
- def1.addErrback(self.failure)
+ if during_battle:
+ logging.info("CleverBot calling done_with_reinforcements")
+ def1 = self.user.callRemote("done_with_reinforcements", game.name)
+ def1.addErrback(self.failure)
+ else:
+ logging.info("CleverBot calling do_not_reinforce %s", recruit)
+ def1 = self.user.callRemote("do_not_reinforce", game.name,
+ legion.markerid)
+ def1.addErrback(self.failure)
- def _summon_angel(self, game, during_battle):
+ # TODO Consider value of this legion and donor legion before deciding
+ # whether to summon. But we may want to summon from a greater legion
+ # if it's 7 high and summoning lets it recruit.
+ def summon_angel(self, game):
logging.info("")
assert game.active_player.name == self.playername
- if during_battle and game.battle_phase != Phase.REINFORCE:
- return
+ during_battle = (game.battle_phase == Phase.REINFORCE)
legion = game.attacker_legion
assert legion.player.name == self.playername
if (legion.can_summon and (not during_battle or
@@ -321,17 +312,6 @@ def _summon_angel(self, game, during_battle):
def1 = self.user.callRemote("done_with_reinforcements", game.name)
def1.addErrback(self.failure)
- def summon_angel_during(self, game):
- """Summon, during the REINFORCE battle phase"""
- self._summon_angel(game, True)
-
- # TODO Consider value of this legion and donor legion before deciding
- # whether to summon. But we may want to summon from a greater legion
- # if it's 7 high and doing this lets it recruit.
- def summon_angel_after(self, game):
- """Summon, after the battle is over."""
- self._summon_angel(game, False)
-
def acquire_angels(self, game, markerid, num_angels, num_archangels):
logging.info("CleverBot.acquire_angels %s %s %s", markerid, num_angels,
num_archangels)
@@ -481,6 +461,10 @@ def move_legions(self, game):
(score, legion, hexlabel, entry_side))
best_moves.sort()
logging.debug("best moves %s", best_moves)
+ if not best_moves:
+ logging.debug("dumping all legions")
+ for legion in game.all_legions():
+ logging.debug(legion)
if player.can_take_mulligan:
legions_with_good_moves = set()
View
16 slugathon/net/Server.py
@@ -832,25 +832,25 @@ def processEnded(self, status):
def add_arguments(parser):
parser.add_argument("-p", "--port", action="store", type=int,
default=config.DEFAULT_PORT, help="listening TCP port")
- parser.add_argument("--passwd", "-a", action="store", type=str,
+ parser.add_argument("--passwd-path", action="store", type=str,
default=prefs.passwd_path(), help="path to passwd file")
- parser.add_argument("--no-passwd", "-n", action="store_true",
+ parser.add_argument("-n", "--no-passwd", action="store_true",
help="do not check passwords")
- parser.add_argument("--log-path", "-l", action="store", type=str,
+ parser.add_argument("-l", "--log-path", action="store", type=str,
help="path to logfile")
def main():
parser = argparse.ArgumentParser()
add_arguments(parser)
- opts, extras = parser.parse_known_args()
- port = opts.port
- server = Server(opts.no_passwd, opts.passwd, opts.port, opts.log_path)
+ args, extras = parser.parse_known_args()
+ port = args.port
+ server = Server(args.no_passwd, args.passwd_path, args.port, args.log_path)
realm = Realm.Realm(server)
- if opts.no_passwd:
+ if args.no_passwd:
checker = UniqueNoPassword(None, server=server)
else:
- checker = UniqueFilePasswordDB(opts.passwd, server=server)
+ checker = UniqueFilePasswordDB(args.passwd_path, server=server)
portal = Portal(realm, [checker])
pbfact = pb.PBServerFactory(portal, unsafeTracebacks=True)
reactor.listenTCP(port, pbfact)

No commit comments for this range

Something went wrong with that request. Please try again.