diff --git a/main.go b/main.go index a220bc2..27a9ca3 100644 --- a/main.go +++ b/main.go @@ -71,80 +71,11 @@ func main() { luaStates := loadLuaStates(matches) achievements := loadAchievements(luaStates) wg := sync.WaitGroup{} - wg.Add(1) + // wg.Add(1) // We can save the achievements to the database while doing something else unrelated - go saveAchievementsToDB(&db, achievements) + // go saveAchievementsToDB(&db, achievements) saveAchievementsToSourceFiles(luaStates, achievements) wg.Wait() - - // --- Pass 1: Extract all data --- - // logger.Info("Starting Pass 1: Extracting data from all Heimdall.lua files...") - // var wgPass1 sync.WaitGroup - // for _, match := range matches { - // wgPass1.Add(1) - // go loadAchievements(filepath.Join(cleanedRoot, match), &wgPass1) - // } - // wgPass1.Wait() - // logger.Info("Finished Pass 1: Loaded %d unique players from %d files.", len(allPlayerNamesGlobal), len(matches)) - // if *debug { - // globalDataMutex.Lock() - // logger.Debug("Total achievements loaded globally: %d", countTotalAchievements(allPlayersAchievementsGlobal)) - // globalDataMutex.Unlock() - // } - - // wgSave := sync.WaitGroup{} - // wgSave.Add(1) - // go func() { - // logger.Info("Saving achievements to database...") - // for playerName, achList := range allPlayersAchievementsGlobal { - // logger.Debug("Saving %d achievements for player %s", len(achList), playerName) - // for _, ach := range achList { - // Save(&ach, &db) - // } - // } - // wgSave.Done() - // }() - - // // --- Process and Send to NSQ --- - // // logger.Info("Starting NSQ message publishing...") - // // nsqMessagesChan := make(chan NSQMessage, 10000) // Increased buffer size - // // var wgNsqWorkers sync.WaitGroup - // // for i := 0; i < nsqWorkers; i++ { - // // wgNsqWorkers.Add(1) - // // go NsqWorker(&wgNsqWorkers, nsqMessagesChan) - // // } - - // // go func() { - // // globalDataMutex.Lock() - // // defer globalDataMutex.Unlock() - // // for playerName, achList := range allPlayersAchievementsGlobal { - // // for _, ach := range achList { - // // // ach.Name is already correctly set during extraction - // // nsqMessagesChan <- ach - // // logger.Debug("Queued NSQ message for Player: %s, AchID: %s", playerName, ach.ID) - // // } - // // } - // // close(nsqMessagesChan) // Close channel when all messages are sent - // // logger.Info("All NSQ messages queued.") - // // }() - - // // --- Pass 2: Update Lua file states (in memory) --- - // logger.Info("Starting Pass 2: Updating Lua states (setting alreadySeen and clearing players)...") - // var wgPass2 sync.WaitGroup - // if len(allPlayerNamesGlobal) > 0 { // Only run pass 2 if there are players to report - // for _, match := range matches { - // wgPass2.Add(1) - // go saveLuaFileState(filepath.Join(cleanedRoot, match), &wgPass2, allPlayerNamesGlobal) - // } - // wgPass2.Wait() - // logger.Info("Finished Pass 2: Lua states updated where applicable.") - // } else { - // logger.Info("Skipping Pass 2 as no players were found globally.") - // } - - // // wgNsqWorkers.Wait() // Wait for all NSQ messages to be processed - // wgSave.Wait() - // logger.Info("All NSQ workers finished. Program complete.") } func saveAchievementsToSourceFiles(luaStates *sync.Map, achievements *sync.Map) { @@ -174,7 +105,7 @@ func saveAchievementsToSourceFiles(luaStates *sync.Map, achievements *sync.Map) wg.Add(1) go func() { defer wg.Done() - updateSeenTable(state, seenTable, achievements, log) + updateSeenTable(state, seenTable, achievements) // State itself should now be updated }() @@ -186,10 +117,13 @@ func saveAchievementsToSourceFiles(luaStates *sync.Map, achievements *sync.Map) } func writeToSource(path string, state *lua.LState) { - + if err := state.DoString(script); err != nil { + logger.Error("Failed to write Lua state: %v", err) + return + } } -func updateSeenTable(state *lua.LState, seenTable lua.LValue, achievements *sync.Map, log *logger.Logger) { +func updateSeenTable(state *lua.LState, seenTable lua.LValue, achievements *sync.Map) { achievements.Range(func(k, v any) bool { playerName := k.(string) state.SetField(seenTable, playerName, lua.LBool(true)) @@ -386,98 +320,3 @@ func Publish(msg NSQMessage) error { } return nil } - -func saveLuaFileState(path string, wg *sync.WaitGroup, allKnownPlayerNames map[string]bool) { - log := logger.Default.WithPrefix(path) - log.Info("Saving Lua state") - defer wg.Done() -} - -// writeLuaTable writes a Lua table back to disk using Lua's own serialization -func writeLuaTable(path string, table *lua.LTable) error { - L := lua.NewState() - defer L.Close() - - // Create a Lua function that will write our table - script := ` - local function writeTable(tbl) - local file = io.open("%s", "w") - if not file then - return false, "Could not open file for writing" - end - - -- Write the original file content up to our table - local original = io.open("%s", "r") - if original then - local content = original:read("*all") - original:close() - - -- Find where our table starts - local startPos = content:find("Heimdall_Achievements%s*=%s*{") - if startPos then - file:write(content:sub(1, startPos-1)) - end - end - - -- Write our table - file:write("Heimdall_Achievements = ") - - -- Use Lua's built-in table serialization - local function serialize(tbl, indent) - indent = indent or 0 - local str = "{\n" - for k, v in pairs(tbl) do - str = str .. string.rep("\t", indent + 1) - if type(k) == "string" then - str = str .. string.format('["%s"] = ', k) - else - str = str .. string.format("[%s] = ", k) - end - - if type(v) == "table" then - str = str .. serialize(v, indent + 1) - elseif type(v) == "string" then - str = str .. string.format('"%s"', v) - elseif type(v) == "boolean" then - str = str .. tostring(v) - else - str = str .. tostring(v) - end - str = str .. ",\n" - end - str = str .. string.rep("\t", indent) .. "}" - return str - end - - file:write(serialize(tbl)) - file:write("\n") - - -- Write the rest of the file - if original then - local content = original:read("*all") - original:close() - - -- Find where our table ends - local endPos = content:find("}%s*$") - if endPos then - file:write(content:sub(endPos)) - end - end - - file:close() - return true - end - - return writeTable(Heimdall_Achievements) - ` - - // Set our table in the Lua state - L.SetGlobal("Heimdall_Achievements", table) - - // Execute the script - if err := L.DoString(script); err != nil { - return fmt.Errorf("failed to write Lua table: %v", err) - } - - return nil -}