Skip to content

Commit 8cbf01e

Browse files
Merge pull request #47 from Aperture-Development/feature-adduserid
AddUserID function
2 parents 90df549 + 898ed80 commit 8cbf01e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

lua/msync/server/sv_mysql.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,45 @@ function MSync.mysql.addUser(ply)
125125
addUserQ:start()
126126
end
127127

128+
--[[
129+
Description: Adds a userid to the users table
130+
Arguments:
131+
- steamid [string] - the steamid of the player to be added
132+
- nickname [string] - OPTIONAL: the nickname of the user to be created
133+
Returns: nothing
134+
]]
135+
function MSync.mysql.addUserID(steamid, nickname)
136+
if not MSync.DBServer then print("[MSync] No Database connected yet. Please connect to a Database to be able to create users."); return end;
137+
if not string.match( steamid, "^STEAM_[0-1]:[0-1]:[0-9]+$" ) then return end;
138+
139+
nickname = nickname or "None Given"
140+
141+
local addUserQ = MSync.DBServer:prepare( [[
142+
INSERT INTO `tbl_users` (steamid, steamid64, nickname, joined)
143+
VALUES (?, ?, ?, ?)
144+
ON DUPLICATE KEY UPDATE nickname=VALUES(nickname);
145+
]] )
146+
147+
if string.len(nickname) > 30 then
148+
nickname = string.sub( nickname, 1, 30 )
149+
end
150+
151+
addUserQ:setString(1, steamid)
152+
addUserQ:setString(2, util.SteamIDTo64(steamid))
153+
addUserQ:setString(3, nickname)
154+
addUserQ:setString(4, os.date("%Y-%m-%d %H:%M:%S", os.time()))
155+
156+
function addUserQ.onSuccess()
157+
print("[MSync] User "..steamid.." successfully created")
158+
end
159+
160+
function addUserQ.onError(q, err, sql)
161+
print("[MSync] Failed to create user "..steamid.." !\nPlease report this to the developer: "..err)
162+
end
163+
164+
addUserQ:start()
165+
end
166+
128167
--[[
129168
Description: Function to print the MySQL informations to the console
130169
Returns: nothing

0 commit comments

Comments
 (0)