dirtylevel.cpp 3.12 KB
#include "dirtylevel.h"

#include <QDateTime>

#include "ovenstatics.h"

namespace {
QDateTime cookStartTime;
bool cookStarted = false;

void setCookingTime(qint64 t)
{
    OvenStatistics::getInstance()->setTotalCookingTime(t, false);
}

void addCookingTime(qint64 t)
{
    OvenStatistics::getInstance()->addTotalCookingTime(t, false);
}

qint64 getCookingTime()
{
    return OvenStatistics::getInstance()->loadTotalCookingTime();
}

void setCookingCount(int c)
{
    OvenStatistics::getInstance()->setTotalCookingCount(c, false);
}

void addCookingCount(int c)
{
    OvenStatistics::getInstance()->addTotalCookingCount(c, false);
}

int getCookingCount()
{
    return OvenStatistics::getInstance()->loadTotalCookingCount();
}

void decountState(int level)
{
    int current = DirtyLevel::state();
    int target = qMax(current - level, 0);
    switch (target)
    {
    case 0:
        setCookingCount(0);
        break;
    case 1:
        setCookingCount(1);
        break;
    case 2:
        setCookingCount(6);
        break;
    case 3:
        setCookingCount(11);
        break;
    case 4:
        setCookingCount(16);
        break;
    case 5:
        setCookingCount(21);
        break;
    }
}
}


void DirtyLevel::cookStart()
{
    cookStarted = true;
    cookStartTime = QDateTime::currentDateTime();
    ::addCookingCount(1);
}

void DirtyLevel::cookEnd()
{
    if (cookStarted)
    {
        cookStarted = false;

        ::addCookingTime(cookStartTime.secsTo(QDateTime::currentDateTime()));
    }
}

void DirtyLevel::wash(int type)
{
    qint64 cookingTime = ::getCookingTime();
    switch (type)
    {
    case 1:
        cookingTime = qMax(cookingTime - 2 * 3600, (qint64) 0);
        decountState(1);
        break;
    case 2:
        cookingTime = qMax(cookingTime - 3 * 3600, (qint64) 0);
        decountState(1);
        break;
    case 3:
        cookingTime = qMax(cookingTime - 4 * 3600, (qint64) 0);
        decountState(2);
        break;
    case 4:
        cookingTime = qMax(cookingTime - 10 * 3600, (qint64) 0);
        decountState(2);
        break;
    case 5:
        cookingTime = qMax(cookingTime - 2 * 3600, (qint64) 0);
        decountState(1);
        break;
    }
    ::setCookingTime(cookingTime);
}

int DirtyLevel::dirty()
{
    if (::getCookingCount() < 1)
        return 0;

    qint64 cookingTime = ::getCookingTime();
    if (cookingTime < 1 * 3600)
        return 1;
    if (cookingTime < 3 * 3600)
        return 2;
    if (cookingTime < 5 * 3600)
        return 3;
    if (cookingTime < 7 * 3600)
        return 4;
    return 5;
}

int DirtyLevel::state()
{
    int cookingCount = ::getCookingCount();
    if (cookingCount < 1)
        return 0;
    if (cookingCount <= 5)
        return 1;
    if (cookingCount <= 10)
        return 2;
    if (cookingCount <= 15)
        return 3;
    if (cookingCount <= 20)
        return 4;
    return 5;
}

void DirtyLevel::setCookingTime(qint64 secs)
{
    ::setCookingTime(secs);
}

int DirtyLevel::cookingTime()
{
    return ::getCookingTime();
}

int DirtyLevel::cookingCount()
{
    return ::getCookingCount();
}

void DirtyLevel::setCookingCount(int count)
{
    ::setCookingCount(count);
}