WoWInterface SVN GuildLottery

[/] [GLotteryData.lua] - Rev 2

Compare with Previous | Blame | View Log

GL_Data = {};
GL_DataInitialized=false;

local L = LibStub("AceLocale-3.0"):GetLocale("Glottery");

function GL_DataInitialize()
        if GL_Data == nil then
                GL_Data={};
        end                             
        if GL_Data.LotteryInfo ==               nil then        GL_Data.LotteryInfo = {};                       end
        if GL_Data.NextLotteryTicket==  nil then        GL_Data.NextLotteryTicket=0;            end
        if GL_Data.StartGold==                  nil then        GL_Data.StartGold=0;                            end
        if GL_Data.LotteryTicketSerie== nil then        GL_Data.LotteryTicketSerie=1000;        end
        if GL_Data.MaxLotteryTickets==  nil then        GL_Data.MaxLotteryTickets=20;           end
        if GL_Data.LotteryTicketPrice== nil then        GL_Data.LotteryTicketPrice=10000;       end
        if GL_Data.LotteryDate==                nil then        GL_Data.LotteryDate=0;                          end

        if GL_Data.RaffleInfo ==                nil then        GL_Data.RaffleInfo = {};                        end
        if GL_Data.NextRaffleTicket==   nil then        GL_Data.NextRaffleTicket=0;                     end
        if GL_Data.RaffleTicketSerie==  nil then        GL_Data.RaffleTicketSerie=1000;         end
        if GL_Data.MaxRaffleTickets==   nil then        GL_Data.MaxRaffleTickets=20;            end
        if GL_Data.RaffleDate==                 nil then        GL_Data.RaffleDate=0;                           end
        if GL_Data.RaffleTicketPrice==  nil then        GL_Data.RaffleTicketPrice=10000;        end
        
        if GL_Data.SecondPrize==                nil then        GL_Data.SecondPrize=20;                         end
        if GL_Data.GuildCut==                   nil then        GL_Data.GuildCut=0;                             end
        if GL_Data.LotteryName==                nil then        GL_Data.LotteryName=L["UI_TITLE_LOTTERY"]; end
        if GL_Data.RaffleName==                 nil then        GL_Data.RaffleName=L["UI_TITLE_RAFFLE"]; end
        
        if GL_Data.RaffleItemOne==              nil then        GL_Data.RaffleItemOne='0';                      end
        if GL_Data.RaffleItemTwo==              nil then        GL_Data.RaffleItemTwo='0';                      end
        if GL_Data.RaffleItemThree==    nil then        GL_Data.RaffleItemThree='0';            end
        if GL_Data.MultipleWinners~=    nil then        GL_Data.MultipleWinners=nil;            end
        if GL_Data.MultiWinners==               nil then        GL_Data.MultiWinners=1;                         end

        if GL_Data.GuaranteedWinner==   nil then        GL_Data.GuaranteedWinner=0;             end
        if GL_Data.Raffle==                             nil then        GL_Data.Raffle=1;                                       end
        GL_DataInitialized=true;
end

function GuildLottery:DataClear(raffle)
        if raffle==1 then
                if GuildLottery:DataCount(raffle)>0 then
                        GL_Data.RaffleTicketSerie=GL_Data.RaffleTicketSerie+1000;
                        if GL_Data.RaffleTicketSerie>9000 then 
                                GL_Data.RaffleTicketSerie=1000;         
                        end
                end
                GL_Data.NextRaffleTicket=GL_Data.RaffleTicketSerie;
                local i,amount;
                amount=GuildLottery:DataCount(raffle);
                for i=1,amount,1 do
                        GuildLottery:DataRemove(1, raffle);
                end
        else
                if GuildLottery:DataCount(raffle)>0 then
                        GL_Data.LotteryTicketSerie=GL_Data.LotteryTicketSerie+1000;
                        if GL_Data.LotteryTicketSerie>9000 then 
                                GL_Data.LotteryTicketSerie=1000;                
                        end
                end
                GL_Data.NextLotteryTicket=GL_Data.LotteryTicketSerie;
                local i,amount;
                amount=GuildLottery:DataCount(raffle);
                for i=1,amount,1 do
                        GuildLottery:DataRemove(1, raffle);
                end
        end
        GuildLottery:UpdateScreen();
        GuildLottery:UpdateUserButtons();       
end

function GuildLottery:DataValid( idx, raffle )
        if GL_DataInitialized==false then
                GL_DataInitialize();
        end

        if GL_Data then
                if raffle==1 and GL_Data.RaffleInfo then
                        if idx==nil then
                                return true;
                        else
                                if GL_Data.RaffleInfo[idx] then
                                        return true;
                                end
                        end 
                elseif raffle==0 and GL_Data.LotteryInfo then
                        if idx==nil then
                                return true;
                        else
                                if GL_Data.LotteryInfo[idx] then
                                        return true;
                                end
                        end 
                else
                        return false;
                end
        end
        return false;
end

