Skip to content

Commit

Permalink
New method House.save (#3386)
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Mar 30, 2021
1 parent 4669f13 commit e95f864
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/iomapserialize.cpp
Expand Up @@ -23,6 +23,8 @@
#include "game.h"
#include "bed.h"

#include <fmt/format.h>

extern Game g_game;

void IOMapSerialize::loadHouseItems(Map* map)
Expand Down Expand Up @@ -68,7 +70,6 @@ bool IOMapSerialize::saveHouseItems()
{
int64_t start = OTSYS_TIME();
Database& db = Database::getInstance();
std::ostringstream query;

//Start the transaction
DBTransaction transaction;
Expand All @@ -93,8 +94,7 @@ bool IOMapSerialize::saveHouseItems()
size_t attributesSize;
const char* attributes = stream.getStream(attributesSize);
if (attributesSize > 0) {
query << house->getId() << ',' << db.escapeBlob(attributes, attributesSize);
if (!stmt.addRow(query)) {
if (!stmt.addRow(fmt::format("{:d},{:s}", house->getId(), db.escapeBlob(attributes, attributesSize)))) {
return false;
}
stream.clear();
Expand Down Expand Up @@ -370,3 +370,44 @@ bool IOMapSerialize::saveHouseInfo()

return transaction.commit();
}

bool IOMapSerialize::saveHouse(House* house)
{
Database& db = Database::getInstance();

//Start the transaction
DBTransaction transaction;
if (!transaction.begin()) {
return false;
}

uint32_t houseId = house->getId();

//clear old tile data
if (!db.executeQuery(fmt::format("DELETE FROM `tile_store` WHERE `house_id` = {:d}", houseId))) {
return false;
}

DBInsert stmt("INSERT INTO `tile_store` (`house_id`, `data`) VALUES ");

PropWriteStream stream;
for (HouseTile* tile : house->getTiles()) {
saveTile(stream, tile);

size_t attributesSize;
const char* attributes = stream.getStream(attributesSize);
if (attributesSize > 0) {
if (!stmt.addRow(fmt::format("{:d},{:s}", houseId, db.escapeBlob(attributes, attributesSize)))) {
return false;
}
stream.clear();
}
}

if (!stmt.execute()) {
return false;
}

//End the transaction
return transaction.commit();
}
3 changes: 3 additions & 0 deletions src/iomapserialize.h
Expand Up @@ -22,6 +22,7 @@

#include "database.h"
#include "map.h"
#include "house.h"

class IOMapSerialize
{
Expand All @@ -31,6 +32,8 @@ class IOMapSerialize
static bool loadHouseInfo();
static bool saveHouseInfo();

static bool saveHouse(House* house);

private:
static void saveItem(PropWriteStream& stream, const Item* item);
static void saveTile(PropWriteStream& stream, const Tile* tile);
Expand Down
16 changes: 16 additions & 0 deletions src/luascript.cpp
Expand Up @@ -29,6 +29,7 @@
#include "protocolstatus.h"
#include "spells.h"
#include "iologindata.h"
#include "iomapserialize.h"
#include "configmanager.h"
#include "teleport.h"
#include "databasemanager.h"
Expand Down Expand Up @@ -2627,6 +2628,8 @@ void LuaScriptInterface::registerFunctions()

registerMethod("House", "kickPlayer", LuaScriptInterface::luaHouseKickPlayer);

registerMethod("House", "save", LuaScriptInterface::luaHouseSave);

// ItemType
registerClass("ItemType", "", LuaScriptInterface::luaItemTypeCreate);
registerMetaMethod("ItemType", "__eq", LuaScriptInterface::luaUserdataCompare);
Expand Down Expand Up @@ -11403,6 +11406,19 @@ int LuaScriptInterface::luaHouseKickPlayer(lua_State* L)
return 1;
}

int LuaScriptInterface::luaHouseSave(lua_State* L)
{
// house:save()
House* house = getUserdata<House>(L, 1);
if (!house) {
lua_pushnil(L);
return 1;
}

pushBoolean(L, IOMapSerialize::saveHouse(house));
return 1;
}

// ItemType
int LuaScriptInterface::luaItemTypeCreate(lua_State* L)
{
Expand Down
2 changes: 2 additions & 0 deletions src/luascript.h
Expand Up @@ -1141,6 +1141,8 @@ class LuaScriptInterface

static int luaHouseKickPlayer(lua_State* L);

static int luaHouseSave(lua_State* L);

// ItemType
static int luaItemTypeCreate(lua_State* L);

Expand Down

0 comments on commit e95f864

Please sign in to comment.