--[[ Some helper functions for building sql queries ]] local fn = nrequire("utility/fn.lua") local track = nrequire("core/inventory/sv_invtracker.lua") local log = nrequire("log.lua") local q = {} function q.serialize_player(ply) if not IsValid(ply) and ply.data and ply.data.inventories then return "" end local sdata = {} local invs = {} for k,v in pairs(ply.data.inventories) do local idata = string.format("%q",v:Serialize()) invs[k] = {v.Name,string.sub(idata,2,#idata-1)} --remove the otter quotes end sdata.inventories = invs sdata.skills = ply.data.skills sdata.quests = ply.data.quests sdata.prayers = ply.data.prayers sdata.credits = ply.data.credits local ret = util.TableToJSON(sdata) return ret end function q.deserialize_player(ply,str) log.debug("Deserializing player's data : " .. ply:Nick() .. "\n" .. str) track.ClearInventories(ply) ply.data = ply.data or {} ply.data.inventories = ply.data.inventories or {} local tbl = util.JSONToTable(str) if not tbl then log.error("Failed to deserialize player " .. ply:Nick() .. "\n" .. str) end local invs = tbl.inventories print("Inventories was", invs) PrintTable(invs) for k,v in pairs(invs) do print("Giveing inventory",v[1],v[2]) track.GiveInventoryWithData(ply,v[1],v[2]) print("Gave inventory ", v[1]) end ply.data.skills = tbl.skills or {} ply.data.quests = tbl.quests or {} ply.data.credits = tbl.credits or 100 print("After deserializeing player, their .data is",ply.data) PrintTable(ply.data) track.SendPlayerData(ply) end function q.s_fmt(fmt,...) local args = {...} fn.map(args,MySQLite.SQLStr) return string.format(fmt,unpack(args)) end return q