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

#include <QDateTime>

namespace {
QDateTime cookStartTime;
bool cookStarted = false;
qint64 cookingTime;
int cookingCount;

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


void DirtyLevel::cookStart()
{
    cookStarted = true;
    cookStartTime = QDateTime::currentDateTime();
    ::cookingCount++;
}

void DirtyLevel::cookEnd()
{
    if (cookStarted)
    {
        cookStarted = true;
        ::cookingTime += cookStartTime.secsTo(QDateTime::currentDateTime());
    }
}

void DirtyLevel::wash(int type)
{
    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;
    }
}

int DirtyLevel::dirty()
{
    if (::cookingCount < 1)
        return 0;
    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()
{
    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)
{
    ::cookingTime = secs;
}

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

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

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