Forum

> > CS2D > Scripts > Overwrite 1: ATM with output saving
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Overwrite 1: ATM with output saving

5 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Overwrite 1: ATM with output saving

Ace Howl
User Off Offline

Zitieren
Hello!
I made a menu script which acts like ATM machine. Could you made me a function that saves local money (CS2D money) onto output file like ATM.txt. ATM only works for registered player.
Source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function use(id,x,y)
local x,y=player(id,"tilex"),player(id,"tiley")
	if x==28 and y==61 then
		menu(id,"ATM,Deposit,Withdraw,Balance")
	end
end

function menu(id,title,button)
	if title=="ATM" then
		if button==1 then
			menu(id,"Deposit,10,50,100")
		elseif button==2 then
			menu(id,"Withdraw,10,50,100")
		elseif button==3 then
			-- this line will shows player balance message
			--unlogged in player can't access this
		end
	end
	if title=="Deposit" then
		-- this line and next is up to solver
		-- unlogged in player can't save money
	end
	if title=="Withdraw" then
		-- this line and next is up to solver	
		-- unlogged in player can't save money
	end
end

11 December 2013: I have to overwrite this post because I can't create a new post, I don't know why.
1× editiert, zuletzt 11.12.13 07:34:37

alt Re: Overwrite 1: ATM with output saving

VaiN
User Off Offline

Zitieren
when you say "registered players" do you mean logged into USGN or your own admin system?

I'll give you some advice here.

You should have a path specified where you want to save/load the ATM data from
1
data_path = "/path/to/save/at/"

You should probably use a table for tracking the users' balances.
1
2
atm = {}
for id=1,32 do atm[id]=0 end

You should also reset the value when a user leaves:
1
2
3
4
addhook("leave","clear")
function clear(id)
     atm[id] = 0 -- reset balance to zero
end

I very highly recommend to save any data directly as a Lua table. this makes loading the data extremely easy.
Spoiler >


By saving this way you can use Lua's loadfile() function to easily load the data.
Spoiler >


Saving/Loading this way has two major benefits.
- It's very easy to add additional data
- It's very efficient. Lua loads the file as code, there's no faster way to load data from a file in Lua.

Now you can just plug in the parts you need into your menu:

Spoiler >

alt Re: Overwrite 1: ATM with output saving

Ace Howl
User Off Offline

Zitieren
user VaiN hat geschrieben
when you say "registered players" do you mean logged into USGN or your own admin system?

I mean that outside player which is registered. Yes, exactly.

EDIT: Wait, did the script you made compatible with CS2D money?
It works, just the saving problem.
What I actually did:
data_path = "/sys/lua/"

function save(id)
     local path = data_path .. player(id,"usgn") .. ".lua"
     local file = io.open(path,"w") -- open file for writing
     file:write("return{balance="..atm[id].."}")
end
2× editiert, zuletzt 11.12.13 13:03:36

alt Profile saving

KimKat
GAME BANNED Off Offline

Zitieren
I would save each profile into seperate files like 1.txt, 2.txt, 3.txt and so on instead of using a single file which probably will get overwritten millions of times and get accessed millions of times. Just reconsider by using seperate files instead of a single file. Just a better and more efficient solution, honestly.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht