Issues (229)

client-ai/main_ai.py (1 issue)

1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable reload does not seem to be defined.
Loading history...
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