Skip to content

Instantly share code, notes, and snippets.

@dbjorkholm
Created February 1, 2016 18:53
Show Gist options
  • Save dbjorkholm/1b9e08b53d7f90406d37 to your computer and use it in GitHub Desktop.
Save dbjorkholm/1b9e08b53d7f90406d37 to your computer and use it in GitHub Desktop.
local config = {
rewards = {
{itemId = 2195, count = 1},
{itemId = 2472, count = 1},
{itemId = 2493, count = 1}
}
}
function onTime(interval)
local players = Game.getPlayers()
if #players == 0 then
return true
end
local winner
do
local i = 0
while i < #players do
local tmpPlayer = players[math.random(#players)]
if not tmpPlayer:getGroup():getAccess() then
winner = tmpPlayer
break
end
i = i + 1
end
end
if not winner then
return true
end
local item = config.rewards[math.random(#config.rewards)]
local rewardItem = Game.createItem(item.itemId, item.count)
if not rewardItem then
return true
end
if winner:addItemEx(rewardItem) ~= RETURNVALUE_NOERROR then
return true
end
Game.broadcastMessage(string.format("%s won %s! Congratulations!", winner:getName(), ItemType(item.itemId):getNameDescription(item.count)), MESSAGE_STATUS_WARNING)
return true
end
function ItemType.getNameDescription(self, count)
local count = count or 1
local description = ""
if self:isRune() then
description = string.format("%s %s (%d charges)", self:getArticle(), self:getName(), count)
elseif self:isStackable() and count > 1 then
description = string.format("%d %s", count, self:getPluralName())
elseif self:getArticle() ~= "" then
description = string.format("%s %s", self:getArticle(), self:getName())
else
description = self:getName()
end
return description
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment