Project

Profile

Help

How to connect?
Download (3.07 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / scripting / trigger.py @ 13e86181

1
# Copyright (c) 2016, LE GOFF Vincent
2
# All rights reserved.
3

    
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are met:
6

    
7
# * Redistributions of source code must retain the above copyright notice, this
8
#   list of conditions and the following disclaimer.
9

    
10
# * Redistributions in binary form must reproduce the above copyright notice,
11
#   this list of conditions and the following disclaimer in the documentation
12
#   and/or other materials provided with the distribution.
13

    
14
# * Neither the name of ytranslate nor the names of its
15
#   contributors may be used to endorse or promote products derived from
16
#   this software without specific prior written permission.
17

    
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

    
29
"""Class containing the Trigger class."""
30

    
31
import re
32

    
33
class Trigger:
34

    
35
    """A trigger object.
36

37
    In a MUD client terminology, a trigger can "watch" the information
38
    received by the client, and do something in return, like sending
39
    an information to the server, playing a sound, calling a script
40
    and so on.
41

42
    """
43

    
44
    def __init__(self, sharp, reaction, action):
45
        self.sharp_engine = sharp
46
        self.reaction = reaction
47
        self.re_reaction = self.find_regex(reaction)
48
        self.action = action
49

    
50
        # Set the trigger's level
51
        self.level = sharp.engine.level
52

    
53
    def __repr__(self):
54
        return "<Trigger for {} (level={})>".format(
55
                repr(self.reaction), self.level.name)
56

    
57
    def find_regex(self, reaction):
58
        """Find and compile the reaction given as argument.
59

60
        If the reaction begins with '^', the reaction is already a
61
        regular expression that just needs to be compiled.  Otherwise,
62
        some automatic actions will be performed on it.
63

64
        """
65
        if reaction.startswith("^"):
66
            return re.compile(reaction)
67

    
68
        reaction = re.escape(reaction)
69

    
70
        # The '*' sign will be replaced by a group
71
        reaction = reaction.replace("\\*", "(.*?)")
72
        reaction = "^" + reaction + "$"
73

    
74
        return re.compile(reaction, re.IGNORECASE)
75

    
76
    def feed(self, line):
77
        """Should the trigger be triggered by the text?"""
78
        if self.re_reaction.search(line):
79
            self.execute()
80

    
81
    def execute(self):
82
        """Execute the trigger."""
83
        self.sharp_engine.execute(self.action)
(5-5/5)