Revision 3678

Date:
2019-09-19 01:25:29
Author:
dsb
Revision Log:
- Prune leftover inscription and config{} fields in :cloneRulesOutgoing().
- :ruleChanged() needs to check inscription and config{} fields for
corresponding rule actions.
- Update working_rules{} after saving rules to reflect their now
effectively unchanged status.
- Make :revertRule() work with new rules (dunno if it can actually reach
that point in that case...).
- Places where the rule is changed in place need to call
:updateRevertButton() and :updateRules(true).
Files:

Legend:

 
Added
 
Removed
 
Modified
  • tome2/trunk/dialogs/AutomatizerRules.lua

     
    266 266
    267 267 function _M:cloneRulesOutgoing(rules)
    268 268 -- On the outgoing pass, populate both the numeric and by-name indexes
    269 -- and clean up the 'orig_name' field.
    269 -- and clean up the 'orig_name' field. We also clean up leftover
    270 -- inscription and config{} fields for rules that don't need them.
    270 271 local ret = {}
    271 272 for _, rule in ipairs(rules) do
    272 273 local rr = table.clone(rule, true, {orig_name=true})
     
    317 318 -- Slight Hack(TM): We know by construction that the 'orig_name' field
    318 319 -- is either absent (for new rules) or the name of an original rule.
    319 320 local orig_rule = Automatizer.atm_rules[rule.orig_name]
    321
    320 322 -- Hack(TM): Avoid false/nil comparison weirdness.
    321 if (rule.global and not orig_rule.global) or (not rule.global and orig_rule.global) then
    323 local function diff_bool(a, b)
    324 return (a and not b) or (b and not a)
    325 end
    326
    327 if diff_bool(rule.global, orig_rule.global) then return true end
    328 if rule.action ~= orig_rule.action then return true end
    329 if rule.action == 'inscribe' and rule.inscription ~= orig_rule.inscription then
    322 330 return true
    323 331 end
    324 if rule.action ~= orig_rule.action then return true end
    332 if rule.action == 'configure' then
    333 if not rule.config or not orig_rule.config then return true end
    334 for _, tag in ipairs(ObjectData.config_types) do
    335 if diff_bool(rule.config[tag], orig_rule.config[tag]) then
    336 return true
    337 end
    338 end
    339 end
    325 340 return not table.equal(rule.match, orig_rule.match, true)
    326 341 end
    327 342
     
    384 399 return
    385 400 end
    386 401
    387
    388 402 local ch = self:changeCount()
    389 403 if not ch then
    390 404 game:unregisterDialog(self)
     
    401 415 end
    402 416 Automatizer.atm_rules = self:cloneRulesOutgoing(self.working_rules)
    403 417 Automatizer:writeAutoRules()
    418 -- Slight Hack(TM): Now that the rules have been saved, none of
    419 -- them are new and all of them correspond to an original rule of
    420 -- the same name.
    421 for _, rule in ipairs(self.working_rules) do
    422 rule.orig_name = rule.name
    423 end
    424 self:updateRules()
    404 425 Dialog:simplePopup('Automatizer: Save', 'Saved '..n..' automatizer rule(s)')
    405 426 end
    406 427 end
     
    415 436 if not ok then
    416 437 local i = self.rule_idx_by_name[self.cur_rule.name]
    417 438 if i then
    418 -- By construction, we know at this point that the rule has an
    419 -- orig_name.
    420 local rr = table.clone(Automatizer.atm_rules[self.cur_rule.orig_name], true)
    421 rr.orig_name = rr.name
    422 self.working_rules[i] = rr
    423 self.cur_rule = rr
    439 if self.cur_rule.orig_name then
    440 local rr = table.clone(Automatizer.atm_rules[self.cur_rule.orig_name], true)
    441 rr.orig_name = rr.name
    442 self.working_rules[i] = rr
    443 self:setCurrentRule(rr)
    444 else
    445 table.remove(self.working_rules, i)
    446 self:setCurrentRule(nil)
    447 end
    424 448 -- This will rebuild the rule tree for us.
    425 449 self:updateRules()
    426 450 end
     
    456 480 for _, tag in ipairs(ObjectData.config_types) do
    457 481 self.cur_rule.config[tag] = self.config_cbs[tag].checked
    458 482 end
    483 self:updateRevertButton()
    484 self:updateRules(true)
    459 485 end
    460 486 end
    461 487
    462 488 function _M:updateInscription(text)
    463 489 if self.cur_rule and self.cur_rule.action == 'inscribe' then
    464 490 self.cur_rule.inscription = text
    491 self:updateRevertButton()
    492 self:updateRules(true)
    465 493 end
    466 494 end
    467 495
     
    473 501 self:populateCardsFromRule(self.cur_rule)
    474 502 end
    475 503 self:updateRevertButton()
    504 self:updateRules(true)
    476 505 end
    477 506
    478 507 function _M:setTestObject(o)
     
    571 600 self:setCurrentRule(nil)
    572 601 self:updateRules(true)
    573 602 self:rebuildRuleTree()
    603 break
    574 604 end
    575 605 end
    576 606 end