Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.28 KB | None | 0 0
  1. -- basic phone module
  2.  
  3. local lang = vRP.lang
  4. local cfg = module("cfg/phone")
  5. local htmlEntities = module("lib/htmlEntities")
  6. local services = cfg.services
  7. local announces = cfg.announces
  8.  
  9. local sanitizes = module("cfg/sanitizes")
  10.  
  11. -- api
  12.  
  13.  
  14. local call_seq = {
  15. {"cellphone@","cellphone_call_in",1},
  16. {"cellphone@","cellphone_call_listen_base",1}
  17. }
  18.  
  19.  
  20. -- Send a service alert to all service listeners
  21. --- sender: a player or nil (optional, if not nil, it is a call request alert)
  22. --- service_name: service name
  23. --- x,y,z: coordinates
  24. --- msg: alert message
  25. function vRP.sendServiceAlert(sender, service_name,x,y,z,msg)
  26. local service = services[service_name]
  27. local answered = false
  28. if service then
  29. local players = {}
  30. for k,v in pairs(vRP.rusers) do
  31. local player = vRP.getUserSource(tonumber(k))
  32. -- check user
  33. if vRP.hasPermission(k,service.alert_permission) and player then
  34. table.insert(players,player)
  35. end
  36. end
  37.  
  38. -- send notify and alert to all listening players
  39. for k,v in pairs(players) do
  40. TriggerClientEvent("pNotify:SendNotification", v,{text = " " ..service.alert_notify.. " "..msg, type = "success", queue = "global", timeout = 35000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  41. -- add position for service.time seconds
  42. local bid = vRPclient.addBlip(v,x,y,z,service.blipid,service.blipcolor,"("..service_name..") "..msg)
  43. SetTimeout(service.alert_time*1000,function()
  44. vRPclient._removeBlip(v,bid)
  45. end)
  46.  
  47. -- call request
  48. if sender ~= nil then
  49. async(function()
  50. local ok = vRP.request(v,lang.phone.service.ask_call({service_name, htmlEntities.encode(msg)}), 30)
  51. if ok then -- take the call
  52. if not answered then
  53. -- answer the call
  54. TriggerClientEvent("pNotify:SendNotification", sender,{text = " " ..service.answer_notify, type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  55. vRPclient._setGPS(v,x,y)
  56. answered = true
  57. else
  58. TriggerClientEvent("pNotify:SendNotification", sender,{text = "Opkaldet er allerede blevet taget", type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  59. end
  60. end
  61. end)
  62. end
  63. end
  64. end
  65. end
  66.  
  67. -- send an sms from an user to a phone number
  68. -- return true on success
  69. function vRP.sendSMS(user_id, phone, msg)
  70. if string.len(msg) > cfg.sms_size then -- clamp sms
  71. sms = string.sub(msg,1,cfg.sms_size)
  72. end
  73.  
  74. local identity = vRP.getUserIdentity(user_id)
  75. local dest_id = vRP.getUserByPhone(phone)
  76. if identity and dest_id then
  77. local dest_src = vRP.getUserSource(dest_id)
  78. if dest_src then
  79. local phone_sms = vRP.getPhoneSMS(dest_id)
  80.  
  81. if #phone_sms >= cfg.sms_history then -- remove last sms of the table
  82. table.remove(phone_sms)
  83. end
  84.  
  85. local from = vRP.getPhoneDirectoryName(dest_id, identity.phone).." ("..identity.phone..")"
  86.  
  87. TriggerClientEvent("pNotify:SendNotification", dest_src,{text = "<b style='color:#99CC99'>Modtaget besked fra " ..from.. ":</b> "..msg, type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  88. vRPclient._playAudioSource(dest_src, cfg.sms_sound, 0.5)
  89. table.insert(phone_sms,1,{identity.phone,msg}) -- insert new sms at first position {phone,message}
  90. return true
  91. end
  92. end
  93. end
  94.  
  95. -- call from a user to a phone number
  96. -- return true if the communication is established
  97. function vRP.phoneCall(user_id, phone)
  98. local identity = vRP.getUserIdentity(user_id)
  99. local src = vRP.getUserSource(user_id)
  100. local dest_id = vRP.getUserByPhone(phone)
  101. if identity and dest_id then
  102. local dest_src = vRP.getUserSource(dest_id)
  103. if dest_src then
  104. local to = vRP.getPhoneDirectoryName(user_id, phone).." ("..phone..")"
  105. local from = vRP.getPhoneDirectoryName(dest_id, identity.phone).." ("..identity.phone..")"
  106.  
  107. vRPclient._phoneHangUp(src) -- hangup phone of the caller
  108. vRPclient._phoneCallWaiting(src, dest_src, true) -- make caller to wait the answer
  109.  
  110. -- notify
  111. TriggerClientEvent("pNotify:SendNotification", dest_src,{text = "Ringer til: "..phone, type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  112.  
  113. TriggerClientEvent("pNotify:SendNotification", dest_src,{text = "Ringer fra: "..identity.phone, type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  114.  
  115.  
  116. -- play dialing sound
  117. vRPclient._setAudioSource(src, "vRP:phone:dialing", cfg.dialing_sound, 0.2)
  118. vRPclient._setAudioSource(dest_src, "vRP:phone:dialing", cfg.ringing_sound, 0.2)
  119.  
  120. local ok = false
  121.  
  122. -- send request to called
  123. if vRP.request(dest_src, lang.phone.call.ask({from}), 15) then -- accepted
  124. vRPclient._phoneHangUp(dest_src) -- hangup phone of the receiver
  125. vRPclient._connectVoice(dest_src, "phone", src) -- connect voice
  126. vRPclient._playAnim(src,false,call_seq,false) -- anim
  127. vRPclient._playAnim(dest_src,false,call_seq,false) -- anim
  128. ok = true
  129. else -- refused
  130. TriggerClientEvent("pNotify:SendNotification", dest_src,{text = "Opkaldet blev ik taget: "..phone, type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  131.  
  132. vRPclient._phoneCallWaiting(src, dest_src, false)
  133. end
  134.  
  135. -- remove dialing sound
  136. vRPclient._removeAudioSource(src, "vRP:phone:dialing")
  137. vRPclient._removeAudioSource(dest_src, "vRP:phone:dialing")
  138.  
  139. return ok
  140. end
  141. end
  142. end
  143.  
  144. -- send an smspos from an user to a phone number
  145. -- return true on success
  146. function vRP.sendSMSPos(user_id, phone, x,y,z)
  147. local identity = vRP.getUserIdentity(user_id)
  148. local dest_id = vRP.getUserByPhone(phone)
  149. if identity and dest_id then
  150. local dest_src = vRP.getUserSource(dest_id)
  151. if dest_src then
  152. local from = vRP.getPhoneDirectoryName(dest_id, identity.phone).." ("..identity.phone..")"
  153. vRPclient._playAudioSource(dest_src, cfg.sms_sound, 0.5)
  154. TriggerClientEvent("pNotify:SendNotification", dest_src,{text = "Modtaget sms-position fra: "..from, type = "success", queue = "global", timeout = 4000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  155. -- add position for 5 minutes
  156. local bid = vRPclient.addBlip(dest_src,x,y,z,162,37,from)
  157. SetTimeout(cfg.smspos_duration*1000,function()
  158. vRPclient._removeBlip(dest_src,{bid})
  159. end)
  160.  
  161. return true
  162. end
  163. end
  164. end
  165.  
  166. -- get phone directory data table
  167. function vRP.getPhoneDirectory(user_id)
  168. local data = vRP.getUserDataTable(user_id)
  169. if data then
  170. if data.phone_directory == nil then
  171. data.phone_directory = {}
  172. end
  173.  
  174. return data.phone_directory
  175. else
  176. return {}
  177. end
  178. end
  179.  
  180. -- get directory name by number for a specific user
  181. function vRP.getPhoneDirectoryName(user_id, phone)
  182. local directory = vRP.getPhoneDirectory(user_id)
  183. for k,v in pairs(directory) do
  184. if v == phone then
  185. return k
  186. end
  187. end
  188.  
  189. return "unknown"
  190. end
  191. -- get phone sms tmp table
  192. function vRP.getPhoneSMS(user_id)
  193. local data = vRP.getUserTmpTable(user_id)
  194. if data then
  195. if data.phone_sms == nil then
  196. data.phone_sms = {}
  197. end
  198.  
  199. return data.phone_sms
  200. else
  201. return {}
  202. end
  203. end
  204.  
  205. -- build phone menu
  206. local phone_menu = {name=lang.phone.title(),css={top="75px",header_color="rgba(0,125,255,0.75)"}}
  207.  
  208. local function ch_directory(player,choice)
  209. local user_id = vRP.getUserId(player)
  210. if user_id then
  211. local phone_directory = vRP.getPhoneDirectory(user_id)
  212. -- build directory menu
  213. local menu = {name=choice,css={top="75px",header_color="rgba(0,125,255,0.75)"}}
  214.  
  215. local ch_add = function(player, choice) -- add to directory
  216. local phone = vRP.prompt(player,lang.phone.directory.add.prompt_number(),"")
  217. local name = vRP.prompt(player,lang.phone.directory.add.prompt_name(),"")
  218. name = sanitizeString(tostring(name),sanitizes.text[1],sanitizes.text[2])
  219. phone = sanitizeString(tostring(phone),sanitizes.text[1],sanitizes.text[2])
  220. if #name > 0 and #phone > 0 then
  221. phone_directory[name] = phone -- set entry
  222. TriggerClientEvent("pNotify:SendNotification", player,{text = "Kontakten blev tilføjet", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  223. else
  224. TriggerClientEvent("pNotify:SendNotification", player,{text = "Ugyldig værdi", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  225. end
  226. end
  227.  
  228. local ch_entry = function(player, choice) -- directory entry menu
  229. -- build entry menu
  230. local emenu = {name=choice,css={top="75px",header_color="rgba(0,125,255,0.75)"}}
  231.  
  232. local name = choice
  233. local phone = phone_directory[name] or ""
  234.  
  235. local ch_remove = function(player, choice) -- remove directory entry
  236. phone_directory[name] = nil
  237. vRP.closeMenu(player) -- close entry menu (removed)
  238. end
  239.  
  240. local ch_sendsms = function(player, choice) -- send sms to directory entry
  241. local msg = vRP.prompt(player,lang.phone.directory.sendsms.prompt({cfg.sms_size}),"")
  242. msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
  243. if vRP.sendSMS(user_id, phone, msg) then
  244. TriggerClientEvent("pNotify:SendNotification", player,{text = "Beskeden blev sendt til "..phone, type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  245. else
  246. TriggerClientEvent("pNotify:SendNotification", player,{text = "Nummeret " ..phone.. " er ikke tilgængeligt", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  247. end
  248. end
  249.  
  250. local ch_sendpos = function(player, choice) -- send current position to directory entry
  251. local x,y,z = vRPclient.getPosition(player)
  252. if vRP.sendSMSPos(user_id, phone, x,y,z) then
  253. TriggerClientEvent("pNotify:SendNotification", player,{text = "Beskeden blev sendt til: " ..phone, type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  254. else
  255. TriggerClientEvent("pNotify:SendNotification", player,{text = "Nummeret " ..phone.. " er ikke tilgængeligt", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  256. end
  257. end
  258.  
  259. local ch_call = function(player, choice) -- call player
  260. if not vRP.phoneCall(user_id, phone) then
  261. vRPclient._notify(player,lang.phone.directory.call.not_reached({phone}))
  262. end
  263. end
  264.  
  265. emenu[lang.phone.directory.call.title()] = {ch_call}
  266. emenu[lang.phone.directory.sendsms.title()] = {ch_sendsms}
  267. emenu[lang.phone.directory.sendpos.title()] = {ch_sendpos}
  268. emenu[lang.phone.directory.remove.title()] = {ch_remove}
  269.  
  270. -- nest menu to directory
  271. emenu.onclose = function() ch_directory(player,lang.phone.directory.title()) end
  272.  
  273. -- open mnu
  274. vRP.openMenu(player, emenu)
  275. end
  276.  
  277. menu[lang.phone.directory.add.title()] = {ch_add}
  278.  
  279. for k,v in pairs(phone_directory) do -- add directory entries (name -> number)
  280. menu[k] = {ch_entry,v}
  281. end
  282.  
  283. -- nest directory menu to phone (can't for now)
  284. -- menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
  285.  
  286. -- open menu
  287. vRP.openMenu(player,menu)
  288. end
  289. end
  290.  
  291. local function ch_sms(player, choice)
  292. local user_id = vRP.getUserId(player)
  293. if user_id then
  294. local phone_sms = vRP.getPhoneSMS(user_id)
  295.  
  296. -- build sms list
  297. local menu = {name=choice,css={top="75px",header_color="rgba(0,125,255,0.75)"}}
  298.  
  299. -- add sms
  300. for k,v in pairs(phone_sms) do
  301. local from = vRP.getPhoneDirectoryName(user_id, v[1]).." ("..v[1]..")"
  302. local phone = v[1]
  303. menu["#"..k.." "..from] = {function(player,choice)
  304. -- answer to sms
  305. local msg = vRP.prompt(player,lang.phone.directory.sendsms.prompt({cfg.sms_size}),"")
  306. msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
  307. if vRP.sendSMS(user_id, phone, msg) then
  308. TriggerClientEvent("pNotify:SendNotification", player,{text = "Beskeden blev sendt til: " ..phone, type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  309. else
  310. TriggerClientEvent("pNotify:SendNotification", player,{text = "Nummeret" ..phone.. " er ikke tilgængeligt", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  311. end
  312. end, lang.phone.sms.info({from,htmlEntities.encode(v[2])})}
  313. end
  314.  
  315. -- nest menu
  316. menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
  317.  
  318. -- open menu
  319. vRP.openMenu(player,menu)
  320. end
  321. end
  322.  
  323. -- build service menu
  324. local service_menu = {name=lang.phone.service.title(),css={top="75px",header_color="rgba(0,125,255,0.75)"}}
  325.  
  326. -- nest menu
  327. service_menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
  328.  
  329. local function ch_service_alert(player,choice) -- alert a service
  330. local service = services[choice]
  331. if service then
  332. local x,y,z = vRPclient.getPosition(player)
  333. local msg = vRP.prompt(player,lang.phone.service.prompt(),"")
  334. msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
  335. TriggerClientEvent("pNotify:SendNotification", player,{text = " "..service.notify, type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  336. vRP.sendServiceAlert(player,choice,x,y,z,msg) -- send service alert (call request)
  337. end
  338. end
  339.  
  340. for k,v in pairs(services) do
  341. service_menu[k] = {ch_service_alert}
  342. end
  343.  
  344. local function ch_service(player, choice)
  345. vRP.openMenu(player,service_menu)
  346. end
  347.  
  348. -- build announce menu
  349. local announce_menu = {name=lang.phone.announce.title(),css={top="75px",header_color="rgba(0,125,255,0.75)"}}
  350.  
  351. -- nest menu
  352. announce_menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
  353.  
  354. local function ch_announce_alert(player,choice) -- alert a announce
  355. local announce = announces[choice]
  356. local user_id = vRP.getUserId(player)
  357. if announce and user_id then
  358. if not announce.permission or vRP.hasPermission(user_id,announce.permission) then
  359. local msg = vRP.prompt(player,lang.phone.announce.prompt(),"")
  360. msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
  361. if string.len(msg) > 10 and string.len(msg) < 1000 then
  362. if announce.price <= 0 or vRP.tryPayment(user_id, announce.price) then -- try to pay the announce
  363. TriggerClientEvent("pNotify:SendNotification", player,{text = "Du betalte " ..announce.price.. " Kr.", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  364.  
  365. msg = htmlEntities.encode(msg)
  366. msg = string.gsub(msg, "\n", "<br />") -- allow returns
  367.  
  368. -- send announce to all
  369. local users = vRP.getUsers()
  370. for k,v in pairs(users) do
  371. vRPclient._announce(v,announce.image,msg)
  372. end
  373. else
  374. TriggerClientEvent("pNotify:SendNotification", player,{text = "Ikke nok penge", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  375. end
  376. else
  377. TriggerClientEvent("pNotify:SendNotification", player,{text = "Ugyldig værdi", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  378. end
  379. else
  380. TriggerClientEvent("pNotify:SendNotification", player,{text = "Ikke tilladt", type = "success", queue = "global", timeout = 3000, layout = "bottomCenter",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
  381. end
  382. end
  383. end
  384.  
  385. for k,v in pairs(announces) do
  386. announce_menu[k] = {ch_announce_alert,lang.phone.announce.item_desc({v.price,v.description or ""})}
  387. end
  388.  
  389. local function ch_announce(player, choice)
  390. vRP.openMenu(player,announce_menu)
  391. end
  392.  
  393. local function ch_hangup(player, choice)
  394. vRPclient._phoneHangUp(player)
  395. end
  396.  
  397. phone_menu[lang.phone.directory.title()] = {ch_directory,lang.phone.directory.description()}
  398. phone_menu[lang.phone.sms.title()] = {ch_sms,lang.phone.sms.description()}
  399. phone_menu[lang.phone.service.title()] = {ch_service,lang.phone.service.description()}
  400. phone_menu[lang.phone.announce.title()] = {ch_announce,lang.phone.announce.description()}
  401. phone_menu[lang.phone.hangup.title()] = {ch_hangup,lang.phone.hangup.description()}
  402.  
  403. -- phone menu static builder after 10 seconds
  404. SetTimeout(10000, function()
  405. local menu = vRP.buildMenu("phone", {})
  406. for k,v in pairs(menu) do
  407. phone_menu[k] = v
  408. end
  409. end)
  410.  
  411. -- add phone menu to main menu
  412.  
  413. vRP.registerMenuBuilder("main", function(add, data)
  414. local player = data.player
  415. local user_id = vRP.getUserId(player)
  416. if user_id == nil then
  417. return
  418. end
  419.  
  420. local choices = {}
  421. if vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone")
  422. or vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone2")
  423. or vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone3")
  424. or vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone4")
  425. then
  426. choices[lang.phone.title()] = {function()
  427. vRP.openMenu(player,phone_menu)
  428. end}
  429. end
  430.  
  431. add(choices)
  432. end)
  433.  
  434. function vRP.hasInventoryItem(user_id, idname)
  435. local count = vRP.getInventoryItemAmount(user_id, idname)
  436. return count > 0
  437. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement