Project

Profile

Help

Revision ee1d7ad7

IDee1d7ad73ee0c09742c26209b221c5d746bd43e5
Parent 90862344
Child 2bbad714

Added by Vincent Le Goff 6 months ago

Fix #136: remove the dependency of the sharp engine on world, add it to sessions

View differences:

src/client.py
264 264

  
265 265
    """Factory used by CocoMUD client to generate Telnet clients."""
266 266

  
267
    def __init__(self, world, panel):
267
    def __init__(self, world, session, panel):
268 268
        self.world = world
269
        self.session = session
269 270
        self.panel = panel
270 271
        self.engine = world.engine
271
        self.sharp_engine = world.sharp_engine
272
        self.sharp_engine = session.sharp_engine
272 273
        self.commands = []
273 274
        self.strip_ansi = False
274 275

  
src/game.py
96 96
        for world in self.worlds.values():
97 97
            world.engine = self
98 98

  
99
    def open(self, host, port, world, panel=None):
99
    def open(self, host, port, world, session, panel=None):
100 100
        """Connect to the specified host and port.
101 101

  
102 102
        This method creates and returns a 'Factory' class initialized
......
108 108
                host=host, port=port))
109 109

  
110 110
        self.prepare_world(world)
111
        factory = CocoFactory(world, panel)
111
        factory = CocoFactory(world, session, panel)
112 112

  
113 113
        if world.protocol.lower() == "ssl":
114 114
            reactor.connectSSL(host, port, factory,
src/session.py
28 28

  
29 29
"""This file contains the Session class."""
30 30

  
31
from log import client as log
32
from sharp.engine import SharpScript
33

  
31 34
class Session:
32 35

  
33 36
    """A class representing a session.
......
38 41

  
39 42
    """
40 43

  
44
    current_sid = 0
45

  
41 46
    def __init__(self, client, world):
47
        self.sid = self.current_sid
48
        type(self).current_sid += 1
42 49
        self.client = client
43 50
        self.world = world
44 51
        self.character = None
45 52
        self.engine = None
46
        self.sharp_engine = None
53
        self._sharp_engine = None
47 54

  
48 55
    def __repr__(self):
49
        return "<Session to the world {}>".format(
50
                self.world and self.world.name or "unknown")
56
        return f"<Session {self.sid} to the world {self.world and self.world.name or 'unknown'}>"
57

  
58
    @property
59
    def sharp_engine(self):
60
        if self._sharp_engine:
61
            return self._sharp_engine
62

  
63
        log.debug(f"Creating a SharpEngine for session {self.sid} (world: {'yes' if self.world else 'no'}, client: {'yes' if self.client else 'no'}, character: {'yes' if self.character else 'no'}, engine: {'yes' if self.engine else 'no'})")
64
        self._sharp_engine = SharpScript(self.engine, self.client, self.world)
65
        return self._sharp_engine
src/sharp/engine.py
37 37
# Constants
38 38
RE_VAR = re.compile(r"(?<!\\)\$\{?([A-Za-z0-9_]+)\}?")
39 39

  
40
class SharpScript(object):
40
class SharpScript:
41 41

  
42 42
    """Class representing a SharpScript engine.
43 43

  
src/ui/dialogs/sharp_script_console.py
43 43

  
44 44
class SharpScriptConsolePanel(AccessPanel):
45 45

  
46
    def __init__(self, parent, engine, world=None, panel=None):
46
    def __init__(self, parent, session):
47 47
        AccessPanel.__init__(self, parent, history=True, lock_input=True)
48 48
        self.output.SetDefaultStyle(wx.TextAttr(wx.WHITE, wx.BLACK))
49
        self.engine = engine
50
        self.world = world
51
        self.panel = panel
49
        self.session = session
52 50
        self.lines = []
53 51

  
54 52
        # Event binding
......
73 71

  
74 72
        if execute:
75 73
            buffer = "\n".join(self.lines)
76
            if self.world and self.world.sharp_engine:
74
            if self.session and self.session.engine:
75
                engine = self.session.engine
76
                sharp_engine = self.session.sharp_engine
77 77
                self.Send("+ " + "+ ".join(self.lines))
78 78

  
79 79
                # Save the TTS_on and TTS_outside, turn them on temporarily
80
                TTS_on = self.engine.TTS_on
81
                TTS_outside = self.engine.TTS_outside
82
                self.engine.TTS_on = True
83
                self.engine.TTS_outside = True
84
                self.engine.redirect_message = self.Send
80
                TTS_on = engine.TTS_on
81
                TTS_outside = engine.TTS_outside
82
                engine.TTS_on = True
83
                engine.TTS_outside = True
84
                engine.redirect_message = self.Send
85 85
                try:
86
                    self.world.sharp_engine.execute(buffer)
