Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- # Copyright (c) 2009 Brian Haskin Jr.
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- # THE SOFTWARE.
- # modified by harvestsnow
- # original version in http://arimaa.janzert.com/aei/aei-1.1.zip
- import sys
- import time
- import random
- import urllib2
- from ConfigParser import SafeConfigParser
- from subprocess import Popen
- import gameroom
- # configuration file parsing
- config = SafeConfigParser(defaults = {"isaei":"True"} )
- try:
- config.readfp(open('gameroom.cfg', 'r'))
- except IOError:
- print "Could not open 'gameroom.cfg'."
- sys.exit(1)
- gameroom_url = config.get("global", "gameroom_url")
- if len(sys.argv)>1:
- bot_section = sys.argv[1]
- else:
- bot_section = config.get("global", "default_engine")
- bot_name = config.get(bot_section, "username")
- bot_passwd = config.get(bot_section, "password")
- cmdline = config.get(bot_section, "cmdline")
- rated = config.getboolean(bot_section, "rated")
- greeting = config.get(bot_section, "greeting")
- timecontrol = config.get(bot_section, "timecontrol")
- is_aei = config.getboolean(bot_section, "isaei")
- max_games = 25
- # logging
- LOG_FILE = "postal.log"
- def log(message):
- print message
- plfile = open(LOG_FILE, "a")
- logline = [time.strftime("%Y-%m-%d %a %H:%M:%S")]
- logline.append(message)
- logline.append("\n")
- logline = " ".join(logline)
- plfile.write(logline)
- plfile.close()
- def check_postals():
- try:
- open("stop_postal", 'r')
- log("Exiting after finding stop file")
- sys.exit()
- except IOError:
- pass
- gr_con = gameroom.GameRoom(gameroom_url)
- gr_con.login(bot_name, bot_passwd)
- games = gr_con.mygames()
- total_games = len(games)
- games = [g for g in games if g['postal'] == '1']
- newgames = [g for g in games if g['player'] == '']
- # No open invite? create one.
- if len(newgames)==0 and total_games<max_games:
- side = random.choice("bw")
- log("Starting a new game.\nWill play on side %s, using timecontrol %s"
- % (side, timecontrol))
- table = gr_con.newgame(side, timecontrol, rated)
- # send the greeting (gameroom.py does it for AEI bots)
- if not is_aei:
- table.reserveseat()
- table.sitdown()
- table.updatestate()
- table.chat( greeting )
- table.leave()
- gr_con.logout()
- postal_games = len(games)
- games = [g for g in games if g['turn'] == g['side']]
- my_turn_games = len(games)
- if games:
- games.sort(key=lambda x: x['turnts'])
- for game_num, game in enumerate(games):
- try:
- open("stop_postal", 'r')
- log("Exiting after finding stop file")
- sys.exit()
- except IOError:
- pass
- log("%d/%d: Playing move against %s game #%s" % (
- game_num+1, my_turn_games, game['player'], game['gid']))
- # call the bot
- if is_aei:
- proc = Popen([sys.executable, "gameroom.py", "move", game['gid']])
- else:
- # make sure the gid is a number!
- # (cf. warning at http://docs.python.org/2/library/subprocess.html#frequently-used-arguments )
- gid = str(int(game['gid']))
- proc = Popen( cmdline.replace("{gid}",gid), shell = True )
- proc.wait()
- return True
- else:
- return False
- if __name__=="__main__":
- while True:
- iplayed = False
- try:
- iplayed = check_postals()
- except urllib2.HTTPError:
- log("Server problem? Trying again later.")
- except urllib2.URLError:
- log("Connection problem? Trying again later.")
- except KeyboardInterrupt:
- log("Exiting after keyboard interruption.")
- sys.exit()
- except Exception,e:
- log("Unknown error: " + e.message)
- raise e
- if iplayed:
- # recheck quickly if a move was made
- time.sleep(30)
- else:
- time.sleep(300)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement