English Text file modification

14 replies
Goto Page
To the start Previous 1 Next To the start
15.04.17 11:38:28 pm
Up
AtomKuh
User
Offline Off
I need a lua script that changes the content of a text file each 10 seconds. For instance, once 10 seconds are ran out, the previous empty text file should now contain the letter a. After the next 10 seconds the letter should be changed to b. Afterwards should the letter be changed back to a.

I tried to do it myself but I failed. I don't understand this I/O Model in lua.

I need this script to determine whether my server is still running or not. As a result I can restart automatically my server with another script.
16.04.17 12:26:24 am
Up
VADemon
User
Offline Off
Code:
1
2
3
4
5
6
7
function updateUptimeFile(path)
     local path = path or "sys/lua/uptime.txt"
     local file = io.open(path, "w")

     file:write(os.date("%H:%M:%S"))
     file:close()
end
16.04.17 12:26:45 am
Up
TheD
User
Offline Off
Not really tested, but this is something. This should cycle every 10 seconds and update the text document. There are other ways, but this is just a quick one to get you into thinking.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local i = 0;
local temp = "a";
addhook("second", "test")
function test()
     i = i + 1;
     if a > 100 then a = 1; end
     if i%10 == 0 then
          local file = io.open("path/to/file", "w");
          if (temp == "a") then temp = "b";
          else temp = "a";
          end
          file:write(temp);
          file:close();
     end
end
http://cs2d-rpg.sytes.net/ - nvRPG -> Coming soon!
16.04.17 01:27:33 am
Up
AtomKuh
User
Offline Off
Thanks you too! By the way is there a method to create a delayed loop without using addhooks?
16.04.17 02:37:33 am
Up
_Yank
User
Offline Off
You can use the timer function and set it to repeat forever
16.04.17 07:53:54 am
Up
MikuAuahDark
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Use locals and upvalues, it's faster
local file = io.open("sys/lua/file.txt", "rb+")
local write_a = false

function updateTextFile()
     -- Start writing on beginning of file
     file:seek("set")
     -- Write either "a" or "b"
     file:write(write_a and "a" or "b")
     -- Apply the changes
     file:flush()
     -- Flip the switch to write either "a" or "b" in next write
     write_a = not(write_a)
end

-- Finally call timer, set count to 0 so it's called infinite times.
timer(10000, "updateTextFile", "", 0)
AuahDark strikes again for one more time in Pixel Art server.
16.04.17 04:18:58 pm
Up
VADemon
User
Offline Off
local file = io.open("sys/lua/file.txt", "rb+")

should be:
local file = io.open("sys/lua/file.txt", "wb+")
18.04.17 12:21:58 am
Up
AtomKuh
User
Offline Off
user MikuAuahDark has written:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Use locals and upvalues, it's faster
local file = io.open("sys/lua/file.txt", "wb+")
local write_a = false

function updateTextFile()
     -- Start writing on beginning of file
     file:seek("set")
     -- Write either "a" or "b"
     file:write(write_a and "a" or "b")
     -- Apply the changes
     file:flush()
     -- Flip the switch to write either "a" or "b" in next write
     write_a = not(write_a)
end

-- Finally call timer, set count to 0 so it's called infinite times.
timer(10000, "updateTextFile", "", 0)


Can someone edit this code that it closes the text document after writing into it, please? I may have some problems if it stays open with another bash script.

Thanks in advance
edited 2×, last 27.04.17 09:42:52 pm
28.04.17 06:41:26 am
Up
ZxC
Moderator
Offline Off
Add
file:close()
at the end of the
updateTextFile()
function which is after 13 line.
Running CS2D in ReactOS like a boss | Good ol' music | WARNING: I'll only accept cheater reports from people which I trust.
28.04.17 11:50:35 am
Up
AtomKuh
User
Offline Off
My code looks now like this:

Spoiler >


I get this error by using it:
Code:
1
LUA ERROR: sys/lua/server.lua:7: attempt to use a closed file
28.04.17 12:01:41 pm
Up
Yates
Reviewer
Offline Off
@user AtomKuh: That's because the file is opened once, then closed the first time
updateTextFile
is used. It is never opened afterwards.

Frankly the local
file
used as a variable for the whole block scope is shitty practice - even if it's faster. Faster does not always mean better.
28.04.17 12:30:53 pm
Up
AtomKuh
User
Offline Off
@user Yates: Thanks, but do you have any idea to change it?

The problem I have is that I want to check with the following bash script whether a text file is being edited or not:
Spoiler >


Somehow it works on text files that I edit manually but not on the one that is being edited by this lua script
28.04.17 12:49:37 pm
Up
Yates
Reviewer
Offline Off
Probably because it's still holding the file in its memory and won't load outside changes.

I wouldn't use a text file to check whether a server is offline. You can ping it to see if it's still responding or not.
28.04.17 02:17:29 pm
Up
MikuAuahDark
User
Offline Off
user Yates has written:
@user AtomKuh: That's because the file is opened once, then closed the first time
updateTextFile
is used. It is never opened afterwards.

Frankly the local
file
used as a variable for the whole block scope is shitty practice - even if it's faster. Faster does not always mean better.


Say no more.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
local write_a = false

function updateTextFile()
     -- Start writing on beginning of file
     local file = io.open("sys/lua/file.txt", "wb+")
     -- Write either "a" or "b"
     file:write(write_a and "a" or "b")
     -- Close the file. Will flush it automatically
     file:close()
     -- Flip the switch to write either "a" or "b" in next write
     write_a = not(write_a)
end

timer(10000, "updateTextFile", "", 0)
AuahDark strikes again for one more time in Pixel Art server.
29.04.17 10:26:06 pm
Up
AtomKuh
User
Offline Off
@user Yates: I used nmap to ping my server when it was online but I dont know how to DDOS/take down my server for test purposes. Do you have any idea?

@user MikuAuahDark: Thanks for editing the code
To the start Previous 1 Next To the start