Merge lp:~andrenasturas/synapse-project/rolldice-plugin into lp:synapse-project

Proposed by André Nasturas
Status: Needs review
Proposed branch: lp:~andrenasturas/synapse-project/rolldice-plugin
Merge into: lp:synapse-project
Diff against target: 192 lines (+166/-0)
3 files modified
src/plugins/Makefile.am (+1/-0)
src/plugins/rolldice-plugin.vala (+164/-0)
src/ui/synapse-main.vala (+1/-0)
To merge this branch: bzr merge lp:~andrenasturas/synapse-project/rolldice-plugin
Reviewer Review Type Date Requested Status
Synapse core team Pending
Review via email: mp+334442@code.launchpad.net

Description of the change

Plugin for Rolldice.

Adds a plugin for the command-line program rolldice, allowing the user to throw dices and see results in synapse as easily as the calculator plugin.
User only have to type a command in rolldice syntax like "6d10+5" and the result will be printed.
Supports multiple dices rolls by returning as many results as rolls requested by the user.

Should be answering any string supported by rolldice :

Pseudo regex : [Ax]?[B]?dC[*D]?[+|-E]?[sF]?
All parts between brackets are optional.

The command can be interpreted as : "Roll B C-sized dices, drop the lowest F, multiply the result by D and add or subtract E, and do it A times independently".
A, B, C, D, E and F are all numbers.

C can also be "%", which is here a shortcut for "100-sized dice".
C cannot be 0 or 1, as these are not supported as number of faces by rolldice, and lead to unexpected results (rolldice use a default 6-sized dice instead).

Usage example :

"d6" : return a random number between 1 and 6.
"3xd4" : return three results, each being a random number between 1 and 4.
"6d10s4" : return a random number between 2 and 20 with a non-normal distribution, as it will roll 6 dices and keep only the two highest scores (dropping the 4 lowest) before summing.
"d5*2" : return a random even number between 2 and 10.
"d6+6" : return a random number between 7 and 12.

To post a comment you must log in.
Revision history for this message
Michael Aquilina (michaelaquilina) wrote :

Andre, this project isnt really active right now (from what I can tell). For this reason I have a fork on github that has a few improvements https://github.com/MichaelAquilina/synapse-project. Could I suggest you also open a PR there so that we get something released?

Unmerged revisions

656. By André Nasturas <email address hidden>

Added support for multiple rolls with multiple results

655. By André Nasturas <email address hidden>

Merge from main

654. By André Nasturas <email address hidden>

