| 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 |  | 
|---|
| 16 | import base64 | 
|---|
| 17 | import ConfigParser | 
|---|
| 18 | import SimpleXMLRPCServer | 
|---|
| 19 |  | 
|---|
| 20 | try: | 
|---|
| 21 | import cStringIO as _StringIO | 
|---|
| 22 | except ImportError: | 
|---|
| 23 | import StringIO as _StringIO | 
|---|
| 24 |  | 
|---|
| 25 | from pycodebattler import warrior | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | CONFIG = ConfigParser.SafeConfigParser() | 
|---|
| 29 | CONFIG.read('pycgibattler.conf') | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | class ScouterError(RuntimeError): | 
|---|
| 33 | pass | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | def 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 |  | 
|---|
| 70 | if __name__ == '__main__': | 
|---|
| 71 | handler = SimpleXMLRPCServer.CGIXMLRPCRequestHandler() | 
|---|
| 72 | handler.register_function(scout, 'Scout') | 
|---|
| 73 | handler.handle_request() | 
|---|