Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local vars = {
- ["Auto start"] = true,
- ["Auto sync"] = false,
- }
- local keys = {
- ["Sync bots now"] = 1,
- ["Toggle auto sync"] = 2,
- }
- local _print = print
- function Log (...) _print(... or "") end
- function Debug (...) ModDebug.Log(... or "") end
- function print (...) ModDebug.Log(... or "") end
- function Var (name) return ModBase.GetExposedVariable(name) end
- table.insert_once = function (table_name, element)
- local found = true
- for k,v in pairs(table_name) do
- if (v == element) then
- found = false
- break
- end
- end
- if (found) then
- table.insert(table_name, element)
- return true
- end
- return false
- end
- --------------
- function Expose ()
- ModDebug.ClearLog()
- print(">>> Expose")
- for k, v in pairs(vars) do
- ModBase.ExposeVariable(k, v, ExposedVariableCallback)
- end
- for k, v in pairs(keys) do
- ModBase.ExposeKeybinding(k, v, OnKeyTrigger)
- end
- print()
- end
- function ExposedVariableCallback (value, name)
- print(string.format("Exposed variable \"%s\" changed to: %s", name, tostring(value)))
- end
- --------------
- function BeforeLoad ()
- print(">>> Loading...")
- print("(Dev mode): ", ModDebug.IsDevMode())
- print()
- end
- function AfterLoad ()
- Log("SyncBots: Loaded")
- print(">>> Loaded")
- for k, v in pairs(vars) do
- print(string.format("%s = %s", k, tostring(Var(k))))
- end
- GetGroups()
- end
- --------------
- local timer = -5 --- timer start
- local timer_threshold = 5
- local timer_debug = 0
- local auto_sync_ispause = false
- local sync_isrunning = false
- function OnKeyTrigger (name)
- print("\n", string.format("<<< \"%s\" Key triggered]", name))
- if name == "Sync bots now" then
- if Var("Auto sync") and not sync_isrunning then
- auto_sync_ispause = false
- end
- timer = 0
- SyncBots_Before()
- elseif name == "Toggle auto sync" then
- auto_sync_ispause = not auto_sync_ispause
- print(string.format("(auto_sync_ispause = %s)", tostring(auto_sync_ispause)))
- end
- end
- function OnUpdate (DeltaTime)
- timer_debug = timer_debug + DeltaTime
- if timer_debug >= 5 then
- timer_debug = 0
- print(string.format("(sync_isrunning = %s, auto_sync_ispause = %s)",
- tostring(sync_isrunning), tostring(auto_sync_ispause)))
- end
- if not Var("Auto sync") or sync_isrunning or auto_sync_ispause then
- return
- end
- timer = timer + DeltaTime
- if timer >= timer_threshold then
- timer = 0
- print("\n", "<<< [Auto sync]")
- SyncBots_Before()
- end
- end
- --------------
- function SyncBots_Before ()
- -- print("[SyncBots_Before]")
- print("sync_isrunning = ", sync_isrunning)
- -- if sync_isrunning then
- -- return
- -- end
- --- Check duplicates
- local groups, duplicates = GetGroups()
- --- Prompt confirm popup, if duplicates exist
- if (#duplicates > 0) then
- PromptDuplicatesWarning(groups, duplicates)
- else
- SyncBots(groups)
- end
- end
- function GetGroups ()
- local groups = {} --- (unique) groups name
- local duplicates = {} --- (unique) groups that use the same name
- ------print()
- for i, group_name in ipairs(ModBot.GetBotGroupNames()) do
- local log = string.format("group (%s) : %s", i, group_name)
- --- if not inserted, another group has already this name
- local inserted = table.insert_once(groups, group_name)
- if not inserted then
- log = log .. " (DUPLICATE)"
- table.insert_once(duplicates, group_name)
- end
- ------print(log)
- end
- --- Debug: Log groups & duplicate names
- ------print("\n", #groups, " groups:")
- ------print("\t", table.concat(groups, "\n\t") )
- if (#duplicates > 0) then
- ------print("\n", #duplicates, " duplicate groups:")
- ------print("\t", table.concat(duplicates, "\n\t") )
- end
- ------print()
- return groups, duplicates
- end
- function PromptDuplicatesWarning (groups, duplicates)
- print("\n", "[Prompt warning]")
- -- if Var("Auto sync") then
- -- print("[Prompt note]")
- -- ModUI.ShowPopup("Auto sync has been paused",
- -- "Press [Sync bot] to restart")
- -- end
- ModUI.ShowPopupConfirm("WARNING: Duplicate group names!\n".."Sync these bots anyway ?",
- "There are at least 2 groups with the same name, all their bots will be sync:\n" ..
- "\"" .. table.concat(duplicates, "\", \"") .. "\"",
- --- YES : Sync anyway (all duplicate groups will be merged)
- function ()
- print("\n", "YES ! Sync them all")
- auto_sync_ispause = false -- overwrite "cancel" response
- SyncBots(groups)
- end,
- --- NO : Ignore all bots in duplicate groups
- function ()
- print("\n", "NO ! Ignore duplicates")
- auto_sync_ispause = false
- SyncBots(groups, duplicates)
- end)
- --- "Cancel"
- --- Note : Will not wait confirmation
- -- User must respond under {timer_threshold} sec before the next auto sync
- print("after popup ?")
- auto_sync_ispause = true
- sync_isrunning = false
- -- timer_threshold = 10
- end
- function SyncBots (groups, duplicates)
- if not duplicates then duplicates = {} end
- print("[SyncBots]")
- print("auto_sync_ispause = ", auto_sync_ispause)
- sync_isrunning = true
- --- Find all bots with the same name to sync in each groups
- for i, group_name in ipairs(groups) do
- --- ... unless the group's in duplicates list
- local is_duplicate = false
- for j, duplicate_name in ipairs(duplicates) do
- if group_name == duplicate_name then
- is_duplicate = true
- break
- end
- end
- if is_duplicate then -- debug
- print("\n", group_name, "\t(DUPLICATE)")
- else
- print("\n", group_name)
- local bots_master = {}
- local bots_id = ModBot.GetAllBotUIDsInGroup(group_name)
- local original_count = #bots_id
- for j, bot_id in ipairs(bots_id) do
- local bot_name = ModBot.GetName(bot_id)
- local log = string.format("(%s)\t%s\t| %s", bot_id, bot_name, ModBot.GetState(bot_id))
- if j > original_count then
- print("\t", "—————")
- end
- --- Ignore: Not name not allowed
- if (bot_name == "---" or bot_name:match("^Mk%d+$")) then
- -- print("\t[IGNORED NAME] !\t", log)
- goto continue
- end
- --- Ignore: Bot's currently learning
- if ModBot.GetIsLearning(bot_id) then
- -- print("\t[LEARNING] !\t", log)
- goto continue
- end
- --- Get bot's script
- if not bots_master[bot_name] then
- --- Ignore: Pushed bot has no master → there is no script for it
- if j > original_count then
- -- print("\t[NO SCRIPT]!\t", log)
- goto continue
- end
- local script = ModBot.GetScriptSavegameFormat(bot_id)
- --- Ignore: Bot's script is empty ; push to the end of the list for re-check it after
- if script == "[]" then
- -- print("\t[NO SCRIPT]!\t", log, " >>> push back")
- table.insert(bots_id, bot_id)
- goto continue
- end
- bots_master[bot_name] = script
- -- print("\t[GetScript]<\t", log)
- --- Set bot's script
- else
- --- Ignored: Bot's currently running a script
- if ModBot.GetIsRunningScript(bot_id) then
- print("\t[RUNNING] !\t", log)
- goto continue
- end
- --- [Debug only] Ignored: Bot's currenly moving -- busy (Moving, Shovel, etc.)
- --- Note : SetScript or StopScript don't work if bot's busy
- if ModBot.GetState(bot_id) ~= "None" then
- print("\t[BUSY] :\t", log)
- goto continue
- end
- --- Set bot's script
- --- Note : Won't sync if the bot is moving
- print("\t[SetScript]>\t", log)
- ModBot.SetScriptSavegameFormat(bot_id, bots_master[bot_name])
- end
- --- Start it, if allowed
- if Var("Auto start") then
- ModBot.StartScript(bot_id)
- end
- ::continue::
- end
- end
- end
- sync_isrunning = false
- print()
- end
Add Comment
Please, Sign In to add comment