SHARE
TWEET

Fixes/additions

a guest Apr 24th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getPhoneRandomNumber()
  2.     return '0' .. math.random(600000000,699999999)
  3. end
  4.  
  5. function getSourceFromIdentifier(identifier, cb)
  6.     TriggerEvent("es:getPlayers", function(users)
  7.         for k , user in pairs(users) do
  8.             if user.getIdentifier() == identifier then
  9.                 cb(k)
  10.                 return
  11.             end
  12.         end
  13.     end)
  14.     cb(nil)
  15. end
  16. function getNumberPhone(identifier)
  17.     local result = MySQL.Sync.fetchAll("SELECT users.phone_number FROM users WHERE users.identifier = @identifier", {
  18.         ['@identifier'] = identifier
  19.     })
  20.     if result[1] ~= nil then
  21.         return result[1].phone_number
  22.     end
  23.     return nil
  24. end
  25.  
  26. function getIdentifierByPhoneNumber(phone_number)
  27.     local result = MySQL.Sync.fetchAll("SELECT users.identifier FROM users WHERE users.phone_number = @phone_number", {
  28.         ['@phone_number'] = phone_number
  29.     })
  30.     if result[1] ~= nil then
  31.         return result[1].identifier
  32.     end
  33.     return nil
  34. end
  35. --====================================================================================
  36. --  Contacts
  37. --====================================================================================
  38. function getContacts(identifier)
  39.     local result = MySQL.Sync.fetchAll("SELECT phone_users_contacts.id, phone_users_contacts.number, phone_users_contacts.display FROM phone_users_contacts WHERE phone_users_contacts.identifier = @identifier", {
  40.         ['@identifier'] = identifier
  41.     })
  42.     return result
  43. end
  44.  
  45. function addContact(source, identifier, number, display)
  46.     local sourcePlayer = tonumber(source)
  47.     print(number .. ' ' ..  display)
  48.     MySQL.Sync.execute("INSERT INTO phone_users_contacts (`identifier`, `number`,`display`) VALUES(@identifier, @number, @display)", {
  49.         ['@identifier'] = identifier,
  50.         ['@number'] = number,
  51.         ['@display'] = display,
  52.     })
  53.     notifyContactChange(sourcePlayer, identifier)
  54. end
  55.  
  56. function updateContact(source, identifier, id, number, display)
  57.     local sourcePlayer = tonumber(source)
  58.     MySQL.Sync.execute("UPDATE phone_users_contacts SET number = @number, display = @display WHERE id = @id", {
  59.         ['@number'] = number,
  60.         ['@display'] = display,
  61.         ['@id'] = id,
  62.     })
  63.     notifyContactChange(sourcePlayer, identifier)
  64. end
  65.  
  66. function deleteContact(source, identifier, id)
  67.     local sourcePlayer = tonumber(source)
  68.     MySQL.Sync.execute("DELETE FROM phone_users_contacts WHERE `identifier` = @identifier AND `id` = @id", {
  69.         ['@identifier'] = identifier,
  70.         ['@id'] = id,
  71.     })
  72.     notifyContactChange(sourcePlayer, identifier)
  73. end
  74.  
  75. function deleteAllContact(identifier)
  76.     MySQL.Sync.execute("DELETE FROM phone_users_contacts WHERE `identifier` = @identifier", {
  77.         ['@identifier'] = identifier
  78.     })
  79. end
  80.  
  81. function notifyContactChange(source, identifier)
  82.     local sourcePlayer = tonumber(source)
  83.     if sourcePlayer ~= nil then
  84.         TriggerClientEvent("gcPhone:contactList", sourcePlayer, getContacts(identifier))
  85.     end
  86. end
  87.  
  88. RegisterServerEvent('gcPhone:addContact')
  89. AddEventHandler('gcPhone:addContact', function(display, phoneNumber)
  90.     local sourcePlayer = tonumber(source)
  91.     local identifier = GetPlayerIdentifiers(source)[1]
  92.     addContact(sourcePlayer, identifier, phoneNumber, display)
  93. end)
  94.  
  95. RegisterServerEvent('gcPhone:updateContact')
  96. AddEventHandler('gcPhone:updateContact', function(id, display, phoneNumber)
  97.     local sourcePlayer = tonumber(source)
  98.     local identifier = GetPlayerIdentifiers(source)[1]
  99.     updateContact(sourcePlayer, identifier, id, phoneNumber, display)
  100. end)
  101.  
  102. RegisterServerEvent('gcPhone:deleteContact')
  103. AddEventHandler('gcPhone:deleteContact', function(id)
  104.     local sourcePlayer = tonumber(source)
  105.     local identifier = GetPlayerIdentifiers(source)[1]
  106.     deleteContact(sourcePlayer, identifier, id)
  107. end)
  108.  
  109. --====================================================================================
  110. --  Messages
  111. --====================================================================================
  112. function getMessages(identifier)
  113.     local result = MySQL.Sync.fetchAll("SELECT phone_messages.* FROM phone_messages LEFT JOIN users ON users.identifier = @identifier WHERE phone_messages.receiver = users.phone_number", {
  114.          ['@identifier'] = identifier
  115.     })
  116.     -- A CHANGER !!!!!!!
  117.     --for k, v in ipairs(result) do  
  118.         --v.time = os.time(v.time) + math.floor(0) - 2*60*60
  119.     --end
  120.     return result
  121.     --return MySQLQueryTimeStamp("SELECT phone_messages.* FROM phone_messages LEFT JOIN users ON users.identifier = @identifier WHERE phone_messages.receiver = users.phone_number", {['@identifier'] = identifier})
  122. end
  123.  
  124. local MessagesToAdd = {}
  125.  
  126. AddEventHandler('playerDropped', function()
  127.     local sourcePlayer = tonumber(source)
  128.     if source ~= nil then
  129.         for k,v in pairs(MessagesToAdd)do
  130.             if MessagesToAdd[k] ~= nil then
  131.                 MySQL.Sync.execute("INSERT INTO phone_messages (`transmitter`, `receiver`, `message`, `time`, `isRead`, `owner`) VALUES(@transmitter, @receiver, @message, @time, @isRead, @owner)", {
  132.                     ['@transmitter'] = MessagesToAdd[k].transmitter,
  133.                     ['@receiver'] = MessagesToAdd[k].receiver,
  134.                     ['@message'] = MessagesToAdd[k].message,
  135.                     ['@time'] = MessagesToAdd[k].time,
  136.                     ['@isRead'] = MessagesToAdd[k].owner,
  137.                     ['@owner'] = MessagesToAdd[k].owner
  138.                 })
  139.             end
  140.             MessagesToAdd = {}
  141.         end
  142.     end
  143. end)
  144.  
  145. local function updatePlayerMessages()
  146.     SetTimeout(6000000, function()
  147.         for k,v in pairs(MessagesToAdd)do
  148.             if MessagesToAdd[k] ~= nil then
  149.                 MySQL.Sync.execute("INSERT INTO phone_messages (`transmitter`, `receiver`, `message`, `time`, `isRead`, `owner`) VALUES(@transmitter, @receiver, @message, @time, @isRead, @owner)", {
  150.                     ['@transmitter'] = MessagesToAdd[k].transmitter,
  151.                     ['@receiver'] = MessagesToAdd[k].receiver,
  152.                     ['@message'] = MessagesToAdd[k].message,
  153.                     ['@time'] = MessagesToAdd[k].time,
  154.                     ['@isRead'] = MessagesToAdd[k].owner,
  155.                     ['@owner'] = MessagesToAdd[k].owner
  156.                 })
  157.             end
  158.         end
  159.         MessagesToAdd = {}
  160.         updatePlayerMessages()
  161.     end)
  162. end
  163. updatePlayerMessages()
  164.  
  165. local lastmessage = {
  166.     transmitter = 0,
  167.     receiver = 0,
  168.     message = 0,
  169.     time = 0,
  170.     isRead = 0,
  171.     owner = 0,
  172. }
  173.  
  174. function _internalAddMessage(transmitter, receiver, message, owner)
  175.     print('ADD MESSAGE: ' .. transmitter .. receiver .. message .. owner)
  176.     local tstamp = os.date("*t", os.time())
  177.     local time = os.date(tstamp.year .. "-" .. tstamp.month .. "-" .. tstamp.day .. " " .. tstamp.hour .. ":" .. tstamp.min .. ":" .. tstamp.sec)
  178.     table.insert(MessagesToAdd, {transmitter=transmitter,receiver=receiver,message=message,owner=owner})
  179.     lastmessage.transmitter = transmitter
  180.     lastmessage.receiver = receiver
  181.     lastmessage.message = message
  182.     lastmessage.time = time
  183.     lastmessage.isRead = owner
  184.     lastmessage.owner = owner
  185.     return lastmessage
  186. end
  187.  
  188. function addMessage(source, identifier, phone_number, message)
  189.     local sourcePlayer = tonumber(source)
  190.     local otherIdentifier = getIdentifierByPhoneNumber(phone_number)
  191.     local myPhone = getNumberPhone(identifier)
  192.     if otherIdentifier ~= nil then
  193.         local tomess = _internalAddMessage(myPhone, phone_number, message, 0)
  194.         getSourceFromIdentifier(otherIdentifier, function (osou)
  195.             if tonumber(osou) ~= nil then
  196.                 -- TriggerClientEvent("gcPhone:allMessage", osou, getMessages(otherIdentifier))
  197.                 TriggerClientEvent("gcPhone:receiveMessage", tonumber(osou), tomess)
  198.             end
  199.         end)
  200.     end
  201.     local memess = _internalAddMessage(phone_number, myPhone, message, 1)
  202.     -- TriggerClientEvent("gcPhone:allMessage", source, getMessages(identifier))
  203.     TriggerClientEvent("gcPhone:receiveMessage", sourcePlayer, memess)
  204.  
  205. end
  206.  
  207. function setReadMessageNumber(identifier, num)
  208.     local mePhoneNumber = getNumberPhone(identifier)
  209.     MySQL.Sync.execute("UPDATE phone_messages SET phone_messages.isRead = 1 WHERE phone_messages.receiver = @receiver AND phone_messages.transmitter = @transmitter", {
  210.         ['@receiver'] = mePhoneNumber,
  211.         ['@transmitter'] = num
  212.     })
  213. end
  214.  
  215. function deleteMessage(msgId)
  216.     MySQL.Sync.execute("DELETE FROM phone_messages WHERE `id` = @id", {
  217.         ['@id'] = msgId
  218.     })
  219. end
  220.  
  221. function deleteAllMessageFromPhoneNumber(identifier, phone_number)
  222.     local mePhoneNumber = getNumberPhone(identifier)
  223.     MySQL.Sync.execute("DELETE FROM phone_messages WHERE `receiver` = @mePhoneNumber and `transmitter` = @phone_number", {
  224.         ['@mePhoneNumber'] = mePhoneNumber,
  225.         ['@phone_number'] = phone_number
  226.     })
  227. end
  228.  
  229. function deleteAllMessage(identifier)
  230.     local mePhoneNumber = getNumberPhone(identifier)
  231.     MySQL.Sync.execute("DELETE FROM phone_messages WHERE `receiver` = @mePhoneNumber", {
  232.         ['@mePhoneNumber'] = mePhoneNumber
  233.     })
  234. end
  235.  
  236. RegisterServerEvent('gcPhone:sendMessage')
  237. AddEventHandler('gcPhone:sendMessage', function(phoneNumber, message)
  238.     local sourcePlayer = tonumber(source)
  239.     local identifier = GetPlayerIdentifiers(source)[1]
  240.     print(identifier)
  241.     addMessage(sourcePlayer, identifier, phoneNumber, message)
  242. end)
  243.  
  244. RegisterServerEvent('gcPhone:deleteMessage')
  245. AddEventHandler('gcPhone:deleteMessage', function(msgId)
  246.     deleteMessage(msgId)
  247. end)
  248.  
  249. RegisterServerEvent('gcPhone:deleteMessageNumber')
  250. AddEventHandler('gcPhone:deleteMessageNumber', function(number)
  251.     local sourcePlayer = tonumber(source)
  252.     local identifier = GetPlayerIdentifiers(source)[1]
  253.     deleteAllMessageFromPhoneNumber(identifier, number)
  254.     TriggerClientEvent("gcPhone:allMessage", sourcePlayer, getMessages(identifier))
  255. end)
  256.  
  257. RegisterServerEvent('gcPhone:deleteAllMessage')
  258. AddEventHandler('gcPhone:deleteAllMessage', function()
  259.     local sourcePlayer = tonumber(source)
  260.     local identifier = GetPlayerIdentifiers(source)[1]
  261.     deleteAllMessage(identifier)
  262.     TriggerClientEvent("gcPhone:allMessage", sourcePlayer, getMessages(identifier))
  263. end)
  264.  
  265. RegisterServerEvent('gcPhone:setReadMessageNumber')
  266. AddEventHandler('gcPhone:setReadMessageNumber', function(num)
  267.     local identifier = GetPlayerIdentifiers(source)[1]
  268.     setReadMessageNumber(identifier, num)
  269. end)
  270.  
  271. RegisterServerEvent('gcPhone:deleteALL')
  272. AddEventHandler('gcPhone:deleteALL', function()
  273.     local sourcePlayer = tonumber(source)
  274.     local identifier = GetPlayerIdentifiers(source)[1]
  275.     deleteAllMessage(identifier)
  276.     deleteAllContact(identifier)
  277.     TriggerClientEvent("gcPhone:contactList", sourcePlayer, {})
  278.     TriggerClientEvent("gcPhone:allMessage", sourcePlayer, {})
  279. end)
  280. --====================================================================================
  281. --  OnLoad
  282. --====================================================================================
  283. AddEventHandler('es:playerLoaded',function(source)
  284.     local sourcePlayer = tonumber(source)
  285.     local identifier = GetPlayerIdentifiers(sourcePlayer)[1]
  286.     local myPhoneNumber = getNumberPhone(identifier)
  287.     print(myPhoneNumber)
  288.     while myPhoneNumber == nil or tonumber(myPhoneNumber) == 0 do
  289.         local randomNumberPhone = getPhoneRandomNumber()
  290.         print('TryPhone: ' .. randomNumberPhone)
  291.         MySQL.Sync.execute("UPDATE users SET phone_number = @randomNumberPhone WHERE identifier = @identifier", {
  292.             ['@randomNumberPhone'] = randomNumberPhone,
  293.             ['@identifier'] = identifier
  294.         })
  295.         myPhoneNumber = getNumberPhone(identifier)
  296.     end
  297.     print(myPhoneNumber)
  298.     TriggerClientEvent("gcPhone:myPhoneNumber", sourcePlayer, myPhoneNumber)
  299.     TriggerClientEvent("gcPhone:contactList", sourcePlayer, getContacts(identifier))
  300.     TriggerClientEvent("gcPhone:allMessage", sourcePlayer, getMessages(identifier))
  301. end)
  302.  
  303. -- Just For reload
  304. RegisterServerEvent('gcPhone:allUpdate')
  305. AddEventHandler('gcPhone:allUpdate', function()
  306.     local sourcePlayer = tonumber(source)
  307.     local identifier = GetPlayerIdentifiers(source)[1]
  308.     TriggerClientEvent("gcPhone:myPhoneNumber", sourcePlayer, getNumberPhone(identifier))
  309.     TriggerClientEvent("gcPhone:contactList", sourcePlayer, getContacts(identifier))
  310.     TriggerClientEvent("gcPhone:allMessage", sourcePlayer, getMessages(identifier))
  311. end)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top