| Conditions | 14 |
| Total Lines | 100 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like main_ai.runAIClient() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # |
||
| 21 | def runAIClient(options): |
||
| 22 | import time |
||
| 23 | import traceback |
||
| 24 | import sys |
||
| 25 | import os |
||
| 26 | |||
| 27 | # tweak PYTHONPATH |
||
| 28 | basepath = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
||
| 29 | |||
| 30 | sys.path.insert(0, os.path.join(basepath, "client/osci")) |
||
| 31 | |||
| 32 | for item in ("libsrvr", "server/lib"): |
||
| 33 | path = os.path.join(basepath, item) |
||
| 34 | if os.path.exists(path): |
||
| 35 | sys.path.insert(0, path) |
||
| 36 | break |
||
| 37 | |||
| 38 | from config import Config |
||
| 39 | |||
| 40 | import osci, random, time |
||
| 41 | import ige.version |
||
| 42 | from ige import log |
||
| 43 | import os, os.path |
||
| 44 | import re |
||
| 45 | |||
| 46 | # log initialization |
||
| 47 | log.message("Starting Outer Space Client", ige.version.versionString) |
||
| 48 | log.debug("sys.path =", sys.path) |
||
| 49 | log.debug("os.name =", os.name) |
||
| 50 | log.debug("sys.platform =", sys.platform) |
||
| 51 | log.debug("os.getcwd() =", os.getcwd()) |
||
| 52 | log.debug("sys.frozen =", getattr(sys, "frozen", None)) |
||
| 53 | |||
| 54 | # create required directories |
||
| 55 | if not os.path.exists(options.configDir): |
||
| 56 | os.makedirs(options.configDir) |
||
| 57 | |||
| 58 | # client |
||
| 59 | import ai_client as client |
||
| 60 | from igeclient.IClient import IClientException |
||
| 61 | if options.ai: |
||
| 62 | try: |
||
| 63 | ai = __import__("AIs." + options.ai) |
||
| 64 | ai = sys.modules["AIs." + options.ai] |
||
| 65 | except: |
||
| 66 | # This prints the type, value, and stack trace of the |
||
| 67 | # current exception being handled. |
||
| 68 | traceback.print_exc() |
||
| 69 | raise |
||
| 70 | else: |
||
| 71 | raise Exception, 'You have to provide AI.' |
||
| 72 | |||
| 73 | import ige.ospace.Const as Const |
||
| 74 | |||
| 75 | import gdata |
||
| 76 | # reload is here for multiprocessing support (as the process is used more |
||
| 77 | # than once |
||
| 78 | reload(client) |
||
|
|
|||
| 79 | gdata.config = Config(os.path.join(options.configDir, 'ais_dummy')) |
||
| 80 | client.initialize(options.server, options) |
||
| 81 | |||
| 82 | import gettext |
||
| 83 | tran = gettext.NullTranslations() |
||
| 84 | tran.install(unicode = 1) |
||
| 85 | |||
| 86 | if options.login: |
||
| 87 | login = options.login |
||
| 88 | else: |
||
| 89 | raise Exception, 'You have to provide login.' |
||
| 90 | |||
| 91 | if options.password: |
||
| 92 | password = options.password |
||
| 93 | else: |
||
| 94 | raise Exception, 'You have to provide password.' |
||
| 95 | |||
| 96 | # first get list of sessions, then iterate over them |
||
| 97 | # this is to prevent exceptions flooding logs |
||
| 98 | # TODO: make session optional argument for main_ai_pool |
||
| 99 | if client.login(options.game, login, password): |
||
| 100 | activePositions = client.cmdProxy.getActivePositions() |
||
| 101 | client.logout() |
||
| 102 | if options.test: |
||
| 103 | return True |
||
| 104 | else: |
||
| 105 | return False |
||
| 106 | |||
| 107 | for playerID, galaxyName, playerType in activePositions: |
||
| 108 | if options.galaxies and galaxyName not in options.galaxies: |
||
| 109 | continue |
||
| 110 | client.login(options.game, login, password) |
||
| 111 | client.cmdProxy.selectPlayer(playerID) |
||
| 112 | client.updateDatabase() |
||
| 113 | try: |
||
| 114 | ai.run(client) |
||
| 115 | except Exception as e: |
||
| 116 | # This prints the type, value, and stack trace of the |
||
| 117 | # current exception being handled. |
||
| 118 | traceback.print_exc() |
||
| 119 | raise e |
||
| 120 | client.logout() |
||
| 121 |