diff --git a/client.js b/client.js index ab6cf8e3..ed069883 100644 --- a/client.js +++ b/client.js @@ -96,7 +96,7 @@ setInterval(function() { // send our RCON command with whatever we got client.exec("/c remote.call('clusterio', 'importMany', '" + sadas + "')"); } -}, 10000) +}, 3000) /* fs.watch(config.factorioDirectory + "/script-output/orders.txt", "utf8", function(eventType, filename) { diff --git a/clusterio_0.0.2/control.lua b/clusterio_0.0.2/control.lua index 2536cd55..cb2ab1b1 100644 --- a/clusterio_0.0.2/control.lua +++ b/clusterio_0.0.2/control.lua @@ -9,23 +9,40 @@ function OnBuiltEntity(event) local entity = event.created_entity --only add entities that are not ghosts if entity.type ~= "entity-ghost" then - if entity.name == INPUT_CHEST_NAME then - global.inputChests = global.inputChests or {} - --add the chests to a lists if these chests so they can be interated over - global.inputChests[HashPosition(entity.position)] = entity - elseif entity.name == OUTPUT_CHEST_NAME then - global.outputChests = global.outputChests or {} - --add the chests to a lists if these chests so they can be interated over - global.outputChests[HashPosition(entity.position)] = entity - elseif entity.name == INPUT_TANK_NAME then - global.inputTanks = global.inputTanks or {} - --add the chests to a lists if these chests so they can be interated over - global.inputTanks[HashPosition(entity.position)] = entity - elseif entity.name == OUTPUT_TANK_NAME then - global.outputTanks = global.outputTanks or {} - --add the chests to a lists if these chests so they can be interated over - global.outputTanks[HashPosition(entity.position)] = entity - end + AddEntity(entity) + end +end + +function AddAllEntitiesOfName(name) + for k, surface in pairs(game.surfaces) do + AddEntities(surface.find_entities_filtered({["name"] = name})) + end +end + +function AddEntities(entities) + for k, entity in pairs(entities) do + AddEntity(entity) + end +end + +function AddEntity(entity) + if entity.name == INPUT_CHEST_NAME then + global.inputChests = global.inputChests or {} + --add the chests to a lists if these chests so they can be interated over + global.inputChests[HashPosition(entity.position)] = entity + elseif entity.name == OUTPUT_CHEST_NAME then + global.outputChests = global.outputChests or {} + --add the chests to a lists if these chests so they can be interated over + global.outputChests[HashPosition(entity.position)] = entity + elseif entity.name == INPUT_TANK_NAME then + global.inputTanks = global.inputTanks or {} + --add the chests to a lists if these chests so they can be interated over + global.inputTanks[HashPosition(entity.position)] = entity + elseif entity.name == OUTPUT_TANK_NAME then + global.outputTanks = global.outputTanks or {} + --add the chests to a lists if these chests so they can be interated over + global.outputTanks[HashPosition(entity.position)] = entity + entity.active = false end end @@ -67,9 +84,8 @@ script.on_event(defines.events.on_preplayer_mined_item, function(event) end) - - script.on_event(defines.events.on_tick, function(event) + global.inputChests = global.inputChests or {} global.outputChests = global.outputChests or {} global.inputTanks = global.inputTanks or {} @@ -79,6 +95,13 @@ script.on_event(defines.events.on_tick, function(event) local todo = game.tick % UPATE_RATE + local onlinePlayers = GetOnlinePlayerCount() + + if global.previousPlayerCount == nil or global.previousPlayerCount ~= onlinePlayers then + Reset() + end + global.previousPlayerCount = onlinePlayers + if todo == 0 then HandleInputChests() elseif todo == 1 then @@ -94,6 +117,33 @@ script.on_event(defines.events.on_tick, function(event) end end) +function GetOnlinePlayerCount() + local onlinePlayers = 0 + for k, player in pairs(game.players) do + if player.connected then + onlinePlayers = onlinePlayers + 1 + end + end + return onlinePlayers +end + +function Reset() + global.outputList = {} + global.inputList = {} + global.itemStorage = {} + + global.inputChests = {} + global.outputChests = {} + global.inputTanks = {} + global.outputTanks = {} + + AddAllEntitiesOfName(INPUT_CHEST_NAME) + AddAllEntitiesOfName(OUTPUT_CHEST_NAME) + AddAllEntitiesOfName(INPUT_TANK_NAME) + AddAllEntitiesOfName(OUTPUT_TANK_NAME) + game.print("reset") +end + function HandleInputChests() for k, v in pairs(global.inputChests) do if v.valid then @@ -114,9 +164,7 @@ function HandleInputTanks() if v.valid then --get the content of the chest local fluid = v.fluidbox[1] - if fluid ~= nil and - fluid.type == "crude-oil" and - math.floor(fluid.amount) > 0 then + if fluid ~= nil and math.floor(fluid.amount) > 0 then AddItemToInputList(fluid.type, math.floor(fluid.amount)) fluid.amount = fluid.amount - math.floor(fluid.amount) end @@ -165,12 +213,23 @@ end function HandleOutputTanks() local MAX_FLUID_AMOUNT = 1000 for k,v in pairs(global.outputTanks) do - local fluid = v.fluidbox[1] or {type = "crude-oil", amount = 0} - if fluid.type == "crude-oil" then + --.recipe.products[1].name + + if v.recipe ~= nil then + local fluidName = v.recipe.products[1].name - local missingFluid = math.ceil(MAX_FLUID_AMOUNT - fluid.amount) + --either get the fluid or reset it to the requested fluid + local fluid = v.fluidbox[1] or {type = fluidName, amount = 0} + if fluid.type ~= fluidName then + fluid = {type = fluidName, amount = 0} + end + + --if any fluid is missing then request the fluid + --from store and give either what it's missing or + --the rest of the liquid in the system + local missingFluid = math.max(math.ceil(MAX_FLUID_AMOUNT - fluid.amount), 0) if missingFluid > 0 then - local fluidToInsert = RequestItemsFromStorage("crude-oil", missingFluid) + local fluidToInsert = RequestItemsFromStorage(fluidName, missingFluid) if fluidToInsert > 0 then fluid.amount = fluid.amount + fluidToInsert else @@ -178,8 +237,9 @@ function HandleOutputTanks() AddItemToOutputList(fluid.type, fluidToRequestAmount) end end + + v.fluidbox[1] = fluid end - v.fluidbox[1] = fluid end end @@ -264,7 +324,15 @@ remote.add_interface("clusterio", GiveItemsToStorage(itemName, itemCount) end end - end + end, + printStorage = function() + local items = "" + for itemName, itemCount in pairs(global.itemStorage) do + items = items.."\n"..itemName..": "..tostring(itemCount) + end + game.print(items) + end, + reset = Reset }) diff --git a/clusterio_0.0.2/data.lua b/clusterio_0.0.2/data.lua index 45134a36..fa1bb0a4 100644 --- a/clusterio_0.0.2/data.lua +++ b/clusterio_0.0.2/data.lua @@ -20,7 +20,7 @@ function MakeLogisticEntity(entity, name, pictureFilename, pictureTablePath, ico entity.icon = iconPath or entity.icon -- add the entity to a technology so it can be unlocked - local wasAddedToTech = AddEntityToTech("construction-robotics", name) + --local wasAddedToTech = AddEntityToTech("construction-robotics", name) data:extend( { @@ -34,7 +34,7 @@ function MakeLogisticEntity(entity, name, pictureFilename, pictureTablePath, ico --shouldn't be enabled to begin with. --but if the recipe isn't attached to a tech then it should --be enabled to begin with because otherwise the player can never use the item ingame - enabled = not wasAddedToTech, + enabled = true, ingredients = { {"steel-chest", 1}, @@ -55,6 +55,7 @@ function MakeLogisticEntity(entity, name, pictureFilename, pictureTablePath, ico stack_size = 50 } }) + return entity end --adds a recipe to a tech and returns true or if that fails returns false @@ -83,7 +84,6 @@ MakeLogisticEntity(table.deepcopy(data.raw["logistic-container"]["logistic-chest MakeLogisticEntity(table.deepcopy(data.raw["container"]["iron-chest"]), INPUT_CHEST_NAME, INPUT_CHEST_PICTURE_PATH, { "picture" }, INPUT_CHEST_ICON_PATH) --make tanks -MakeLogisticEntity(table.deepcopy(data.raw["storage-tank"]["storage-tank"]), OUTPUT_TANK_NAME, OUTPUT_TANK_PICTURE_PATH, { "pictures", "picture", "sheet" }, OUTPUT_TANK_ICON_PATH) MakeLogisticEntity(table.deepcopy(data.raw["storage-tank"]["storage-tank"]), INPUT_TANK_NAME, INPUT_TANK_PICTURE_PATH, { "pictures", "picture", "sheet" }, INPUT_TANK_ICON_PATH) @@ -92,6 +92,73 @@ MakeLogisticEntity(table.deepcopy(data.raw["storage-tank"]["storage-tank"]), IN +-------------------------------------------------------- +--[[This section is purely to create the output tank]]-- +-------------------------------------------------------- + + + + + + + +local CRAFING_FLUID_CATEGORY_NAME = "crafting-fluids" + +data:extend( +{ + { + type = "recipe-category", + name = CRAFING_FLUID_CATEGORY_NAME + } +}) + +local fluidCreator = MakeLogisticEntity(table.deepcopy(data.raw["assembling-machine"]["assembling-machine-3"]), OUTPUT_TANK_NAME, OUTPUT_TANK_PICTURE_PATH, { "animation" }, OUTPUT_TANK_ICON_PATH) +fluidCreator.fluid_boxes = +{ + { + production_type = "output", + pipe_picture = assembler3pipepictures(), + pipe_covers = pipecoverspictures(), + base_area = 250, + base_level = 1, + pipe_connections = + { + { + type="output", position = {0, 2} + } + } + }, + off_when_no_fluid_recipe = false +} +fluidCreator.crafting_categories = {CRAFING_FLUID_CATEGORY_NAME} +fluidCreator.energy_usage = "1kW" +fluidCreator.ingredient_count = 1 +fluidCreator.module_specification.module_slots = 0 + +for k,v in pairs(data.raw.fluid) do + data:extend( + { + { + type = "recipe", + name = v.name, + icon = v.icon, + category = CRAFING_FLUID_CATEGORY_NAME, + energy_required = 1, + subgroup = "barrel", + order = "b[fill-crude-oil-barrel]", + enabled = true, + ingredients = {}, + results= + { + {type="fluid", name=v.name, amount=-1} + } + } + }) +end + + + + diff --git a/clusterio_0.0.2/graphics/icons/none.png b/clusterio_0.0.2/graphics/icons/none.png new file mode 100644 index 00000000..67c44794 Binary files /dev/null and b/clusterio_0.0.2/graphics/icons/none.png differ diff --git a/clusterio_0.0.2/info.json b/clusterio_0.0.2/info.json index 7e4449e0..93e439f0 100644 --- a/clusterio_0.0.2/info.json +++ b/clusterio_0.0.2/info.json @@ -6,6 +6,6 @@ "date": "19-08-2016", "author": "Danielv123 & Keyboardhack", "dependencies": ["base >= 0.13"], -"factorio_version": "0.13", +"factorio_version": "0.14", "description": "Adds 2 chests to transfer items between saves and servers over the internet." } \ No newline at end of file diff --git a/create_shortcut.bat b/create_shortcut.bat deleted file mode 100644 index bc653533..00000000 --- a/create_shortcut.bat +++ /dev/null @@ -1,31 +0,0 @@ -@echo off -@cls - -@set /p host="Enter host (default: "127.0.0.1"): " -@if "%host%"=="" set host=127.0.0.1 - -@set /p port="Enter port (default: 25575): " -@if "%port%"=="" set port=25575 - -@set /p passwd="Enter password: " -@if "%passwd%"=="" set passwd= - -set name=connect_%host%-%port% - -@set /p name="Enter shortcut name (default: "%name%.bat"): " -@if "%name%"=="" set name=connect_%host%-%port% - -set command=@mcrcon.exe -t -H %host% -P %port% -p %passwd% - -@echo %command% >> %name%.bat -@echo. -@echo Command: "%command%" -@echo. -@echo Shortcut "%name%.bat" created! -@echo. - -@set "host=" -@set "port=" -@set "passwd=" - -@pause diff --git a/launchEverything.bat b/launchEverything.bat index 33516b3f..f08a766e 100644 --- a/launchEverything.bat +++ b/launchEverything.bat @@ -2,14 +2,4 @@ cd "Factorio_0.13.17" @RD /S /Q "temp" cd "bin/x64/" start factorio.exe --start-server testmap.zip --rcon-port 12345 --rcon-password 123 --server-settings server-settings.json -cd .. -cd .. -cd .. - -::start cmd /C node client.js -::start cmd /C node master.js - -::cd "Factorio 0.13.9 - Copy/bin/x64" -::@start factorio.exe - pause \ No newline at end of file diff --git a/master.js b/master.js index 49b46cd7..6e1bdaf7 100644 --- a/master.js +++ b/master.js @@ -93,6 +93,13 @@ app.get("/inventory", function(req, res) { res.send(docs); }); }); +// endpoint for getting the chartjs library +app.get("/chart.js", function(req, res) { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + // Check it and send it + res.end(fs.readFileSync("node_modules/chartjs/chart.js")); +}); var server = app.listen(config.masterPort || 8080, function () { console.log("Listening on port %s...", server.address().port); diff --git a/static/index.html b/static/index.html index 9d55d35e..ae504e47 100644 --- a/static/index.html +++ b/static/index.html @@ -1,7 +1,8 @@ - + Clusterio + @@ -25,6 +26,13 @@

Storage

+ + +
+

Production

+
+ +
\ No newline at end of file diff --git a/static/main.js b/static/main.js index d56bb0dc..21f4256c 100644 --- a/static/main.js +++ b/static/main.js @@ -1,10 +1,25 @@ - contents = { "iron-plate":100, "copper-plate":7312, } +function djb2(str){ + var hash = 5381; + for (var i = 0; i < str.length; i++) { + hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */ + } + return hash; +} + +function hashColor(str) { + var hash = djb2(str); + var r = (hash & 0xFF0000) >> 16; + var g = (hash & 0x00FF00) >> 8; + var b = hash & 0x0000FF; + return "#" + ("0" + r.toString(16)).substr(-2) + ("0" + g.toString(16)).substr(-2) + ("0" + b.toString(16)).substr(-2); +} + function drawcontents(data) { keys = Object.keys(data); result = ""; @@ -14,14 +29,43 @@ function drawcontents(data) { result = result + "
" document.getElementById("contents").innerHTML = result; } + setInterval(function() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { data = JSON.parse(xmlhttp.responseText); drawcontents(data); + // render our piechart with up to date information + piedata = []; + for(i=0;i