view Looting.lua @ 104:9aa2dcbbdc87

Detect when an int is really a date and print it that way
author John@Yosemite-PC
date Sun, 06 May 2012 08:30:15 -0400
parents 0cd1d46e7b66
children
line wrap: on
line source
local bsk=bsk
local _G=_G
local table=table 
local pairs=pairs
local setmetatable=setmetatable
local ipairs=ipairs
local string=string
local sformat=string.format
local tostring=tostring
local type=type
local getn=getn

local event = LibStub("AceEvent-3.0")
bsk.event = event

setfenv(1,bsk)

local isMasterLootEvent = false

local function OpenMasterLootList()
    print("Open!")
    isMasterLootEvent = true
end

local function UpdateMasterLootList()
    print("Update MLL!")
end

local function LootClosed()
    print("Close!")
    if isMasterLootEvent then
        isMasterLootEvent = false -- end the event
        InitiateCloseLooting()
    end
end

local function LootOpened()
    print("Open loot!")
    isMasterLootEvent = false
    local n = _G.GetNumLootItems()
    --for i = 1,n do
    --    _G.LootSlot(i)
    --end
    local items = {}
    local threshold = _G.GetLootThreshold()
    print("threshold: ", threshold)
    for i = 1,n do
        local _,_,_,rarity,_ = _G.GetLootSlotInfo(i)
        print("item: rarity: ",rarity)
        local link = _G.GetLootSlotLink(i)
        if link and rarity >= threshold then
            table.insert(items,{link=link,mlid=i})
            print("Item: ", link, i)
            isMasterLootEvent = true
        end
    end
    if not isMasterLootEvent then return end

    print("Let's get started SKing")
    -- todo: check that I am ML and that I'm an admin!

    if masterLooterIsMe and admin then
        InitiateBeginLoot(items,stateactivelist)

        local chan -- todo: idiom
        if _G.GetNumRaidMembers() > 0 then chan = "RAID" else chan = "PARTY" end
        _G.SendChatMessage("The following items are available -",chan)
        for i,v in pairs(items) do
            _G.SendChatMessage(v.link,chan)
        end
    end
end

function FreeLoot(item,person)
    PrintTable(item)
    PrintTable(person)
    for ci = 1, 40 do
        if _G.GetMasterLootCandidate(ci) == person.textPlain then
            print("GML",item.mlid,ci)
            _G.GiveMasterLoot(item.mlid, ci)
            return true
        end
    end
    
    print("Could not assign loot to ", person.textPlain)
end

function ExpensiveLoot(item,lref)
    if getn(statebids) > 0 then
        if FreeLoot(item,statebids[1]) then
            local chan -- todo: idiom
            if _G.GetNumRaidMembers() > 0 then chan = "RAID" else chan = "PARTY" end
            _G.SendChatMessage(sformat("Awarding %s to %s!",item.link,statebids[1].textPlain),chan)
            --_G.SendChatMessage(sformat("Awarding %s to %s!",item.link,statebids[1].textPlain),"GUILD") -- todo: enable
            lref:SuicidePerson(statebids[1].value)
            InitiateCloseBidding()
        else
            printf("Could not suicide %s for item; they are ineligible or offline",statebids[1].textPlain)
        end
        return
    end
    if getn(staterolls) > 0 then
        if FreeLoot(item,staterolls[1]) then
            local chan -- todo: idiom
            if _G.GetNumRaidMembers() > 0 then chan = "RAID" else chan = "PARTY" end
            _G.SendChatMessage(sformat("Awarding %s to %s!",item.link,staterolls[1].textPlain),chan)
            InitiateCloseBidding()
        else
            printf("Could not suicide %s for item; they are ineligible or offline",staterolls[1].textPlain)
        end
        return
    end
    _G.error("Trying to suicide+loot without bids or rolls")
end

function DirectSuicideLoot(item,person,lref)
    if FreeLoot(item,person) then
        lref:SuicidePerson(person.value)
    else
        printf("Could not suicide %s for item; they are ineligible or offline", person.textPlain)
    end
end

function WhisperReceived(...)
    local _,message,sender = ...
    local senderAction = function(func) 
        local le = PersonList:Select(sender)
        if le then
            local person = ConvertLe2Line(le)
            if person then 
                func(person)
            end
        end
    end

    if state == "bidding" and admin and masterLooterIsMe then
        message = _G.strtrim(message)
        message = _G.strlower(message)
        if message == "bid" then
            senderAction(InitiateBid)
        elseif message == "retract" then
            senderAction(InitiateRetract)
        elseif message == "roll" then
            senderAction(InitiateRollRequest)
        end
    end
end

local statelistener = 
{
    ["StateEvent"] = function(self)
        if state == "bidding" then
            event:RegisterEvent("CHAT_MSG_WHISPER", WhisperReceived)
        else
            --event:UnregisterEvent("CHAT_MSG_WHISPER") -- todo
        end
    end,
}


masterLooter = nil
masterLooterIsMe = false

function UpdateML()
    local lootmethod, masterlooterPartyID, masterlooterRaidID = _G.GetLootMethod()
    if lootmethod == "master" then
        local oldMasterLooter
        oldMasterLooter = masterLooter
        masterLooterIsMe = false
        if masterlooterPartyID and masterlooterPartyID > 0 then
            masterLooter = _G.UnitName("party"..masterlooterPartyID)
        elseif masterlooterRaidID and masterlooterRaidID > 0 then
            masterLooter = _G.UnitName("raid"..masterlooterRaidID)
        else
            masterLooter = _G.UnitName("player") 
            masterLooterIsMe = true
        end

        if masterLooter ~= oldMasterLooter then
            statelistener:StateEvent() -- todo: this isn't how to fire an event!

            if not masterLooterIsMe then
                Comm:RequestCatchup()
            end
        end
    else
        masterLooter = nil
        masterLooterIsMe = false
    end
end

function InitializeLooting()
    event:RegisterEvent("OPEN_MASTER_LOOT_LIST",OpenMasterLootList)
    event:RegisterEvent("UPDATE_MASTER_LOOT_LIST",UpdateMasterLootList)
    event:RegisterEvent("LOOT_CLOSED",LootClosed)
    event:RegisterEvent("LOOT_OPENED",LootOpened)

    event:RegisterEvent("LOOT_SLOT_CLEARED",function(_,index) InitiateLSClear(index) end)
    --event:RegisterEvent("LOOT_SLOT_CHANGED",function() print("LSChanged") end)

    RegisterListenerStateChange(statelistener)
    statelistener:StateEvent()

    UpdateML()
    event:RegisterEvent("PARTY_LOOT_METHOD_CHANGED",UpdateML)
end