/ mta-venux/account/s_account.lua

http://mta-venux.googlecode.com/ · Lua · 321 lines · 242 code · 50 blank · 29 comment · 50 complexity · 7c9d7ec8a7fefa4cf40fbd6711671e0b MD5 · raw file

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