• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Player deaths won't get added to database!

Status
Not open for further replies.

Kiman

Reality OT Staff
Joined
Aug 18, 2009
Messages
530
Reaction score
7
Location
Sweden
I have an issue, and I've tried most of the things to fix it. My player deaths aren't getting added to my MySQL database.

I use TFS 0.2.5

Playerdeath.lua

Code:
dofile("./config.lua")

function onDeath(cid, corpse, killer)
	doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You are dead.")
	if deathListEnabled == "yes" then
		if sqlType == "mysql" then
			env = assert(luasql.mysql())
			con = assert(env:connect(mysqlDatabase, mysqlUser, mysqlPass, mysqlHost, mysqlPort))
		else -- sqlite
			env = assert(luasql.sqlite3())
			con = assert(env:connect(sqliteDatabase))
		end
	if (isPlayer(killer) == TRUE) and (getPlayerLevel(cid) >= 400) then
		local item = doPlayerAddItem(killer, 2157, 1)
doSetItemSpecialDescription(item, "It's a trohpy you gained for going rampage, trade them in for nice items")
broadcastMessage(getCreatureName(killer).." ["..getPlayerLevel(killer).."] just pwned "..getCreatureName(cid).." ["..getPlayerLevel(cid).."]", MESSAGE_STATUS_WARNING)
end
		local byPlayer = FALSE
		if killer == FALSE then
			killerName = "field item"
		else
			if isPlayer(killer) == TRUE then
				byPlayer = TRUE
			end
			killerName = getCreatureName(killer)
		end
		assert(con:execute("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`) VALUES (" .. getPlayerGUID(cid) .. ", " .. os.time() .. ", " .. getPlayerLevel(cid) .. ", '" .. escapeString(killerName) .. "', " .. byPlayer .. ");"))
		local cursor = assert(con:execute("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. getPlayerGUID(cid) .. ";"))
		local deathRecords = numRows(cursor)
		if sqlType == "mysql" then
			while deathRecords > maxDeathRecords do
				delete = assert(con:execute("DELETE FROM `player_deaths` WHERE `player_id` = " .. getPlayerGUID(cid) .. " ORDER BY `time` LIMIT 1;"))
				deathRecords = deathRecords - 1
			end
		else
			while deathRecords > maxDeathRecords do
				delete = assert(con:execute("DELETE FROM `player_deaths` WHERE `rowid` = (SELECT `rowid` FROM `player_deaths` WHERE `player_id` = " .. getPlayerGUID(cid) .. " ORDER BY `time` LIMIT 1);"))
				deathRecords = deathRecords - 1
			end
		end			
		con:close()
		env:close()
	end
end

Login.lua
Code:
  -- ### CONFIG ###
-- message send to player by script "type" (types you can check in "global.lua")
SHOP_MSG_TYPE = 17
-- time (in seconds) between connections to SQL database by shop script
SQL_interval = 30
-- ### END OF CONFIG ###
SQL_COMUNICATION_INTERVAL = SQL_interval * 1000
local castle = castleHandler:new()
function onLogin(cid)
    castle:onLogin(cid)
    if(InitShopComunication == 0) then
        local eventServ = addEvent(sql_communication, SQL_COMUNICATION_INTERVAL, {})
        InitShopComunication = eventServ
    end
    registerCreatureEvent(cid, "PlayerDeath")
	registerCreatureEvent(cid, "onPrepareDeath")
    registerCreatureEvent(cid, "sqlRequest")
	registerCreatureEvent(cid, "onPrepareDeath")
	registerCreatureEvent(cid, "inquisitionPortals")
	return TRUE
end

function sql_communication(parameters)
    dofile("./config.lua")
    env = assert(luasql.mysql())
    con = assert(env:connect(mysqlDatabase, mysqlUser, mysqlPass, mysqlHost, mysqlPort))
    result_plr = assert(con:execute("SELECT * FROM z_ots_comunication WHERE `type` = 'login';"))
    todo = result_plr:fetch({}, "a")
    while todo do
        id = tonumber(todo.id)
        action = tostring(todo.action)
        delete = tonumber(todo.delete_it)
        cid = getPlayerByName(tostring(todo.name))
        if isPlayer(cid) == TRUE then
            local itemtogive_id = tonumber(todo.param1)
            local itemtogive_count = tonumber(todo.param2)
            local container_id = tonumber(todo.param3)
            local container_count = tonumber(todo.param4)
            local add_item_type = tostring(todo.param5)
            local add_item_name = tostring(todo.param6)
            local received_item = 0
            local full_weight = 0
            if add_item_type == 'container' then
                container_weight = getItemWeight(container_id, 1)
                items_weight = container_count * getItemWeight(itemtogive_id, itemtogive_count)
                full_weight = items_weight + container_weight
            else
                full_weight = getItemWeight(itemtogive_id, itemtogive_count)
            end
            local free_cap = getPlayerFreeCap(cid)
            if full_weight <= free_cap then
                if add_item_type == 'container' then
                    local new_container = doCreateItemEx(container_id, 1)
                    local iter = 0
                    while iter ~= container_count do
                        doAddContainerItem(new_container, itemtogive_id, itemtogive_count)
                        iter = iter + 1
                    end
                    received_item = doPlayerAddItemEx(cid, new_container)
                else
                    local new_item = doCreateItemEx(itemtogive_id, itemtogive_count)
                    received_item = doPlayerAddItemEx(cid, new_item)
                end
                if received_item == RETURNVALUE_NOERROR then
                    doPlayerSendTextMessage(cid, SHOP_MSG_TYPE, 'You received >> '.. add_item_name ..' << from OTS shop.')
                    delete = assert(con:execute("DELETE FROM `z_ots_comunication` WHERE `id` = " .. id .. ";"))
                    assert(con:execute("UPDATE `z_shop_history_item` SET `trans_state`='realized', `trans_real`=" .. os.time() .. " WHERE id = " .. id .. ";"))
                else
                    doPlayerSendTextMessage(cid, SHOP_MSG_TYPE, '>> '.. add_item_name ..' << from OTS shop is waiting for you. Please make place for this item in your backpack/hands and wait about '.. SQL_interval ..' seconds to get it.')
                end
            else
                doPlayerSendTextMessage(cid, SHOP_MSG_TYPE, '>> '.. add_item_name ..' << from OTS shop is waiting for you. It weight is '.. full_weight ..' oz., you have only '.. free_cap ..' oz. free capacity. Put some items in depot and wait about '.. SQL_interval ..' seconds to get it.')
            end
            end
        todo = result_plr:fetch (todo, "a")
    end
    con:close()
    env:close()
    local eventServ = addEvent(sql_communication, SQL_COMUNICATION_INTERVAL, parameters)
end

Creaturescripts.xml
Code:
	<event type="death" name="PlayerDeath" script="playerdeath.lua"/>

player_deaths DB structure
Code:
CREATE TABLE IF NOT EXISTS `player_deaths` (
  `player_id` int(11) NOT NULL,
  `time` bigint(20) unsigned NOT NULL DEFAULT '0',
  `level` int(11) NOT NULL DEFAULT '1',
  `killed_by` varchar(255) NOT NULL,
  `is_player` tinyint(1) NOT NULL DEFAULT '1',
  KEY `player_id` (`player_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Can you see anything wrong ?
 
Last edited:
Fixed it myself,

For people with the same problem
delete the Playerdeath line and add this somewhere in the login.lua

Code:
function onLogin(cid)
registerCreatureEvent(cid, "PlayerDeath")
return TRUE
end

@EDIT: I fixed it for a second, but then from nowhere the problem came back.
 
Last edited:
In login.lua you all ready have
registerCreatureEvent(cid, "PlayerDeath")
registerCreatureEvent(cid, "onPrepareDeath")
registerCreatureEvent(cid, "sqlRequest")
registerCreatureEvent(cid, "onPrepareDeath")
registerCreatureEvent(cid, "inquisitionPortals")
return TRUE
end
 
No need for help, finally fixed it. Something weird was going on in my Login.lua
Ty anyways though
CLOSED!
 
Status
Not open for further replies.
Back
Top