Forum

> > CS2D > Scripts > Reading users from another file
Forums overviewCS2D overview Scripts overviewLog in to reply

English Reading users from another file

5 replies
To the start Previous 1 Next To the start

old Reading users from another file

krabob OP
User Off Offline

Quote
Hello, could anyone tell me how you would read users from another txt file.

Also, I would like to know how you would be able to add types of users (vip,members,etc.) from the server, that's running the script, by saying "!makevip <usgn> or <id>" and "!makemember <usgn> or <id>", which would add the usgns into the txt file.

Currently, this is how the script identifies the users.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Admin = {xxxx}
Aom = {xxxxx}
Mods = {xxxxx}
Members = {xxxxx}
Vip = {xxxxxx}

function totable(t,match)
     local cmd = {}
     if not match then match = "[^%s]+" end
     for word in string.gmatch(t,match) do
          table.insert(cmd, word)
     end
     return cmd
end

-- Init Array
function initArray(m)
	local array = {}
	for i = 1, m do
		array[i]=0
	end
	return array
end


function isAdmin(id)
     for _, usgn in ipairs(Admin) do
          if player(id,'usgn') == usgn then
               return true
          end
     end
     return false
end

function isMod(id)
     for _, usgn in ipairs(Mods) do
          if player(id,'usgn') == usgn then
               return true
          end
     end
     return false
end

function isAom(id)
     for _, usgn in ipairs(Aom) do
          if player(id,'usgn') == usgn then
               return true
          end
     end
     return false
end

function isMemb(id)
     for _, usgn in ipairs(Members) do
          if player(id,'usgn') == usgn then
               return true
          end
     end
     return false
end

function isVip(id)
     for _, usgn in ipairs(Vip) do
          if player(id,'usgn') == usgn then
               return true
          end
     end
     return false
end


------------the functions for all of the users


~Thanks in advance.

old Re: Reading users from another file

EngiN33R
Moderator Off Offline

Quote
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
cfile = "sys/uconfig.cfg"

ranks = {
	6 = "Super Admin",
	5 = "Admin",
	4 = "Moderator",
	3 = "VIP",
	2 = "Member",
	1 = "User",
}

function writeInFile()
	local file=assert(io.open(cfile,"w"))
	file:write("users = {")
	for k,v in pairs(users) do
		file:write("["..k.."] = "..v..",")
	end
	file:write("}")
end

function loadFile()
	dofile(cfile)
end

loadFile()

function getRank(usgn)
	return users[usgn]
end

function findByUsgn(usgn)
	for _,ply in pairs(player(0,"table")) do
		if player(ply,"usgn")==usgn then
			return ply
		end
	end
	return nil
end

function setUser(usgn,rank) --the rank should be numeric
	users[usgn]=rank
	msg2(findByUsgn(usgn),"©000255000You have been promoted to "..ranks[rank])
end

addhook("say","addusers",10096)
function addusers(id,t)
	if t:sub(1,8)=="!promote" then -- syntax: !promote ID RANK, where ID - player id and rank is numeric rank
		local iid=tonumber(t:sub(9,11))
		local rank=tonumber(t:sub(12))
		setUser(player(iid,"usgn"),rank)
		writeInFile()
	end
end

addhook("join","findplayer")
function findplayer(id)
	if not users[player(id,"usgn")] then
		users[player(id,"usgn")] = 1
	end
end

This should be in the 'sys/uconfig.cfg' file:

1
2
3
users = {
	[7749] = 6 -- example shown on my usgn
}

That's how I did it.

old Re: Reading users from another file

krabob OP
User Off Offline

Quote
@user EngiN33R: Thanks, but when I used your code I get this error: "LUA ERROR: sys/lua/users.lua:4: '}' expected (to close '{' at line 3) near '=' "
Could you tell me what to change?

Also in 'my' code, to state an admin's functions, I would've had to write this "if isAdmin(id) then". What would I have to type in your code?

Thanks again.

old Re: Reading users from another file

EngiN33R
Moderator Off Offline

Quote
Oops, my mistake. Put the numbers that go in the ranks table in square brackets (the ones in '6 = "Super Admin"').

You'll need to write the following with my code:

1
if getRank(player(id,"usgn"))==6 then -- id is player ID, 6 is the numerical rank for super admin - change it to other numbers for other ranks

old Re: Reading users from another file

krabob OP
User Off Offline

Quote
Yup, it worked. Although, the promote thingy was a bit confusing; I would have to say "!promote <id> 6" to make someone member and 1 to make him superadmin, etc.

No big thing though, I changed the rank order around and everything's perfect.

Thanks EngiN33R.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview