summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2016-07-05 01:15:04 +0200
committermathieui <mathieui@mathieui.net>2016-07-05 01:15:04 +0200
commitd55ce5b99689c1be0885daf0d2992b795df3e666 (patch)
treedc6a7589f3f0f93e73c4955463fab20be3abb7e3
parent43d7b029f88b121c477bb142ca9b7ee62ad77083 (diff)
downloadpoezio-d55ce5b99689c1be0885daf0d2992b795df3e666.tar.gz
poezio-d55ce5b99689c1be0885daf0d2992b795df3e666.tar.bz2
poezio-d55ce5b99689c1be0885daf0d2992b795df3e666.tar.xz
poezio-d55ce5b99689c1be0885daf0d2992b795df3e666.zip
Fix #3197 (add a dice plugin)
also add some doc for the marquee plugin
-rw-r--r--doc/source/plugins/dice.rst6
-rw-r--r--doc/source/plugins/index.rst11
-rw-r--r--doc/source/plugins/marquee.rst6
-rw-r--r--plugins/dice.py103
4 files changed, 126 insertions, 0 deletions
diff --git a/doc/source/plugins/dice.rst b/doc/source/plugins/dice.rst
new file mode 100644
index 00000000..c6638a35
--- /dev/null
+++ b/doc/source/plugins/dice.rst
@@ -0,0 +1,6 @@
+.. _dice-plugin:
+
+Dice
+====
+
+.. automodule:: dice
diff --git a/doc/source/plugins/index.rst b/doc/source/plugins/index.rst
index faad1e4d..0a66b803 100644
--- a/doc/source/plugins/index.rst
+++ b/doc/source/plugins/index.rst
@@ -266,6 +266,15 @@ Plugin index
Change the title of the terminal according to the name
of the current tab.
+ Marquee
+ :ref:`Documentation <marquee-plugin>`
+
+ Reproduce the behavior of the ``<marquee/>`` html tag.
+
+ Dice
+ :ref:`Documentation <dice-plugin>`
+
+ Roll one or several dice using message corrections.
.. toctree::
:hidden:
@@ -309,3 +318,5 @@ Plugin index
reorder
cyber
csi
+ dice
+ marquee
diff --git a/doc/source/plugins/marquee.rst b/doc/source/plugins/marquee.rst
new file mode 100644
index 00000000..4bed7a0c
--- /dev/null
+++ b/doc/source/plugins/marquee.rst
@@ -0,0 +1,6 @@
+.. _marquee-plugin:
+
+Marquee
+=======
+
+.. automodule:: marquee
diff --git a/plugins/dice.py b/plugins/dice.py
new file mode 100644
index 00000000..67072730
--- /dev/null
+++ b/plugins/dice.py
@@ -0,0 +1,103 @@
+"""
+Dice plugin: roll some dice
+
+Usage of this plugin is not recommended.
+
+Commands
+--------
+
+.. glossary::
+
+ /roll <number of dice> [duration of the roll]
+ Roll one or several unicode dice
+
+Configuration
+-------------
+
+.. glossary::
+ :sorted:
+
+ refresh
+ **Default:** ``0.5``
+
+ Interval in seconds between each correction (the closest to 0 is the fastest)
+
+ default_duration
+ **Default:** ``5``
+
+ Total duration of the animation.
+"""
+
+import random
+
+from poezio import tabs
+from poezio.decorators import command_args_parser
+from poezio.plugin import BasePlugin
+
+DICE = ('\u2680', '\u2681', '\u2682', '\u2683', '\u2684', '\u2685')
+
+class DiceRoll:
+ __slots__ = ['duration', 'total_duration', 'dice_number', 'msgtype',
+ 'jid', 'last_msgid', 'increments']
+ def __init__(self, total_duration, dice_number, is_muc, jid, msgid, increments):
+ self.duration = 0
+ self.total_duration = total_duration
+ self.dice_number = dice_number
+ self.msgtype = "groupchat" if is_muc else "chat"
+ self.jid = jid
+ self.last_msgid = msgid
+ self.increments = increments
+
+ def reroll(self):
+ self.duration += self.increments
+
+ def is_finished(self):
+ return self.duration >= self.total_duration
+
+class Plugin(BasePlugin):
+ default_config = {"roll": {"refresh": 0.5, "default_duration": 5}}
+
+ def init(self):
+ for tab_t in [tabs.MucTab, tabs.ConversationTab, tabs.PrivateTab]:
+ self.api.add_tab_command(tab_t, 'roll', self.command_dice,
+ help='Roll a die',
+ usage='<number> [duration]')
+
+ @command_args_parser.quoted(1, 1, [''], True)
+ def command_dice(self, args):
+ tab = self.api.current_tab()
+ duration = self.config.get('default_duration')
+ try:
+ num_dice = int(args[0])
+ if args[1]:
+ duration = float(args[1])
+ except ValueError:
+ self.core.command.help("roll")
+ return
+ else:
+ if num_dice <= 0 or duration < 0:
+ self.core.command.help("roll")
+ return
+
+ firstroll = ''.join(random.choice(DICE) for _ in range(num_dice))
+ tab.command_say(firstroll)
+ is_muctab = isinstance(tab, tabs.MucTab)
+ msg_id = tab.last_sent_message["id"]
+ increment = self.config.get('refresh')
+ roll = DiceRoll(duration, num_dice, is_muctab, tab.name, msg_id, increment)
+ event = self.api.create_delayed_event(increment, self.delayed_event, roll)
+ self.api.add_timed_event(event)
+
+ def delayed_event(self, roll):
+ if roll.is_finished():
+ return
+ roll.reroll()
+ message = self.core.xmpp.make_message(roll.jid)
+ message["type"] = roll.msgtype
+ message["body"] = ''.join(random.choice(DICE) for _ in range(roll.dice_number))
+ message["replace"]["id"] = roll.last_msgid
+ message.send()
+ roll.last_msgid = message['id']
+ event = self.api.create_delayed_event(roll.increments,
+ self.delayed_event, roll)
+ self.api.add_timed_event(event)