Files
xmas/s_script.lua
2020-12-19 12:59:49 +01:00

192 lines
6.3 KiB
Lua

g_data = {}
g_data["presents"] = {}
g_data["leaders"] = {}
g_data["gameOptions"] = {}
g_data["finished"] = false
g_data["event"] = false
g_data["validObjects"] = {}
function cacheGameOptions()
g_data["gameOptions"] = {}
g_data["gameOptions"].gamemode = getString('gamemode','DM')
g_data["gameOptions"].minplayers = getNumber('minplayers',3)
g_data["gameOptions"].giftfactor = getNumber('giftfactor',1)
g_data["gameOptions"].spawnprobability = getNumber('spawnprobability',10)
g_data["gameOptions"].eventstart = getNumber('eventstart',18)
g_data["gameOptions"].eventduration = getNumber('eventduration',2)
g_data["gameOptions"].endmonth = getNumber('endmonth',1)
g_data["gameOptions"].endday = getNumber('endday',1)
g_data["gameOptions"].endhour = getNumber('endhour',0)
g_data["gameOptions"].endminute = getNumber('endminute',0)
if g_data["gameOptions"].gamemode == 'DM' then
g_data["validObjects"] = {
[3458]=true,
[8558]=true,
[8838]=true,
[8947]=true,
[6959]=true,
[18450]=true,
[7657]=true,
[9623]=true}
elseif g_data["gameOptions"].gamemode == 'DD' then
g_data["validObjects"] = {
[3458]=true,
[8558]=true,
[8838]=true,
[3095]=true,
[13649]=true,
[1482]=true,
[12857]=true}
end
end
function spawnPresents()
local playerCount = #Element.getAllByType("player")
local probability
if playerCount < g_data["gameOptions"].minplayers then
outputDebugString("[xmas]: No presents this time. Not enough players.")
return
else
probability = math.random(0, g_data["gameOptions"].spawnprobability)
end
local objects = {}
for index, object in pairs(Element.getAllByType("object")) do
if g_data["validObjects"][object.model] then
if isElementVisible(object) then
if isElementFlat(object) then --only use flat objects
table.insert(objects, object)
end
end
end
end
outputDebugString("[xmas]: " .. #objects .. " possible places with " .. probability .. "% chance.")
local count = 0
for i=1, math.floor(#objects*(probability/100)) do
-- limit amount of presents spawned based on playercount
if count > (playerCount*2) then
break
end
-- get random object from table
local index = math.random(#objects)
local object = objects[index]
-- model specific offsets with randomness in present placement
local offset = getModelOffset(object)
-- spawn present and create colsphere for hit detection
local v = object.position
local o = Object(1974, v.x+offset.x, v.y+offset.y, v.z+offset.z, 0,0,0,true)
o:setCollisionsEnabled(false)
local c = ColShape.Sphere(v.x+offset.x, v.y+offset.y, v.z+offset.z, 3.0)
table.insert(g_data["presents"], {o, c})
count = count + 1
-- remove object from list
table.remove(objects, index)
end
if #g_data["presents"] == 0 then
outputChatBox("* #ffffff"..#g_data["presents"] .. " presents spawned on the map. Blame Ryan... #ff0000*", root,255,0,0,true)
else
outputChatBox("* #ffffff"..#g_data["presents"] .. " presents spawned on the map. Try to find them all #ff0000*", root,255,0,0,true)
end
outputDebugString("[xmas]: " .. #g_data["presents"] .. " presents spawned.")
end
function rotatePresents()
for i, v in ipairs(g_data["presents"]) do
local pos = v[1].position
moveObject(v[1], 5000, pos.x, pos.y, pos.z, 0, 0, 360)
end
end
setTimer(rotatePresents, 5000, 0)
function addPoints(pts, player)
--SKC specific!!! Just tested for DD
--[[local result = executeSQLQuery( "SELECT score FROM stats WHERE LOWER(nick)=?" , string.lower(getAccountName(getPlayerAccount(player))) )
if result == false or #result == 0 then return end
local pts_before = result[1]["score"]
executeSQLQuery( "UPDATE stats SET score=? WHERE LOWER(nick)=?", pts_before+pts, string.lower(getAccountName(getPlayerAccount(player))) )
exports.dd_player_stats:updatePlayerVisualPoints(player)]]
end
function checkTime()
local t = getRealTime()
local hour = t.hour
local minute = t.minute
local day = t.monthday
local month = t.month
outputDebugString("[xmas]: "..month..' - '..day..' - '..hour..':'..minute)
-- check if competition ended
if month == (g_data["gameOptions"].endmonth-1) then
if day >= g_data["gameOptions"].endday then
if hour >= g_data["gameOptions"].endhour then
if minute >= g_data["gameOptions"].endminute and not g_data["finished"] then
outputChatBox("* #ffffffThe Christmas Competition has now ended! Thanks to all those who took part #ff0000*", root, 255,0,0, true)
--outputChatBox("* #ffffffThe Top 5 will be contacted shortly about the prizes #ff0000*", root, 255,0,0, true)
g_data["finished"] = true
end
end
end
end
-- check if it's event time
if hour >= g_data["gameOptions"].eventstart and hour < (g_data["gameOptions"].eventstart+g_data["gameOptions"].eventduration) then
if g_data["event"] == false then
outputDebugString("[xmas]: Event started.")
outputChatBox("* #ffffffThe Christmas Competition has started for "..g_data["gameOptions"].eventduration.." hours. Collect as many presents as possible #ff0000*", root, 255,0,0, true)
end
g_data["event"] = true
else
if g_data["event"] then
outputDebugString("[xmas]: Event ended.")
outputChatBox("* #ffffffThe Christmas Competition has ended for today. #ff0000*", root, 255,0,0, true)
end
g_data["event"] = false
end
-- check if it's about to be event time
if hour == (g_data["gameOptions"].eventstart-1) and minute == 55 then
outputChatBox("* #ffffffThe Christmas Competition will start in 5 minutes #ff0000*", root, 255,0,0, true)
end
end
setTimer(checkTime, 50000, 0)
function updateTop(player)
if player then
outputChatBox(" * #ffffff" ..player.name.. "#ffffff updated the top 3", root,255,0,0,true)
end
g_data["leaders"] = {}
local result = executeSQLQuery([[
SELECT `name`, `presents`
FROM `xmas`
WHERE `presents`>0
ORDER BY `presents` DESC
LIMIT 10]])
for i,v in ipairs(result) do
local prez = v.presents
local name = v.name
table.insert(g_data["leaders"], {name, prez})
end
triggerClientEvent("updateTables", resourceRoot, g_data["leaders"])
end
function showTop()
outputChatBox("** #ffffffChristmas Event Top 3 #ff0000**", root, 255,0,0,true)
outputChatBox(" ")
for i, v in ipairs(g_data["leaders"]) do
if v[1] and i < 11 then
outputChatBox(i .. ". #ffffff" .. v[1] .. " - #ff0000" ..v[2], root, 255,0,0,true)
end
end
outputChatBox("** #ffffffEnds 1st January 2021 #ff0000**", root, 255,0,0,true)
end