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

#include <diceparser/parsingtoolbox.h>

RoundNode::RoundNode(Mode mode) : m_scalarResult(new ScalarResult), m_mode(mode) {}

void RoundNode::run(ExecutionNode* previous)
{
    m_previousNode= previous;

    if(m_cmd == nullptr)
        return;

    m_cmd->execute(this);
    auto internal= ParsingToolBox::getLeafNode(m_cmd);
    if(!internal)
        return;

    auto endResult= internal->getResult();

    auto scalar= endResult->getResult(Dice::RESULT_TYPE::SCALAR).toDouble();

    int resVal;
    switch(m_mode)
    {
    case FLOOR:
        resVal= std::floor(scalar);
        break;
    case CEIL:
        resVal= std::ceil(scalar);
        break;
    case ROUND:
        resVal= std::round(scalar);
        break;
    }

    m_scalarResult->setValue(resVal);
    m_result= m_scalarResult.get();
}

QString RoundNode::toString(bool withLabel) const
{

    return withLabel ? QString("%1 [label=\"RoundNode\"]").arg(m_id) : m_id;
}

qint64 RoundNode::getPriority() const
{
    qint64 priority= 0;
    if(nullptr != m_nextNode)
    {
        priority= m_nextNode->getPriority();
    }

    return priority;
}

ExecutionNode* RoundNode::getCopy() const
{
    RoundNode* node= new RoundNode(m_mode);
    if(nullptr != m_nextNode)
    {
        node->setNextNode(m_nextNode->getCopy());
    }
    return node;
}

void RoundNode::setCommand(ExecutionNode* cmd)
{
    m_cmd= cmd;
}