Example #1
0
Scene* SwimScene::update()
{
    draw( 0, 0, "Pool.png");
    drawCenter( 120, 560, "SwimTimer.png" );

    elapsedTime = timer.elapsed();
    drawText( 100, 560, doubleToQString(static_cast<double>(elapsedTime) / 1000) );

    if( userSwimCount >= diff ) {
        userSwim();
        userSwimCount = 0;
    }
    if( getGameClass()->getGamemode() == OLYMPIC ) {
        opponentSwimCount++;
        if(opponentSwimCount >= 5) {
            opponentSwim();
            opponentSwimCount = 0;
        }
    }
    for(int i=0; i<numOfPlayers; i++) {
        switch(swimmerShapeCount[i]) {
        case 0:
            drawCenter( 650 - position[i], 235 + 70*i, characterImgName[i] + "1.png");
            break;
        case 1:
            drawCenter( 650 - position[i], 235 + 70*i, characterImgName[i] + "2.png");
            break;
        case 2:
            drawCenter( 650 - position[i], 235 + 70*i, characterImgName[i] + "3.png");
            break;
        case 3:
        default:
            drawCenter( 650 - position[i], 235 + 70*i, characterImgName[i] + "4.png");
            break;
        }
    }
    if(isFinished()) {
        sendResult();
    }
    return nextScene;
}
Example #2
0
QString luaTrajectory::pop(lua_State *luaL, QStringList& attribs)
{
#ifdef DEBUG_OBSERVER
    printf("\ngetState - Trajectory\n\n");
    luaStackToQString(12);

    qDebug() << attribs;
#endif

    QString msg;

    // id
    msg.append(QString::number(getId()));
    msg.append(PROTOCOL_SEPARATOR);

    // subjectType
    msg.append(QString::number(subjectType));
    msg.append(PROTOCOL_SEPARATOR);

    int pos = lua_gettop(luaL);

    //------------
    int attrCounter = 0;
    int elementCounter = 0;
    // bool contains = false;
    double num = 0;
    QString text, key, attrs, elements;

    lua_pushnil(luaL);
    while(lua_next(luaL, pos ) != 0)
    {
        if (lua_type(luaL, -2) == LUA_TSTRING)
        {
            key = QString(luaL_checkstring(luaL, -2));
        }
        else
        {
            if (lua_type(luaL, -2) == LUA_TNUMBER)
            {
                char aux[100];
                double number = luaL_checknumber(luaL, -2);
                sprintf(aux, "%g", number);
                key = QString(aux);
            }
        }

        if ((attribs.contains(key)) || (key == "cells"))
        {
            attrCounter++;
            attrs.append(key);
            attrs.append(PROTOCOL_SEPARATOR);

            switch( lua_type(luaL, -1) )
            {
            case LUA_TBOOLEAN:
                attrs.append(QString::number(TObsBool));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString::number( lua_toboolean(luaL, -1)));
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TNUMBER:
                num = luaL_checknumber(luaL, -1);
                doubleToQString(num, text, 20);
                attrs.append(QString::number(TObsNumber));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(text);
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TSTRING:
                text = QString(luaL_checkstring(luaL, -1));
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append( (text.isEmpty() || text.isNull() ? VALUE_NOT_INFORMED : text) );
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TTABLE:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append("Lua-Address(TB): " + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);

                // Recupera a tabela de cells e delega a cada
                // celula sua serialização
                if (key == "cells")
                {
                    int top = lua_gettop(luaL);

                    lua_pushnil(luaL);
                    while(lua_next(luaL, top) != 0)
                    {
                        int cellTop = lua_gettop(luaL);

                        lua_pushstring(luaL, "cObj_");
                        lua_gettable(luaL, cellTop);

                        luaCell*  cell;
                        cell = (luaCell*)Luna<luaCell>::check(L, -1);
                        lua_pop(luaL, 1);

                        // luaCell->pop(...) requer uma celula no topo da pilha
                        QString cellMsg = cell->pop(L, QStringList() << "x" << "y");
                        elements.append(cellMsg);
                        elementCounter++;

                        lua_pop(luaL, 1);
                    }
                    // break;
                }
                break;
            }

            case LUA_TUSERDATA	:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append("Lua-Address(UD): " + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            case LUA_TFUNCTION:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(FT): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            default:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(O): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }
            }
        }
        lua_pop(luaL, 1);
    }

    // #attrs
    msg.append(QString::number(attrCounter));
    msg.append(PROTOCOL_SEPARATOR );

    // #elements
    msg.append(QString::number(elementCounter));
    msg.append(PROTOCOL_SEPARATOR );
    msg.append(attrs);

    msg.append(PROTOCOL_SEPARATOR);
    msg.append(elements);
    msg.append(PROTOCOL_SEPARATOR);

