Changeset 77 for pycgibattler
- Timestamp:
- 12/24/10 02:16:07 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pycgibattler/trunk/index.cgi
r76 r77 95 95 return code, warrior 96 96 97 def flatten(self):97 def entry(self): 98 98 code, warrior = self.load() 99 99 return { … … 103 103 'warrior': warrior, 104 104 'skills': [warrior.skill(s) for s in warrior.skill_list()], 105 } 106 107 def flatten(self): 108 code, warrior = self.load() 109 skills = [] 110 for name in warrior.skill_list(): 111 sk = warrior.skill(name) 112 skills.append({ 113 'name': name, 114 'type': str(sk.skilltype()).split('.')[-1], 115 'level': sk.level(), 116 'point': sk.point(), 117 }) 118 return { 119 'id': self.id(), 120 'mtime': int(self.mtime()), 121 'name': warrior.name(), 122 'hitpoint': warrior.hitpoint(), 123 'skillpoint': warrior.skillpoint(), 124 'strength': warrior.strength(), 125 'concentration': warrior.concentration(), 126 'defense': warrior.defense(), 127 'agility': warrior.agility(), 128 'luck': warrior.luck(), 129 'skills': skills, 105 130 } 106 131 … … 216 241 217 242 243 def jsondump(data): 244 sys.stdout.write('Content-type: application/json; charset=UTF-8\r\n\r\n') 245 sys.stdout.write(json.dumps(data, ensure_ascii=False) + '\n') 246 sys.stdout.flush() 247 248 218 249 def main(): 219 250 form = cgi.FieldStorage() 220 251 252 if 'mode' in form and form.getfirst('mode') == 'json': 253 if 'id' in form: 254 data = {} 255 ids = form['id'] 256 257 if type(ids) is not list: 258 ids = [ids] 259 260 if len(ids) > CONFIG.getint('battle', 'max_entries'): 261 return jsondump({}) 262 263 try: 264 for id_ in ids: 265 data[id_.value] = CharacterManager(id_.value).flatten() 266 except: 267 return jsondump({}) 268 269 return jsondump(data) 270 271 return jsondump([cm.id() for cm in CharacterManager.list()]) 272 221 273 if 'id' in form: 222 entries = [CharacterManager(form.getfirst('id')). flatten()]274 entries = [CharacterManager(form.getfirst('id')).entry()] 223 275 return httpdump(CONFIG.get('template', 'character'), entries=entries) 224 276 225 277 if 'warrior' in form: 226 278 ids = form['warrior'] 227 if type(ids) is list: 228 if len(ids) > CONFIG.getint('battle', 'max_entries'): 229 raise ValueError('battle warriors too long') 230 warriors = [CharacterManager(id_.value).load()[1] for id_ in ids] 231 result = do_battle(warriors) 232 return httpdump(CONFIG.get('template', 'battle'), result=result) 279 if type(ids) is not list: 280 ids = [ids] 281 if len(ids) > CONFIG.getint('battle', 'max_entries'): 282 raise ValueError('battle warriors too long') 283 warriors = [CharacterManager(id_.value).load()[1] for id_ in ids] 284 result = do_battle(warriors) 285 return httpdump(CONFIG.get('template', 'battle'), result=result) 233 286 234 287 if 'filename' in form: … … 241 294 CharacterManager.sweep() 242 295 243 entries = [cm. flatten() for cm in CharacterManager.list()]296 entries = [cm.entry() for cm in CharacterManager.list()] 244 297 return httpdump(CONFIG.get('template', 'index'), entries=entries) 245 298
Note: See TracChangeset
for help on using the changeset viewer.