This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PixelTerror's profile picture

Player Proximity Sensor + Logger

Started by PixelTerror, 24 October 2015 - 12:07 PM
PixelTerror #1
Posted 24 October 2015 - 02:07 PM
This is my setup, the monitor is just for debugging, I don't really need it in the final version.
Spoiler


prox = peripheral.wrap("top")
mon = peripheral.wrap("right")

while true do
players = prox.getPlayers()
mon.clear()
mon.setCursorPos(2,3)
mon.write(players)

if players ~= "{}" then
  local h = fs.open("visitor", fs.exists("visitor") and "a" or "w")
  h.writeLine(players)
  h.close()
end

sleep(5)

end

What I would like to do is to write to the "visitor" file every time someone comes in my base their name and the time of visit, it can be ingame time or real time, doesn't matter but the problem is I can't figure out a way to pull out player names from the tables and I can't figure out how to not write to the file when no one is near and also not to have double entries of the same player, also I don't know how to deal with the table if there are two players near the sensor at the same time or more, currently it is writing "{}" every 5 seconds when no one is near the sensor. I looked at a lot of code and I just got even more confused. I am playing on a direwolf20 1.8.0 pack server.

Thank you for your time!
TYKUHN2 #2
Posted 24 October 2015 - 03:46 PM
I really cannot say because I cannot see the full format it provides, and it seems it isn't that mod I found OpenCCSensors since getPlayers is nil so I would like a full table it provides or the mod you are using.
HPWebcamAble #3
Posted 24 October 2015 - 03:46 PM
So first of all:

if players ~= "{}" then
players is a table, and "{}" is a string. So they will never be equal.
In fact, it's pretty difficult to compare tables with '=='. Long story short, don't unless you know how, or if you can't avoid it.

You can check if it's empty like this:

if #players ~= 0 then
It may not work depending on the format of the table, so let me know if it doesn't


and also not to have double entries of the same player
Well, if you are logging nearby players ever 5 seconds, chances are that someone will stick around for a bit, giving them several entries, 1 every five seconds.
Do you not want it to work like that? It might be a bit more difficult (Though it isn't impossible)


I can't figure out a way to pull out player names from the tables
You'd use a for-loop:

local h = fs.open("visitor", fs.exists("visitor") and "a" or "w")

for a,b in pairs( players ) do --# According to your screenshot, 'a' should be a number, 'b' will be a table

  h.write( b.name .. ":" .. os.time() )

end
h.close()
If you run this block of code every five seconds, it SHOULD write all players in range into the file

Comment if you have any problems or questions :)/>
Edited on 24 October 2015 - 03:53 PM
TYKUHN2 #4
Posted 24 October 2015 - 05:03 PM
-snip-
–# Append mode automatically creates a file when necessary
-snip-

Actually most later versions of ComputerCraft don't create a file with append mode, instead returns an error.
HPWebcamAble #5
Posted 24 October 2015 - 05:52 PM
Actually most later versions of ComputerCraft don't create a file with append mode, instead returns an error.

Huh, coulda sworn that was fixed. Oh well, updated my code.
Bomb Bloke #6
Posted 25 October 2015 - 12:13 AM
It was fixed. Dunno which version. I should do some tests and amend the wiki.

I still prefer using "w" mode to create new files, if only because the affected ComputerCraft versions didn't magically pop out of existence when the fixed ones were released.
PixelTerror #7
Posted 25 October 2015 - 08:13 AM
Thank you so much HPW, that fixed everything, now it works exactly how I want it. At least if the system logs the player every 5 seconds I will know how long they stayed. :ph34r:/>

http://pastebin.com/bja5AY71


prox = peripheral.wrap("top")
mon = peripheral.wrap("right")

while true do
players = prox.getPlayers()
mon.clear()
mon.setCursorPos(2,3)

if #players ~= 0 then
  local h = fs.open("visitor", fs.exists("visitor") and "a" or "w")
    for a,b in pairs(players) do
    h.writeLine( b.name .. " @ " .. os.time() )
    mon.write(b.name)
    end
  h.close()
end

sleep(5)

end