将当前播放时间添加到总播放时间中。

我有一个数据库,里面有以秒为单位的总游戏时间。我想从数据库中获取这些秒数,加上当前会话播放时间(以秒为单位),然后更新数据库。这应该每5秒发生一次。我已经做到了,但因为我执行了currentSession + totalTimePlayedDB,它会一遍又一遍地加上我的整个当前会话的持续时间...有什么想法吗?

local currentPlayTime = player:TimeConnected()
print(math.Round(currentPlayTime))
local playerValues = MySQLite.queryValue([[SELECT time FROM chiz_time WHERE sid=']].. player:SteamID() ..[[']], function(time)
    if time == "" then
        time = math.Round(currentPlayTime)

        else
            time = math.Round(time + time - currentPlayTime )
    end

    MySQLite.query([[UPDATE chiz_time SET time = ']].. time ..[[' WHERE sid=']].. player:SteamID() ..[[']])
end)
点赞
用户501459
用户501459

我使用 currentSessiontotalTimePlayedDB 做计算,它不断添加我当前播放的全部时间。

你只需要计算出距离上一次保存时间的差值。

在你的某处初始化代码中:

lastSaveTime = 0

在你的保存例程中:

totalTimePlayedDB = totalTimePlayedDB + currentSession - lastSaveTime
如果(totalTimePlayedDB成功写入数据库) 那么
   lastSaveTime = currentSession
end
2014-08-18 18:26:02