Esempio n. 1
0
int BroadcastClientRpc(lua_State* L)
{

    const char* pszEntity = luaL_checkstring(L, 1);
    const char* pszFunc = luaL_checkstring(L, 2);

    world* the_world = GetWorld();
    if (!the_world)
    {
        return 0;
    }

    const SEntityDef* pDef = the_world->GetDefParser().GetEntityDefByName(pszEntity);
    if (!pDef)
    {
        return 0;
    }

    map<string, _SEntityDefMethods*>::const_iterator iter11 = \
        pDef->m_clientMethods.find(pszFunc);
    if(iter11 == pDef->m_clientMethods.end())
    {
        return 0;
    }

    //把pszEntity和pszFunc从堆栈中移除掉
    lua_remove(L, 1);
    lua_remove(L, 1);


    const _SEntityDefMethods* pMethods = iter11->second;
    const list<VTYPE>& args = pMethods->m_argsType;
    int nArgCount = lua_gettop(L);
    if(nArgCount != (int)args.size())
    {
        return 0;
    }

    uint16_t nFuncId = (uint16_t)pDef->m_clientMethodsMap.GetIntByStr(pszFunc);

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

    (*u) << the_world->GetDefParser().GetTypeId(pszEntity) << nFuncId;

    //打包rpc的所有参数为一个string
    int idx = 0;
    list<VTYPE>::const_iterator iter = args.begin();
    for(; iter != args.end(); ++iter)
    {
        ++idx;
        VTYPE vt = *iter;
        u->FillPlutoFromLua(vt, L, idx);
    }

    (*u) << EndPluto;

    the_world->GetServer()->AddLocalRpcPluto(u);

    vector<CMailBox*>& mbs = the_world->GetServer()->GetAllServerMbs();
    vector<CMailBox*>::iterator iter2 = mbs.begin();
    for(; iter2 != mbs.end(); ++iter2)
    {
        CMailBox* basemb = *iter2;
        if(basemb && basemb->GetServerMbType() == SERVER_BASEAPP)
        {
            //发给每个其他baseapp的mb的pluto都是发给本地进程的那个pluto的拷贝
            CPluto* u2 = new CPluto(u->GetBuff(), u->GetLen());
            basemb->PushPluto(u2);
        }
    }

    return 0;
}
Esempio n. 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;
}
Esempio n. 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;
    }

}
Esempio n. 4
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;
    }

}
Esempio n. 5
0
    int CEntityBase::lTable2Insert(lua_State* L)
    {
        //param 1 is userdata

        uint32_t nCbId = (uint32_t)luaL_checkint(L, 2);	//callback id
        const char* pszTbl = luaL_checkstring(L, 3);	//table name
        luaL_checkany(L, 4);							//table

        if(!lua_istable(L, 4))
        {
            lua_pushstring(L, "tableInsert,param 4 need a table.");
            lua_error(L);
            return 0;
        }

        world* the_world = GetWorld();
        CMailBox* mb = the_world->GetServerMailbox(SERVER_DBMGR);
        if(mb == NULL)
        {
            return 0;
        }

        CDefParser& def = the_world->GetDefParser();
        const SEntityDef* pDef = def.GetEntityDefByName(pszTbl);
        if(pDef == NULL)
        {
            lua_pushfstring(L, "error entity name:%s", pszTbl);
            lua_error(L);
            return 0;
        }

        CPluto* u = new CPluto;
        u->Encode(MSGID_DBMGR_TABLE2_INSERT) << m_mymb.m_nServerMailboxId << m_id << nCbId << def.GetTypeId(pszTbl);

        lua_pushnil(L);
        while(lua_next(L, 4) != 0)
        {
            const char* pszKey = luaL_checkstring(L, -2);

            map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.find(pszKey);
            if(iter != pDef->m_properties.end())
            {
                _SEntityDefProperties* p = iter->second;
                if(p && p->m_bSaveDb)
                {
                    u->FillField<uint16_t>(pDef->m_propertiesMap.GetIntByStr(pszKey));
                    u->FillPlutoFromLua(p->m_nType, L, 6);
                    lua_pop(L, 1);
                    continue;
                }
            }

            delete u;
            lua_pushfstring(L, "%s hasn't field %s or field not need to save.", pszTbl, pszKey);
            lua_error(L);
            return 0;
        }

        u->endPluto();
        //print_hex_pluto(*u);

        mb->PushPluto(u);

        return 0;
    }