PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/ mta-venux/account/s_account.lua

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