Gnomish Raffle Machine

Pilus edited this page Aug 11, 2015 · 1 revision
Clone this wiki locally

local obj = GHI_SlashCmd("raffle")

local ticketCount = GHI_Load("ticketTable") or {0,}

local entrants = GHI_Load("entrantTable") or {{},}

local prevWinners = GHI_Load("winnerTable") or {{},}

local numRaffles = GHI_Load("numRaffles") or 1

local currentRaffle = 1

``

--GUI information

local raffleGUI = CreateFrame("Frame",nil,UIParent)

raffleGUI:SetSize(512,180)

raffleGUI:SetScale(0.75)

raffleGUI:SetPoint("CENTER",UIParent,"CENTER",0,-50)

local backdrop = {

bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",

edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",

tile = true,

tileSize = 32,

edgeSize = 32,

insets = {

left = 11,

right = 12,

top = 12,

bottom = 11

}

}

raffleGUI:SetBackdrop(backdrop)

raffleGUI:SetMovable(true)

raffleGUI:EnableMouse(true)

raffleGUI:RegisterForDrag("LeftButton")

raffleGUI:SetScript("OnDragStart", raffleGUI.StartMoving)

raffleGUI:SetScript("OnDragStop", raffleGUI.StopMovingOrSizing)

---- Now we begin adding the elements to the window ----window titleraffleGUI.title = raffleGUI:CreateFontString()

raffleGUI.title:SetPoint("TOPLEFT",raffleGUI,"TOPLEFT",32,-16)

raffleGUI.title:SetFontObject("GameFontGreen")

raffleGUI.title:SetText("Gnomish Raffle Machine")

raffleGUI.title:SetSize(raffleGUI.title:GetStringWidth(),raffleGUI.title:GetStringHeight())

----Raffle Number Box

raffleGUI.raffNumBox = CreateFrame("EditBox","raffBox",raffleGUI,"InputBoxTemplate")

raffleGUI.raffNumBox:SetSize(64,32)

raffleGUI.raffNumBox:SetAutoFocus(false)

raffleGUI.raffNumBox:SetNumeric(true)

raffleGUI.raffNumBox:SetPoint("LEFT",raffleGUI.title,"RIGHT",8,0)

raffleGUI.raffNumBox:SetText("1")

raffleGUI.raffNumBox:SetScript("OnEnter", function()

raffleGUI.help:SetText("This is the number of the raffle being entered in.",1,1,1,true)

end )

raffleGUI.raffNumBox:SetScript("OnLeave", function()

raffleGUI.help:SetText("")

end )

----entrant

titleraffleGUI.entryTit = raffleGUI:CreateFontString()

raffleGUI.entryTit:SetPoint("TOPRIGHT",raffleGUI,"TOPRIGHT",-96,-16)

raffleGUI.entryTit:SetFontObject("GameFontNormal")

raffleGUI.entryTit:SetText("Raffle Entries")

raffleGUI.entryTit:SetSize(raffleGUI.entryTit:GetStringWidth(),raffleGUI.entryTit:GetStringHeight())

----help text frame

raffleGUI.help = raffleGUI:CreateFontString()

raffleGUI.help:SetPoint("BOTTOMLEFT",raffleGUI,"BOTTOMLEFT",16,8)

raffleGUI.help:SetFontObject("GameFontNormalLeftYellow")

raffleGUI.help:SetTextHeight(12)

raffleGUI.help:SetText("")

raffleGUI.help:SetSize(480,32)

----close button

raffleGUI.closeButton = CreateFrame("Button",nil,raffleGUI,"UIPanelCloseButton")

raffleGUI.closeButton:SetSize(32,32)

raffleGUI.closeButton:SetPoint("TOPRIGHT",raffleGUI,"TOPRIGHT",-4,-4)

----Roll Button

raffleGUI.rollButton = CreateFrame("Button",nil,raffleGUI,"UIPanelButtonTemplate")

raffleGUI.rollButton:SetPoint("LEFT",raffleGUI,"LEFT",16,32)

raffleGUI.rollButton:SetWidth(64)

raffleGUI.rollButton:SetHeight(32)

raffleGUI.rollButton:SetText("Roll")

``

raffleGUI.rollButton:SetScript("OnEnter", function()

raffleGUI.help:SetText("Roll a random number between 1 and the maximum number of tickets. It will not pick the same ticket twice.")

end )

raffleGUI.rollButton:SetScript("OnLeave", function()

raffleGUI.help:SetText("")

end )

``

----Clear Button

raffleGUI.clearButton = CreateFrame("Button",nil,raffleGUI,"UIPanelButtonTemplate")

raffleGUI.clearButton:SetPoint("LEFT",raffleGUI,"LEFT",16,0)

raffleGUI.clearButton:SetWidth(64)

raffleGUI.clearButton:SetHeight(32)

raffleGUI.clearButton:SetText("Clear")

