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

Revision 396, 6.2 KB checked in by zenbitz, 10 years ago (diff)

dialogue fixes

  • 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
47    def handleQuit(self, command):
48        """
49        Implements the quit console command
50        @type command: string
51        @param command: The command to run
52        @return: The resultstring"""
53       
54        self.app_listener.quitGame()
55        return "Terminating ..."
56
57    def handleGrid(self, command):
58        """
59        Implements the grid console command
60        @type command: string
61        @param command: The command to run
62        @return: The resultstring"""
63
64        self.app_listener.world.active_map.toggle_renderer('GridRenderer')
65        return "Grid toggled"
66
67    def handleRun(self, command):
68        """
69        Toggles run/walk mode of the PC player
70        @type command: string
71        @param command: The command to run.
72        @return: The response"""
73       
74        if self.app_listener.model.pc_run == 1:
75            self.app_listener.model.pc_run = 0
76            return "PC is now walking"
77        else:
78            self.app_listener.model.pc_run = 1
79            return "PC is now running"
80           
81
82    def handleHelp(self, command):
83        """
84        Implements the help console command
85        @type command: string
86        @param command: The command to run
87        @return: The resultstring"""
88
89        res=""
90        for cmd in self.commands:
91            res += "%10s: %s\n" % (cmd["cmd"], cmd["help"])
92
93        return res
94
95    def handlePython(self, command):
96        """
97        Implements the python console command
98        @type command: string
99        @param command: The command to run
100        @return: The resultstring"""
101
102        result = None
103        python_regex = re.compile('^python')
104        python_matches = python_regex.match(command.lower())
105        if (python_matches is not None):
106            end_python = command[python_matches.end() + 1:]
107            try:
108                result = str(eval(compile(end_python,'<string>','eval')))
109            except Exception, e:
110                result = str(e)
111               
112        return result
113
114    def handleLoad(self, command):
115        """
116        Implements the load console command
117        @type command: string
118        @param command: The command to run
119        @return: The resultstring"""
120
121        result = None
122        load_regex = re.compile('^load')
123        l_matches = load_regex.match(command.lower())
124        if (l_matches is not None):
125            end_load = l_matches.end()
126            try:
127                l_args = command.lower()[end_load + 1:].strip()
128                l_path, l_filename = l_args.split(' ')
129                self.app_listener.model.load_save = True
130                self.app_listener.model.savegame = os.path.join(l_path, l_filename)
131                result = "Load triggered"
132
133            except Exception, l_error:
134                self.app_listener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error))
135                result="Failed to load file"
136
137        return result
138
139    def handleSave(self, command):
140        """
141        Implements the save console command
142        @type command: string
143        @param command: The command to run
144        @return: The resultstring"""
145
146        save_regex = re.compile('^save')
147        s_matches = save_regex.match(command.lower())
148        if (s_matches != None):
149            end_save = s_matches.end()
150            try:
151                s_args = command.lower()[end_save+1:].strip()
152                s_path, s_filename = s_args.split(' ')
153                self.app_listener.model.save(s_path, s_filename)
154                result = "Saved to file: " + s_path + s_filename
155
156            except Exception, s_error:
157                self.app_listener.engine.getGuiManager().getConsole(). \
158                    println('Error: ' + str(s_error))
159                result = "Failed to save file"
160
161        return result
162
163
164    def handleConsoleCommand(self, command):
165        """
166        Implements the console logic
167        @type command: string
168        @param command: The command to run
169        @return: The response string """
170
171        result = None       
172        for cmd in self.commands:
173            regex = re.compile('^' + cmd["cmd"])
174            if regex.match(command.lower()):
175                result=cmd["callback"](command)
176
177        if result is None:
178            result = "Invalid command, enter help for more information"
179
180        return result
181
182                 
183
Note: See TracBrowser for help on using the repository browser.