#ifdef DEBUG_OBSERVER
    // save(msg);
    // qDebug() << msg.split(PROTOCOL_SEPARATOR); //, QString::SkipEmptyParts);
#endif

    return msg;
}
Example #3
0
QString luaCell::pop(lua_State *luaL, QStringList& attribs)
{
    double num = 0;
    bool boolAux = false;

    QString msg, attrs, key, text;

    int attrCounter = 0;
    int cellsPos = lua_gettop(luaL);
    // int type = lua_type (luaL, cellsPos);

    // @RAIAN: Serializa a vizinhanca
    if(attribs.contains("@getNeighborhoodState"))
    {
#ifndef TME_BLACK_BOARD
        // solucao provisoria
        attribs.pop_back();
#endif

        QString elements;

        NeighCmpstInterf neighborhoods = this->getNeighborhoods();
        NeighCmpstInterf::iterator itAux = neighborhoods.begin();

        while(itAux != neighborhoods.end())
        {
            QString neighborhoodID(itAux->first.c_str());

            if(attribs.contains(QString("neighborhood (") + neighborhoodID + QString(")")))
            {
                // Neighborhood ID
                msg.append(QString("neighborhood (") + neighborhoodID + QString(")"));
                msg.append(PROTOCOL_SEPARATOR);

                // subject TYPE
                msg.append(QString::number(TObsNeighborhood));
                msg.append(PROTOCOL_SEPARATOR);

                // Pega as informações da célula central (this)
                QString cellMsg = this->pop(luaL, QStringList() << "x" << "y");

                elements.append(cellMsg);

                CellNeighborhood *neigh = itAux->second;
                CellNeighborhood::iterator itNeigh = neigh->begin();
                int neighSize = neigh->size();

                //Number of Attributes - VARIFICAR SE ESTA CERTO
                msg.append(QString::number(0));
                msg.append(PROTOCOL_SEPARATOR);

                // Number of internal subjects
                msg.append(QString::number(neighSize + 1));
                msg.append(PROTOCOL_SEPARATOR);
                msg.append(PROTOCOL_SEPARATOR);

                while(itNeigh != neigh->end())
                {
                    luaCell* neighbor = (luaCell*)itNeigh->second;
                    CellIndex neighIdx = (CellIndex)itNeigh->first;
                    double weight = neigh->getWeight(neighIdx);

                    int ref = neighbor->getReference(luaL);
                    cellMsg = neighbor->pop(luaL, QStringList() << "x" << "y" << "@getWeight");
                    lua_settop(luaL, 0);

                    cellMsg.append(QString::number(TObsNumber));
                    cellMsg.append(PROTOCOL_SEPARATOR);
                    cellMsg.append(QString::number(weight));
                    cellMsg.append(PROTOCOL_SEPARATOR);
                    cellMsg.append(PROTOCOL_SEPARATOR);

                    elements.append(cellMsg);

                    itNeigh++;
                }
            }
            itAux++;
        }

        msg.append(elements);
        msg.append(PROTOCOL_SEPARATOR);
    }// @RAIAN: FIM
    else
    {

        // id
        msg.append(QString::number(getId())); // QString("%1").arg(this->ref));
        msg.append(PROTOCOL_SEPARATOR);

        // subjectType
        msg.append(QString::number(subjectType));
        msg.append(PROTOCOL_SEPARATOR);


        lua_pushnil(luaL);
        while(lua_next(luaL, cellsPos ) != 0)
        {
            key = QString(luaL_checkstring(luaL, -2));

            if( attribs.contains(key) )
            {
                attrCounter++;
                attrs.append(key);
                attrs.append(PROTOCOL_SEPARATOR);

                switch( lua_type(luaL, -1) )
                {
                case LUA_TBOOLEAN:
                    boolAux = lua_toboolean(luaL, -1);
                    attrs.append(QString::number(TObsBool));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString::number(boolAux));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;

                case LUA_TNUMBER:
                    num = luaL_checknumber(luaL, -1);
                    doubleToQString(num, text, 20);
                    attrs.append(QString::number(TObsNumber));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(text);
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;

                case LUA_TSTRING:
                    text = QString(luaL_checkstring(luaL, -1));
                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append( (text.isEmpty() || text.isNull() ? VALUE_NOT_INFORMED : text) );
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;

                case LUA_TTABLE:
                {
                    char result[100];
                    sprintf( result, "%p", lua_topointer(luaL, -1) );
                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(TB): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;
                }
                case LUA_TUSERDATA:
                {
                    char result[100];
                    sprintf( result, "%p", lua_topointer(luaL, -1) );
                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(UD): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;
                }

                case LUA_TFUNCTION:
                {
                    char result[100];
                    sprintf(result, "%p", lua_topointer(luaL, -1) );
                    attrs.append(QString::number(TObsText) );
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(FT): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;
                }

                default:
                {
                    char result[100];
                    sprintf(result, "%p", lua_topointer(luaL, -1) );
                    attrs.append(QString::number(TObsText) );
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(O): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;
                }
                }
            }
            lua_pop(luaL, 1);
        }

        //@RAIAN: Para uso na serializacao da Vizinhanca
        if(attribs.contains("@getWeight"))
        {
            attrCounter++;
            attrs.append("@getWeight");
            attrs.append(PROTOCOL_SEPARATOR);
        }
        //@RAIAN: FIM

        // #attrs
        msg.append(QString::number(attrCounter));
        msg.append(PROTOCOL_SEPARATOR );

        // #elements
        msg.append(QString::number(0));
        msg.append(PROTOCOL_SEPARATOR );

        msg.append(attrs);

        //@RAIAN: Para uso na serializacao da Vizinhanca
        if(!attribs.contains("@getWeight"))
            msg.append(PROTOCOL_SEPARATOR);
        //@RAIAN: FIM
    }

    return msg;
}
Example #4
0
QString luaLocalAgent::pop(lua_State *luaL, QStringList& attribs)
{
    QString msg;

    if (notNotify)
        return msg;

    // id
    msg.append(QString::number(getId()));
    msg.append(PROTOCOL_SEPARATOR);

    // subjectType
    msg.append(QString::number(subjectType));
    msg.append(PROTOCOL_SEPARATOR);

    int position = lua_gettop(luaL);

    int attrCounter = 0;
    int elementCounter = 0;
    // bool contains = false;
    double num = 0;
    QString text, key, attrs, elements;

    QStringList coordList = QStringList() << "x" << "y";

    // Percorre as celulas do espa(C)o recuperando o
    // estado do automato
    if (lua_istable(luaL, position - 1))
    {
        lua_pushnil(luaL);
        while (lua_next(luaL, position - 1) != 0)
        {
            if (lua_type(luaL, -2) == LUA_TSTRING)
                key = luaL_checkstring(luaL, -2);

            if (key == "cells")
            {
                int top = lua_gettop(luaL);

                lua_pushnil(luaL);
                while (lua_next(luaL, top) != 0)
                {
                    int cellTop = lua_gettop(luaL);
                    lua_pushstring(luaL, "cObj_");
                    lua_gettable(luaL, cellTop);

                    luaCell*  cell;
                    cell =(luaCell*)Luna<luaCell>::check(L, -1);
                    lua_pop(luaL, 1); // lua_pushstring

                    // luaCell->popCell(...) requer uma celula no topo da pilha
                    QString cellMsg = cell->pop(L, coordList);

                    ControlMode *ctrlMode = cell->getControlMode(this);

                    if (ctrlMode)
                    {
                        cellMsg.append("currentState" + attrClassName);
                        cellMsg.append(PROTOCOL_SEPARATOR);
                        cellMsg.append(QString::number(TObsText));
                        cellMsg.append(PROTOCOL_SEPARATOR);

                        cellMsg.append(ctrlMode->getControlModeName().c_str());
                        cellMsg.append(PROTOCOL_SEPARATOR);

                        // Adiciona o atributo currentState no protocolo
                        int idx = cellMsg.indexOf(PROTOCOL_SEPARATOR);
                        QString attNum = QString(cellMsg[idx + 3]);
                        cellMsg.replace(idx + 3, 1, attNum.setNum(attNum.toInt() + 1));

                        elements.append(cellMsg);
                        elementCounter++;
                    }
                    else
                    {
                        notNotify = true;

                        if (execModes != Quiet)
                        {
                            string err_out = string("Warning: Failed on retrieve Automaton subject state!!");
                            lua_getglobal(L, "customWarning");
                            lua_pushstring(L, err_out.c_str());
                            lua_pushnumber(L, 4);
                            lua_call(L, 2, 0);
                        }
                        return QString();
                    }

                    lua_pop(luaL, 1);
                }
            }
            lua_pop(luaL, 1);
        }
    }

    lua_pushnil(luaL);
    while (lua_next(luaL, position) != 0)
    {
        // Caso o indice no seja um string causava erro
        if (lua_type(luaL, -2) == LUA_TSTRING)
        {
            key = QString(luaL_checkstring(luaL, -2));
        }
        else
        {
            if (lua_type(luaL, -2) == LUA_TNUMBER)
            {
                char aux[100];
                double number = luaL_checknumber(luaL, -2);
                sprintf(aux, "%g", number);
                key = QString(aux);
            }
        }

        bool contains = attribs.contains(key);

        if (contains)
        {
            attrCounter++;
            attrs.append(key);
            attrs.append(PROTOCOL_SEPARATOR);

            switch (lua_type(luaL, -1))
            {
            case LUA_TBOOLEAN:
                attrs.append(QString::number(TObsBool));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(lua_toboolean(luaL, -1));
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TNUMBER:
                num = luaL_checknumber(luaL, -1);
                doubleToQString(num, text, 20);
                attrs.append(QString::number(TObsNumber));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(text);
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TSTRING:
                text = QString(luaL_checkstring(luaL, -1));
                attrs.append(QString::number(TObsText));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append((text.isEmpty() || text.isNull() ? VALUE_NOT_INFORMED : text));
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TTABLE:
                {
                    char result[100];
                    sprintf(result, "%p", lua_topointer(luaL, -1));
                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua Address(TB): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                }
                break;

            case LUA_TUSERDATA:
                {
                    char result[100];
                    sprintf(result, "%p", lua_topointer(luaL, -1));

                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(UD): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);

                    //if (isudatatype(luaL, -1, "TeState"))
                    //{
                    //    ControlMode*  lcm =(ControlMode*)Luna<luaControlMode>::check(L, -1);

                    //    QString state(lcm->getControlModeName().c_str());
                    //    attrCounter++;
                    //    attrs.append(state);
                    //    attrs.append(PROTOCOL_SEPARATOR);
                    //    attrs.append(QString::number(TObsText));
                    //    attrs.append(PROTOCOL_SEPARATOR);
                    //    attrs.append(state);
                    //    attrs.append(PROTOCOL_SEPARATOR);
                    //}
                    break;
                }

            case LUA_TFUNCTION:
                {
                    char result[100];
                    sprintf(result, "%p", lua_topointer(luaL, -1));
                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(FT): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;
                }

            default:
                {
                    char result[100];
                    sprintf(result, "%p", lua_topointer(luaL, -1));
                    attrs.append(QString::number(TObsText));
                    attrs.append(PROTOCOL_SEPARATOR);
                    attrs.append(QString("Lua-Address(O): ") + QString(result));
                    attrs.append(PROTOCOL_SEPARATOR);
                    break;
                }
            }
        }
        lua_pop(luaL, 1);
    }

    if (attribs.contains("currentState"))
    {
        QString currState;

        attrCounter++;
        attrs.append("currentState");
        attrs.append(PROTOCOL_SEPARATOR);
        attrs.append(QString::number(TObsText));
        attrs.append(PROTOCOL_SEPARATOR);

        currState = "Where?";
        if (whereCell)
        {
            ControlMode *cm = whereCell->getControlMode(this);
            if (cm)
            {
                currState = QString(cm->getControlModeName().c_str());
            }
            else
            {
                if (execModes != Quiet){
                    string err_out = string("Warning: Could not find the Automaton inside an Environment object.");
                    lua_getglobal(L, "customWarning");
                    lua_pushstring(L, err_out.c_str());
                    lua_pushnumber(L, 5);
                    lua_call(L, 2, 0);
                }
            }
        }

        attrs.append(currState);
        attrs.append(PROTOCOL_SEPARATOR);
    }

    msg.append(QString::number(attrCounter));
    msg.append(PROTOCOL_SEPARATOR);
    msg.append(QString::number(elementCounter));
    msg.append(PROTOCOL_SEPARATOR);
    msg.append(attrs);
    msg.append(PROTOCOL_SEPARATOR);
    msg.append(elements);
    msg.append(PROTOCOL_SEPARATOR);

    return msg;
}
Example #5
0
QString luaSociety::pop(lua_State *luaL, QStringList& attribs)
{
    QString msg;

    // id
    msg.append(QString::number(getId()));
    msg.append(PROTOCOL_SEPARATOR);

    // subjectType
    msg.append(QString::number(subjectType));
    msg.append(PROTOCOL_SEPARATOR);

    // recupero a referencia na pilha lua
    Reference<luaSociety>::getReference(luaL);

    int societyPos = lua_gettop(luaL);

    int attrCounter = 0;
    int elementCounter = 0;
    // bool contains = false;
    double num = 0;
    QString text, key, attrs, elements;

    lua_pushnil(luaL);
    while (lua_next(luaL, societyPos) != 0)
    {
        key = QString(luaL_checkstring(luaL, -2));

        if ((attribs.contains(key)) ||(key == "cells"))
        {
            attrCounter++;
            attrs.append(key);
            attrs.append(PROTOCOL_SEPARATOR);

            switch (lua_type(luaL, -1))
            {
            case LUA_TBOOLEAN:
                attrs.append(QString::number(TObsBool));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString::number(lua_toboolean(luaL, -1)));
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TNUMBER:
                num = luaL_checknumber(luaL, -1);
                doubleToQString(num, text, 20);
                attrs.append(QString::number(TObsNumber));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(text);
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TSTRING:
                text = QString(luaL_checkstring(luaL, -1));
                attrs.append(QString::number(TObsText));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append((text.isEmpty() || text.isNull() ? VALUE_NOT_INFORMED : text));
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TTABLE:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1));
                attrs.append(QString::number(TObsText));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(TB): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            case LUA_TUSERDATA	:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1));
                attrs.append(QString::number(TObsText));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(UD): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            case LUA_TFUNCTION:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1));
                attrs.append(QString::number(TObsText));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(FT): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            default:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1));
                attrs.append(QString::number(TObsText));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(O): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }
            }
        }
        lua_pop(luaL, 1);
    }

    // #attrs
    msg.append(QString::number(attrCounter));
    msg.append(PROTOCOL_SEPARATOR);

    // #elements
    msg.append(QString::number(elementCounter));
    msg.append(PROTOCOL_SEPARATOR);
    msg.append(attrs);

    msg.append(PROTOCOL_SEPARATOR);
    msg.append(elements);
    msg.append(PROTOCOL_SEPARATOR);

    return msg;
}
Example #6
0
QString luaEnvironment::pop(lua_State *luaL, QStringList& /*attribs*/)
{
    QString msg;

    // id
    msg.append(QString::number(getId()));
    msg.append(PROTOCOL_SEPARATOR);

    // subjectType
    msg.append(QString::number(subjectType));
    msg.append(PROTOCOL_SEPARATOR);

    // recupero a referencia na pilha lua
    // @DANIEL
    // lua_rawgeti(luaL, LUA_REGISTRYINDEX, ref);
    Reference<luaEnvironment>::getReference(luaL);

    int environPos = lua_gettop(luaL);

    int attrCounter = 0;
    int elementCounter = 0;
    // bool contains = false;
    double num = 0;
    QString text, key, attrs, elements;

    lua_pushnil(luaL);
    while(lua_next(luaL, environPos ) != 0)
    {
        if (lua_type(luaL, -2) == LUA_TSTRING) 
        { 
            key  = luaL_checkstring(luaL, -2); 
        } 
        else 
        { 
            if (lua_type(luaL, -2) == LUA_TNUMBER) 
            { 
                char aux[100]; 
                double number = luaL_checknumber(luaL, -2); 
                sprintf(aux, "%g", number); 
                key = aux; 
            } 
        }

        attrCounter++;
        attrs.append(key);
        attrs.append(PROTOCOL_SEPARATOR);

        switch( lua_type(luaL, -1) )
        {
            case LUA_TBOOLEAN:
                attrs.append(QString::number(TObsBool));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString::number( lua_toboolean(luaL, -1)));
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TNUMBER:
                num = luaL_checknumber(luaL, -1);
                doubleToQString(num, text, 20);
                attrs.append(QString::number(TObsNumber));
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(text);
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TSTRING:
                text = QString(luaL_checkstring(luaL, -1));
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append( (text.isEmpty() || text.isNull() ? VALUE_NOT_INFORMED : text) );
                attrs.append(PROTOCOL_SEPARATOR);
                break;

            case LUA_TTABLE:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(TB): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);

                break;
            }

            case LUA_TUSERDATA:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(UD):") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            case LUA_TFUNCTION:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(FT): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }

            default:
            {
                char result[100];
                sprintf(result, "%p", lua_topointer(luaL, -1) );
                attrs.append(QString::number(TObsText) );
                attrs.append(PROTOCOL_SEPARATOR);
                attrs.append(QString("Lua-Address(O): ") + QString(result));
                attrs.append(PROTOCOL_SEPARATOR);
                break;
            }
        }

        lua_pop(luaL, 1);
    }


    // #attrs
    msg.append(QString::number(attrCounter));
    msg.append(PROTOCOL_SEPARATOR );

    // #elements
    msg.append(QString::number(elementCounter));
    msg.append(PROTOCOL_SEPARATOR );
    msg.append(attrs);

    msg.append(PROTOCOL_SEPARATOR);
    msg.append(elements);
    msg.append(PROTOCOL_SEPARATOR);
    return msg;
}