SHOW:
|
|
- or go back to the newest paste.
1 | #! /usr/bin/python | |
2 | # Copyright (c) 2009 Brian Haskin Jr. | |
3 | # | |
4 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | # of this software and associated documentation files (the "Software"), to deal | |
6 | # in the Software without restriction, including without limitation the rights | |
7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | # copies of the Software, and to permit persons to whom the Software is | |
9 | # furnished to do so, subject to the following conditions: | |
10 | # | |
11 | # The above copyright notice and this permission notice shall be included in | |
12 | # all copies or substantial portions of the Software. | |
13 | # | |
14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
20 | # THE SOFTWARE. | |
21 | ||
22 | # modified by harvestsnow | |
23 | # original version in http://arimaa.janzert.com/aei/aei-1.1.zip | |
24 | ||
25 | import sys | |
26 | import time | |
27 | import random | |
28 | import urllib2 | |
29 | ||
30 | from ConfigParser import SafeConfigParser | |
31 | from subprocess import Popen | |
32 | ||
33 | import gameroom | |
34 | ||
35 | ||
36 | # configuration file parsing | |
37 | config = SafeConfigParser(defaults = {"isaei":"True"} ) | |
38 | try: | |
39 | config.readfp(open('gameroom.cfg', 'r')) | |
40 | except IOError: | |
41 | print "Could not open 'gameroom.cfg'." | |
42 | sys.exit(1) | |
43 | ||
44 | gameroom_url = config.get("global", "gameroom_url") | |
45 | if len(sys.argv)>1: | |
46 | bot_section = sys.argv[1] | |
47 | else: | |
48 | bot_section = config.get("global", "default_engine") | |
49 | ||
50 | bot_name = config.get(bot_section, "username") | |
51 | bot_passwd = config.get(bot_section, "password") | |
52 | cmdline = config.get(bot_section, "cmdline") | |
53 | rated = config.getboolean(bot_section, "rated") | |
54 | greeting = config.get(bot_section, "greeting") | |
55 | timecontrol = config.get(bot_section, "timecontrol") | |
56 | ||
57 | is_aei = config.getboolean(bot_section, "isaei") | |
58 | - | max_games = 1 |
58 | + | max_games = 25 |
59 | ||
60 | # logging | |
61 | LOG_FILE = "postal.log" | |
62 | ||
63 | def log(message): | |
64 | print message | |
65 | plfile = open(LOG_FILE, "a") | |
66 | logline = [time.strftime("%Y-%m-%d %a %H:%M:%S")] | |
67 | logline.append(message) | |
68 | logline.append("\n") | |
69 | logline = " ".join(logline) | |
70 | plfile.write(logline) | |
71 | plfile.close() | |
72 | ||
73 | ||
74 | def check_postals(): | |
75 | try: | |
76 | open("stop_postal", 'r') | |
77 | log("Exiting after finding stop file") | |
78 | sys.exit() | |
79 | except IOError: | |
80 | pass | |
81 | gr_con = gameroom.GameRoom(gameroom_url) | |
82 | gr_con.login(bot_name, bot_passwd) | |
83 | games = gr_con.mygames() | |
84 | total_games = len(games) | |
85 | games = [g for g in games if g['postal'] == '1'] | |
86 | ||
87 | newgames = [g for g in games if g['player'] == ''] | |
88 | ||
89 | # No open invite? create one. | |
90 | if len(newgames)==0 and total_games<max_games: | |
91 | side = random.choice("bw") | |
92 | log("Starting a new game.\nWill play on side %s, using timecontrol %s" | |
93 | % (side, timecontrol)) | |
94 | table = gr_con.newgame(side, timecontrol, rated) | |
95 | ||
96 | # send the greeting (gameroom.py does it for AEI bots) | |
97 | if not is_aei: | |
98 | table.reserveseat() | |
99 | table.sitdown() | |
100 | table.updatestate() | |
101 | table.chat( greeting ) | |
102 | table.leave() | |
103 | ||
104 | gr_con.logout() | |
105 | ||
106 | postal_games = len(games) | |
107 | games = [g for g in games if g['turn'] == g['side']] | |
108 | my_turn_games = len(games) | |
109 | if games: | |
110 | games.sort(key=lambda x: x['turnts']) | |
111 | for game_num, game in enumerate(games): | |
112 | try: | |
113 | open("stop_postal", 'r') | |
114 | log("Exiting after finding stop file") | |
115 | sys.exit() | |
116 | except IOError: | |
117 | pass | |
118 | ||
119 | log("%d/%d: Playing move against %s game #%s" % ( | |
120 | game_num+1, my_turn_games, game['player'], game['gid'])) | |
121 | # call the bot | |
122 | if is_aei: | |
123 | proc = Popen([sys.executable, "gameroom.py", "move", game['gid']]) | |
124 | else: | |
125 | # make sure the gid is a number! | |
126 | # (cf. warning at http://docs.python.org/2/library/subprocess.html#frequently-used-arguments ) | |
127 | gid = str(int(game['gid'])) | |
128 | proc = Popen( cmdline.replace("{gid}",gid), shell = True ) | |
129 | proc.wait() | |
130 | return True | |
131 | else: | |
132 | return False | |
133 | ||
134 | if __name__=="__main__": | |
135 | while True: | |
136 | iplayed = False | |
137 | try: | |
138 | iplayed = check_postals() | |
139 | except urllib2.HTTPError: | |
140 | log("Server problem? Trying again later.") | |
141 | except urllib2.URLError: | |
142 | log("Connection problem? Trying again later.") | |
143 | except KeyboardInterrupt: | |
144 | log("Exiting after keyboard interruption.") | |
145 | sys.exit() | |
146 | except Exception,e: | |
147 | log("Unknown error: " + e.message) | |
148 | raise e | |
149 | ||
150 | if iplayed: | |
151 | # recheck quickly if a move was made | |
152 | time.sleep(30) | |
153 | else: | |
154 | time.sleep(300) |