int32_t LuaScriptInterface::luaDoSaveHouse(lua_State* L)
{
//doSaveHouse({list})
IntegerVec list;
if(lua_istable(L, -1))
{
lua_pushnil(L);
while(lua_next(L, -2))
list.push_back(popNumber(L));
lua_pop(L, 2);
}
else
list.push_back(popNumber(L));
House* house;
std::vector<House*> houses;
for(IntegerVec::const_iterator it = list.begin(); it != list.end(); ++it)
{
if(!(house = Houses::getInstance()->getHouse(*it)))
{
std::stringstream s;
s << "House not found, ID: " << (*it);
errorEx(s.str());
lua_pushboolean(L, false);
return 1;
}
houses.push_back(house);
}
Database* db = Database::getInstance();
DBTransaction trans(db);
if(!trans.begin())
{
lua_pushboolean(L, false);
return 1;
}
for(std::vector<House*>::iterator it = houses.begin(); it != houses.end(); ++it)
{
if(!IOMapSerialize::getInstance()->saveHouse(db, *it))
{
std::stringstream s;
s << "Unable to save house information, ID: " << (*it)->getId();
errorEx(s.str());
}
if(!IOMapSerialize::getInstance()->saveHouseItems(db, *it))
{
std::stringstream s;
s << "Unable to save house items, ID: " << (*it)->getId();
errorEx(s.str());
}
}
lua_pushboolean(L, trans.commit());
return 1;
}