function GuildLottery:DataGetUserIdx ( user, raffle )
        local i;
        for i=1, GuildLottery:DataCount(raffle), 1 do
                if raffle==1 then
                        if GL_Data.RaffleInfo[i].User == user then
                                return i;
                        end
                else
                        if GL_Data.LotteryInfo[i].User == user then
                                return i;
                        end

                end
        end
        return 0;
end

function GuildLottery:DataGetTicketIdx ( ticketnumber, raffle )
        local i,j;
        for i=1, GuildLottery:DataCount(raffle), 1 do
                local Tickets=GuildLottery:DataGetTickets(i, raffle);
                for j=1, GuildLottery:DataGetTicketCount(i, raffle), 1 do
                        if Tickets[j]==ticketnumber then
                                return i;
                        end
                end
        end
        return 0;
end

function GuildLottery:DataGetNotified( idx, raffle )
        if GuildLottery:DataValid(idx, raffle) then
                if raffle==1 then
                        return GL_Data.RaffleInfo[idx].Notified;
                else
                        return GL_Data.LotteryInfo[idx].Notified;
                end
        end
        return 0;
end


function GuildLottery:DataSetNotified( idx, value, raffle )
        if GuildLottery:DataValid(idx, raffle) then
                if raffle==1 then
                        GL_Data.RaffleInfo[idx].Notified = value;
                else
                        GL_Data.LotteryInfo[idx].Notified = value;
                end
        end
end

function GuildLottery:DataGetUser( idx, raffle )
        if GuildLottery:DataValid(idx, raffle) then
                if raffle==1 then
                        return GL_Data.RaffleInfo[idx].User;
                else
                        return GL_Data.LotteryInfo[idx].User;
                end
        end
        return "";
end

function GuildLottery:DataSetUser(idx, value)
        if GuildLottery:DataValid(idx, GL_Data.Raffle) then
                if GL_Data.Raffle==GL_RAFFLE then
                        GL_Data.RaffleInfo[idx].User = value;
                else
                        GL_Data.LotteryInfo[idx].User = value;
                end
        end
end

function GuildLottery:DataGetTickets( idx, raffle )
        if GuildLottery:DataValid(idx, raffle) then
                if raffle==1 then
                        return GL_Data.RaffleInfo[idx].Tickets;
                else
                        return GL_Data.LotteryInfo[idx].Tickets;
                end
        end
        return nil;
end

function GuildLottery:DataGetTicketCount( idx, raffle )
        if GuildLottery:DataValid(idx, raffle) then
                if raffle==GL_RAFFLE then
                        return table.getn(GL_Data.RaffleInfo[idx].Tickets);
                else
                        return table.getn(GL_Data.LotteryInfo[idx].Tickets);
                end
        end
        return 0;
end

function GuildLottery:DataGetTotalTicketCount(raffle)
        local i,count;
        count=0;
        for i=1, GuildLottery:DataCount(raffle), 1 do
                count=count+GuildLottery:DataGetTicketCount(i, raffle);
        end
        return count;
end

function GuildLottery:DataSetTickets( idx, value )
        if GuildLottery:DataValid(idx, GL_Data.Raffle) then
                if GL_Data.Raffle==GL_RAFFLE then
                        GL_Data.RaffleInfo[idx].Tickets = value;
                else
                        GL_Data.LotteryInfo[idx].Tickets = value;
                end
        end
end

function GuildLottery:DataAddTickets( idx, value )
        if GuildLottery:DataValid(idx, GL_Data.Raffle) then
                local i=0;
                for i=1, table.getn(value), 1 do
                        if GL_Data.Raffle==GL_RAFFLE then
                                table.insert(GL_Data.RaffleInfo[idx].Tickets,value[i]);
                        else
                                table.insert(GL_Data.LotteryInfo[idx].Tickets,value[i]);
                        end
                end
        end
end

function GuildLottery:DataCount(raffle)
        if GuildLottery:DataValid(nil, raffle) then
                if raffle==1 then
                        return table.getn(GL_Data.RaffleInfo);
                else
                        return table.getn(GL_Data.LotteryInfo);
                end
        end
        return 0;
end

function GuildLottery:DataInsert( idx, info )

        if GuildLottery:DataValid(nil, GL_Data.Raffle) then
                if idx>=GuildLottery:DataCount(GL_Data.Raffle)+1 then
                        if GL_Data.Raffle==GL_RAFFLE then
                                table.insert(GL_Data.RaffleInfo, info);
                        else
                                table.insert(GL_Data.LotteryInfo, info);
                        end
                else
                        if GL_Data.Raffle==GL_RAFFLE then
                                table.insert(GL_Data.RaffleInfo, idx, info);
                        else
                                table.insert(GL_Data.LotteryInfo, idx, info);
                        end
                end
        end
end


function GuildLottery:DataRemove( idx, raffle )
        local i;
        if GuildLottery:DataValid(idx, raffle) then
                if raffle==GL_RAFFLE then
                        table.remove(GL_Data.RaffleInfo, idx);
                else
                        table.remove(GL_Data.LotteryInfo, idx);
                end
        end
end

Compare with Previous | Blame