Changeset 89
- Timestamp:
- 01/07/11 15:39:46 (14 years ago)
- Location:
- pycgibattler/trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
pycgibattler/trunk/index.cgi
r86 r89 24 24 import copy 25 25 import fcntl 26 import base64 26 27 import shutil 27 28 import hashlib 29 import xmlrpclib 28 30 import ConfigParser 29 31 … … 131 133 132 134 @classmethod 133 def dump(klass, name, code ):135 def dump(klass, name, code, data): 134 136 self = klass(hashlib.sha512(code).hexdigest()) 135 137 cname = os.path.join(self._prefix, os.path.basename(name)) 136 warrior = self.make_warrior(code )138 warrior = self.make_warrior(code, data) 137 139 138 140 with open(self._locker, 'w') as fp: … … 157 159 158 160 @classmethod 159 def make_warrior(klass, code ):161 def make_warrior(klass, code, data): 160 162 chara_names = [x.strip() for x in open(os.path.expanduser( 161 163 CONFIG.get('character', 'name_list_file'))).xreadlines()] … … 177 179 skills.append(sk) 178 180 179 return pycodebattler.warrior.Warrior.make( 180 name, _StringIO.StringIO(code), skills) 181 return pycodebattler.warrior.Warrior( 182 name, 183 int(data['hitpoint']), 184 int(data['skillpoint']), 185 int(data['strength']), 186 int(data['concentration']), 187 int(data['defense']), 188 int(data['agility']), 189 int(data['luck']), 190 skills, 191 ) 181 192 182 193 @classmethod … … 247 258 248 259 260 def scouter(lang, code): 261 if lang not in CONFIG.options('scouter_api'): 262 raise ValueError('invalid language') 263 264 proxy = xmlrpclib.ServerProxy(CONFIG.get('scouter_api', lang)) 265 data = proxy.Scout({'version': 1.0, 266 'code': base64.standard_b64encode(code)}) 267 268 if type(data) is not dict: 269 raise ValueError('invalid api response') 270 if 'version' not in data: 271 raise ValueError('invalid api response (version not found)') 272 if data['version'] != 1.0: 273 raise ValueError('invalid api response (invalid version)') 274 if 'result' not in data: 275 raise ValueError('invalid api response (result not found)') 276 if type(data['result']) is not dict: 277 raise ValueError('invalid api response (invalid result)') 278 return data['result'] 279 280 281 def handle_mode_json(form): 282 if 'id' not in form: 283 ids = [cm.id() for cm in CharacterManager.list()] 284 else: 285 tmp = form['id'] 286 if type(tmp) is not list: 287 tmp = [form['id']] 288 ids = [id_.value for id_ in tmp] 289 290 data = {} 291 try: 292 for id_ in ids: 293 data[id_] = CharacterManager(id_).flatten() 294 except: 295 return jsondump({'result': {}}) 296 297 return jsondump({'result': data}) 298 299 300 def handle_id(form): 301 entries = [CharacterManager(form.getfirst('id')).entry()] 302 return httpdump(CONFIG.get('template', 'character'), entries=entries) 303 304 305 def handle_warrior(form): 306 ids = form['warrior'] 307 308 if type(ids) is not list: 309 ids = [ids] 310 311 if len(ids) > CONFIG.getint('battle', 'max_entries'): 312 raise ValueError('battle warriors too long') 313 314 warriors = [CharacterManager(id_.value).load()[1] for id_ in ids] 315 result = do_battle(warriors) 316 return httpdump(CONFIG.get('template', 'battle'), result=result) 317 318 319 def handle_file(form): 320 item = form['filename'] 321 lang = form.getfirst('lang') 322 323 if not item.file or not item.filename or not lang: 324 raise ValueError('missing parameter') 325 326 code = item.file.read(CONFIG.getint('limit', 'max_size')) 327 data = scouter(lang, code) 328 CharacterManager.dump(item.filename, code, data) 329 return True 330 331 249 332 def main(): 250 333 form = cgi.FieldStorage() 251 334 252 335 if 'mode' in form and form.getfirst('mode') == 'json': 253 if 'id' not in form: 254 ids = [cm.id() for cm in CharacterManager.list()] 255 else: 256 tmp = form['id'] 257 if type(tmp) is not list: 258 tmp = [form['id']] 259 ids = [id_.value for id_ in tmp] 260 261 data = {} 262 try: 263 for id_ in ids: 264 data[id_] = CharacterManager(id_).flatten() 265 except: 266 return jsondump({'result': {}}) 267 return jsondump({'result': data}) 268 336 return handle_mode_json(form) 269 337 if 'id' in form: 270 entries = [CharacterManager(form.getfirst('id')).entry()] 271 return httpdump(CONFIG.get('template', 'character'), entries=entries) 272 338 return handle_id(form) 273 339 if 'warrior' in form: 274 ids = form['warrior'] 275 if type(ids) is not list: 276 ids = [ids] 277 if len(ids) > CONFIG.getint('battle', 'max_entries'): 278 raise ValueError('battle warriors too long') 279 warriors = [CharacterManager(id_.value).load()[1] for id_ in ids] 280 result = do_battle(warriors) 281 return httpdump(CONFIG.get('template', 'battle'), result=result) 282 283 if 'filename' in form: 284 item = form['filename'] 285 if item.file and item.filename: 286 code = item.file.read(CONFIG.getint('limit', 'max_size')) 287 if code: 288 CharacterManager.dump(item.filename, code) 340 return handle_warrior(form) 341 if 'filename' in form and 'lang' in form: 342 if not handle_file(form): 343 return httpdump(CONFIG.get('template', 'error')) 289 344 290 345 CharacterManager.sweep() -
pycgibattler/trunk/pycgibattler.conf.sample
r72 r89 24 24 max_turn = 30 25 25 max_entries = 5 26 27 [scouter_api] 28 python = http://example.com/pycodescouter -
pycgibattler/trunk/templates/80error.tmpl
r62 r89 10 10 <h3><span class="sanchor">■</span> エラーが発生しました!</h3> 11 11 <p> 12 Python コード以外のファイルや,$config.get('limit', 'max_size') バイトを超えるファイルは登録出来ません.12 バトルに参加できるのは最大 $config.get('battle', 'max_entries') 人です. 13 13 </p> 14 14 <p> 15 バトルに参加できるのは最大 $config.get('battle', 'max_entries') 人です.15 下記以外のファイルや,$config.get('limit', 'max_size') バイトを超えるファイルは登録出来ません. 16 16 </p> 17 <ul> 18 #for $lang in $config.options('scouter_api') 19 <li>$lang</li> 20 #end for 21 </ul> 17 22 </div> 18 23 </div> -
pycgibattler/trunk/templates/90footer.tmpl
r82 r89 14 14 <form action="$script" method="post" enctype="multipart/form-data"> 15 15 <input type="file" name="filename" /><br /> 16 言語: <select name="lang"> 17 #for $lang in $config.options('scouter_api') 18 <option value="$lang">$lang</option> 19 #end for 20 </select> 21 <br /> 16 22 <input type="submit" value="登録" /> 17 23 </form>
Note: See TracChangeset
for help on using the changeset viewer.