Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.30 KB | None | 0 0
  1. identifier, config = ...
  2.  
  3. if config.centerPosMore then
  4.     config.centerPos.x, config.centerPos.y =
  5.     config.centerPos.x+config.centerPosMore.x,
  6.     config.centerPos.y+config.centerPosMore.y
  7. end
  8.  
  9. duel = CreateTemplate("duel", identifier)
  10.  
  11. duel:blockJoinWhenStart()
  12.  
  13. duel:setCanTeleportInside(false)
  14. duel:setCanUseReviveInside(false)
  15. duel:setCanUsePotionsInside(false)
  16. duel:setCanUseMedicineInside(false)
  17.  
  18. duel:setResetFunction(
  19. function()
  20.     duel:setBattleActive(false)
  21.     duel:setThrowItemsOnTheFloorInside(true)
  22.     duel:setCanFlyInside(true)
  23.     duel:setCanRideInside(true)
  24. end)
  25.  
  26. duel:setupVariables(
  27. function()
  28.     duel.m.totalTime = 0
  29.     duel.m.queueType = 1
  30.     duel.m.playersCanTurn = true
  31.     duel.m.canUseSpells = true
  32.     duel.m.playerUsedFirstPoke = {false, false, false, false}
  33.     duel.m.playerElos = {}
  34.     duel.m.playersDown = {0, 0}
  35. end)
  36.  
  37. duel:setStopFunction(
  38. function(whoAreWin, penalty)
  39.     local players = duel:getPlayers()
  40.     local winner, loser, winnerElo, loserElo, newEloWinner, newEloLoser, whoAreLoser
  41.  
  42.     if duel.m.queueType == 2 then
  43.         winnerElo, loserElo, newEloWinner, newEloLoser, whoAreLoser = {}, {}, {}, {}, {}
  44.     end
  45.  
  46.     if not (penalty) then
  47.         local timeStart = duel.m.totalTime-(config.totalTime*60)
  48.        
  49.         if getTime()-timeStart < 180 then
  50.             penalty = config.otherPenaltys
  51.         else
  52.             penalty = 0
  53.         end
  54.     end
  55.  
  56.     for x = 1, #players do
  57.         local current = players[x]
  58.  
  59.         current:set("inDuel", nil)
  60.         current:set("cantMoveItens", nil)
  61.  
  62.         if not duel.m.playersCanTurn then          
  63.             current:teleport(current:getPosInStorage("posSoloDuelArena"..identifier))
  64.         end
  65.  
  66.         if whoAreWin then
  67.             if duel.m.queueType == 1 then
  68.                 if whoAreWin:getCid() == current:getCid() then
  69.                     winner = current
  70.                     winnerElo = duel.m.playerElos[x]
  71.                 else
  72.                     loser = current
  73.                     loserElo = duel.m.playerElos[x]
  74.                 end
  75.             else
  76.                 if current:getCid() == whoAreWin[1]:getCid() or current:getCid() == whoAreWin[2]:getCid() then
  77.                     if not winner then
  78.                         if x == 1 or x == 3 then
  79.                             winner = 1
  80.                         else
  81.                             winner = 2
  82.                         end
  83.                     end
  84.                 else
  85.                     table.insert(whoAreLoser, current)
  86.  
  87.                     if not loser then
  88.                         if x == 1 or x == 3 then
  89.                             loser = 1
  90.                         else
  91.                             loser = 2
  92.                         end
  93.                     end
  94.                 end
  95.             end
  96.         else
  97.             current:sendBlueMessage(config.msgDraw)
  98.         end
  99.  
  100.         setBlockDuel(current:getCid(), false)
  101.         duel:exit(current)
  102.     end
  103.  
  104.     if whoAreWin then
  105.         if duel.m.queueType == 1 then
  106.             newEloWinner, newEloLoser = doSoloDuelRating(winnerElo, loserElo, 1, 0)
  107.             newEloLoser = newEloLoser-penalty
  108.  
  109.             winner:sendBlueMessage(string.format(config.msgWinner, math.floor(newEloWinner)))
  110.             loser:sendBlueMessage(string.format(config.msgLoser, math.floor(newEloLoser)))
  111.  
  112.             config.funcStop(players, whoAreWin, {{player = winner, newElo = newEloWinner}, {player = loser, newElo = newEloLoser}}, duel.m.queueType)
  113.         else
  114.             newEloWinner, newEloLoser = doTeamDuelRating(duel.m.playerElos[winner], duel.m.playerElos[loser], 1, 0)
  115.        
  116.             for x = 1, 2 do
  117.                 newEloLoser[x] = newEloLoser[x]-penalty
  118.                 whoAreWin[x]:sendBlueMessage(string.format(config.msgWinner, math.floor(newEloWinner[x])))
  119.                 whoAreLoser[x]:sendBlueMessage(string.format(config.msgLoser, math.floor(newEloLoser[x])))    
  120.             end
  121.  
  122.             config.funcStop(players, whoAreWin, {{player = whoAreWin[1], newElo = newEloWinner[1]}, {player = whoAreWin[2], newElo = newEloWinner[2]}, {player = whoAreLoser[1], newElo = newEloLoser[1]}, {player = whoAreLoser[2], newElo = newEloLoser[2]}}, duel.m.queueType)
  123.         end
  124.     end
  125. end)
  126.  
  127. function startAfter()
  128.     local players = duel:getPlayers()
  129.     local canStart = {}
  130.  
  131.     for x = 1, #players do
  132.         players[x]:backMountary()
  133.         players[x]:backPokemon()
  134.         canStart[x] = false
  135.            
  136.         if #players[x]:getActivePokeballList() >= config.numberPokesDuel then
  137.             if getTilePzInfo(players[x]:getPosition()) == TRUE then
  138.                 canStart[x] = true
  139.             end
  140.         end
  141.     end
  142.  
  143.     if duel.m.queueType == 1 then
  144.         if canStart[1] == false or canStart[2] == false then
  145.             local whoAreWin = false
  146.  
  147.             if canStart[1] == true and canStart[2] == false then
  148.                 whoAreWin = players[1]
  149.             elseif canStart[1] == false and canStart[2] == true then
  150.                 whoAreWin = players[2]
  151.             end
  152.  
  153.             duel:stop(whoAreWin, config.penaltyDisqualification)
  154.             return
  155.         end
  156.     else
  157.         if (canStart[1] == false or canStart[3] == false) or (canStart[2] == false or canStart[4] == false) then
  158.             local whoAreWin = false
  159.  
  160.             if (canStart[1] == true and canStart[3] == true) and (canStart[2] == false or canStart[4] == false) then
  161.                 whoAreWin = {players[1], players[3]}
  162.             elseif (canStart[2] == true and canStart[4] == true) and (canStart[1] == false or canStart[3] == false) then
  163.                 whoAreWin = {players[2], players[4]}
  164.             end
  165.  
  166.             duel:stop(whoAreWin, config.penaltyDisqualification)
  167.             return
  168.         end
  169.     end
  170.  
  171.     for index, current in pairs(players) do
  172.         current:setPosInStorage("posSoloDuelArena"..identifier, current:getPosition())    
  173.         current:teleport({x=config.centerPos.x+config.bornDistance[duel.m.queueType][index].x,y=config.centerPos.y+config.bornDistance[duel.m.queueType][index].y,z=config.centerPos.z})
  174.         if index == 3 or index == 4 then
  175.             current:setLookDir(getDirectionTo(current:getPosition(), {x=config.centerPos.x, y=config.centerPos.y+1, config.centerPos.z}))
  176.         else
  177.             current:setLookDir(getDirectionTo(current:getPosition(), config.centerPos))
  178.         end
  179.         current:sendBlueMessage(config.msgDelay)
  180.     end
  181.  
  182.     duel.m.canUseSpells = false
  183.     duel.m.playersCanTurn = false
  184.     duel:addEvent(startMainDuel, config.delayToStart*1000, nil)
  185.     --duel:setBattleActive(true)
  186.     duel:setThrowItemsOnTheFloorInside(false)
  187.     duel:setCanFlyInside(false)
  188.     duel:setCanRideInside(false)
  189.  
  190.     local timesToSend = {30, 20, 5, 1, 1, 1, 1}
  191.     local correctString = {30, 10, 5, 4, 3, 2, 1}
  192.     startClockMsgs({msgType = "second", format = timesToSend, index = -1, correctString = correctString})
  193. end
  194.  
  195. function startClockMsgs(params)
  196.     if not duel:isActive() then -- remove error msg; this module needs a rework
  197.         return
  198.     end
  199.  
  200.     local msgType, format, index, correctString = params.msgType, params.format, params.index, params.correctString
  201.    
  202.     local players = duel:getPlayers()
  203.  
  204.     if index >= 0 then
  205.         index = index+1
  206.  
  207.         for _, current in pairs(players) do
  208.             if msgType == "first" then
  209.                 current:sendBlueMessage("|" .. correctString[index].."| seconds to start.")
  210.             else
  211.                 current:sendBlueMessage("You have |"..correctString[index].."| seconds to choose your pokemon.")
  212.             end
  213.         end
  214.     else
  215.         index = 0
  216.     end
  217.  
  218.     params.index = index
  219.  
  220.     if format[index+1] then
  221.         duel:addEvent(startClockMsgs, format[index+1]*1000, params)
  222.     end
  223. end
  224.  
  225. duel:setStartFunction(
  226. function(eloPlayer1, eloPlayer2)
  227.     local players = duel:getPlayers()
  228.  
  229.     if #players >= 4 then
  230.         duel.m.queueType = 2
  231.     end
  232.  
  233.     duel.m.playerElos = {eloPlayer1, eloPlayer2}
  234.     duel.m.totalTime = getTime()+(config.totalTime*60)
  235.     duel:addEvent(startAfter, config.delayToEnter*1000, nil)
  236.  
  237.     for index, current in pairs(players) do
  238.         current:set("inDuel", true)
  239.         local nameAndClan
  240.  
  241.         if duel.m.queueType == 1 then
  242.             if index == 1 then
  243.                 nameAndClan = players[2]:getName().." ("..players[2]:getFirstVocationName()..", Level: "..players[2]:getLevel()..")"
  244.             else
  245.                 nameAndClan = players[1]:getName().." ("..players[1]:getFirstVocationName()..", Level: "..players[1]:getLevel()..")"
  246.             end
  247.         else
  248.             if index == 1 or index == 3 then
  249.                 nameAndClan = players[2]:getName().." ("..players[2]:getFirstVocationName()..", Level: "..players[2]:getLevel()..") and "..players[4]:getName().." ("..players[4]:getFirstVocationName()..", Level: "..players[4]:getLevel()..")"
  250.             else
  251.                 nameAndClan = players[1]:getName().." ("..players[1]:getFirstVocationName()..", Level: "..players[1]:getLevel()..") and "..players[3]:getName().." ("..players[3]:getFirstVocationName()..", Level: "..players[3]:getLevel()..")"
  252.             end
  253.         end
  254.  
  255.         stopDuel(current:getCid())
  256.         setBlockDuel(current:getCid(), true)
  257.  
  258.         current:sendRedMessage("Match Found! " .. string.format(config.msgBeforeEnter, nameAndClan))
  259.     end
  260.  
  261.     local timesToSend = {30, 20, 5, 1, 1, 1, 1}
  262.     local correctString = {30, 10, 5, 4, 3, 2, 1}
  263.  
  264.     startClockMsgs({msgType = "first", format = timesToSend, index = -1, correctString = correctString})
  265.     config.funcStart(players)
  266. end)
  267.  
  268. function startMainDuel()
  269.     local players = duel:getPlayers()
  270.     playersReady = {true, true}
  271.  
  272.     for x = 1, #players do
  273.         players[x]:set("cantMoveItens", nil)
  274.  
  275.         if not (players[x]:getFirstSummon()) then
  276.             if duel.m.queueType == 1 then
  277.                 playersReady[x] = false
  278.             else
  279.                 if x == 1 or x == 3 then
  280.                     playersReady[1] = false
  281.                 else
  282.                     playersReady[2] = false
  283.                 end
  284.             end  
  285.         end
  286.  
  287.         setBlockDuel(players[x]:getCid(), false)
  288.     end
  289.  
  290.     if playersReady[1] == false or playersReady[2] == false then
  291.         local whoAreWin = false
  292.  
  293.         if playersReady[1] == true and playersReady[2] == false then
  294.             if duel.m.queueType == 1 then
  295.                 whoAreWin = players[1]
  296.             else
  297.                 whoAreWin = {players[1], players[3]}
  298.             end
  299.  
  300.         elseif playersReady[1] == false and playersReady[2] == true then
  301.             if duel.m.queueType == 1 then
  302.                 whoAreWin = players[2]
  303.             else
  304.                 whoAreWin = {players[2], players[4]}
  305.             end
  306.         end
  307.  
  308.         duel:stop(whoAreWin, config.penaltyDisqualification)
  309.         return
  310.     else
  311.         if duel.m.queueType == 1 then
  312.             startDuel({{cid = players[1]:getCid()}, {cid = players[2]:getCid()}}, config.numberPokesDuel)
  313.         else
  314.             startDuel({{cid = players[1]:getCid()}, {cid = players[3]:getCid()}, {cid = players[2]:getCid()}, {cid = players[4]:getCid()}}, config.numberPokesDuel)
  315.         end
  316.     end
  317.  
  318.     duel:addEvent(function() duel.m.canUseSpells = true end, 5*1000, nil)
  319. end
  320.  
  321. function onWinDuelInArena(params)
  322.     if not duel:isActive() then return end
  323.  
  324.     local player = GetCreature(params.cid)
  325.     local players = duel:getPlayers()
  326.  
  327.     if duel.m.queueType == 1 then
  328.         for x = 1, #players do
  329.             if players[x]:getCid() == player:getCid() then
  330.                 duel:stop(player)
  331.                 return true
  332.             end
  333.         end
  334.     else
  335.         for x = 1, #players do
  336.             if players[x]:getCid() == player:getCid() then
  337.                
  338.                 if x == 1 or x == 3 then
  339.                     duel:stop({players[1], players[3]})
  340.                 else
  341.                     duel:stop({players[2], players[4]})
  342.                 end
  343.  
  344.                 return true
  345.             end
  346.         end
  347.     end
  348.  
  349.     return true
  350. end
  351.  
  352. g_Events.addEvent("onWinDuel", "onWinDuelInArena"..identifier, onWinDuelInArena)
  353.  
  354. duel:setLoopFunction(
  355. function()
  356.     local currentTime = getTime()
  357.  
  358.     if currentTime > duel.m.totalTime or g_game.getTournament().hasFinishedSubscriptions() then
  359.         duel:stop(false)
  360.     end
  361. end, 1000)
  362.  
  363. duel:createAction("canTurn", function() return duel.m.playersCanTurn end, "canTurnInSoloDuel", true)
  364. duel:createAction("canWalkToPosition", function() return duel.m.playersCanTurn end, "canWalkInSoloDuel", true)
  365.  
  366. g_Events.addEvent("canPokemonCastSpell", "soloDuelArenaCanUseSpell"..identifier,
  367. function(params)
  368.     if not duel:isActive() then return true end
  369.    
  370.     local monster = GetCreature(params.cid)
  371.     local master = monster:getMaster()
  372.  
  373.     if master == nil then
  374.         return true
  375.     end
  376.  
  377.     local player = GetCreature(master)
  378.  
  379.     if not player:get("inDuel") then return true end
  380.  
  381.     for _, current in pairs(duel:getPlayers()) do
  382.         if current:getCid() == player:getCid() then
  383.             return duel.m.canUseSpells
  384.         end
  385.     end
  386.  
  387.     return true
  388. end)
  389.  
  390. g_Events.addEvent("canSummonPokemon", "soloDuelArenaCanUsePoke"..identifier,
  391. function(params)
  392.     if not duel:isActive() then return true end
  393.     local player = GetCreature(params.cid)
  394.  
  395.     if not player:get("inDuel") then return true end
  396.  
  397.     for _, current in pairs(duel:getPlayers()) do
  398.         if current:getCid() == player:getCid() then
  399.             if not duel.m.canUseSpells then
  400.                 player:set("cantMoveItens", true)
  401.             end
  402.  
  403.             return true
  404.         end
  405.     end
  406.  
  407.     return true
  408. end)
  409.  
  410. duel:addFuncOnPlayerLogout(function(player)
  411.     local players = duel:getPlayers()
  412.  
  413.     if duel.m.queueType == 1 then
  414.         if player:getCid() == players[1]:getCid() then
  415.             duel:stop(players[2], config.penaltyDisqualification)
  416.         else
  417.             duel:stop(players[1], config.penaltyDisqualification)
  418.         end
  419.     else
  420.         if player:getCid() == players[1]:getCid() or player:getCid() == players[3]:getCid() then
  421.             duel:stop({players[2], players[4]}, config.penaltyDisqualification)
  422.         else
  423.             duel:stop({players[1], players[3]}, config.penaltyDisqualification)
  424.         end
  425.     end
  426.  
  427.     return true
  428. end)
  429.  
  430. runOnce("canMoveItensInSoloDuel", function()
  431.     function canMoveItensInSoloDuel(params)
  432.         local player = GetCreature(params.cid)
  433.        
  434.         if player:get("cantMoveItens") then
  435.             return false
  436.         end
  437.  
  438.         return true
  439.     end
  440.  
  441.     g_Events.addEvent("canDragThings", "canMoveItensInSoloDuel", canMoveItensInSoloDuel)
  442. end)
  443.  
  444. function start(player1, player2, eloPlayer1, eloPlayer2, player21, player22, eloInd1, eloInd2)
  445.     if not (player21 and player22) then
  446.         if duel:multipleJoin({player1, player2}) then
  447.             duel:start(eloPlayer1, eloPlayer2)
  448.             return true
  449.         end
  450.     else
  451.         if duel:multipleJoin({player1, player2, player21, player22}) then
  452.             duel:start(eloInd1, eloInd2)
  453.             return true
  454.         end
  455.     end
  456.  
  457.     return false
  458. end
  459.  
  460. function stop()
  461.     if duel:isActive() then
  462.         duel:stop()
  463.         return true
  464.     else
  465.         return false
  466.     end
  467. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement