PageRenderTime 39ms CodeModel.GetById 17ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ mta-venux/account/s_account.lua

http://mta-venux.googlecode.com/
Lua | 321 lines | 242 code | 50 blank | 29 comment | 36 complexity | 7c9d7ec8a7fefa4cf40fbd6711671e0b MD5 | raw file
  1--[[
  2--    <<< VENUX _ SWIFT >>>
  3--      DEV: AeroXbird
  4--]]
  5
  6--TODO LIST!
  7-- * SAVE MORE DATA INTO ELEMENT
  8
  9_root = getRootElement();
 10
 11local attempts = 0
 12local startingPay = 200 -- change this to change how much your players get when they create a character.
 13
 14pedTable = {}
 15
 16-- TODO: Add new values once I get the time.
 17function _construct()
 18	for i, v in ipairs(getElementsByType("player")) do -- find every one of you's in this place
 19		toggleAllControls (v, false, false, true ) 
 20		setElementData(v, "loggedin", false ) -- because we are not logged in
 21		setElementData(v, "adminrank", 0 ) -- just for good measure
 22		setElementData(v, "username", getPlayerName( source )) -- saving the username you come in with.
 23		setElementData(v, "loginAttempts", 0)
 24		setElementData(v, "ooc", 0 ) -- dont need people spamming people at main menu
 25		setCameraMatrix(v, 783.44, -1287.73, 23.5, 733.9, -1276.6, 13.5)
 26		fadeCamera(v, true, 1)
 27	end
 28end
 29
 30-- TODO: Add some new values once I get the time.
 31function _joinConstruct()
 32	if ( getElementType(source) == "player" ) then
 33		toggleAllControls (source, false, false, true ) 
 34		--triggerClientEvent(source, "toggleDashboard", source, 1)
 35		setElementData(source, "loggedin", false )
 36		setElementData(source, "adminrank", 0 )
 37		setElementData(source, "username", getPlayerName( source ) )
 38		setElementData(source, "loginAttempts", 0)
 39		setElementData(source, "ooc", 0 )
 40		setPlayerNametagColor(player, 127, 127, 127)
 41		fadeCamera(source, true, 1)
 42		setCameraMatrix(source, 783.44, -1287.73, 23.5, 733.9, -1276.6, 13.5)
 43	end
 44end
 45
 46-- TODO: Make this a little more secure.
 47function attemptLogin(thePlayer, username, password)
 48	if ( username ~= "" and password ~= "" ) then 
 49		local result = exports.sql:query_assoc_single("SELECT * FROM `accounts` WHERE username = '".. tostring(username) .."' AND password = '".. exports.sql:escape_string( md5( password ) ) .."'")
 50			 if ( result ) then -- result is actually valid
 51				setElementData(thePlayer, "adminrank", result.rank )
 52				setElementData(thePlayer, "userid", result.id + 25565)
 53				setElementData(thePlayer, "accountid", result.id)
 54				setElementData(thePlayer, "atCharSelect", true)
 55				setElementData(thePlayer, "adminduty", result.adminduty)
 56				setElementDimension(thePlayer, getElementData(thePlayer, "userid"))
 57
 58				triggerClientEvent(thePlayer, "characterSelect", thePlayer)
 59				triggerClientEvent(thePlayer, "toggleDashboard", thePlayer, 0)
 60				setupCharacterSelect(thePlayer)
 61				setElementDimension(thePlayer, getElementData(thePlayer, "userid"))
 62				
 63			else
 64				attempts = tonumber(getElementData(thePlayer, "loginAttempts"))
 65				attempts = tonumber(attempts) + 1
 66				setElementData(thePlayer, "loginAttempts", attempts)
 67				
 68			if attempts >= 3 then
 69				kickPlayer(thePlayer, true, false, false, _root, "Too many login attempts")
 70			else
 71				outputChatBox("Username or password is invalid! You only have "..(3-attempts).." attempts left!",thePlayer, 255,0,0 )
 72				--return outputDebugString("ERROR 1: Failed to retrieve account for user: ".. getPlayerName(thePlayer) .."   function: attemptLogin", 3)
 73			end
 74			end
 75	else
 76		return outputDebugString("ERRROR 2: Failed to get the players username, password or element type mismatch at function attemptLogin for user:".. getPlayerName(thePlayer), 3)
 77	end
 78end
 79
 80-- TODO: Make this a little more secure shall we?
 81function attemptRegister(thePlayer, username, password)
 82	if ( username ~= "" and password ~= "" ) then
 83		local success, error = exports.sql:query_free("INSERT INTO `accounts` VALUES('', '".. tostring(username) .."', '".. md5( tostring( password ) ) .."', '".. getPlayerSerial(thePlayer) .."', '0')")
 84			if ( error ) then
 85				return outputDebugString( error )
 86			else
 87				return outputChatBox("Successfully registered your account!", thePlayer, 100, 0, 100) 
 88			end
 89	end
 90end
 91
 92function setupCharacterSelect(thePlayer)
 93local accountID = getElementData(thePlayer, "accountid")
 94pedTable[accountID] = {}
 95	if ( getElementData(thePlayer, "accountid") ~= "" ) then
 96		q = exports.sql:query_assoc("SELECT * FROM `characters` WHERE accountid = ".. tonumber(getElementData(thePlayer, "accountid") ).. " ")
 97			triggerClientEvent(thePlayer, "pickAped", thePlayer, true)
 98			-- location stuff will go here, to set the first ped's location, then we will work that up in the loop.
 99			local startX = 763.26171875
