From ab919c64382d89181395afe063fbfe0c6175b4b6 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 20:23:46 -0400 Subject: [PATCH 01/12] Load database in function, change while->for --- .gitignore | 2 ++ SoloShuffler.lua | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..741246b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +settings.json diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 83d49d1..48a5a9a 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -129,13 +129,16 @@ else console.log("Initial seed " .. seed) end - -i = 0 -while i < databaseSize do - i = i + 1 - romSet[i] = userdata.get("rom" .. i) +function loadRomList() + -- local i = 0 + for i=0, databaseSize do + -- i = i + 1 + romSet[i] = userdata.get("rom" .. i) + end end +loadRomList() + console.log("Time Limit " .. timeLimit) --Commenting delay out until we implement it in the setup bot. Feel free to use it yourself. From e90bb7bf3d00ecb9af3562d0c90ea8cb83424993 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 20:37:31 -0400 Subject: [PATCH 02/12] Change var i to local, remove global --- .gitignore | 7 ++++++- SoloShuffler.lua | 14 +++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 741246b..4f52b0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ -.vscode +*.vscode settings.json +*.txt +CompletedROMs/* +CurrentROMs/* +PlayCount/* +TimeLogs/* diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 48a5a9a..fd4f284 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -2,11 +2,15 @@ diff = 0 lowTime = 5 highTime = 30 newGame = 0 -i = 0 + +-- i = 0 x = 0 + romSet = {} + gamePath = ".\\CurrentROMs\\" settingsPath = "settings.xml" + if userdata.get("countdown") ~= nil then countdown = userdata.get("countdown") else @@ -49,17 +53,17 @@ function openCurrentTime(rom) timeDatabase = io.open("CurrentGameSwitchCount.txt","w") timeDatabase:write(savePlayCount) timeDatabase:close() - + end - - + + function ends_with(str, ending) return ending == "" or str:sub(-#ending) == ending end function dirLookup(directory) -- Reads all ROM names in the CurrentROMs folder. - i = 0 + local i = 0 for directory in io.popen([[dir ".\CurrentROMs" /b]]):lines() do if ends_with(directory, ".bin") then console.log("SKIP: " .. directory) From e27ca8228197aac178b409333c25799de6785708 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 20:44:32 -0400 Subject: [PATCH 03/12] Move database update into function for cleanliness --- SoloShuffler.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index fd4f284..fd22d92 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -207,14 +207,18 @@ function nextGame(game) -- Changes to the next game and saves the current settin userdata.set("highTime",highTime) userdata.set("consoleID",emu.getsystemid()) userdata.set("countdown",countdown) - x = 0 - while x < databaseSize do - x = x + 1 - userdata.set("rom" .. x, romSet[x]) - end + + updateDatabase() end end +function updateDatabase() + console.log("Updating Database...") + for x=0, databaseSize do + userdata.set("rom" .. x, romSet[x]) + end +end + buffer = 0 -- Sets countdown location. Adding 8 makes it appear correct for the NES. if emu.getsystemid() == "NES" then buffer = 8 From db2d44d6432db5af9d303003dd9a618c747aaddc Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 21:12:16 -0400 Subject: [PATCH 04/12] ranNumber -> randNumber --- CompletedROMs/DeleteMe | 1 - CurrentROMs/DeleteMe | 1 - SoloShuffler.lua | 19 ++++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-) delete mode 100644 CompletedROMs/DeleteMe delete mode 100644 CurrentROMs/DeleteMe diff --git a/CompletedROMs/DeleteMe b/CompletedROMs/DeleteMe deleted file mode 100644 index 8b13789..0000000 --- a/CompletedROMs/DeleteMe +++ /dev/null @@ -1 +0,0 @@ - diff --git a/CurrentROMs/DeleteMe b/CurrentROMs/DeleteMe deleted file mode 100644 index 8b13789..0000000 --- a/CurrentROMs/DeleteMe +++ /dev/null @@ -1 +0,0 @@ - diff --git a/SoloShuffler.lua b/SoloShuffler.lua index fd22d92..b3ff54c 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -56,8 +56,6 @@ function openCurrentTime(rom) end - - function ends_with(str, ending) return ending == "" or str:sub(-#ending) == ending end @@ -173,18 +171,21 @@ function nextGame(game) -- Changes to the next game and saves the current settin dirLookup(directory) newGame = romSet[1] else - ranNumber = math.random(1,databaseSize) - if romSet[ranNumber] ~= nil then - newGame = romSet[ranNumber] + local randNumber = math.random(1,databaseSize) + if romSet[randNumber] ~= nil then + newGame = romSet[randNumber] + -- console.log("Random Number" .. randNumber .. " was in range") + -- console.log("Size of romSet: " .. #romSet .. " Size of database: " .. databaseSize) else + -- console.log("Random Number was out of range") dirLookup(directory) - newGame = userdata.get("rom" .. ranNumber) + newGame = userdata.get("rom" .. randNumber) --console.log("Ran dirLookup()") end while currentGame == newGame or newGame == nil do - ranNumber = math.random(1,databaseSize) - newGame = romSet[ranNumber] - console.log("Reroll! " .. ranNumber) + randNumber = math.random(1,databaseSize) + newGame = romSet[randNumber] + console.log("Reroll! " .. randNumber) end end currentGame = newGame From a01a006ebe41374661bac5c6b893243f1ff8bb4f Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 21:44:28 -0400 Subject: [PATCH 05/12] Made function names consistent --- SoloShuffler.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index b3ff54c..0127a5f 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -131,7 +131,7 @@ else console.log("Initial seed " .. seed) end -function loadRomList() +function getRomList() -- local i = 0 for i=0, databaseSize do -- i = i + 1 @@ -139,7 +139,7 @@ function loadRomList() end end -loadRomList() +getRomList() console.log("Time Limit " .. timeLimit) @@ -209,11 +209,11 @@ function nextGame(game) -- Changes to the next game and saves the current settin userdata.set("consoleID",emu.getsystemid()) userdata.set("countdown",countdown) - updateDatabase() + setRomList() end end -function updateDatabase() +function setRomList() console.log("Updating Database...") for x=0, databaseSize do userdata.set("rom" .. x, romSet[x]) From 682be8836b58182835cac78a7528701faf4d86c4 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 22:20:02 -0400 Subject: [PATCH 06/12] Cleanup of openCurrentTime() Moevd globals to locals where required, added comments --- SoloShuffler.lua | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 0127a5f..93b1abb 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -1,27 +1,25 @@ -diff = 0 -lowTime = 5 -highTime = 30 +-- Define Globals +diff = 0 -- Current frame +lowTime = 5 -- Min shuffle time +highTime = 30 -- Max shuffle time newGame = 0 --- i = 0 -x = 0 - romSet = {} gamePath = ".\\CurrentROMs\\" settingsPath = "settings.xml" +currentChangeCount = 0 -- Num times a game has been swapped to +currentGame = 1 + +saveOldTime = 0 +savePlayCount = 0 + if userdata.get("countdown") ~= nil then countdown = userdata.get("countdown") else countdown = false end -currentChangeCount = 0 -currentGame = 1 -c = {} -readOldTime = "" -saveOldTime = 0 -savePlayCount = 0 if userdata.get("currentChangeCount") ~= nil then -- Syncs up the last time settings changed so it doesn't needlessly read the CurrentROMs folder again. currentChangeCount = userdata.get("currentChangeCount") @@ -29,24 +27,27 @@ end databaseSize = userdata.get("databaseSize") function openCurrentTime(rom) - oldTime = io.open(".\\TimeLogs\\" .. currentGame .. ".txt","a+") - readOldTimeString = oldTime:read("*line") + -- Read playtime of current game from file + local oldTime = io.open(".\\TimeLogs\\" .. currentGame .. ".txt","a+") + local readOldTimeString = oldTime:read("*line") + local readOldTime = "0" if readOldTimeString ~= nil then readOldTime = readOldTimeString - else - readOldTime = 0 end oldTime:close() saveOldTime = readOldTime - oldCount = io.open(".\\PlayCount\\" .. currentGame .. ".txt","a+") - readOldCountString = oldCount:read("*line") + + -- Read playcount of current game from file + local oldCount = io.open(".\\PlayCount\\" .. currentGame .. ".txt","a+") + local readOldCountString = oldCount:read("*line") + local readOldCount = 0 if readOldCountString ~= nil then readOldCount = tonumber(readOldCountString) - else - readOldCount = 0 end oldCount:close() savePlayCount = readOldCount + 1 + + -- Update database with time and count info romDatabase = io.open("CurrentGameTime.txt","w") romDatabase:write(gameinfo.getromname() .. " play time: " .. saveOldTime) romDatabase:close() From 4ff89af4694edfcd3f2f52d535e58e80f2d500dd Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 22:31:53 -0400 Subject: [PATCH 07/12] setBuffer() function Made the countdown and buffer defaults unambiguous, cleaned up their init checks. Also put buffer init into a function. --- SoloShuffler.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 93b1abb..60e8b8d 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -15,10 +15,11 @@ currentGame = 1 saveOldTime = 0 savePlayCount = 0 +countdown = false; -- Countdown is disabled by default +buffer = 0 -- Buffer for countdown graphics + if userdata.get("countdown") ~= nil then countdown = userdata.get("countdown") -else - countdown = false end if userdata.get("currentChangeCount") ~= nil then -- Syncs up the last time settings changed so it doesn't needlessly read the CurrentROMs folder again. @@ -221,11 +222,15 @@ function setRomList() end end -buffer = 0 -- Sets countdown location. Adding 8 makes it appear correct for the NES. -if emu.getsystemid() == "NES" then - buffer = 8 +function setBuffer() + buffer = 0 -- Sets countdown location. Adding 8 makes it appear correct for the NES. + if emu.getsystemid() == "NES" then + buffer = 8 + end end +setBuffer() + function startCountdown(count) -- Draws the countdown box and text if countdown == true then gui.drawBox(client.bufferwidth()/2-60,buffer,client.bufferwidth()-(client.bufferwidth()/2+1-60),15+buffer,"white","black") From 9ec54e9960a2166d95a7876872e28dd65dd8322a Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 22:37:03 -0400 Subject: [PATCH 08/12] Removed redundant code As far as I can tell it doesn't do anything. --- SoloShuffler.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 60e8b8d..23d4bf5 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -244,12 +244,6 @@ function startCountdown(count) -- Draws the countdown box and text end end -if userdata.get("currentChangeCount") ~= null then - currentChangeCount = userdata.get("currentChangeCount") -else - currentChangeCount = 0 -end - function saveTime(currentRom) currentGameTime = io.open(".\\TimeLogs\\" .. currentGame .. ".txt","w") if saveOldTime ~= nil then From 38beb5730dab59b7110611b35107ec506f3c93f0 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Fri, 23 Oct 2020 22:43:33 -0400 Subject: [PATCH 09/12] Update saveTime function Changed globals to locals where possible, made default timeLimit clearer --- SoloShuffler.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 23d4bf5..9f73bd2 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -2,6 +2,7 @@ diff = 0 -- Current frame lowTime = 5 -- Min shuffle time highTime = 30 -- Max shuffle time +timeLimit = 5 -- Current time limit newGame = 0 romSet = {} @@ -246,6 +247,7 @@ end function saveTime(currentRom) currentGameTime = io.open(".\\TimeLogs\\" .. currentGame .. ".txt","w") + local newTime = 0 if saveOldTime ~= nil then newTime = saveOldTime + timeLimit else @@ -254,10 +256,9 @@ function saveTime(currentRom) currentGameTime:write(newTime) currentGameTime:close() currentGamePlayCount = io.open(".\\PlayCount\\" .. currentGame .. ".txt","w") + local newPlayCount = 1 if savePlayCount ~= nil then newPlayCount = savePlayCount - else - newPlayCount = 1 end currentGamePlayCount:write(newPlayCount) currentGamePlayCount:close() From 725e456a146920af798140a42bafb72fa4e518a7 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Sun, 25 Oct 2020 01:50:34 -0400 Subject: [PATCH 10/12] Store seed in getSettings Made seed global, allowing settingsValue to become local. Made other variables local as well. Seed is now retrieved in the getSettings function. --- .gitignore | 1 + SoloShuffler.lua | 18 ++++++++++-------- TimeLogs/DeleteMe | 1 - 3 files changed, 11 insertions(+), 9 deletions(-) delete mode 100644 TimeLogs/DeleteMe diff --git a/.gitignore b/.gitignore index 4f52b0c..d49a303 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.vscode settings.json *.txt +Backup/* CompletedROMs/* CurrentROMs/* PlayCount/* diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 9f73bd2..f49b98b 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -4,6 +4,7 @@ lowTime = 5 -- Min shuffle time highTime = 30 -- Max shuffle time timeLimit = 5 -- Current time limit newGame = 0 +seed = 0 -- Random seed romSet = {} @@ -84,11 +85,11 @@ function getSettings(filename) -- Gets the settings saved by the RaceShufflerSet if fp == nil then return nil end - settingsName = {} - settingsValue = {} - newLine = "a" - newSetting = "a" - k = 1 + --settingsName = {} + local settingsValue = {} + local newLine = "a" + local newSetting = "a" + local k = 1 for line in fp:lines() do -- Gets lines from the settings xml. newLine = string.match(line,'%l+%u*%l+') newSetting = string.match(line,'%p%a+%p(%w+)') @@ -101,6 +102,7 @@ function getSettings(filename) -- Gets the settings saved by the RaceShufflerSet fp:close() -- Closes settings.xml lowTime = settingsValue["value2"] -- Stores minimum value highTime = settingsValue["value3"] -- Stores maximum value + seed = settingsValue["value4"] -- Stores current seed changedRomCount = tonumber(settingsValue["value6"]) -- Stores value indicating if ROMs have been changed if settingsValue["value5"] == "true" then countdown = true @@ -117,7 +119,7 @@ if databaseSize ~= nil then lowTime = userdata.get("lowTime") highTime = userdata.get("highTime") seed = (userdata.get("seed")) - math.randomseed(seed) + math.randomseed(seed) -- Sets the seed math.random() if lowTime ~= highTime then timeLimit = math.random(lowTime * 60,highTime * 60) @@ -128,8 +130,8 @@ else getSettings(settingsPath) timeLimit = 5 dirLookup(directory) - seed = settingsValue["value4"] - math.randomseed(seed) + --seed = settingsValue["value4"] + math.randomseed(seed) -- Sets the seed math.random() console.log("Initial seed " .. seed) end diff --git a/TimeLogs/DeleteMe b/TimeLogs/DeleteMe deleted file mode 100644 index 8b13789..0000000 --- a/TimeLogs/DeleteMe +++ /dev/null @@ -1 +0,0 @@ - From 1d8a758a5ff217b134dba7049b47b785caf1e7c9 Mon Sep 17 00:00:00 2001 From: Tempystral Date: Sun, 25 Oct 2020 04:30:48 -0400 Subject: [PATCH 11/12] Update nextGame() function Fixed bugs in nextGame() function. - Removing all but one game now works correctly in all conditions - States are no longer saved and loaded when there is one game left and it has not changed --- SoloShuffler.lua | 89 +++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index f49b98b..0dad827 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -12,6 +12,7 @@ gamePath = ".\\CurrentROMs\\" settingsPath = "settings.xml" currentChangeCount = 0 -- Num times a game has been swapped to +changedRomCount = 0 currentGame = 1 saveOldTime = 0 @@ -108,7 +109,7 @@ function getSettings(filename) -- Gets the settings saved by the RaceShufflerSet countdown = true else countdown = false - console.log(tostring(settingsValue["value5"])) + console.log("Countdown: " .. tostring(settingsValue["value5"])) end end @@ -165,34 +166,37 @@ function cleanup() end function nextGame(game) -- Changes to the next game and saves the current settings into userdata - if databaseSize > 0 then - getSettings(settingsPath) - diff = 0 - if currentChangeCount < changedRomCount then -- Only do dirLookup() if settings have changed + diff = 0 + getSettings(settingsPath) -- Check for changes to settings, roms + if currentChangeCount < changedRomCount then -- Only do dirLookup() if settings have changed + dirLookup(directory) + currentChangeCount = changedRomCount + end + + if databaseSize == 0 then + do return end + elseif databaseSize == 1 then + newGame = romSet[1] + else + -- Select Game + local randNumber = math.random(1,databaseSize) + if romSet[randNumber] ~= nil then + newGame = romSet[randNumber] + else dirLookup(directory) - currentChangeCount = changedRomCount + newGame = userdata.get("rom" .. randNumber) end - if databaseSize == 1 then - dirLookup(directory) - newGame = romSet[1] - else - local randNumber = math.random(1,databaseSize) - if romSet[randNumber] ~= nil then - newGame = romSet[randNumber] - -- console.log("Random Number" .. randNumber .. " was in range") - -- console.log("Size of romSet: " .. #romSet .. " Size of database: " .. databaseSize) - else - -- console.log("Random Number was out of range") - dirLookup(directory) - newGame = userdata.get("rom" .. randNumber) - --console.log("Ran dirLookup()") - end - while currentGame == newGame or newGame == nil do - randNumber = math.random(1,databaseSize) - newGame = romSet[randNumber] - console.log("Reroll! " .. randNumber) - end + -- Reroll game if you get the same one twice + while currentGame == newGame or newGame == nil do + randNumber = math.random(1,databaseSize) + newGame = romSet[randNumber] + console.log("Reroll! " .. randNumber) end + + end + + -- Swap games + if currentGame ~= newGame then currentGame = newGame userdata.set("first",1) savestate.saveslot(1) @@ -200,22 +204,23 @@ function nextGame(game) -- Changes to the next game and saves the current settin savestate.loadslot(1) console.log(currentGame .. " loaded!") userdata.set("currentGame",currentGame) - userdata.set("timeLimit",timeLimit) - romDatabase = io.open("CurrentROM.txt","w") - romDatabase:write(gameinfo.getromname()) - romDatabase:close() - --console.log(emu.getsystemid()) - randIncrease = math.random(1,20) - userdata.set("seed",seed + randIncrease) -- Changes the seed so the next game/time don't follow a pattern. - userdata.set("currentChangeCount",currentChangeCount) - userdata.set("databaseSize",databaseSize) - userdata.set("lowTime",lowTime) - userdata.set("highTime",highTime) - userdata.set("consoleID",emu.getsystemid()) - userdata.set("countdown",countdown) - - setRomList() - end + end + -- Always do these things + userdata.set("timeLimit",timeLimit) + romDatabase = io.open("CurrentROM.txt","w") + romDatabase:write(gameinfo.getromname()) + romDatabase:close() + --console.log(emu.getsystemid()) + randIncrease = math.random(1,20) + userdata.set("seed",seed + randIncrease) -- Changes the seed so the next game/time don't follow a pattern. + userdata.set("currentChangeCount",currentChangeCount) + userdata.set("databaseSize",databaseSize) + userdata.set("lowTime",lowTime) + userdata.set("highTime",highTime) + userdata.set("consoleID",emu.getsystemid()) + userdata.set("countdown",countdown) + + setRomList() end function setRomList() From bf0aa9d9bac942762db89cad6eaf6003658dc45e Mon Sep 17 00:00:00 2001 From: Tempystral Date: Sun, 25 Oct 2020 04:49:28 -0400 Subject: [PATCH 12/12] Don't update currentChangeCount for same game When one game is left, don't update the change count between countdowns. This will still write a "1" if the game had not been played before. --- SoloShuffler.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/SoloShuffler.lua b/SoloShuffler.lua index 0dad827..10600b8 100644 --- a/SoloShuffler.lua +++ b/SoloShuffler.lua @@ -204,16 +204,19 @@ function nextGame(game) -- Changes to the next game and saves the current settin savestate.loadslot(1) console.log(currentGame .. " loaded!") userdata.set("currentGame",currentGame) + userdata.set("currentChangeCount",currentChangeCount) -- Only needs to update if the game has changed + + romDatabase = io.open("CurrentROM.txt","w") -- Update CurrentROM file + romDatabase:write(gameinfo.getromname()) + romDatabase:close() end -- Always do these things userdata.set("timeLimit",timeLimit) - romDatabase = io.open("CurrentROM.txt","w") - romDatabase:write(gameinfo.getromname()) - romDatabase:close() + --console.log(emu.getsystemid()) randIncrease = math.random(1,20) userdata.set("seed",seed + randIncrease) -- Changes the seed so the next game/time don't follow a pattern. - userdata.set("currentChangeCount",currentChangeCount) + userdata.set("databaseSize",databaseSize) userdata.set("lowTime",lowTime) userdata.set("highTime",highTime) @@ -271,9 +274,9 @@ function saveTime(currentRom) currentGamePlayCount:close() end -if databaseSize == 1 then +--[[if databaseSize == 1 then timeLimit = 6000 -end +end]] while true do -- The main cycle that causes the emulator to advance and trigger a game switch. if (diff >= timeLimit - 180) then