aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libparser/node/forloopnode.cpp
blob: aba6ff40b247ab87fcd6b5478b25d6639c918e48 (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
#include "forloopnode.h"

#include "die.h"

MockNode::MockNode() {}

void MockNode::run(ExecutionNode* node)
{
    return;
}

void MockNode::setResult(Result* result)
{
    m_result= result;
}

QString MockNode::toString(bool) const
{
    return {};
};
qint64 MockNode::getPriority() const
{
    return 0;
}
ExecutionNode* MockNode::getCopy() const
{
    return new MockNode();
}
// end mocknode

ForLoopNode::ForLoopNode() : m_diceResult(new DiceResult) {}

void ForLoopNode::setInternal(ExecutionNode* node)
{
    m_internal.reset(node);
}

void ForLoopNode::run(ExecutionNode* previous)
{
    if(nullptr != previous)
    {
        auto prevResult= dynamic_cast<DiceResult*>(previous->getResult());
        if(nullptr != prevResult)
        {
            m_diceResult->setPrevious(prevResult);
            QList<Die*> diceList= prevResult->getResultList();
            for(Die* dice : diceList)
            {
                MockNode node;
                DiceResult diceResult;
                diceResult.insertResult(dice);
                node.setResult(&diceResult);
                m_internal->run(&node);

                auto tmp= m_internal.get();
                while(nullptr != tmp->getNextNode())
                {
                    tmp= tmp->getNextNode();
                }
                Result* internalResult= tmp->getResult();
                auto value= internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toInt();

                Die* neodie= new Die();
                *neodie= *dice;
                neodie->setValue(value);
                m_diceResult->insertResult(neodie);
                node.setResult(nullptr);
                diceResult.clear();
                dice->displayed();
            }
        }
    }
    m_result= m_diceResult;
    if(m_nextNode != nullptr)
        m_nextNode->run(this);
}

qint64 ForLoopNode::getPriority() const
{
    return 2;
}

QString ForLoopNode::toString(bool withLabel) const
{
    if(withLabel)
    {
        return QString("%1 [label=\"ForLoopNode Node\"]").arg(m_id);
    }
    else
    {
        return m_id;
    }
}

ExecutionNode* ForLoopNode::getCopy() const
{
    auto node= new ForLoopNode();
    if(m_internal)
    {
        node->setInternal(m_internal->getCopy());
    }
    return node;
}