Add Rolldice plugin

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/plugins/Makefile.am'
--- src/plugins/Makefile.am 2016-04-01 05:58:51 +0000
+++ src/plugins/Makefile.am 2017-11-29 11:04:07 +0000
@@ -49,6 +49,7 @@
49 pastebin-plugin.vala \49 pastebin-plugin.vala \
50 pidgin-plugin.vala \50 pidgin-plugin.vala \
51 rhythmbox-plugin.vala \51 rhythmbox-plugin.vala \
52 rolldice-plugin.vala \
52 selection-plugin.vala \53 selection-plugin.vala \
53 test-slow-plugin.vala \54 test-slow-plugin.vala \
54 xnoise-media-player-plugin.vala \55 xnoise-media-player-plugin.vala \
5556
=== added file 'src/plugins/rolldice-plugin.vala'
--- src/plugins/rolldice-plugin.vala 1970-01-01 00:00:00 +0000
+++ src/plugins/rolldice-plugin.vala 2017-11-29 11:04:07 +0000
@@ -0,0 +1,164 @@
1/*
2 * Copyright (C) 2017 André Nasturas <andre.nasturas+synapse@delfosia.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. *
17 */
18
19namespace Synapse
20{
21 public class RolldicePlugin : Object, Activatable, ItemProvider
22 {
23 public bool enabled { get; set; default = true; }
24
25 public void activate ()
26 {
27
28 }
29
30 public void deactivate ()
31 {
32
33 }
34
35 private class Result : TextMatch
36 {
37 public int default_relevancy { get; set; default = 0; }
38
39 public Result (string result, string match_string)
40 {
41 Object (
42 title: "%s".printf (result),
43 description: "%s : %s".printf (match_string, result),
44 has_thumbnail: false,
45 icon_name: "applications-boardgames"
46 );
47 }
48
49 public override string get_text ()
50 {
51 return title;
52 }
53 }
54
55 static void register_plugin ()
56 {
57 PluginRegistry.get_default ().register_plugin (
58 typeof (RolldicePlugin),
59 _("Rolldice"),
60 _("Rolls virtual dice."),
61 "applications-boardgames",
62 register_plugin,
63 Environment.find_program_in_path ("rolldice") != null,
64 _("rolldice is not installed")
65 );
66 }
67
68 static construct
69 {
70 register_plugin ();
71 }
72
73 private Regex regex;
74
75 construct
76 {
77 /*
78 This regex is based on the regex provided by rolldice documentation.
79 It should catch a string formed like 6x5d7*2+4s3, which will be interpreted as
80 "Roll five seven-sized dices, drop the lowest three, multiply the result by 2 and add 4,
81 and do it 6 times independently."
82 Also added a specific modification to prevent the regex catching "d0" and "d1" substrings
83 (zero-sized and one-sized dice) to prevent unexcepted results (as it is not supported by
84 rolldice, it may throw 6-sized dices instead, which could be surprising when the user ask
85 for one one-sized dice and could except a 1 as result.)
86 */
87
88 try
89 {
90 regex = new Regex (
91 "^([0-9]+x)?[0-9]*d(([2-9]|[1-9][0-9]+)|%)(\\*[0-9]+)?((\\+|\\-)[0-9]+)?(s[0-9]+)?$",
92 RegexCompileFlags.OPTIMIZE);
93 } catch (Error e) {
94 critical ("Error creating regexp.");
95 }
96 }
97
98 public bool handles_query (Query query)
99 {
100 return (QueryFlags.ACTIONS in query.query_type);
101 }
102
103 public async ResultSet? search (Query query) throws SearchError
104 {
105 string input = query.query_string.replace (" ", "");
106 bool matched = regex.match (input);
107 if (!matched && input.length > 1)
108 {
109 input = input[0 : input.length - 1];
110 matched = regex.match (input);
111 }
112 if (matched)
113 {
114 Pid pid;
115 int read_fd, write_fd;
116 string[] argv = {"rolldice"};
117 string? solution = null;
118
119 try
120 {
121 Process.spawn_async_with_pipes (null, argv, null,
122 SpawnFlags.SEARCH_PATH,
123 null, out pid, out write_fd, out read_fd);
124 UnixInputStream read_stream = new UnixInputStream (read_fd, true);
125 DataInputStream rolldice_output = new DataInputStream (read_stream);
126
127 UnixOutputStream write_stream = new UnixOutputStream (write_fd, true);
128 DataOutputStream rolldice_input = new DataOutputStream (write_stream);
129
130 rolldice_input.put_string (input + "\n", query.cancellable);
131 yield rolldice_input.close_async (Priority.DEFAULT, query.cancellable);
132 solution = yield rolldice_output.read_line_async (Priority.DEFAULT_IDLE, query.cancellable);
133 solution = yield rolldice_output.read_line_async (Priority.DEFAULT_IDLE, query.cancellable);
134 /* As rolldice send the input on the standard output, we read it twice to get the result */
135
136 if (solution != null)
137 {
138 ResultSet results = new ResultSet ();
139 string[] solutions = solution.split(" ");
140
141 for (int i = 0; i < solutions.length; i++)
142 {
143 string s = solutions[i];
144 if (s.length > 0) // Prevent empty solutions due to the split with a final whitespace
145 {
146 Result result = new Result (s, query.query_string);
147 results.add (result, MatchScore.AVERAGE+i); // Prevent ResultSet to be sorted
148 }
149 }
150 query.check_cancellable ();
151 return results;
152 }
153 }
154 catch (Error err)
155 {
156 if (!query.is_cancelled ()) warning ("%s", err.message);
157 }
158 }
159
160 query.check_cancellable ();
161 return null;
162 }
163 }
164}
0165
=== modified file 'src/ui/synapse-main.vala'
--- src/ui/synapse-main.vala 2016-04-01 05:58:51 +0000
+++ src/ui/synapse-main.vala 2017-11-29 11:04:07 +0000
@@ -177,6 +177,7 @@
177 typeof (PassPlugin),177 typeof (PassPlugin),
178 typeof (ChatActions),178 typeof (ChatActions),
179 typeof (ZealPlugin),179 typeof (ZealPlugin),
180 typeof (RolldicePlugin),
180#if HAVE_ZEITGEIST181#if HAVE_ZEITGEIST
181 typeof (ZeitgeistPlugin),182 typeof (ZeitgeistPlugin),
182 typeof (ZeitgeistRelated),183 typeof (ZeitgeistRelated),