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

Revision 317, 6.1 KB checked in by barra_parpg, 10 years ago (diff)

Patch by sirren & mvbarracuda:

  • New player character animations
  • Changed PC object id to player for consistency reasons (agentxmlgen.py uses the folder name for the object id)
  • Renamed parpg-editor >> parpg_editor (use underscores as space characters!)
  • SVL eol-style >> native 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 
17
18class Console:
19    def __init__(self, app_listener):
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        exit_help = "Terminate application"
26        grid_help = "Toggle grid display"
27        run_help = "Toggle player run/walk"
28        help_help = "Show this help string"
29        load_help = "load directory file"
30        python_help = "Run some python code"
31        quit_help = "Terminate application"
32        save_help = "save directory file"
33
34        self.commands = [
35            {"cmd":"exit"  ,"callback":self.handleQuit  ,"help": exit_help},
36            {"cmd":"grid"  ,"callback":self.handleGrid  ,"help": grid_help},
37            {"cmd":"run"   ,"callback":self.handleRun   ,"help": run_help},
38            {"cmd":"help"  ,"callback":self.handleHelp  ,"help": help_help},
39            {"cmd":"load"  ,"callback":self.handleLoad  ,"help": load_help},
40            {"cmd":"python","callback":self.handlePython,"help": python_help},
41            {"cmd":"quit"  ,"callback":self.handleQuit  ,"help": quit_help},
42            {"cmd":"save"  ,"callback":self.handleSave  ,"help": save_help},
43        ]
44        self.app_listener=app_listener
45
46    def handleQuit(self, command):
47        """
48        Implements the quit console command
49        @type command: string
50        @param command: The command to run
51        @return: The resultstring"""
52       
53        self.app_listener.quitGame()
54        return "Terminating ..."
55
56    def handleGrid(self, command):
57        """
58        Implements the grid console command
59        @type command: string
60        @param command: The command to run
61        @return: The resultstring"""
62
63        self.app_listener.world.active_map.toggle_renderer('GridRenderer')
64        return "Grid toggled"
65
66    def handleRun(self, command):
67        """
68        Toggles run/walk mode of the PC player
69        @type command: string
70        @param command: The command to run.
71        @return: The response"""
72       
73        if self.app_listener.model.pc_run == 1:
74            self.app_listener.model.pc_run = 0
75            return "PC is now walking"
76        else:
77            self.app_listener.model.pc_run = 1
78            return "PC is now running"
79           
80
81    def handleHelp(self, command):
82        """
83        Implements the help console command
84        @type command: string
85        @param command: The command to run
86        @return: The resultstring"""
87
88        res=""
89        for cmd in self.commands:
90            res += "%10s: %s\n" % (cmd["cmd"], cmd["help"])
91
92        return res
93
94    def handlePython(self, command):
95        """
96        Implements the python console command
97        @type command: string
98        @param command: The command to run
99        @return: The resultstring"""
100
101        result = None
102        python_regex = re.compile('^python')
103        python_matches = python_regex.match(command.lower())
104        if (python_matches is not None):
105            end_python = command[python_matches.end() + 1:]
106            try:
107                result = str(eval(end_python))
108            except Exception, e:
109                result = str(e)
110               
111        return result
112
113    def handleLoad(self, command):
114        """
115        Implements the load console command
116        @type command: string
117        @param command: The command to run
118        @return: The resultstring"""
119
120        result = None
121        load_regex = re.compile('^load')
122        l_matches = load_regex.match(command.lower())
123        if (l_matches is not None):
124            end_load = l_matches.end()
125            try:
126                l_args = command.lower()[end_load + 1:].strip()
127                l_path, l_filename = l_args.split(' ')
128                self.app_listener.model.load(l_path, l_filename)
129                result = "Loaded file: " + l_path + l_filename
130
131            except Exception, l_error:
132                self.app_listener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error))
133                result="Failed to load file"
134
135        return result
136
137    def handleSave(self, command):
138        """
139        Implements the save console command
140        @type command: string
141        @param command: The command to run
142        @return: The resultstring"""
143
144        save_regex = re.compile('^save')
145        s_matches = save_regex.match(command.lower())
146        if (s_matches != None):
147            end_save = s_matches.end()
148            try:
149                s_args = command.lower()[end_save+1:].strip()
150                s_path, s_filename = s_args.split(' ')
151                self.app_listener.model.save(s_path, s_filename)
152                result = "Saved to file: " + s_path + s_filename
153
154            except Exception, s_error:
155                self.app_listener.engine.getGuiManager().getConsole(). \
156                    println('Error: ' + str(s_error))
157                result = "Failed to save file"
158
159        return result
160
161
162    def handleConsoleCommand(self, command):
163        """
164        Implements the console logic
165        @type command: string
166        @param command: The command to run
167        @return: The response string """
168
169        result = None       
170        for cmd in self.commands:
171            regex = re.compile('^' + cmd["cmd"])
172            if regex.match(command.lower()):
173                result=cmd["callback"](command)
174
175        if result is None:
176            result = "Invalid command, enter help for more information"
177
178        return result
179
180                 
181
Note: See TracBrowser for help on using the repository browser.