comparison Comm.lua @ 96:082ff877c443

Small refactor
author John@Doomsday
date Mon, 23 Apr 2012 09:11:21 -0400
parents 2058d86778b4
children 19fd02bff870
comparison
equal deleted inserted replaced
95:5df2c9cdb8c8 96:082ff877c443
81 end, 81 end,
82 82
83 ["TS"] = function(self,packet,sender,isloop) 83 ["TS"] = function(self,packet,sender,isloop)
84 if isloop then return end 84 if isloop then return end
85 if masterLooterIsMe and admin then 85 if masterLooterIsMe and admin then
86 -- only non-admins will send this message, send them the present 86 -- only non-admins will send this message
87 -- working state
88 87
89 local t = packet 88 local t = packet
90 local remoteBase = table.remove(t,1) 89 local remoteBase = table.remove(t,1)
91 90
92 -- if their base is older than ours, this is easy - send them our 91 -- if their base is older than ours, this is easy - send them our
98 -- admin rebased and we aren't aware of that. but the other admin 97 -- admin rebased and we aren't aware of that. but the other admin
99 -- wouldn't have done that if they didn't believe all the admins 98 -- wouldn't have done that if they didn't believe all the admins
100 -- were synced up to the release point. So do we trust that and use 99 -- were synced up to the release point. So do we trust that and use
101 -- this as a rebase opportunity? print an error? just reply that we 100 -- this as a rebase opportunity? print an error? just reply that we
102 -- don't have anything new to share with them? only send them 101 -- don't have anything new to share with them? only send them
103 --
104 102
105 if remoteBase == db.profile.time then 103 local success, o = CreateChangeDiff(remoteBase,t)
106 local j = 1 -- index in foreign list 104 if success and getn(o) > 0 then
107 local n = getn(t) 105 Send("CU",o)
108 local o = {} 106 end
109 for i,v in pairs(db.profile.changes) do -- for each timestamp in our list 107 if not success then -- push
110 if t and t[j] < v.time then
111 table.insert(o,v)
112 end
113 while j<n and t[j] <= v.time do j = j+1 end -- advance the foreign pointer past our current entry
114 end -- j>=n ? add because the remote hit end of road. lt? add because it's a missing stamp
115 print("Received request at timebase",remoteBase,"and returning:")
116 PrintTable(o)
117 if getn(o) > 0 then
118 Send("CU",o) -- todo: send privately to the requster
119 end
120 else
121 print("Received request at differing timebase",remoteBase,db.profile.time," ... pushing") 108 print("Received request at differing timebase",remoteBase,db.profile.time," ... pushing")
122 self:Push() -- todo: send privately to requester 109 self:Push() -- todo: send privately to requester
123 end 110 end
124 end 111 end
125 end, 112 end,
126 113
127 ["CU"] = function(self,packet,sender,isloop) -- blindly trust an admin loot master 114 ["CU"] = function(self,packet,sender,isloop) -- blindly trust an admin loot master
128 if isloop then return end 115 if isloop then return end
129 116 IntegrateChangeDiff(packet)
130 local c = packet
131 local old = db.profile.changes
132
133 local new = {}
134
135 local op = 1
136 local cp = 1
137
138 local no = getn(old)
139 local nc = getn(c)
140
141 if no == 0 then
142 db.profile.changes = c
143 else
144 while op <= no or cp <= nc do -- lists are pre-sorted. insertion merge them
145 if cp > nc then -- inelegant
146 table.insert(new,old[op])
147 op = op + 1
148 elseif op > no then
149 table.insert(new,c[cp])
150 cp = cp + 1
151 elseif c[cp].time < old[op].time then
152 table.insert(new,c[cp])
153 cp = cp + 1
154 elseif c[cp].time > old[op].time then
155 table.insert(new,old[op])
156 op = op + 1
157 else
158 error("Bad update received from ",sender)
159 end
160 end
161 print("Updating changes - ",getn(new), "entries")
162 db.profile.changes = new
163 end
164
165 CreateWorkingStateFromChanges(db.profile.changes)
166 if changeListener then
167 changeListener:DataEvent()
168 end
169 end, 117 end,
170 118
171 ["RequestCatchup"] = function(self) 119 ["RequestCatchup"] = function(self)
172 if not admin then 120 if not admin then
173 --local string = _g.tostring(timestamp) 121 --local string = _g.tostring(timestamp)