raffleGUI.clearButton:SetScript("OnEnter", function()

raffleGUI.help:SetText("Clear the current raffle, removing all ticket numbers and entrant names.")

end )

raffleGUI.clearButton:SetScript("OnLeave", function()

raffleGUI.help:SetText("")

end )

``

----Ticket Button

raffleGUI.tickButton = CreateFrame("Button",nil,raffleGUI,"UIPanelButtonTemplate")

raffleGUI.tickButton:SetPoint("LEFT",raffleGUI,"LEFT",16,-32)

raffleGUI.tickButton:SetWidth(64)

raffleGUI.tickButton:SetHeight(32)

raffleGUI.tickButton:SetText("Ticket")

raffleGUI.tickButton:SetScript("OnEnter", function()

raffleGUI.help:SetText("Print a ticket and enter the target's name in the raffle.",1,1,1,true)

end )

raffleGUI.tickButton:SetScript("OnLeave", function()

raffleGUI.help:SetText("")

end )

----Raffle Number Box

raffleGUI.tickNumBox = CreateFrame("EditBox","tickBox",raffleGUI,"InputBoxTemplate")

raffleGUI.tickNumBox:SetSize(48,32)

raffleGUI.tickNumBox:SetAutoFocus(false)

raffleGUI.tickNumBox:SetNumeric(true)

raffleGUI.tickNumBox:SetPoint("LEFT",raffleGUI.tickButton,"RIGHT",8,0)

raffleGUI.tickNumBox:SetText("1")

raffleGUI.tickNumBox:SetScript("OnEnter", function()

raffleGUI.help:SetText("Enter the amount of tickets to print.",1,1,1,true)

end )

raffleGUI.tickNumBox:SetScript("OnLeave", function()

raffleGUI.help:SetText("")

end )

``

----- Entry List -----

--- This is a scrollable list of all the entrants with their numbers.

local raffleEntries = CreateFrame("ScrollFrame","EntryList",raffleGUI,"UIPanelScrollFrameTemplate")

raffleEntries:SetSize(196,raffleGUI:GetHeight()-48)

raffleEntries:SetPoint("BOTTOMRIGHT",raffleGUI,"BOTTOMRIGHT",-34,10)

raffleEntries.ScrollBar:SetThumbTexture("Interface\\Buttons\\UI-ScrollBar-Knob")

raffleEntries.ScrollBar:SetBackdrop({

bgFile = "Interface\\Buttons\\UI-SliderBar-Background",

edgeFile = "Interface\\Buttons\\UI-SliderBar-Border",

tile = true, tileSize = 8, edgeSize = 8,

insets = { left = 3, right = 3, top = 6, bottom = 6 }})

raffleEntries.ScrollBar:SetMinMaxValues(1,100)

raffleEntries.ScrollBar:SetValue(1)

raffleEntries.ScrollBar:SetValueStep(1)

raffleEntries.ScrollBar:SetScript("OnValueChanged",function(self,value)

self:GetParent():SetVerticalScroll(value)

end)

``

--- list contianer frame ---

raffleEntries.list = CreateFrame("Frame",nil,raffleEntries)

raffleEntries.list:SetPoint("TOP",raffleEntries,"TOP")

raffleEntries.list:SetWidth(raffleEntries:GetWidth())

if ticketCount[currentRaffle] >=1 then

raffleEntries.list:SetHeight(10*ticketCount[currentRaffle])

else

raffleEntries.list:SetHeight(10)

end

raffleEntries:SetScrollChild(raffleEntries.list)

----entry list text

raffleEntries.list.text = raffleEntries.list:CreateFontString()

local listTextSize = 13

raffleEntries.list.text:SetParent(raffleEntries.list)

raffleEntries.list.text:SetPoint("TOP",raffleEntries.list,"TOP",0,-16)

raffleEntries.list.text:SetFontObject("GameFontHighlightLeft")

raffleEntries.list.text:SetJustifyV("TOP")

raffleEntries.list.text:SetText("None")

raffleEntries.list.text:SetWordWrap(true)

raffleEntries.list.text:SetWidth(raffleEntries.list:GetWidth())

if ticketCount[currentRaffle] >= 1 then

raffleEntries.list.text:SetHeight(listTextSize*ticketCount[currentRaffle])

else

raffleEntries.list.text:SetHeight(listTextSize)

end

``

----functions

local showGUI = function()

if not raffleGUI:IsShown() then

raffleGUI:Show()

else

raffleGUI:Show()

end

end

-- This function updates the list of entrants.

local updateEntrants = function()

local text = ""

for i,v in pairs(entrants[currentRaffle]) do

local entrytext = i..") "..v.."\n"

text = text..entrytext

end

raffleEntries.list.text:SetText(text)

raffleEntries.list.text:SetHeight(listTextSize*ticketCount[currentRaffle])

raffleEntries.list.text:SetHeight(listTextSize*ticketCount[currentRaffle])

end

