Fix loading of history on connect.

The for loop that used to do it loaded the wrong messages.
New method calculates correctly and also doesn't spend time loading from database if we're going to throw it away.
This commit is contained in:
Leshana
2020-05-26 15:50:50 -04:00
parent ddd2fc53bd
commit 3c31b90bff
2 changed files with 10 additions and 5 deletions

View File

@@ -138,8 +138,9 @@ GLOBAL_DATUM_INIT(iconCache, /savefile, new("data/iconCache.sav")) //Cache of ic
//Perform DB shenanigans
/datum/chatOutput/proc/load_database()
set waitfor = FALSE
var/list/results = vchat_get_messages(owner.ckey) //If there's bad performance on reconnects, look no further
for(var/i in max(1, results.len - message_buffer)) // Only send them the number of buffered messages, instead of the ENTIRE log
// Only send them the number of buffered messages, instead of the ENTIRE log
var/list/results = vchat_get_messages(owner.ckey, message_buffer) //If there's bad performance on reconnects, look no further
for(var/i in results.len to 1 step -1)
var/list/message = results[i]
var/count = 10
to_chat_immediate(owner, message["time"], message["message"])

View File

@@ -108,12 +108,16 @@ GLOBAL_DATUM(vchatdb, /database)
return vchat_exec_update(messagedef)
//Get a player's message history
/proc/vchat_get_messages(var/ckey, var/oldest = 0)
//Get a player's message history. If limit is supplied, messages will be in reverse order.
/proc/vchat_get_messages(var/ckey, var/limit)
if(!ckey)
return
var/list/getdef = list("SELECT * FROM messages WHERE ckey = ? AND worldtime >= ?", ckey, oldest)
var/list/getdef
if (limit)
getdef = list("SELECT * FROM messages WHERE ckey = ? ORDER BY id DESC LIMIT [text2num(limit)]", ckey)
else
getdef = list("SELECT * FROM messages WHERE ckey = ?", ckey)
return vchat_exec_query(getdef)