aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/node/rerolldicenode.cpp
blob: f51eb9e0e5583dd50d9716a2b1f2d0aaa8f03f70 (plain) (blame)
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
#include "rerolldicenode.h"
#include "parsingtoolbox.h"

RerollDiceNode::RerollDiceNode()
    : m_diceResult(new DiceResult()),m_adding(false),m_validator(nullptr)
{
    m_result=m_diceResult;
}
RerollDiceNode::~RerollDiceNode()
{
	if(nullptr!=m_validator)
	{
		delete m_validator;
		m_validator = nullptr;
	}
}
void RerollDiceNode::run(ExecutionNode* previous)
{
    m_previousNode = previous;
    if((nullptr!=previous)&&(nullptr!=previous->getResult()))
    {
        DiceResult* previous_result = dynamic_cast<DiceResult*>(previous->getResult());
        m_result->setPrevious(previous_result);
        if(nullptr!=previous_result)
        {
            for(Die* die: previous_result->getResultList())
            {
                Die* tmpdie = new Die();
                *tmpdie=*die;
                m_diceResult->insertResult(tmpdie);
                die->displayed();
            }
            //m_diceResult->setResultList(list);

            QList<Die*>& list = m_diceResult->getResultList();

            for(Die* die: list)
            {
                if(m_validator->hasValid(die,false))
                {
                    if(m_instruction != nullptr)
                    {
                        m_instruction->run(this);
                        auto lastNode = ParsingToolBox::getLatestNode(m_instruction);
                        if(lastNode != nullptr)
                        {
                            auto lastResult = dynamic_cast<DiceResult*>(lastNode->getResult());
                            if(lastResult != nullptr)
                            {
                                list.append(lastResult->getResultList());
                            }
                        }
                    }
                    else
                    {
                        die->roll(m_adding);
                    }
                }
            }

            if(nullptr!=m_nextNode)
            {
                m_nextNode->run(this);
            }
        }
        else
        {
            m_errors.insert(ExecutionNode::DIE_RESULT_EXPECTED,
                            QObject::tr(" The a operator expects dice result. Please check the documentation and fix your command."));
        }
    }
}
void RerollDiceNode::setValidator(Validator* val)
{
      m_validator = val;
}
QString RerollDiceNode::toString(bool wl) const
{
	if(wl)
	{
		return QString("%1 [label=\"RerollDiceNode validatior: %2\"]").arg(m_id).arg(m_validator->toString());
	}
	else
	{
		return m_id;
	}
	//return QString("RerollDiceNode [label=\"RerollDiceNode validatior:%1\"");
}
void RerollDiceNode::setAddingMode(bool b)
{
    m_adding = b;
}
qint64 RerollDiceNode::getPriority() const
{
    qint64 priority=0;
    if(nullptr!=m_nextNode)
    {
        priority = m_nextNode->getPriority();
    }


    return priority;
}
ExecutionNode* RerollDiceNode::getCopy() const
{
    RerollDiceNode* node = new RerollDiceNode();
    node->setValidator(m_validator);
    node->setAddingMode(m_adding);
    if(nullptr!=m_nextNode)
    {
        node->setNextNode(m_nextNode->getCopy());
    }
    return node;
}

ExecutionNode *RerollDiceNode::getInstruction() const
{
    return m_instruction;
}

void RerollDiceNode::setInstruction(ExecutionNode *instruction)
{
    m_instruction = instruction;
}