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

Revision 281, 5.5 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket: #94: Patch by amo-ej1. "grid" console command added to display a grid overlay on the map. Console command logic moved to it's own class (console.py). Also unit tests added for console class. fixes[s:trac, t:94]

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 
17
18class Console:
19    def __init__(self, appListener):
20        """
21        Constructor
22        @type appListener: ApplicationListener
23        @param appListener: ApplicationListener object providing a link with
24        the engine, the world and the model"""
25
26        self.commands = [
27            {"cmd":"exit"  ,"callback":self.handleQuit  ,"help":"Terminate application"},
28            {"cmd":"grid"  ,"callback":self.handleGrid  ,"help":"Toggle grid display  "},
29            {"cmd":"help"  ,"callback":self.handleHelp  ,"help":"Show this help string"},
30            {"cmd":"load"  ,"callback":self.handleLoad  ,"help":"load directory file  "},
31            {"cmd":"python","callback":self.handlePython,"help":"Run some python code "},
32            {"cmd":"quit"  ,"callback":self.handleQuit  ,"help":"Terminate application"},
33            {"cmd":"save"  ,"callback":self.handleSave  ,"help":"save directory file  "},
34        ]
35        self.appListener=appListener
36
37    def handleQuit(self, command):
38        """
39        Implements the quit console command
40        @type command: string
41        @param command: The command to run
42        @return: The resultstring"""
43       
44        self.appListener.quitGame()
45        return "Terminating ..."
46
47    def handleGrid(self, command):
48        """
49        Implements the grid console command
50        @type command: string
51        @param command: The command to run
52        @return: The resultstring"""
53
54        self.appListener.world.activeMap.toggle_renderer('GridRenderer')
55        return "Grid toggled"
56
57    def handleHelp(self, command):
58        """
59        Implements the help console command
60        @type command: string
61        @param command: The command to run
62        @return: The resultstring"""
63
64        res=""
65        for cmd in self.commands:
66            res+= "%10s: %s\n" % (cmd["cmd"], cmd["help"])
67
68        return res
69
70    def handlePython(self, command):
71        """
72        Implements the python console command
73        @type command: string
74        @param command: The command to run
75        @return: The resultstring"""
76
77        result=None
78        python_regex = re.compile('^python')
79        python_matches = python_regex.match(command.lower())
80        if (python_matches != None):
81            end_python = command[python_matches.end()+1:]
82            try:
83                result = str(eval(end_python))
84            except Exception, e:
85                result = str(e)
86               
87        return result
88
89    def handleLoad(self, command):
90        """
91        Implements the load console command
92        @type command: string
93        @param command: The command to run
94        @return: The resultstring"""
95
96        result=None
97        load_regex = re.compile('^load')
98        l_matches = load_regex.match(command.lower())
99        if (l_matches != None):
100            end_load = l_matches.end()
101            try:
102                l_args = command.lower()[end_load+1:].strip()
103                l_path, l_filename = l_args.split(' ')
104                self.appListener.model.load(l_path, l_filename)
105                result = "Loaded file: " + l_path + l_filename
106
107            except Exception, l_error:
108                self.appListener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error))
109                result="Failed to load file"
110
111        return result
112
113    def handleSave(self, command):
114        """
115        Implements the save console command
116        @type command: string
117        @param command: The command to run
118        @return: The resultstring"""
119
120        save_regex = re.compile('^save')
121        s_matches = save_regex.match(command.lower())
122        if (s_matches != None):
123            end_save = s_matches.end()
124            try:
125                s_args = command.lower()[end_save+1:].strip()
126                s_path, s_filename = s_args.split(' ')
127                self.appListener.model.save(s_path, s_filename)
128                result = "Saved to file: " + s_path + s_filename
129
130            except Exception, s_error:
131                self.appListener.engine.getGuiManager().getConsole().println('Error: ' + str(s_error))
132                result = "Failed to save file"
133
134        return result
135
136    def handleConsoleCommand(self, command):
137        """
138        Implements the console logic
139        @type command: string
140        @param command: The command to run
141        @return: The response string """
142
143        result = None       
144        for cmd in self.commands:
145            regex = re.compile('^' + cmd["cmd"])
146            if regex.match(command.lower()):
147                result=cmd["callback"](command)
148
149        if result==None:
150            result = "Invalid command, enter help for more information"
151
152        return result
153
154                 
155
Note: See TracBrowser for help on using the repository browser.