86
                    sharp_engine.execute(buffer)
87 87
                except Exception:
88 88
                    error = t("ui.dialog.sharp_script_console.error") + "\n"
89 89
                    error += traceback.format_exc().strip()
90 90
                    self.Send(error)
91 91
                finally:
92 92
                    self.lines[:] = []
93
                    self.engine.TTS_on = TTS_on
94
                    self.engine.TTS_outside = TTS_outside
95
                    self.engine.redirect_message = None
93
                    engine.TTS_on = TTS_on
94
                    engine.TTS_outside = TTS_outside
95
                    engine.redirect_message = None
96 96

  
97 97
    def OnPaste(self, e):
98 98
        """Paste several lines in the input field.
......
116 116

  
117 117
class SharpScriptConsoleDialog(wx.Dialog):
118 118

  
119
    def __init__(self, engine, world=None, panel=None):
119
    def __init__(self, session):
120 120
        wx.Dialog.__init__(self, None, title=t("ui.dialog.sharp_script_console.title"))
121
        self.engine = engine
122
        self.world = world
121
        self.session = session
123 122

  
124 123
        # Build the dialog
125 124
        sizer = wx.BoxSizer(wx.VERTICAL)
126 125
        self.SetSizer(sizer)
127 126

  
128 127
        # Add in the panel
129
        self.panel = SharpScriptConsolePanel(self, engine, world, panel)
128
        self.panel = SharpScriptConsolePanel(self, session)
130 129

  
131 130
        # Finish designing the window
132 131
        sizer.Add(self.panel)
......
137 136

  
138 137
    def OnClose(self, e):
139 138
        """Close the console."""
139
        if self.session and self.session.world:
140
            print("Saving this world.")
141
            self.session.world.save()
142

  
140 143
        self.Destroy()
src/ui/window.py
96 96
        """Return the currently selected tab (a MUDPanel0."""
97 97
        return self.tabs.GetCurrentPage()
98 98

  
99
    @property
100
    def session(self):
101
        return self.panel and self.panel.session or None
102

  
99 103
    def _get_client(self):
100 104
        return self.panel.client
101 105
    def _set_client(self, client):
......
260 264
        self.SetMenuBar(menubar)
261 265

  
262 266
    def InitUI(self, world=None):
267
        """Initializes the window, connect to a first world."""
263 268
        self.create_updater(just_checking=True)
264 269
        session = Session(None, None)
265 270
        if world is None:
......
272 277

  
273 278
            world = session.world
274 279
            character = session.character
280
            session.engine = self.engine
275 281

  
276 282
        self.connection = None
277 283
        self.tabs.AddPage(MUDPanel(self.tabs, self, self.engine, world,
......
310 316
            return
311 317

  
312 318
        world = session.world
319
        session.engine = self.engine
313 320
        self.SetTitle("{} [CocoMUD]".format(world.name))
314 321
        panel = MUDPanel(self.tabs, self, self.engine, world, session)
315 322
        panel.CreateClient()
......
375 382

  
376 383
    def OnSharpScriptConsole(self, e):
377 384
        """Open the Python console dialog box."""
378
        dialog = SharpScriptConsoleDialog(self.engine, self.world, self.panel)
385
        dialog = SharpScriptConsoleDialog(self.session)
379 386
        dialog.ShowModal()
380 387

  
381 388
    def OnAlias(self, e):
......
607 614
        world = self.world
608 615
        hostname = world.hostname
609 616
        port = world.port
610
        client = engine.open(hostname, port, world, self)
617
        client = engine.open(hostname, port, world, session, self)
611 618
        client.strip_ansi = not self.rich
612 619
        world.load()
613 620
        client.commands = self.login()
614
        self.session.client = client
615 621
        return client
616 622

  
617 623
    def login(self):
src/world.py
71 71
        self.characters = {}
72 72
        self.settings = None
73 73
        self.lock = RLock()
74
        self.loaded = False
74 75

  
75 76
        # World's access to general data
76 77
        self.engine = None
......
99 100

  
100 101
    def load(self):
101 102
        """Load the config.set script."""
103
        if self.loaded: # The world has already been loaded, don't duplicate
104
            return
105

  
102 106
        from game import Level
103 107
        level = self.engine.level
104 108
        self.engine.level = Level.world
......
122 126

  
123 127
        # Put the engine level back
124 128
        self.engine.level = level
129
        self.loaded = True
125 130

  
126 131
        if to_save:
127 132
            self.save()
......
321 326
        """Create a session attached to this world."""
322 327
        session = Session(client, self)
323 328
        session.engine = self.engine
324
        session.sharp_engine = self.sharp_engine
325 329
        return session
326 330

  
327 331
    def open_notepad(self):

Also available in: Unified diff