1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/***************************************************************************
* Copyright (C) 2014 by Renaud Guezennec *
* http://renaudguezennec.homelinux.org/accueil,3.html *
* *
* This file is part of DiceParser *
* *
* DiceParser is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QStringList>
#include "diceparser.h"
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QTextStream>
QTextStream out(stdout, QIODevice::WriteOnly);
QString diceToText(ExportedDiceResult& dice)
{
QStringList resultGlobal;
foreach(int face, dice.keys())
{
QStringList result;
ListDiceResult diceResult = dice.value(face);
//patternColor = patternColorarg();
foreach (DiceAndHighlight tmp, diceResult)
{
QStringList diceListStr;
QStringList diceListChildren;
for(int i =0; i < tmp.first.size(); ++i)
{
quint64 dievalue = tmp.first[i];
QString prefix("%1");
if(tmp.second)
{
prefix = "\e[0;31m%1\e[0m";
}
if(i==0)
{
diceListStr << prefix.arg(QString::number(dievalue));
}
else
{
diceListChildren << prefix.arg(QString::number(dievalue));
}
}
if(!diceListChildren.isEmpty())
{
diceListStr << QString("[%1]").arg(diceListChildren.join(' '));
}
result << diceListStr.join(' ');
// qDebug() << result << tmp.first << tmp.second;
}
if(dice.keys().size()>1)
{
resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face);
}
else
{
resultGlobal << result;
}
}
return resultGlobal.join(' ');
}
void startDiceParsing(QString& cmd,QString& treeFile,bool highlight)
{
DiceParser* parser = new DiceParser();
if(parser->parseLine(cmd))
{
//
if(treeFile.isEmpty())
{
parser->Start();
if(!parser->getErrorMap().isEmpty())
{
out << "Error" << parser->humanReadableError()<< "\n";
return;
}
ExportedDiceResult list;
parser->getLastDiceResult(list);
QString diceText = diceToText(list);
QString scalarText;
QString str;
if(parser->hasIntegerResultNotInFirst())
{
scalarText = QString("%1").arg(parser->getLastIntegerResult());
}
else if(!list.isEmpty())
{
scalarText = QString("%1").arg(parser->getSumOfDiceResult());
}
str = QString("Result: \e[0;31m%1\e[0m, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(parser->getDiceCommand());
if(parser->hasStringResult())
{
str = parser->getStringResult();
}
out << str << "\n";
}
else
{
parser->writeDownDotTree(treeFile);
}
}
else
{
out << parser->humanReadableError()<< "\n";;
}
}
void usage()
{
QString help = "Usage: ./dice [options]\n\
\n\
Options:\n\
-c, --color-off Disable color to highlight result\n\
-v, --version Show the version and quit.\n\
--reset-settings Erase the settings and use the default\n\
parameters\n\
-d, --dot-file <dotfile> Instead of rolling dice, generate the\n\
execution tree and write it in <dotfile>\n\
-t, --translation <translationfile> path to the translation file:\n\
<translationfile>\n\
-h, --help Display this help\n\
";
out << help;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc,argv);
QCoreApplication::setApplicationName("dice");
QCoreApplication::setApplicationVersion("1.0");
QStringList commands;
QString cmd;
QString dotFileStr;
bool colorb=true;
QCommandLineParser optionParser;
QCommandLineOption color(QStringList() << "c"<< "color-off", "Disable color to highlight result");
QCommandLineOption version(QStringList() << "v"<< "version", "Show the version and quit.");
QCommandLineOption reset(QStringList() << "reset-settings", "Erase the settings and use the default parameters");
QCommandLineOption dotFile(QStringList() << "d"<<"dot-file", "Instead of rolling dice, generate the execution tree and write it in <dotfile>","dotfile");
QCommandLineOption translation(QStringList() << "t"<<"translation", "path to the translation file: <translationfile>","translationfile");
QCommandLineOption help(QStringList() << "h"<<"help", "Display this help");
if(!optionParser.addOption(color))
{
out << optionParser.errorText() << "\n";
}
optionParser.addOption(version);
optionParser.addOption(reset);
optionParser.addOption(dotFile);
optionParser.addOption(translation);
optionParser.addOption(help);
for(int i=0;i<argc;++i)
{
commands << QString::fromLatin1(argv[i]);
}
optionParser.process(commands);
if(optionParser.isSet(color))
{
commands.removeAt(0);
colorb = false;
}
else if(optionParser.isSet(version))
{
out << "Rolisteam DiceParser v1.0.0"<< "\n";
out << "More Details: www.rolisteam.org"<< "\n";
return 0;
}
else if(optionParser.isSet(reset))
{
return 0;
}
else if(optionParser.isSet(dotFile))
{
dotFileStr = optionParser.value(dotFile);
}
else if(optionParser.isSet(translation))
{
}
else if(optionParser.isSet(help))
{
cmd = "help";
}
QStringList cmdList = optionParser.positionalArguments();
// qDebug()<< "rest"<< cmdList;
if(!cmdList.isEmpty())
{
cmd = cmdList.first();
}
if(!cmd.isEmpty())
{
startDiceParsing(cmd,dotFileStr,colorb);
if(cmd=="help")
{
usage();
}
}
/*commands<< "10d10c[>6]+@c[=10]"
<< "1L[cheminée,chocolat,épée,arc,chute de pierre]"
<< "10d10c[>=6]-@c[=1]"
<< "10d10c[>=6]-@c[=1]-@c[=1]"
<< "10d10c[>6]+@c[=10]"
<< "1+1D10"
<< "3d10c[>=5]"
<< "3nwod"
<< "1+(4*3)D10"
<< "2+4/4"
<< "2D10*2D20*8"
<<"1+(4*3)D10"
<<"(4D6)D10"
<< "1D100a[>=95]a[>=96]a[>=97]a[>=98]a[>=99]e[>=100]"
<< "3D100"
<< "4k3"
<< "10D10e[>=6]sc[>=6]"
<< "100190D6666666s"
<< "10D10e10s"
<< "10D10s"
<< "15D10e10c[8-10]"
<< "10d10e11"
<< "1D8+2D6+7"
<< "100190D6666666s"
<< "D25"
<< "D25+D10"
<< "D25;D10"
<< "8+8+8"
<< "1D20-88"
<< "100*1D20*2D6"
<< "100/28*3"
<< "100/8"
<< "100*3*8"
<< "help"
<< "la"
<< "400000D20/400000"
<< "100*3*8";//
*/
return 0;
}
|