aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2018-01-25 01:06:07 +0100
committerRenaud G <renaud@rolisteam.org>2018-01-25 01:06:07 +0100
commitbb9eeae7d5798d5c2c8579949c544b55be1bcbca (patch)
tree768c7da75e6611fd0bc128b659255dcfc7cd1f84
parent8904bc61ad71f407fbefa4b80793ba424f2ce88b (diff)
downloadOneRoll-bb9eeae7d5798d5c2c8579949c544b55be1bcbca.tar.gz
OneRoll-bb9eeae7d5798d5c2c8579949c544b55be1bcbca.zip
-Add management of unique for dice and list.
-rw-r--r--diceparser.cpp6
-rw-r--r--node/dicerollernode.cpp28
-rw-r--r--node/dicerollernode.h4
-rw-r--r--node/listsetrollnode.cpp58
-rw-r--r--node/listsetrollnode.h1
-rw-r--r--parsingtoolbox.cpp2
6 files changed, 78 insertions, 21 deletions
diff --git a/diceparser.cpp b/diceparser.cpp
index b4dba75..24bf3c4 100644
--- a/diceparser.cpp
+++ b/diceparser.cpp
@@ -650,6 +650,8 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node)
{
qint64 max;
qint64 min;
+ bool unique = (ParsingToolBox::UNIQUE == m_parsingToolbox->readListOperator(str)) ?
+ true : false;
Die::ArithmeticOperator op;
bool hasOp= m_parsingToolbox->readArithmeticOperator(str,op);
@@ -661,6 +663,7 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node)
return false;
}
DiceRollerNode* drNode = new DiceRollerNode(max);
+ drNode->setUnique(unique);
if(hasOp)
{
drNode->setOperator(op);
@@ -675,11 +678,10 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node)
}
else if(m_parsingToolbox->readDiceRange(str,min,max))
{
-
// qint64 face = abs(num - end);
//qDebug() << face << end;
DiceRollerNode* drNode = new DiceRollerNode(max,min);
-
+ drNode->setUnique(unique);
if(hasOp)
{
drNode->setOperator(op);
diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp
index 5a2ea04..1f501e5 100644
--- a/node/dicerollernode.cpp
+++ b/node/dicerollernode.cpp
@@ -29,6 +29,13 @@ void DiceRollerNode::run(ExecutionNode* previous)
{
m_errors.insert(NO_DICE_TO_ROLL,QObject::tr("No dice to roll"));
}
+ auto possibleValue = (m_max-m_min)+1;
+ //qDebug() << possibleValue;
+ if( possibleValue < m_diceCount && m_unique)
+ {
+ m_errors.insert(TOO_MANY_DICE,QObject::tr("More unique values asked than possible values (D operator)"));
+ return;
+ }
for(quint64 i=0; i < m_diceCount ; ++i)
{
@@ -37,7 +44,16 @@ void DiceRollerNode::run(ExecutionNode* previous)
die->setBase(m_min);
die->setMaxValue(m_max);
die->roll();
- //qDebug() << die->getValue() << "value";
+ if(m_unique)
+ {
+ const auto& equal = [](const Die* a,const Die* b){
+ return a->getValue() == b->getValue();
+ };
+ while(m_diceResult->contains(die,equal))
+ {
+ die->roll(false);
+ }
+ }
m_diceResult->insertResult(die);
}
if(nullptr!=m_nextNode)
@@ -94,3 +110,13 @@ void DiceRollerNode::setOperator(const Die::ArithmeticOperator &dieOperator)
m_operator = dieOperator;
m_diceResult->setOperator(dieOperator);
}
+
+bool DiceRollerNode::getUnique() const
+{
+ return m_unique;
+}
+
+void DiceRollerNode::setUnique(bool unique)
+{
+ m_unique = unique;
+}
diff --git a/node/dicerollernode.h b/node/dicerollernode.h
index 0857b30..2b4e3dd 100644
--- a/node/dicerollernode.h
+++ b/node/dicerollernode.h
@@ -47,12 +47,16 @@ public:
Die::ArithmeticOperator getOperator() const;
void setOperator(const Die::ArithmeticOperator & dieOperator);
+ bool getUnique() const;
+ void setUnique(bool unique);
+
private:
quint64 m_diceCount;
qint64 m_max; /// faces
DiceResult* m_diceResult;
qint64 m_min;
Die::ArithmeticOperator m_operator;
+ bool m_unique;
};
#endif // DICEROLLERNODE_H
diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp
index 21d5403..d7d4566 100644
--- a/node/listsetrollnode.cpp
+++ b/node/listsetrollnode.cpp
@@ -65,17 +65,24 @@ void ListSetRollNode::run(ExecutionNode* previous)
if(nullptr!=result)
{
quint64 diceCount = result->getResult(Result::SCALAR).toReal();
- m_result->setPrevious(result);
- QStringList rollResult;
- for(quint64 i=0; i < diceCount ; ++i)
+ if(diceCount > m_values.size() && m_unique)
{
- Die* die = new Die();
- computeFacesNumber(die);
- die->roll();
- m_diceResult->insertResult(die);
- getValueFromDie(die,rollResult);
+ m_errors.insert(TOO_MANY_DICE,QObject::tr("More unique values asked than possible values (L operator)"));
+ }
+ else
+ {
+ m_result->setPrevious(result);
+ QStringList rollResult;
+ for(quint64 i=0; i < diceCount ; ++i)
+ {
+ Die* die = new Die();
+ computeFacesNumber(die);
+ die->roll();
+ m_diceResult->insertResult(die);
+ getValueFromDie(die,rollResult);
+ }
+ m_stringResult->setText(rollResult.join(","));
}
- m_stringResult->setText(rollResult.join(","));
if(nullptr!=m_nextNode)
{
m_nextNode->run(this);
@@ -110,12 +117,10 @@ void ListSetRollNode::computeFacesNumber(Die* die)
{
if(((i==0)||(max<range.getEnd()))&&(range.isFullyDefined()))
{
- // qDebug()<< range.isFullyDefined() << range.getEnd();
max= range.getEnd();
}
++i;
}
- //qDebug() << "set Faces"<<max;
die->setMaxValue(max);
}
@@ -126,20 +131,39 @@ void ListSetRollNode::getValueFromDie(Die* die,QStringList& rollResult)
{
if(die->getValue()-1<m_values.size())
{
- rollResult << m_values[die->getValue()-1];
+ auto str = m_values[die->getValue()-1];
+ while(m_unique && rollResult.contains(str))
+ {
+ die->roll(false);
+ str = m_values[die->getValue()-1];
+ }
+ rollResult << str;
}
}
else
{
Q_ASSERT(m_values.size() == m_rangeList.size());
- int i=0;
- foreach (Range range, m_rangeList)
+ bool found = false;
+ while(!found)
{
- if(range.hasValid(die,false))
+ int i=0;
+ for (Range& range: m_rangeList)
{
- rollResult << m_values[i];
+ auto it = std::find(m_rangeIndexResult.begin(),m_rangeIndexResult.end(),i);
+ auto isValid = range.hasValid(die,false);
+ if((isValid && !m_unique)||
+ (isValid && it == m_rangeIndexResult.end()))
+ {
+ m_rangeIndexResult.push_back(i);
+ rollResult << m_values[i];
+ found=true;
+ }
+ ++i;
+ }
+ if(!found)
+ {
+ die->roll(false);
}
- ++i;
}
}
}
diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h
index 2cc36f0..15d00e4 100644
--- a/node/listsetrollnode.h
+++ b/node/listsetrollnode.h
@@ -54,6 +54,7 @@ private:
QStringList m_values;
DiceResult* m_diceResult;
StringResult* m_stringResult;
+ std::vector<int> m_rangeIndexResult;
bool m_unique;
QList<Range> m_rangeList;
};
diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp
index ea74a00..4ed0917 100644
--- a/parsingtoolbox.cpp
+++ b/parsingtoolbox.cpp
@@ -583,9 +583,9 @@ bool ParsingToolBox::readDiceRange(QString& str,qint64& start, qint64& end)
}
ParsingToolBox::LIST_OPERATOR ParsingToolBox::readListOperator(QString& str)
{
-
if(str.startsWith('u'))
{
+ str=str.remove(0,1);
return UNIQUE;
}
return NONE;