100			local startY = -1296.1162109375
101			local startZ = 13.5625
102			local myY = startY
103
104				-- X: 763.26171875  Y: -1291.1162109375  Z: 13.5625
105				if ( q == false ) then
106						outputDebugString("Error: This player doesnt have any characters.", 3) -- output info message.
107						outputChatBox("* You do not have any characters yet, please create one to play.", thePlayer, 180, 100, 0 )
108				else
109
110					for key, result in ipairs( q ) do
111
112						myY = myY + 3 -- move to right
113
114						ped = createPed(result.charskin, startX, myY, startZ, -90)
115
116							if (getElementType(ped) == "ped" ) then
117								outputDebugString("Ped created is indeed a ped!",3)
118							end 
119
120						setElementDimension( ped, getElementData(thePlayer, "userid") )
121						setElementData( ped, "characterid", result.id )
122						setElementData( ped, "skin", result.charskin )
123						setElementData( ped, "charName", result.charactername )
124						setElementData( ped, "charOwner", getPlayerName(thePlayer) )
125
126						pedTable[accountID][key] = ped
127						outputDebugString( tostring(myY) )
128					end
129				end
130	else
131		return outputDebugString("ERROR 5: setupCharacterSelect could not be started, first IF failed.", 2)
132	end
133end
134
135function removePed(source)
136local accountID = getElementData(source, "accountid")
137	for k, v in pairs(pedTable[accountID]) do
138		destroyElement(pedTable[accountID][k])
139		pedTable[accountID][k] = nil
140	end
141end
142	
143function playerSpawn(player, charactername)
144	if ( player and charactername ) then
145			local result = exports.sql:query_assoc_single("SELECT * FROM `characters` WHERE charactername = '".. charactername .."' ")
146			if ( result ) then 
147
148				spawnPlayer(player, 0, 0, 0, 0, characterskin )
149
150				setElementPosition(player, result.savedX, result.savedY, result.savedZ) -- We are most likely already spawned, so we just reset pos and skin.
151				setElementModel(player, tonumber( result.charskin ))
152
153				setPlayerMoney( player, result.cash )
154			
155				bindKey(player, "end", "down", "home")
156			
157				setElementDimension(player, 0)
158				setCameraTarget(player)
159				
160				setElementData(player, "ooc", 1 )
161				setElementData(player, "loggedin", true)
162				setElementData(player, "atCharSelect", false)
163				setElementData(player, "characterName", charactername)
164				setElementData(player, "characterID", result.charid)
165				
166				if ( getElementData(player, "adminduty" ) == 1 ) then
167					setPlayerNametagColor(player, 255, 194, 14)
168				elseif ( getElementData( player, "adminduty" ) == 0 ) then
169					setPlayerNametagColor(player, 255, 255, 255)
170				end
171				
172				setPlayerName(player, charactername)
173			
174				toggleAllControls (player, true, true, true ) 
175				triggerClientEvent(player, "clean_up", player)
176				triggerEvent("onCharacterSpawned", player, player, charactername)
177				triggerClientEvent(player, "pickAped", false)
178				showCursor(player, false)
179			else
180				return false end
181	end
182end
183
184function onCharacterSpawned( player, charactername )
185-- this function is triggered after we have spawned.
186	outputChatBox("Welcome to Roleplay!", player, 255, 130, 0)
187	--outputChatBox("Press the 'end' key to change characters.", player, 255,184,0)
188
189end
190addEvent("onCharacterSpawned", true)
191addEventHandler("onCharacterSpawned", _root, onCharacterSpawned)
192
193-- This will handle the quitting, once a player quits, we will use this to save his information.
194function _playerQuit(quitType, reason, responsibleElement)
195	if ( quitType == "Timed Out" ) then -- the poor lad timed out, lets help him save his shit.
196		local saveplayer = exports.save:savePlayer( source )
197		if ( saveplayer ) then 
198				if ( getPedOccupiedVehicle(source) ) then
199					-- TODO: insert vehicle saving options here, also locking vehicle should be here.
200				end
201		else
202			return false
203		end
204	elseif ( quitType == "Quit" ) then
205		local saveplayer = exports.save:savePlayer( source )
206		if ( saveplayer ) then 
207				if ( getPedOccupiedVehicle(source) ) then
208					-- TODO: insert vehicle saving options here, also locking vehicle should be here.
209				end
210		else
211			return false
212		end
213	end
214end
215
216function createCharacter(name, weight, height, skin, age, gender)
217	local accntID = getElementData(source, "accountid")
218	result = exports.sql:query_assoc_single("SELECT charactername FROM `characters` WHERE charactername = '".. name .."' ")
219	
220	if ( result ~= false ) then ---the char name exists
221		outputChatBox("Please select another character name. (Name in use!)")
222	else
223		local name = string.gsub(name, " ", "_")
224		local dbid = exports.sql:query_free("INSERT INTO characters SET charactername='"..exports.sql:escape_string(name).."', accountid='"..tonumber(accntID).."', charskin='"..skin.."',  age='"..age.."', intelligence='".. math.random(80, 160) .."',  faction_id='1', faction_name='San Andreas Government', faction_rank='1', hospitalized='0', driverslicense='0', drivingexperience='0', charlevel='0', charexp='0', perk1='', perk2='', cash='"..startingPay.."', savedX='835.009765625', savedY='-1313.96875', savedZ='13.546875', health='100', armor='0', interior='0', dimension='0'")
225		outputChatBox("* Your character has successfully been created!", source, 0, 255, 127)
226		triggerClientEvent(source, "characterSelect", source)
227	end
228end			  
229
230function reloadPeds( player )
231	if ( pedTable ~= nil ) then
232		triggerClientEvent(player, "pickAped", player, false)
233		removePed( player )
234		setupCharacterSelect( player )
235	end
236end
237
238addEvent("reloadPeds", true)
239addEventHandler("reloadPeds", _root, reloadPeds)
240
241--------------------------------------------------------------------------
242--|| Exported functions ||-
243--------------------------------------------------------------------------
244-- FUNCTION: getCharacterName( int characterID )
245-- Description: Allows you to retrieve a characters' name from the ID attached to the player.
246function getCharacterName( characterID )
247	if ( characterID ) then
248		local q = exports.sql:query_assoc_single("SELECT charactername FROM `characters` WHERE charid = '".. tonumber( characterID ) .."' ")
249			if ( q ) then
250				return string.gsub( q.charactername, "_", " " )
251			else
252				return false;
253			end
254	else
255		return MISSING_ARGUMENTS
256	end
257end
258
259-- FUNCTION: getCharacterID ( string charactername )
260-- Description: Retrieve a characters' ID by parsing their name.
261function getCharacterID( charactername )
262	if ( charactername ) then
263		local success, error = exports.sql:query_assoc_single( "SELECT charid FROM `characters` WHERE charactername = '".. tostring( exports.sql:escape_string( charactername ) ) .."'");
264		if ( success ) then
265			return success.charid
266		else
267			return false
268		end
269	else
270		return MISSING_ARGUMENTS
271	end
272end
273
274-- FUNCTION: getCharacterExists( int characterID )
275-- Description: Checks if a character with a specific ID exists, if it does, returns true, if ID does not exist, return false.
276function getCharacterExists( characterID )
277	if ( characterID ) then
278		local success, error = exports.sql:query_assoc_single( "SELECT charactername FROM `characters` WHERE charid = '".. tonumber( characterID ) .."'")
279		if ( success ) then
280			return true
281		else
282			return false
283		end
284	else
285		return MISSING_ARGUMENTS
286	end
287end
288
289
290---------------------------------------------------------------------------
291
292function returnHome(source, commandName)
293	setElementDimension(source, tonumber(getElementData(source, "userid"))) -- make the current character disapear
294	setupCharacterSelect(source) -- populate the players characters
295	triggerClientEvent(source, "characterSelect", source)
296	unbindKey(source, "end", "down", "home")
297	setElementData(source, "loggedin", false) -- player is no longer logged in
298	setElementData(source, "ooc", 0 ) -- no one can talk to the player
299	showCursor(source, true)
300end
301
302addCommandHandler("home", returnHome)
303
304addEvent("createCharacter", true)
305addEventHandler("createCharacter", _root, createCharacter)
306
307addEvent("onClientWantLogin", true)
308addEventHandler("onClientWantLogin", _root, attemptLogin)
309
310addEvent("removeCharacterPeds", true)
311addEventHandler("removeCharacterPeds", _root, removePed)
312
313addEvent("onClientRegister", true)
314addEventHandler("onClientRegister", _root, attemptRegister)
315
316addEvent("onCharacterSpawn", true)
317addEventHandler("onCharacterSpawn", _root, playerSpawn)
318
319addEventHandler("onPlayerJoin", _root, _joinConstruct)
320addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), _construct)
321addEventHandler("onPlayerQuit", _root, _playerQuit)