-- This function clears all data on a raffle to start fresh

local clearRaff = function()

ticketCount[currentRaffle] = 0

GHI_Save("ticketTable",ticketCount)

entrants[currentRaffle] = {}

GHI_Save("entrantTable",entrants)

prevWinners[currentRaffle] = {}

GHI_Save("winnerTable",prevWinners)

updateEntrants()

print(GHI_ColorString("Ticket Count has been reset and all entries and winners cleared",1,0,0))

end

-- This function prints a ticket.

local ticket = function()

local person = UnitName("target")

local today = date("%b %d, %Y")

local tickObj = GHI_CreateItem()

if person then

local ticksMade = tonumber(raffleGUI.tickNumBox:GetText())

local i = 1

local currTick = ticketCount[currentRaffle]

while i <= ticksMade do

ticketCount[currentRaffle] = ticketCount[currentRaffle]+1

GHI_Save("ticketTable",ticketCount)

table.insert(entrants[currentRaffle],ticketCount[currentRaffle], person)

GHI_Save("entrantTable",entrants)

i = i+1

end

i = 1

print("Current Number of Tickets: "..GHI_ColorString(ticketCount[currentRaffle],1,0.25,0.25))

---- Set up ticket info to be made

tickObj.SetName("Harbor Market Raffle Ticket")

tickObj.SetIcon("Interface\\Icons\\INV_Misc_PunchCards_Blue")

tickObj.SetQuality(1)

tickObj.SetWhite1("This ticket belongs to "..person..".")

tickObj.SetWhite2("Good for "..today..".")

if ticksMade > 1 then

tickObj.SetComment("Ticket Numbers: "..(currTick + 1) .." - "..(currTick + ticksMade))

else

tickObj.SetComment("Ticket Number: "..ticketCount[currentRaffle])

end

tickObj.SetStackSize(1)

tickObj.Save()

updateEntrants()

if ticksMade > 1 then

GHI_SendChatMessage("Your ticket numbers are "..(currTick + 1) .." - "..(currTick + ticksMade).." in raffle number "..currentRaffle..". If you forget this, don't worry we have a record.","WHISPER",nil,person,6)

else

GHI_SendChatMessage("Your ticket number is "..ticketCount[currentRaffle].." in raffle number "..currentRaffle..". If you forget this, don't worry we have a record.","WHISPER",nil,person,6)

end

raffleGUI.tickNumBox:SetText("1")

else

print(GHI_ColorString("You must target someone to enter them in the raffle.",1,0,0))

end

end

-- This function picks a winner

local roll = function()

local winner = random(1,ticketCount[currentRaffle])

local sound = "Sound\\Interface\\UI_PetBattle_Victory02.ogg"

for i,v in pairs(prevWinners[currentRaffle]) do

if winner == v then

print("Ticket: "..winner.." was chosen, but they already won, choosing a new ticket.")

winner = random(1,ticketCount[currentRaffle])

end

end

table.insert(prevWinners[currentRaffle],winner)

GHI_Save("winnerTable",prevWinners)

GHI_Emote("presses some buttons on a Gnomish device.",1)

GHI_Emote("watches as the machine randomly selects a winner and spits out the ticket.",5)

GHI_SendChatMessage("Congratulations, you won the raffle.","WHISPER",nil,entrants[currentRaffle][winner],6)

GHI_PlayAreaSound(sound, 50, 9)

GHI_SendChatMessage("And the winning ticket number is "..winner.."!","YELL",nil,nil,10)

GHI_SendChatMessage("Congratulations to "..entrants[currentRaffle][winner].."!","YELL",nil,nil,16)

end

``

---- slash commands

obj.SetDefaultFunc(showGUI)

``

----button commands

raffleGUI.tickButton:SetScript("OnClick",ticket)

raffleGUI.rollButton:SetScript("OnClick",roll)

raffleGUI.clearButton:SetScript("OnClick",clearRaff)

``

raffleGUI:SetScript("OnShow",updateEntrants)

raffleGUI.raffNumBox:SetScript("OnTextChanged",function() currentRaffle = tonumber(raffleGUI.raffNumBox:GetText()) updateEntrants() end)

----final initalization

if ticketCount[currentRaffle] > 0 then

print(GHI_ColorString("Welcome to the Gnomish Raffle Machine.",0,0.5,1))

print(GHI_ColorString("There is a raffle currently in progress.",1,0.25,0.25))

print("Type "..GHI_ColorString("/raffle",0.5,0.5,0).. " to hide or show the GUI.")

else

print(GHI_ColorString("Welcome to the Gnomish Raffle Machine.",0,0.5,1))

print("To start a new raffle and clear previous entries type "..GHI_ColorString("/raffle clear",0.5,0.5,0))

print("Type "..GHI_ColorString("/raffle",0.5,0.5,0).." to hide or show the GUI.")

end

updateEntrants()

raffleGUI:Show() -- Then show the GUI