Skip to content

Commit

Permalink
merge: Merge pull request #5 from lantisnt/retail
Browse files Browse the repository at this point in the history
Cross server sync support
  • Loading branch information
SamMousa authored Mar 5, 2023
2 parents e482b8e + f7db118 commit e97f5c6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
libs/
node_modules/
package-lock.json
26 changes: 19 additions & 7 deletions source/LogEntry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local privateCreator = '_a'
local privateCounter = '_b'
local privateTimestamp = '_c'
local privateClass = '_d'
local privateRealm = '_e'



Expand All @@ -36,6 +37,7 @@ LogEntry[privateStaticClass] = 'LE'

local counter = 0
local lastTimestamp = 0
local _, myRealm = Util.getIntegerGuid("player")

-- private constructor
local function constructor(self)
Expand Down Expand Up @@ -72,7 +74,7 @@ function LogEntry.staticClassName(metatable)
end


function LogEntry:new(creator)
function LogEntry:new(creator, realm)
local o = constructor(self)
o[privateClass] = LogEntry.staticClassName(self)

Expand All @@ -85,14 +87,19 @@ function LogEntry:new(creator)
end
o[privateCounter] = counter
if creator == nil then
o[privateCreator] = Util.getIntegerGuid("player")
o[privateCreator], o[privateRealm] = Util.getIntegerGuid("player")
elseif type(creator) == 'string' then
o[privateCreator] = Util.getIntegerGuid(creator)
if (o[privateCreator] == nil) then
o[privateCreator], o[privateRealm] = Util.getIntegerGuid(creator)
if (o[privateCreator] == nil) or (o[privateRealm] == nil) then
error(string.format("Failed to convert string `%s` into number", creator))
end
else
o[privateCreator] = creator
o[privateCreator], o[privateRealm] = creator, realm
if type(o[privateCreator]) ~= 'number' or type(o[privateRealm]) ~= 'number'then
error(string.format("Failed to fill data `creator [%s] -> %s | realm [%s] -> %s` ",
tostring(creator), tostring(o[privateCreator]),
tostring(realm), tostring(o[privateRealm])))
end
end

return o
Expand Down Expand Up @@ -122,13 +129,18 @@ end
Returns the numbers to be used
]]--
function LogEntry:numbersForHash()
return {self[privateTimestamp], self[privateCounter], self[privateCreator]}
return {self[privateTimestamp], self[privateCounter], self[privateRealm] or myRealm, self[privateCreator]}
end

function LogEntry:creator()
return self[privateCreator]
end

function LogEntry:realm()
return self[privateRealm] or myRealm
end


function LogEntry:counter()
return self[privateCounter]
end
Expand All @@ -148,7 +160,7 @@ function LogEntry.sortedList(data)
end

function LogEntry.fields()
return {privateTimestamp, privateCounter, privateCreator}
return {privateTimestamp, privateCounter, privateRealm, privateCreator}
end

function LogEntry:uuid()
Expand Down
9 changes: 5 additions & 4 deletions source/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,14 @@ function Util.getIntegerGuid(target)
end

function Util.getIntegerFromGuid(guid)
return tonumber(string.sub(guid, -8), 16)
local _, realm, int = strsplit("-", guid)
return tonumber(int, 16), tonumber(realm, 10)
end

local GUIDPrefix = string.sub(UnitGUID("player"), 1, -9)
local _, myRealm = Util.getIntegerGuid("player")

function Util.getGuidFromInteger(int)
return GUIDPrefix .. string.format("%08X", int)
function Util.getGuidFromInteger(int, realm)
return string.format("Player-%d-%08X", realm or myRealm, int)
end

function Util.guid()
Expand Down
2 changes: 1 addition & 1 deletion tests/LogEntryTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local sortedList = LogEntry.sortedList(data)

local entries = {}
for i = 1, 100 do
local entry = LogEntry:new(15)
local entry = LogEntry:new(15, 123)
entries[#entries + 1] = entry
sortedList:uniqueInsert(entry)
-- try duplicate insert
Expand Down
4 changes: 2 additions & 2 deletions tests/UtilTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ beginTests()
local Util = LibStub("EventSourcing/Util")


local int = Util.getIntegerGuid("player")
local guid = Util.getGuidFromInteger(int)
local int, realm = Util.getIntegerGuid("player")
local guid = Util.getGuidFromInteger(int, realm)

assertSame(guid, UnitGUID("player"))

Expand Down
23 changes: 19 additions & 4 deletions tests/_wowstubs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ end

function UnitGUID(target)
if target == "player" then
return "00000000-0000-BOB-0000-0ABCDABCD"
return "Player-1234-ABCDABCD"
elseif target == "Bob" then
return "00000000-0000-BOB-0000-0ABCDABCD"
return "Player-111-0ABCDABC"
elseif target == "Joe" then
return "00000000-0000-JOE-0000-0ABCDABCE"
return "Player-012-ABCDABCE"
else
error(string.format("Unknown target '%s'", target))
end

end

function GetPlayerInfoByGUID(guid)
if "00000000-0000-BOB-0000-0000000000" == guid then
if "Player-111-0ABCDABC" == guid then
return nil, nil, nil, nil, nil, "Bob", nil
else
error(string.format("Unknown GUID: %s", guid))
Expand All @@ -74,3 +74,18 @@ end
date = os.date

WOW_STUB = true

strsplit = function(delimiter, str, pieces)
pieces = tonumber(pieces) or math.huge
if delimiter == nil then
delimiter = "%s"
end
local t = {}
for token in string.gmatch(str, "([^"..delimiter.."]+)") do
t[#t+1] = token
if #t >= pieces then
break
end
end
return unpack(t)
end

0 comments on commit e97f5c6

Please sign in to comment.