AGuyNamedJens / FactorioChatBot

Two-Directional chat bot connecting Discord and Factorio chats together, written in nodejs.
MIT License
14 stars 9 forks source link

Ability to add more events besides join/leave and chat messages. #6

Open AGuyNamedJens opened 2 years ago

AGuyNamedJens commented 2 years ago

It does require manual save editing. The user would need to add custom events for this in control.lua which is located in the save file.

They would be adding a line like so: script.on_event(defines.events.on_rocket_launched, function(event) print("A rocket was launched") end)

I'll test it on my own factorio server soon. Will update this issue accordingly.

AGuyNamedJens commented 2 years ago

Looks like print won't work unless we dump everything with custom startup parameters into a different file.. log ("CustomLogger: " .. serpent.block(msg)) does, which logs


17.390 Script @/opt/factorio/temp/currently-playing/control.lua:6: CustomLogger: {
  created_entity = {
    __self = "userdata"
  },
  item = {
    __self = 0
  },
  name = 6,
  player_index = 1,
  stack = {
    __self = 0
  },
  tick = 10521775
}``` in factorio-current.log, seems like we're going to have to listen to the factorio-current.log to implement this?
AGuyNamedJens commented 2 years ago

So currently I have an idea for this, it does require a new custom log file, and the user editing their own save zip. It seems to work as it's logging everything needed right now. Only thing left seems to be parsing the ids into actual things if we want to log each placed item as shown in this image. Should work the same for the rest of the events.

image

Mattie112 commented 2 years ago

Yeah it might be a nice feature but it is kinda complicated for someone to set up. Why wouldn't it be able to write to the same log file?

Might still want to offer this though, perhaps create a small companion mod that does this or allowing this for users who don't want mods.

What exactly do you want me to test? To add that custom logger and see what it logs?

AGuyNamedJens commented 2 years ago

It's not complicated, it's simply opening their save zip, edit control.lua and add events. Why it can't use the same file? Honestly i have no idea, seems like factorio either throws the custom events into factorio current, or into a special folder called scripts.. The parameters added on startup only logs joins, leaves and chats..

And yeah, I'm indeed considering making it a mod for the lazy people.

If you check out the rewrite branch, it should have the functions there.

AGuyNamedJens commented 2 years ago

local baseFolder = "custom/"; local baseFile = "info_file.txt"

local function log_message(msg) game.write_file(baseFolder..baseFile, "[LOGGER] Placement: " .. serpent.line( msg, {comment = false}))

end

script.on_event(defines.events.on_built_entity, function(event) log_message(event) end)`

Also a good thing to mention, add this into control.lua, then use /opt/factorio/script-output/custom/info_file.txt as the event file in config.json. This logs all placements into the consoleChannel. I'm working on a Typescript rewrite with one of the forks that were made, which means we now have the master, rewrite and typescript branches. rewrite will be master's rewrite, while typescript will include a lot more features, such as automatic update checking for mods and the server itself.

This event would log image

Mattie112 commented 2 years ago

Sorry for the late reply had a bunch of issues (unrelated) before being able to try it out. Also got config failures but as I build a docker container (and that was not merged) the path was simply not correct haha that was confusing.

But I have trouble getting this to work. I unzip the savegame, add those lines to control.lua and re-save it. But then when I try to start my server:

   3.610 Error ServerMultiplayerManager.cpp:91: MultiplayerManager failed: "level.dat not found."

So it won't load anymore.

Mattie112 commented 2 years ago

Please ignore the message above, I copied the wrong error and I found the issue, the copy/paste from github included a ` so that was the problem.

I do get the log file, let me see if I can compile the bot for it.

Mattie112 commented 2 years ago

Yes the bot seems happy, should it post messages with this extended output? Not yet right?

Mattie112 commented 2 years ago

Research should also be possible: https://lua-api.factorio.com/latest/events.html#on_research_finished

Now by seeing this the code you use in the control.lua is most likely the same as for a mod. So yeah would be nice imo to support both :) I might look into this tonight (the mod part) to see how this works.

AGuyNamedJens commented 2 years ago

Yes the bot seems happy, should it post messages with this extended output? Not yet right?

Currently the rewrite branch should be able to post the output from those events, it needs consoleChannel to have a channel ID.

Research should also be possible: https://lua-api.factorio.com/latest/events.html#on_research_finished

Now by seeing this the code you use in the control.lua is most likely the same as for a mod. So yeah would be nice imo to support both :) I might look into this tonight (the mod part) to see how this works.

Yes, it's pretty similar to a mod, it would definitely be great if we make a mod for it.

Mattie112 commented 2 years ago

Ah, I missed the consoleChannel, will try it again tonight. Also: I will look into the modding part just to see what it takes :)

I think for a first version: research (finish and perhaps started) rocket launch player dies

AGuyNamedJens commented 2 years ago

Sure thing!

You should also check out the typescript branch, there's a lot of new features there as well, such as the update checker. Eventually we'll move everything from rewrite to typescript which will eventually end up as master

Mattie112 commented 2 years ago

Created a small companion mod:

https://mods.factorio.com/mod/FactorioChatBot-companion https://github.com/Mattie112/FactorioChatBot-companion

(obviously it requires implementation by this bot but this is just an idea on what we can do)

Not 100% perfect yet, the file append is not working (so it logs only the last line). Perhaps you will miss some things now as it might write faster then the bot can read.

Do you want me to submit a PR for this? Perhaps to the typescript branch? Or the rewrite one?

(edit: found the append problem will make a new release tomorrow)

AGuyNamedJens commented 2 years ago

Interesting! You could submit a PR, sure.