source: trunk/game/scripts/console.py @ 416

Revision 416, 6.4 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #187: Patch by eliedebrauwer. Added shortcuts so from the console self.world, self.engine and self.game_state point to, well world, engine and game_state. fixes[s:trac, t:187]

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This program is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7
8#   This program is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12
13#   You should have received a copy of the GNU General Public License
14#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16import re 
17import os
18
19class Console:
20    def __init__(self, app_listener):
21        """
22        Constructor
23        @type appListener: ApplicationListener
24        @param appListener: ApplicationListener object providing a link with
25        the engine, the world and the model"""
26        exit_help = "Terminate application"
27        grid_help = "Toggle grid display"
28        run_help = "Toggle player run/walk"
29        help_help = "Show this help string"
30        load_help = "load directory file"
31        python_help = "Run some python code"
32        quit_help = "Terminate application"
33        save_help = "save directory file"
34
35        self.commands = [
36            {"cmd":"exit"  ,"callback":self.handleQuit  ,"help": exit_help},
37            {"cmd":"grid"  ,"callback":self.handleGrid  ,"help": grid_help},
38            {"cmd":"run"   ,"callback":self.handleRun   ,"help": run_help},
39            {"cmd":"help"  ,"callback":self.handleHelp  ,"help": help_help},
40            {"cmd":"load"  ,"callback":self.handleLoad  ,"help": load_help},
41            {"cmd":"python","callback":self.handlePython,"help": python_help},
42            {"cmd":"quit"  ,"callback":self.handleQuit  ,"help": quit_help},
43            {"cmd":"save"  ,"callback":self.handleSave  ,"help": save_help},
44        ]
45        self.app_listener = app_listener
46        self.world = self.app_listener.world
47        self.engine = self.app_listener.model
48        self.game_state = self.app_listener.world.data.game_state
49
50    def handleQuit(self, command):
51        """
52        Implements the quit console command
53        @type command: string
54        @param command: The command to run
55        @return: The resultstring"""
56       
57        self.app_listener.quitGame()
58        return "Terminating ..."
59
60    def handleGrid(self, command):
61        """
62        Implements the grid console command
63        @type command: string
64        @param command: The command to run
65        @return: The resultstring"""
66
67        self.app_listener.world.active_map.toggle_renderer('GridRenderer')
68        return "Grid toggled"
69
70    def handleRun(self, command):
71        """
72        Toggles run/walk mode of the PC player
73        @type command: string
74        @param command: The command to run.
75        @return: The response"""
76       
77        if self.app_listener.model.pc_run == 1:
78            self.app_listener.model.pc_run = 0
79            return "PC is now walking"
80        else:
81            self.app_listener.model.pc_run = 1
82            return "PC is now running"
83           
84
85    def handleHelp(self, command):
86        """
87        Implements the help console command
88        @type command: string
89        @param command: The command to run
90        @return: The resultstring"""
91
92        res=""
93        for cmd in self.commands:
94            res += "%10s: %s\n" % (cmd["cmd"], cmd["help"])
95
96        return res
97
98    def handlePython(self, command):
99        """
100        Implements the python console command
101        @type command: string
102        @param command: The command to run
103        @return: The resultstring"""
104
105        result = None
106        python_regex = re.compile('^python')
107        python_matches = python_regex.match(command.lower())
108        if (python_matches is not None):
109            end_python = command[python_matches.end() + 1:]
110            try:
111                result = str(eval(compile(end_python,'<string>','eval')))
112            except Exception, e:
113                result = str(e)
114               
115        return result
116
117    def handleLoad(self, command):
118        """
119        Implements the load console command
120        @type command: string
121        @param command: The command to run
122        @return: The resultstring"""
123
124        result = None
125        load_regex = re.compile('^load')
126        l_matches = load_regex.match(command.lower())
127        if (l_matches is not None):
128            end_load = l_matches.end()
129            try:
130                l_args = command.lower()[end_load + 1:].strip()
131                l_path, l_filename = l_args.split(' ')
132                self.app_listener.model.load_save = True
133                self.app_listener.model.savegame = os.path.join(l_path, l_filename)
134                result = "Load triggered"
135
136            except Exception, l_error:
137                self.app_listener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error))
138                result="Failed to load file"
139
140        return result
141
142    def handleSave(self, command):
143        """
144        Implements the save console command
145        @type command: string
146        @param command: The command to run
147        @return: The resultstring"""
148
149        save_regex = re.compile('^save')
150        s_matches = save_regex.match(command.lower())
151        if (s_matches != None):
152            end_save = s_matches.end()
153            try:
154                s_args = command.lower()[end_save+1:].strip()
155                s_path, s_filename = s_args.split(' ')
156                self.app_listener.model.save(s_path, s_filename)
157                result = "Saved to file: " + s_path + s_filename
158
159            except Exception, s_error:
160                self.app_listener.engine.getGuiManager().getConsole(). \
161                    println('Error: ' + str(s_error))
162                result = "Failed to save file"
163
164        return result
165
166
167    def handleConsoleCommand(self, command):
168        """
169        Implements the console logic
170        @type command: string
171        @param command: The command to run
172        @return: The response string """
173
174        result = None       
175        for cmd in self.commands:
176            regex = re.compile('^' + cmd["cmd"])
177            if regex.match(command.lower()):
178                result=cmd["callback"](command)
179
180        if result is None:
181            result = "Invalid command, enter help for more information"
182
183        return result
184
185                 
186
Note: See TracBrowser for help on using the repository browser.