Project

Profile

Help

Revision a5c338e8

Added by Vincent Le Goff almost 4 years ago

Put the macros and triggers in the world itself

View differences:

src/client.py
54 54

  
55 55
    """Class to receive data from the MUD."""
56 56

  
57
    def __init__(self, host, port=4000, timeout=0.1, engine=None):
57
    def __init__(self, host, port=4000, timeout=0.1, engine=None,
58
            world=None):
58 59
        """Connects to the MUD."""
59 60
        threading.Thread.__init__(self)
60 61
        self.client = None
61 62
        self.timeout = timeout
62 63
        self.engine = engine
64
        self.world = world
63 65
        self.running = False
64
        self.sharp_engine = SharpScript(engine, self)
65
        self.triggers = []
66
        self.macros = []
66
        self.sharp_engine = SharpScript(engine, self, world)
67 67

  
68 68
        # Try to connect to the specified host and port
69 69
        self.client = Telnet(host, port)
......
76 76
            msg = self.client.read_very_eager()
77 77
            if msg:
78 78
                for line in msg.splitlines():
79
                    for trigger in self.triggers:
79
                    for trigger in self.world.triggers:
80 80
                        trigger.feed(line)
81 81

  
82 82
                self.handle_message(msg)
......
111 111

  
112 112
    """
113 113

  
114
    def __init__(self, host, port=4000, timeout=0.1, engine=None):
115
        Client.__init__(self, host, port, timeout, engine)
114
    def __init__(self, host, port=4000, timeout=0.1, engine=None,
115
            world=None):
116
        Client.__init__(self, host, port, timeout, engine, world)
116 117
        self.window = None
117 118
        if self.client:
118 119
            self.client.set_option_negotiation_callback(self.handle_option)
119 120

  
120
    def load_script(self, world):
121
        """Load the config.set script."""
122
        from game import Level
123
        level = self.engine.level
124
        self.engine.level = Level.world
125
        path = world.path
126
        path = os.path.join(path, "config.set")
127
        if os.path.exists(path):
128
            file = open(path, "r")
129
            content = file.read()
130
            file.close()
131

  
132
            # Execute the script
133
            self.sharp_engine.execute(content)
134

  
135
        # Put the engine level back
136
        self.engine.level = level
137

  
138 121
    def link_window(self, window):
139 122
        """Link to a window (a GUI object).
140 123

  
......
147 130
        """
148 131
        self.window = window
149 132
        window.client = self
150
        self.load_script(window.world)
151 133

  
152 134
    def handle_message(self, msg, force_TTS=False, screen=True,
153 135
            speech=True, braille=True):
src/cocomud.py
48 48

  
49 49
# Create the client and ClientWindow
50 50
window = ClientWindow(engine)
51
world = window.world
51 52
hostname = window.panel.world.hostname
52 53
port = window.panel.world.port
53
client = engine.open(hostname, port)
54
client = engine.open(hostname, port, world)
54 55
client.link_window(window)
56
world.load()
55 57
client.start()
56 58
app.MainLoop()
src/game.py
76 76
        self.TTS_on = self.settings["options.TTS.on"]
77 77
        self.TTS_outside = self.settings["options.TTS.outside"]
78 78

  
79
    def open(self, host, port):
79
        # For each world, set the game engine
80
        for world in self.worlds.values():
81
            world.engine = self
82

  
83
    def open(self, host, port, world):
80 84
        """Connect to the specified host and port.
81 85

  
82 86
        This method creates and returns a 'GUIClient' class initialized
83 87
        with the specified information.
84 88

  
85 89
        """
86
        client = GUIClient(host, port, engine=self)
90
        client = GUIClient(host, port, engine=self, world=world)
91
        world.client = client
92
        world.sharp_engine = client.sharp_engine
87 93
        return client
src/sharp/engine.py
42 42

  
43 43
    """
44 44

  
45
    def __init__(self, engine, client):
45
    def __init__(self, engine, client, world):
