Tibia Mod
Forum zostało Przeniesione tutaj:
http://tibiamod.xaa.pl/

Zapraszam.

Join the forum, it's quick and easy

Tibia Mod
Forum zostało Przeniesione tutaj:
http://tibiamod.xaa.pl/

Zapraszam.
Tibia Mod
Czy chcesz zareagować na tę wiadomość? Zarejestruj się na forum za pomocą kilku kliknięć lub zaloguj się, aby kontynuować.

Loteria [globalevents]

Go down

Loteria [globalevents] Empty Loteria [globalevents]

Pisanie by darkes Sro Lis 14, 2012 12:15 pm

Cześć,
z nudów dla odświeżenia LUA po sporej przerwie napisałem sobie loterie, może komuś się przyda / ktoś powie nad czym popracować.
W configuracji można ustawić ilość nagród (nie tylko ilość itemu) i ilość zwycięzców. Swoją drogą skrypt sam `wylicza` kiedy będzie następna loteria.

Do globalevents/globalevents.xml:
Kod:
<globalevent name="lottery" interval="3600" event="script" value="lottery.lua"/> 
3600 - czas powtarzania loterii w sekundach

Tworzymy plik globalevents/scripts/lottery.lua i wklejamy:
Kod:
local config =
{
    winnersCount = 2,
    rewardsCount = 1,
    rewards =
    {
        {itemid = 2270, count = 1},
    },
    messages =
    {
        color = 19, -- message color
        prefix = '[LOTTERY]: ', -- message prefix
        winner = 'Congratulations $winnerName! You won $items.', -- message for winner
        all =
        {
            -- message for all
            one = "$winnerName is winner. Next lottery $nextLotteryTime.",
            more = "$winnersName are winners. Next lottery $nextLotteryTime.",
        },
    },
}


function onThink(interval, lastExecution)
    local players = getPlayersOnline();
    local messageForWinner, messageForAll = config['messages'].prefix .. config['messages'].winner, config['messages'].prefix;
    local winners, rewards = {}, {};

    if(#players == 0) then
        return true;
    end
   
    if(config.winnersCount == 1) then
        table.insert(winners, players[math.random(1, #players)]);
        messageForAll = messageForAll .. config['messages']['all'].one;
        messageForAll = string.gsub(messageForAll, '$winnerName', getCreatureName(winners[1]));
    elseif(config.winnersCount > 1) then
        local winnersName = '';
        local winner;
        for i=1, config.winnersCount do
            winner = players[math.random(1, #players)];
            table.insert(winners, winner);
            winnersName = winnersName .. getCreatureName(winner);
            if(i < config.winnersCount) then winnersName = winnersName .. ', '; end
        end
        messageForAll = messageForAll .. config['messages']['all'].more;
        messageForAll = string.gsub(messageForAll, '$winnersName', winnersName);
    end
   
    messageForAll = string.gsub(messageForAll, '$nextLotteryTime', os.date('%X %x', os.time()+interval));
   
    for w=1, #winners do
        if(config.rewardsCount == 1) then
            local reward = config.rewards[math.random(1, #config.rewards)];
            table.insert(rewards, reward);
            doPlayerAddItem(winners[w], reward.itemid, reward.count);
            messageForWinner = string.gsub(messageForWinner, '$winnerName', getCreatureName(winners[w]));
            messageForWinner = string.gsub(messageForWinner, '$items', reward.count .. 'x' ..getItemInfo(reward.itemid).name);
        elseif(config.rewardsCount > 1) then
            local itemsName, reward;
            for i=1, config.rewardsCount do
                reward = config.rewards[math.random(1, #config.rewards)];
                table.insert(rewards, reward);
                doPlayerAddItem(winners[w], reward.itemid, reward.count)
                itemsName = itemsName .. reward.count .. 'x' .. getItemInfo(reward.itemid).name;
                if(not i == config.rewardsCount) then itemsName = itemsName .. ', '; end
            end
            messageForWinner = string.gsub(messageForWinner, '$winnerName', getCreatureName(winners[w]));
            messageForWinner = string.gsub(messageForWinner, '$items', itemsName);
        end
        doPlayerSendTextMessage(winners[w], config['messages'].color, messageForWinner);
    end
   
    return doBroadcastMessage(messageForAll);
end


Konfiguracje można oczywiście zmienić np aby dawało inny item zmieniamy lub dodajemy {itemid = id itemka, count = ilość}. Skrypt pochodzi od użytkownika animaxxx z t.net.

darkes
Uploader

Liczba postów : 27
Join date : 13/11/2012

Powrót do góry Go down

Powrót do góry


 
Permissions in this forum:
Nie możesz odpowiadać w tematach