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

Revision 479, 6.7 KB checked in by maximinus_parpg, 10 years ago (diff)

More code cleanup.
Some preperation for sorting out the beer quest.

  • 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        @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   = "Usage: load directory file"
30        python_help = "Run some python code"
31        quit_help   = "Terminate application"
32        save_help   = "Usage: save directory file"
33        pause_help  = "Pause/Unpause the game"
34
35        self.commands = [
36            {"cmd":"exit"  ,"callback":self.handleQuit  ,"help": exit_help},
37            {"cmd":"grid"  ,"callback":self.handleGrid  ,"help": grid_help},
38            {"cmd":"help"  ,"callback":self.handleHelp  ,"help": help_help},
39            {"cmd":"load"  ,"callback":self.handleLoad  ,"help": load_help},
40            {"cmd":"pause" ,"callback":self.handlePause ,"help": pause_help},
41            {"cmd":"python","callback":self.handlePython,"help": python_help},
42            {"cmd":"run"   ,"callback":self.handleRun   ,"help": run_help},
43            {"cmd":"save"  ,"callback":self.handleSave  ,"help": save_help},
44            {"cmd":"quit"  ,"callback":self.handleQuit  ,"help": quit_help},
45        ]
46        self.app_listener = app_listener
47        self.world = self.app_listener.world
48        self.engine = self.app_listener.model
49        self.game_state = self.app_listener.world.data.game_state
50
51    def handleQuit(self, command):
52        """Implements the quit console command
53           @type command: string
54           @param command: The command to run
55           @return: The resultstring"""
56        self.app_listener.quitGame()
57        return "Terminating ..."
58
59    def handlePause(self, command):
60        """Implements the pause console command
61           @type command: string
62           @param command: The command to run
63           @return: The resultstring"""
64        self.world.togglePause()
65        return "Game (un)paused"
66   
67    def handleGrid(self, command):
68        """Implements the grid console command
69           @type command: string
70           @param command: The command to run
71           @return: The resultstring"""
72        self.app_listener.world.active_map.toggleRenderer('GridRenderer')
73        return "Grid toggled"
74
75    def handleRun(self, command):
76        """Toggles run/walk mode of the PC player
77           @type command: string
78           @param command: The command to run.
79           @return: The response"""
80        if self.app_listener.model.pc_run == 1:
81            self.app_listener.model.pc_run = 0
82            return "PC is now walking"
83        else:
84            self.app_listener.model.pc_run = 1
85            return "PC is now running"
86           
87    def handleHelp(self, command):
88        """Implements the help console command
89           @type command: string
90           @param command: The command to run
91           @return: The resultstring"""
92        res = ""
93        for cmd in self.commands:
94            res += "%10s: %s\n" % (cmd["cmd"], cmd["help"])
95        return res
96
97    def handlePython(self, command):
98        """Implements the python console command
99           @type command: string
100           @param command: The command to run
101           @return: The resultstring"""
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        return result
112
113    def handleLoad(self, command):
114        """Implements the load console command
115           @type command: string
116           @param command: The command to run
117           @return: The resultstring"""
118        result = None
119        load_regex = re.compile('^load')
120        l_matches = load_regex.match(command.lower())
121        if (l_matches is not None):
122            end_load = l_matches.end()
123            try:
124                l_args = command.lower()[end_load + 1:].strip()
125                l_path, l_filename = l_args.split(' ')
126                self.app_listener.model.load_save = True
127                self.app_listener.model.savegame = os.path.join(l_path, l_filename)
128                result = "Load triggered"
129
130            except Exception, l_error:
131                self.app_listener.engine.getGuiManager().getConsole().println('Error: ' + str(l_error))
132                result="Failed to load file"
133
134        return result
135
136    def handleSave(self, command):
137        """Implements the save console command
138           @type command: string
139           @param command: The command to run
140           @return: The resultstring"""
141        save_regex = re.compile('^save')
142        s_matches = save_regex.match(command.lower())
143        if (s_matches != None):
144            end_save = s_matches.end()
145            try:
146                s_args = command.lower()[end_save+1:].strip()
147                s_path, s_filename = s_args.split(' ')
148                self.app_listener.model.save(s_path, s_filename)
149                result = "Saved to file: " + s_path + s_filename
150
151            except Exception, s_error:
152                self.app_listener.engine.getGuiManager().getConsole(). \
153                    println('Error: ' + str(s_error))
154                result = "Failed to save file"
155        return result
156
157    def handleConsoleCommand(self, command):
158        """Implements the console logic
159           @type command: string
160           @param command: The command to run
161           @return: The response string """
162        result = None       
163        for cmd in self.commands:
164            regex = re.compile('^' + cmd["cmd"])
165            if regex.match(command.lower()):
166                result=cmd["callback"](command)
167
168        if result is None:
169            result = "Invalid command, enter help for more information"
170        return result
Note: See TracBrowser for help on using the repository browser.