46 46
        self.engine = engine
47 47
        self.client = client
48
        self.world = world
48 49
        self.locals = {}
49 50
        self.globals = globals()
50 51

  
51 52
        # Adding the functions
52 53
        for name, function in FUNCTIONS.items():
53
            function = function(engine, client, self)
54
            function = function(engine, client, self, world)
54 55
            self.globals[name] = function.run
55 56

  
56 57
    def execute(self, code):
src/sharp/function.py
32 32

  
33 33
    """The function class, parent of all SharpScript functions."""
34 34

  
35
    def __init__(self, engine, client, sharp):
35
    def __init__(self, engine, client, sharp, world=None):
36 36
        self.engine = engine
37 37
        self.client = client
38 38
        self.sharp_engine = sharp
39 39
        self.init()
40
        self.world = world
40 41

  
41 42
    def init(self):
42 43
        """Another secondary constructor."""
src/sharp/functions/macro.py
40 40
        """Creates the macro."""
41 41
        key, modifiers = key_code(shortcut)
42 42
        macro = ObjMacro(key, modifiers, action, self.sharp_engine)
43
        self.client.macros.append(macro)
43
        if self.world:
44
            self.world.macros.append(macro)
src/sharp/functions/trigger.py
38 38
    def run(self, reaction, action):
39 39
        """Say the text."""
40 40
        trigger = ObjTrigger(self.sharp_engine, reaction, action)
41
        if self.client:
42
            self.client.triggers.append(trigger)
41
        if self.world:
42
            self.world.triggers.append(trigger)
src/ui/window.py
107 107

  
108 108
    def OnMacro(self, e):
109 109
        """Open the macro dialog box."""
110
        dialog = MacroDialog(self.engine)
110
        dialog = MacroDialog(self.engine, self.world)
111 111
        dialog.ShowModal()
112 112
        dialog.Destroy()
113 113

  
......
258 258
            skip = self.HandleHistory(modifiers, key)
259 259

  
260 260
        # Look for matching macros
261
        for macro in self.client.macros:
262
            code = (macro.key, macro.modifiers)
263
            if code == (key, modifiers):
264
                macro.execute(self.engine, self.client)
261
        if self.world:
262
            for macro in self.world.macros:
263
                code = (macro.key, macro.modifiers)
264
                if code == (key, modifiers):
265
                    macro.execute(self.engine, self.client)
265 266

  
266 267
        if e.GetEventObject() is self.output:
267 268
            shortcut = key_name(key, modifiers)
src/world.py
44 44

  
45 45
    def __init__(self, location):
46 46
        self.location = location
47
        self.settings = None
48 47
        self.name = ""
49 48
        self.hostname = ""
50 49
        self.port = 4000
51 50
        self.characters = {}
51
        self.settings = None
52

  
53
        # World's access to general data
54
        self.client = None
55
        self.engine = None
56
        self.sharp_engine = None
57

  
58
        # World's configuration
59
        self.macros = []
60
        self.triggers = []
52 61

  
53 62
    def __repr__(self):
54 63
        return "<World {} (hostname={}, port={})>".format(
......
58 67
    def path(self):
59 68
        return "worlds/" + self.location
60 69

  
70
    def load(self):
71
        """Load the config.set script."""
72
        from game import Level
73
        level = self.engine.level
74
        self.engine.level = Level.world
75
        path = self.path
76
        path = os.path.join(path, "config.set")
77
        if os.path.exists(path):
78
            file = open(path, "r")
79
            content = file.read()
80
            file.close()
81

  
82
            # Execute the script
83
            self.sharp_engine.execute(content)
84

  
85
        # Put the engine level back
86
        self.engine.level = level
87

  
61 88
    def save(self):
62
        """Save the world in its configuraiton file."""
89
        """Save the world in its configuration file."""
63 90
        if not os.path.exists(self.path):
64 91
            os.mkdir(self.path)
65 92

  

Also available in: Unified diff