Ignore:
Timestamp:
12/24/10 02:16:07 (13 years ago)
Author:
atzm
Message:

api support, for experimental test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pycgibattler/trunk/index.cgi

    r76 r77  
    9595            return code, warrior 
    9696 
    97     def flatten(self): 
     97    def entry(self): 
    9898        code, warrior = self.load() 
    9999        return { 
     
    103103            'warrior': warrior, 
    104104            '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, 
    105130        } 
    106131 
     
    216241 
    217242 
     243def 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 
    218249def main(): 
    219250    form = cgi.FieldStorage() 
    220251 
     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 
    221273    if 'id' in form: 
    222         entries = [CharacterManager(form.getfirst('id')).flatten()] 
     274        entries = [CharacterManager(form.getfirst('id')).entry()] 
    223275        return httpdump(CONFIG.get('template', 'character'), entries=entries) 
    224276 
    225277    if 'warrior' in form: 
    226278        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) 
    233286 
    234287    if 'filename' in form: 
     
    241294    CharacterManager.sweep() 
    242295 
    243     entries = [cm.flatten() for cm in CharacterManager.list()] 
     296    entries = [cm.entry() for cm in CharacterManager.list()] 
    244297    return httpdump(CONFIG.get('template', 'index'), entries=entries) 
    245298 
Note: See TracChangeset for help on using the changeset viewer.