source: pycgibattler/trunk/scouter.cgi @ 90

Revision 90, 2.0 KB checked in by atzm, 14 years ago (diff)

propset

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4#  Copyright (C) 2010 by Atzm WATANABE <atzm@atzm.org>
5#
6#  This program is free software; you can redistribute it and/or modify it
7#  under the terms of the GNU General Public License (version 2) as
8#  published by the Free Software Foundation.  It is distributed in the
9#  hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10#  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11#  PURPOSE.  See the GNU General Public License for more details.
12#
13# $Id$
14#
15
16import base64
17import ConfigParser
18import SimpleXMLRPCServer
19
20try:
21    import cStringIO as _StringIO
22except ImportError:
23    import StringIO as _StringIO
24
25from pycodebattler import warrior
26
27
28CONFIG = ConfigParser.SafeConfigParser()
29CONFIG.read('pycgibattler.conf')
30
31
32class ScouterError(RuntimeError):
33    pass
34
35
36def scout(data):
37    if 'version' not in data:
38        raise ScouterError('version not found')
39    if data['version'] != 1.0:
40        raise ScouterError('invalid version')
41    if 'code' not in data:
42        raise ScouterError('code not found')
43    if not isinstance(data['code'], basestring):
44        raise ScouterError('invalid code (not string)')
45
46    try:
47        code = base64.standard_b64decode(data['code'])
48    except TypeError:
49        raise ScouterError('invalid code (not base64 encoded)')
50
51    if len(code) > CONFIG.getint('limit', 'max_size'):
52        raise ScouterError('code length exceeded')
53
54    wrr = warrior.Warrior.make('', _StringIO.StringIO(code))
55
56    return {
57        'version': 1.0,
58        'result':  {
59            'hitpoint':      wrr.hitpoint(),
60            'skillpoint':    wrr.skillpoint(),
61            'strength':      wrr.strength(),
62            'concentration': wrr.concentration(),
63            'defense':       wrr.defense(),
64            'agility':       wrr.agility(),
65            'luck':          wrr.luck(),
66        },
67    }
68
69
70if __name__ == '__main__':
71    handler = SimpleXMLRPCServer.CGIXMLRPCRequestHandler()
72    handler.register_function(scout, 'Scout')
73    handler.handle_request()
Note: See TracBrowser for help on using the repository browser.