Exemple #1
0
int lIncrementalUpdateItems(lua_State* L)
{

    int n  = lua_gettop(L);
    if( n != 4 )
    {
        LogError("lIncrementalUpdateItems", "Parameters number from lua not enough");
        lua_pop(L, n);
        lua_pushnumber(L, -1);
        return 1;
    }
    if( LUA_TTABLE != lua_type(L, 1) && LUA_TSTRING != lua_type(L, 2) && LUA_TSTRING != lua_type(L, 3))
    {
        LogError("Parameters type lua error", "%d", -1);
        lua_pop(L, n);
        lua_pushnumber(L, -1);
        return 1;
    }

    luaL_checkany(L, 4);

    const char* tName = lua_tostring(L, 2);
    const string tblName(tName); //更新的表名字

    const SEntityDef* pDef = GetWorldbase().GetDefParser().GetEntityDefByName(tblName);
    if( !pDef )
    {
        LogError("world:init().error", "%s", "Not the Entitydef");

        lua_pushnumber(L, -1);
        return 1;
    }

    CLuaCallback& cb = GetWorld()->GetCallback();
    int32_t ref = (int32_t)cb.Ref(L);

    const char* oName = lua_tostring(L, 3);
    lua_pop(L, 2);

    const string optName(oName); //操作内型
    uint16_t nBaseappId = GetWorld()->GetMailboxId();

    CPluto *u = new CPluto();
    u->Encode(MSGID_DBMGR_INCREMENTAL_UPDATE_ITEMS);

    *u << tblName << optName << nBaseappId << ref << (uint16_t)0;

    int i = 1;
    while(true)
    {
        map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin();

        lua_pushnumber(L, i++);
        lua_gettable(L, -2);
        if( lua_type(L, -1) != LUA_TTABLE )
        {
            break;
        }

        //独立处理道具的id字段
        lua_pushstring(L, "id");
        lua_gettable(L, -2);
        if( lua_type(L, -1) == LUA_TNIL )
        {
            LogError("table Item data is not enough: ", "%s", "id");
            delete u;
            u = NULL;

            cb.Unref(L, ref);

            lua_pushnumber(L, -1);
            return 1;
        }

        u->FillPlutoFromLua(V_INT64, L, -1);
        lua_pop(L, 1);

        //处理def中存盘字段
        for(; iter != pDef->m_properties.end(); ++iter)
        {
            const _SEntityDefProperties* pProp = iter->second;
            if( IsBaseFlag( pProp->m_nFlags ) && pProp->m_bSaveDb )
            {
                //cout<<"attribution name = "<<pProp->m_name.c_str()<<endl;
                lua_pushstring(L, pProp->m_name.c_str());

                lua_gettable(L, -2);
                if( lua_type(L, -1) == LUA_TNIL )
                {
                    LogError("table Item data is not enough: ", "%s", pProp->m_name.c_str());
                    delete u;
                    u = NULL;

                    cb.Unref(L, ref);

                    lua_pushnumber(L, -1);
                    return 1;
                }
                u->FillPlutoFromLua(pProp->m_nType, L, -1);

                lua_pop(L, 1);
            }
        }
        lua_pop(L, 1);
    }

    (*u) << EndPluto;
    
    uint32_t pos = (MSGLEN_TEXT_POS + sizeof(uint16_t)*2 + tblName.size() + optName.size() + sizeof(nBaseappId) + sizeof(int32_t));
    uint16_t value = (u->GetLen() - pos - sizeof(uint16_t));

    u->ReplaceField(pos, value);
    //PrintHexPluto(*u);

    CMailBox* mb = GetWorldbase().GetServerMailbox(SERVER_DBMGR);
    if(mb)
    {
        mb->PushPluto(u);

        lua_pushnumber(L, 0);
        return 1;
    }
    else
    {
        delete u;
        u = NULL;

        cb.Unref(L, ref);

        lua_pushnumber(L, -1);
        return 1;
    }

}
Exemple #2
0
//mogo.UpdateBatchToDb(arrayList, table, uniq, cbF)
int lUpdateBatchToDb(lua_State* L)
{
	int n  = lua_gettop(L);
	if( n != 4 && n != 3)
	{
		LogError("lUpdateBatchToDb", "Parameters number from lua not enough");
		lua_pop(L, n);
		lua_pushnumber(L, 1);
		return 1;
	}
	if( LUA_TTABLE != lua_type(L, 1) || LUA_TSTRING != lua_type(L, 2) || LUA_TSTRING != lua_type(L, 3) )
	{
		LogError("Parameters type lua error", "%d", -1);
		lua_pop(L, n);
		lua_pushnumber(L, 2);
		//lua_pop(L, 2);
		return 1;
	}
	CLuaCallback& cb = GetWorld()->GetCallback();
	int32_t ref = LUA_REFNIL;
	if(n == 4)
	{
		luaL_checkany(L, 4);
		ref = (int32_t)cb.Ref(L);
	}

	//cout<<lua_gettop(L)<<endl;

	const char* pszUniqKey = lua_tostring(L, 3);
	lua_pop(L, 1);
	const string uniqKey(pszUniqKey);

	const char* item = lua_tostring(L, 2);
	lua_pop(L, 1);
	const string itemName(item);
	uint16_t nBaseappId = GetWorld()->GetMailboxId();
	//map<string, map<string, VOBJECT> > items;
	CPluto *u = new CPluto();
	u->Encode(MSGID_DBMGR_UPDATE_BATCH);
	//ostringstream oss;
	*u << itemName << uniqKey << nBaseappId << ref << (uint16_t)0;
	//char str[10240];
	//const string itemName= "Item";
	const SEntityDef* pDef = GetWorldbase().GetDefParser().GetEntityDefByName(itemName);
	if( !pDef )
	{
		LogError("lUpdateBatchToDb", "Not the Entitydef");
		lua_pushnumber(L, 3);
		delete u;
		u = NULL;
		if(LUA_REFNIL != ref)
			cb.Unref(L, ref);

		return 1;
	}

	int i = 1;
	while(true)
	{
		map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin();

		//CPluto *u = new CPluto();
		//cout<<u->GetLen()<<endl;
		//LogDebug("parse table list start", "%d", 1);
		//oss <<"[";
		lua_pushnumber(L, i++);
		lua_gettable(L, -2);
		if( lua_type(L, -1) != LUA_TTABLE )
		{
			break;
		}
		for(; iter != pDef->m_properties.end(); ++iter)
		{

			const _SEntityDefProperties* pProp = iter->second;
			//VOBJECT *v = new VOBJECT();

			if(IsBaseFlag(pProp->m_nFlags))
			{
				//cout<<"attribution name = "<<pProp->m_name.c_str()<<endl;
				lua_pushstring(L, pProp->m_name.c_str());

				lua_gettable(L, -2);
				if( lua_type(L, -1) == LUA_TNIL )
				{
					LogError("lUpdateBatchToDb", "table Item[%s] data is nil. ", pProp->m_name.c_str());
					delete u;
					u = NULL;

					if(LUA_REFNIL != ref)
						cb.Unref(L, ref);

					lua_pushnumber(L, -1);
					return 1;
				}
				u->FillPlutoFromLua(pProp->m_nType, L, -1);

				lua_pop(L, 1);
			}
		}
		lua_pop(L, 1);
		//LogDebug("parse table list end", "%d", 1);

	}

	//printf("%d", u->GetLen());

	(*u) << EndPluto;
	// PrintHexPluto(*u);
	//printf("%d, %d", u->GetLen(), itemName.size());
	//*u << itemName << uniqKey << nBaseappId << ref << (uint16_t)0;
	uint32_t pos = (MSGLEN_TEXT_POS + sizeof(uint16_t) + itemName.size() + sizeof(uint16_t) + uniqKey.size() + sizeof(nBaseappId) + sizeof(int32_t));
	uint16_t value = (u->GetLen() - pos - sizeof(uint16_t));
	//printf("%d %d", pos, value);
	u->ReplaceField(pos, value);
	//PrintHexPluto(*u);

	CMailBox* mb = GetWorldbase().GetServerMailbox(SERVER_DBMGR);
	if(mb)
	{
		//LogInfo("upate items data to db ", "msg_id = %d, itemName = %s, status = %d", MSGID_DBMGR_UPDATE_BATCH, itemName.c_str(), 1);
		mb->PushPluto(u);
		//以下两行为测试数据用
	}
    else
    {
		if(LUA_REFNIL != ref)
			cb.Unref(L, ref);

        delete u;
        u = NULL;
    }
	return 0;
}
Exemple #3
0
int lWriteArrayToDb(lua_State* L)
{

    int n  = lua_gettop(L);
    if( n != 4 )
    {
        LogError("lWriteArrayToDb", "Parameters number from lua not enough");
        lua_pop(L, n);
        lua_pushnumber(L, -1);
        return 1;
    }
    if( LUA_TTABLE != lua_type(L, 1) && LUA_TSTRING != lua_type(L, 2) && LUA_TNUMBER != lua_type(L, 3) )
    {
        LogError("Parameters type lua error", "%d", -1);
        lua_pop(L, n);
        lua_pushnumber(L, -1);
        return 1;
    }

    luaL_checkany(L, 4);

    const char* item = lua_tostring(L, 2);
    const string itemName(item);


    const SEntityDef* pDef = GetWorldbase().GetDefParser().GetEntityDefByName(itemName);
    if( !pDef )
    {
        LogError("world:init().error", "%s", "Not the Entitydef");

        lua_pushnumber(L, -1);
        return 1;
    }

    CLuaCallback& cb = GetWorld()->GetCallback();
    int32_t ref = (int32_t)cb.Ref(L);
    TDBID dbid = lua_tonumber(L, 3);

    lua_pop(L, 2);

    uint16_t nBaseappId = GetWorld()->GetMailboxId();
    CPluto *u = new CPluto();
    u->Encode(MSGID_DBMGR_UPDATE_ITEMS);

    *u << itemName << dbid << nBaseappId << ref << (uint16_t)0;


    int i = 1;
    while(true)
    {
        map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin();

        //CPluto *u = new CPluto();
        //cout<<u->GetLen()<<endl;
        //LogDebug("parse table list start", "%d", 1);
        //oss <<"[";
        lua_pushnumber(L, i++);
        lua_gettable(L, -2);
        if( lua_type(L, -1) != LUA_TTABLE )
        {
            break;
        }
        for(; iter != pDef->m_properties.end(); ++iter)
        {

            const _SEntityDefProperties* pProp = iter->second;
            //VOBJECT *v = new VOBJECT();

            if(IsBaseFlag(pProp->m_nFlags) && pProp->m_bSaveDb )
            {
                //cout<<"attribution name = "<<pProp->m_name.c_str()<<endl;
                lua_pushstring(L, pProp->m_name.c_str());

                lua_gettable(L, -2);
                if( lua_type(L, -1) == 0 )
                {
                    LogError("table Item data is not enough", "%s", pProp->m_name.c_str());
                    delete u;
                    u = NULL;

                    cb.Unref(L, ref);

                    lua_pushnumber(L, -1);
                    return 1;
                }
                u->FillPlutoFromLua(pProp->m_nType, L, -1);

                lua_pop(L, 1);
            }
        }
        lua_pop(L, 1);
        //LogDebug("parse table list end", "%d", 1);
    }

    //printf("%d", u->GetLen());

    (*u) << EndPluto;
    // PrintHexPluto(*u);
    //printf("%d, %d", u->GetLen(), itemName.size());
    uint32_t pos = (MSGLEN_TEXT_POS + sizeof(uint16_t) + itemName.size() + 
        sizeof(dbid) + sizeof(nBaseappId) + sizeof(int32_t));
    uint16_t value = (u->GetLen() - pos - sizeof(uint16_t));
    //printf("%d %d", pos, value);
    u->ReplaceField(pos, value);
    //PrintHexPluto(*u);

    CMailBox* mb = GetWorldbase().GetServerMailbox(SERVER_DBMGR);
    if(mb)
    {
        //LogInfo("lWriteArrayToDb", "msg_id = %d, itemName = %s, status = %d", MSGID_DBMGR_UPDATE_ITEMS, itemName.c_str(), 1);
        mb->PushPluto(u);

        lua_pushnumber(L, 0);
        return 1;
    }
    else
    {
        delete u;
        u = NULL;

        cb.Unref(L, ref);

        lua_pushnumber(L, -1);
        return 1;
    }

}