Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- basic phone module
- local lang = vRP.lang
- local cfg = module("cfg/phone")
- local htmlEntities = module("lib/htmlEntities")
- local services = cfg.services
- local announces = cfg.announces
- local sanitizes = module("cfg/sanitizes")
- -- api
- local call_seq = {
- {"cellphone@","cellphone_call_in",1},
- {"cellphone@","cellphone_call_listen_base",1}
- }
- -- Send a service alert to all service listeners
- --- sender: a player or nil (optional, if not nil, it is a call request alert)
- --- service_name: service name
- --- x,y,z: coordinates
- --- msg: alert message
- function vRP.sendServiceAlert(sender, service_name,x,y,z,msg)
- local service = services[service_name]
- local answered = false
- if service then
- local players = {}
- for k,v in pairs(vRP.rusers) do
- local player = vRP.getUserSource(tonumber(k))
- -- check user
- if vRP.hasPermission(k,service.alert_permission) and player then
- table.insert(players,player)
- end
- end
- -- send notify and alert to all listening players
- for k,v in pairs(players) do
- 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"}})
- -- add position for service.time seconds
- local bid = vRPclient.addBlip(v,x,y,z,service.blipid,service.blipcolor,"("..service_name..") "..msg)
- SetTimeout(service.alert_time*1000,function()
- vRPclient._removeBlip(v,bid)
- end)
- -- call request
- if sender ~= nil then
- async(function()
- local ok = vRP.request(v,lang.phone.service.ask_call({service_name, htmlEntities.encode(msg)}), 30)
- if ok then -- take the call
- if not answered then
- -- answer the call
- 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"}})
- vRPclient._setGPS(v,x,y)
- answered = true
- else
- 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"}})
- end
- end
- end)
- end
- end
- end
- end
- -- send an sms from an user to a phone number
- -- return true on success
- function vRP.sendSMS(user_id, phone, msg)
- if string.len(msg) > cfg.sms_size then -- clamp sms
- sms = string.sub(msg,1,cfg.sms_size)
- end
- local identity = vRP.getUserIdentity(user_id)
- local dest_id = vRP.getUserByPhone(phone)
- if identity and dest_id then
- local dest_src = vRP.getUserSource(dest_id)
- if dest_src then
- local phone_sms = vRP.getPhoneSMS(dest_id)
- if #phone_sms >= cfg.sms_history then -- remove last sms of the table
- table.remove(phone_sms)
- end
- local from = vRP.getPhoneDirectoryName(dest_id, identity.phone).." ("..identity.phone..")"
- 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"}})
- vRPclient._playAudioSource(dest_src, cfg.sms_sound, 0.5)
- table.insert(phone_sms,1,{identity.phone,msg}) -- insert new sms at first position {phone,message}
- return true
- end
- end
- end
- -- call from a user to a phone number
- -- return true if the communication is established
- function vRP.phoneCall(user_id, phone)
- local identity = vRP.getUserIdentity(user_id)
- local src = vRP.getUserSource(user_id)
- local dest_id = vRP.getUserByPhone(phone)
- if identity and dest_id then
- local dest_src = vRP.getUserSource(dest_id)
- if dest_src then
- local to = vRP.getPhoneDirectoryName(user_id, phone).." ("..phone..")"
- local from = vRP.getPhoneDirectoryName(dest_id, identity.phone).." ("..identity.phone..")"
- vRPclient._phoneHangUp(src) -- hangup phone of the caller
- vRPclient._phoneCallWaiting(src, dest_src, true) -- make caller to wait the answer
- -- notify
- 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"}})
- 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"}})
- -- play dialing sound
- vRPclient._setAudioSource(src, "vRP:phone:dialing", cfg.dialing_sound, 0.2)
- vRPclient._setAudioSource(dest_src, "vRP:phone:dialing", cfg.ringing_sound, 0.2)
- local ok = false
- -- send request to called
- if vRP.request(dest_src, lang.phone.call.ask({from}), 15) then -- accepted
- vRPclient._phoneHangUp(dest_src) -- hangup phone of the receiver
- vRPclient._connectVoice(dest_src, "phone", src) -- connect voice
- vRPclient._playAnim(src,false,call_seq,false) -- anim
- vRPclient._playAnim(dest_src,false,call_seq,false) -- anim
- ok = true
- else -- refused
- 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"}})
- vRPclient._phoneCallWaiting(src, dest_src, false)
- end
- -- remove dialing sound
- vRPclient._removeAudioSource(src, "vRP:phone:dialing")
- vRPclient._removeAudioSource(dest_src, "vRP:phone:dialing")
- return ok
- end
- end
- end
- -- send an smspos from an user to a phone number
- -- return true on success
- function vRP.sendSMSPos(user_id, phone, x,y,z)
- local identity = vRP.getUserIdentity(user_id)
- local dest_id = vRP.getUserByPhone(phone)
- if identity and dest_id then
- local dest_src = vRP.getUserSource(dest_id)
- if dest_src then
- local from = vRP.getPhoneDirectoryName(dest_id, identity.phone).." ("..identity.phone..")"
- vRPclient._playAudioSource(dest_src, cfg.sms_sound, 0.5)
- 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"}})
- -- add position for 5 minutes
- local bid = vRPclient.addBlip(dest_src,x,y,z,162,37,from)
- SetTimeout(cfg.smspos_duration*1000,function()
- vRPclient._removeBlip(dest_src,{bid})
- end)
- return true
- end
- end
- end
- -- get phone directory data table
- function vRP.getPhoneDirectory(user_id)
- local data = vRP.getUserDataTable(user_id)
- if data then
- if data.phone_directory == nil then
- data.phone_directory = {}
- end
- return data.phone_directory
- else
- return {}
- end
- end
- -- get directory name by number for a specific user
- function vRP.getPhoneDirectoryName(user_id, phone)
- local directory = vRP.getPhoneDirectory(user_id)
- for k,v in pairs(directory) do
- if v == phone then
- return k
- end
- end
- return "unknown"
- end
- -- get phone sms tmp table
- function vRP.getPhoneSMS(user_id)
- local data = vRP.getUserTmpTable(user_id)
- if data then
- if data.phone_sms == nil then
- data.phone_sms = {}
- end
- return data.phone_sms
- else
- return {}
- end
- end
- -- build phone menu
- local phone_menu = {name=lang.phone.title(),css={top="75px",header_color="rgba(0,125,255,0.75)"}}
- local function ch_directory(player,choice)
- local user_id = vRP.getUserId(player)
- if user_id then
- local phone_directory = vRP.getPhoneDirectory(user_id)
- -- build directory menu
- local menu = {name=choice,css={top="75px",header_color="rgba(0,125,255,0.75)"}}
- local ch_add = function(player, choice) -- add to directory
- local phone = vRP.prompt(player,lang.phone.directory.add.prompt_number(),"")
- local name = vRP.prompt(player,lang.phone.directory.add.prompt_name(),"")
- name = sanitizeString(tostring(name),sanitizes.text[1],sanitizes.text[2])
- phone = sanitizeString(tostring(phone),sanitizes.text[1],sanitizes.text[2])
- if #name > 0 and #phone > 0 then
- phone_directory[name] = phone -- set entry
- 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"}})
- else
- 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"}})
- end
- end
- local ch_entry = function(player, choice) -- directory entry menu
- -- build entry menu
- local emenu = {name=choice,css={top="75px",header_color="rgba(0,125,255,0.75)"}}
- local name = choice
- local phone = phone_directory[name] or ""
- local ch_remove = function(player, choice) -- remove directory entry
- phone_directory[name] = nil
- vRP.closeMenu(player) -- close entry menu (removed)
- end
- local ch_sendsms = function(player, choice) -- send sms to directory entry
- local msg = vRP.prompt(player,lang.phone.directory.sendsms.prompt({cfg.sms_size}),"")
- msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
- if vRP.sendSMS(user_id, phone, msg) then
- 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"}})
- else
- 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"}})
- end
- end
- local ch_sendpos = function(player, choice) -- send current position to directory entry
- local x,y,z = vRPclient.getPosition(player)
- if vRP.sendSMSPos(user_id, phone, x,y,z) then
- 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"}})
- else
- 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"}})
- end
- end
- local ch_call = function(player, choice) -- call player
- if not vRP.phoneCall(user_id, phone) then
- vRPclient._notify(player,lang.phone.directory.call.not_reached({phone}))
- end
- end
- emenu[lang.phone.directory.call.title()] = {ch_call}
- emenu[lang.phone.directory.sendsms.title()] = {ch_sendsms}
- emenu[lang.phone.directory.sendpos.title()] = {ch_sendpos}
- emenu[lang.phone.directory.remove.title()] = {ch_remove}
- -- nest menu to directory
- emenu.onclose = function() ch_directory(player,lang.phone.directory.title()) end
- -- open mnu
- vRP.openMenu(player, emenu)
- end
- menu[lang.phone.directory.add.title()] = {ch_add}
- for k,v in pairs(phone_directory) do -- add directory entries (name -> number)
- menu[k] = {ch_entry,v}
- end
- -- nest directory menu to phone (can't for now)
- -- menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
- -- open menu
- vRP.openMenu(player,menu)
- end
- end
- local function ch_sms(player, choice)
- local user_id = vRP.getUserId(player)
- if user_id then
- local phone_sms = vRP.getPhoneSMS(user_id)
- -- build sms list
- local menu = {name=choice,css={top="75px",header_color="rgba(0,125,255,0.75)"}}
- -- add sms
- for k,v in pairs(phone_sms) do
- local from = vRP.getPhoneDirectoryName(user_id, v[1]).." ("..v[1]..")"
- local phone = v[1]
- menu["#"..k.." "..from] = {function(player,choice)
- -- answer to sms
- local msg = vRP.prompt(player,lang.phone.directory.sendsms.prompt({cfg.sms_size}),"")
- msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
- if vRP.sendSMS(user_id, phone, msg) then
- 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"}})
- else
- 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"}})
- end
- end, lang.phone.sms.info({from,htmlEntities.encode(v[2])})}
- end
- -- nest menu
- menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
- -- open menu
- vRP.openMenu(player,menu)
- end
- end
- -- build service menu
- local service_menu = {name=lang.phone.service.title(),css={top="75px",header_color="rgba(0,125,255,0.75)"}}
- -- nest menu
- service_menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
- local function ch_service_alert(player,choice) -- alert a service
- local service = services[choice]
- if service then
- local x,y,z = vRPclient.getPosition(player)
- local msg = vRP.prompt(player,lang.phone.service.prompt(),"")
- msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
- 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"}})
- vRP.sendServiceAlert(player,choice,x,y,z,msg) -- send service alert (call request)
- end
- end
- for k,v in pairs(services) do
- service_menu[k] = {ch_service_alert}
- end
- local function ch_service(player, choice)
- vRP.openMenu(player,service_menu)
- end
- -- build announce menu
- local announce_menu = {name=lang.phone.announce.title(),css={top="75px",header_color="rgba(0,125,255,0.75)"}}
- -- nest menu
- announce_menu.onclose = function(player) vRP.openMenu(player, phone_menu) end
- local function ch_announce_alert(player,choice) -- alert a announce
- local announce = announces[choice]
- local user_id = vRP.getUserId(player)
- if announce and user_id then
- if not announce.permission or vRP.hasPermission(user_id,announce.permission) then
- local msg = vRP.prompt(player,lang.phone.announce.prompt(),"")
- msg = sanitizeString(msg,sanitizes.text[1],sanitizes.text[2])
- if string.len(msg) > 10 and string.len(msg) < 1000 then
- if announce.price <= 0 or vRP.tryPayment(user_id, announce.price) then -- try to pay the announce
- 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"}})
- msg = htmlEntities.encode(msg)
- msg = string.gsub(msg, "\n", "<br />") -- allow returns
- -- send announce to all
- local users = vRP.getUsers()
- for k,v in pairs(users) do
- vRPclient._announce(v,announce.image,msg)
- end
- else
- 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"}})
- end
- else
- 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"}})
- end
- else
- 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"}})
- end
- end
- end
- for k,v in pairs(announces) do
- announce_menu[k] = {ch_announce_alert,lang.phone.announce.item_desc({v.price,v.description or ""})}
- end
- local function ch_announce(player, choice)
- vRP.openMenu(player,announce_menu)
- end
- local function ch_hangup(player, choice)
- vRPclient._phoneHangUp(player)
- end
- phone_menu[lang.phone.directory.title()] = {ch_directory,lang.phone.directory.description()}
- phone_menu[lang.phone.sms.title()] = {ch_sms,lang.phone.sms.description()}
- phone_menu[lang.phone.service.title()] = {ch_service,lang.phone.service.description()}
- phone_menu[lang.phone.announce.title()] = {ch_announce,lang.phone.announce.description()}
- phone_menu[lang.phone.hangup.title()] = {ch_hangup,lang.phone.hangup.description()}
- -- phone menu static builder after 10 seconds
- SetTimeout(10000, function()
- local menu = vRP.buildMenu("phone", {})
- for k,v in pairs(menu) do
- phone_menu[k] = v
- end
- end)
- -- add phone menu to main menu
- vRP.registerMenuBuilder("main", function(add, data)
- local player = data.player
- local user_id = vRP.getUserId(player)
- if user_id == nil then
- return
- end
- local choices = {}
- if vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone")
- or vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone2")
- or vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone3")
- or vRP.hasPermission(user_id, "player.phone") and vRP.hasInventoryItem(user_id, "phone4")
- then
- choices[lang.phone.title()] = {function()
- vRP.openMenu(player,phone_menu)
- end}
- end
- add(choices)
- end)
- function vRP.hasInventoryItem(user_id, idname)
- local count = vRP.getInventoryItemAmount(user_id, idname)
- return count > 0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement