GoreMotel Posted August 4, 2015 Share Posted August 4, 2015 (edited) Hi! I've written a small mod that serializes data about the game world and connected players, so that I can display that info on a website. I'm using JSON for serialization, writing the data to a text file, then I fetch and display it with jQuery using an Ajax request. Here is the code: local periodiclocal io = GLOBAL.iolocal os = GLOBAL.oslocal mode = GLOBAL.TheNet:GetServerGameMode()local isdedicated = GLOBAL.TheNet:GetServerIsDedicated()local json = GLOBAL.require("json")GLOBAL.assert(json.encode)local function Writer() local file = io.open(MODROOT.."serverstats.txt", "w") if file then local state = GLOBAL.TheWorld.state local clients = GLOBAL.TheNet:GetClientTable() local clientsno = 0 local stats = {} local players = {} for i,j in pairs(clients) do if ((not isdedicated or j["performance"] == nil) and (j["playerage"] > 0)) then local player = {} player["playerage"] = j["playerage"] player["name"] = j["name"] player["prefab"] = j["prefab"] player["r"] = string.format("%.4f", j["colour"][1]) player["g"] = string.format("%.4f", j["colour"][2]) player["b"] = string.format("%.4f", j["colour"][3]) players[clientsno] = player clientsno = clientsno + 1 end end stats["time"] = os.time() stats["cycles"] = state.cycles + 1 stats["phase"] = state.phase stats["season"] = state.season stats["remaining"] = state.remainingdaysinseason stats["precipitation"] = state.precipitation stats["mode"] = mode stats["clientsno"] = clientsno if (clientsno > 0) then stats["players"] = players end local jstats = json.encode(stats) file:write(jstats) file:close() else print("ERROR! Can't write server stats.") periodic:Cancel() endendAddSimPostInit(function() periodic = GLOBAL.scheduler:ExecutePeriodic(30, Writer) end) It works fine, but I'm new to Lua and I haven't coded at all for a long while and I'm wondering if I've written this efficiently? Is there stuff that could be changed so that the code gets executed faster? Do note that everything gets repeated every 30 seconds, will that affect the performance of the server in a big way? Edited August 4, 2015 by GoreMotel Link to comment Share on other sites More sharing options...
Maris Posted August 5, 2015 Share Posted August 5, 2015 Why you ask if it works fine?Also give the url to your website plz. Link to comment Share on other sites More sharing options...
GoreMotel Posted August 5, 2015 Author Share Posted August 5, 2015 Well, I am curious if I've written efficient code, so that the server isn't severely taxed. I'd also like to find out if others tried to implement something similar and how they did it. You can see the stats in action here: link. Or check the picture below: Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now