Forum

> > CS2D > Scripts > Login , register & Save system ?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Login , register & Save system ?

21 replies
Page
To the start Previous 1 2 Next To the start

old Re: Login , register & Save system ?

r4ndxm
User Off Offline

Quote
@user Dousea: It only writes 40 characters per line. Not actually a big problem

But you should make a limit when registering, like 12 characters for the username and 20 for the password, to be sure.

old Re: Login , register & Save system ?

Dousea
User Off Offline

Quote
I added user r4ndxm's idea about limiting username and password characters to 12 and 20. As a bonus I added a function to check if username and password characters are A-Z, a-z, and 0-9 characters.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
io.open("accounts.txt", "w"):close()

local function validchars(text)
	for index = 1, #text do
		local char = text:sub(index, index)
		
		if (not ((char:byte() >= 48 and char:byte() <= 57) or (char:byte() >= 65 and char:byte() <= 90) or char:byte() == 95 or (char:byte() >= 97 and char:byte() <= 122))) then
			return false
		end
	end
	
	return true
end

logged = {}

addhook("say", "sayhook")
addhook("leave", "leavehook")

function sayhook(id, message)
	if (message:sub(1, 9) == "!register" or message:sub(1, 6) == "!login") then
		local strings = {}
		
		for string in message:gmatch("[^%s]+") do
			table.insert(strings, string)
		end
		
		local arguments = select("#", unpack(strings))
		
		if (arguments > 3 or arguments < 3) then
			msg2(id, "\169255000000Please insert username and password properly without any whitespace!@C")
			msg2(id, "Use \"<username> <password>\" structure!@C")
			
			return 1
		end
		
		local file = io.open("accounts.txt", "a+")
		
		if (message:sub(1, 9) == "!register") then
			if (#strings[2] >= 4 and #strings[2] <= 12) then
				if (validchars(strings[2])) then
					for line in file:lines() do
						if (line:sub(1, #strings[2]) == strings[2]) then
							msg2(id, "\169255000000Account with that username already exists!@C")
							file:close()
							
							return 1
						end
					end
					
					if (#strings[3] >= 4 and #strings[3] <= 20) then
						if (validchars(strings[3])) then
							file:write(strings[2] .. " " .. strings[3] .. "\n")
							
							msg2(id, "You can now log in using your registered account!@C")
							msg2(id, "Please log in via \"!login <username> <password>\"!@C")
						else
							msg2(id, "\169255000000Password only takes A-Z, a-z, and 0-9 characters!@C")
						end
					else
						msg2(id, "\169255000000Password can't be less than 4 characters and more than 20 characters!@C")
					end
				else
					msg2(id, "\169255000000Username only takes A-Z, a-z, and 0-9 characters!@C")
				end
			else
				msg2(id, "\169255000000Username can't be less than 4 characters and can't be more than 12 characters!@C")
			end
		elseif (message:sub(1, 6) == "!login") then
			if (not logged[id]) then
				for line in file:lines() do
					if (line:sub(1, #strings[2]) == strings[2]) then
						if (line:sub(#strings[2] + 2, #line) == strings[3]) then
							logged[id] = true
							
							msg2(id, "Welcome, " .. strings[2] .. "!@C")
							file:close()
							
							return 1
						else
							msg2(id, "\169255000000Wrong password!@C")
							file:close()
							
							return 1
						end
					end
				end
				
				msg2(id, "\169255000000Account doesn't exist!@C")
				msg2(id, "Please register via \"!register <username> <password>\"!@C")
			else
				msg2(id, "\169255000000You already logged in!@C")
			end
		end
		
		file:close()
		
		return 1
	elseif (message:sub(1, 7) == "!logout") then
		if (logged[id]) then
			logged[id] = false
			
			msg2(id, "You are logged out!@C")
		end
		
		return 1
	end
end

function leavehook(id)
	if (logged[id]) then
		logged[id] = false
	end
end
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview