Advertisement
Guest User

1vs1 Fights

a guest
Apr 20th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package xdm;
  16.  
  17. import java.util.ArrayList;
  18.  
  19. import net.sf.l2j.gameserver.ThreadPoolManager;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  21. import net.sf.l2j.gameserver.util.Broadcast;
  22. import net.sf.l2j.util.Rnd;
  23.  
  24. /**
  25.  * @author Drake2wow
  26.  *
  27.  */
  28. public class RandomFights
  29. {
  30.     protected static ArrayList<L2PcInstance> _participants = new ArrayList<>();
  31.    
  32.     public static void createFight()
  33.     {
  34.         new Fight();
  35.     }
  36.    
  37.     protected static L2PcInstance pickPlayer()
  38.     {
  39.         if (_participants.isEmpty())
  40.             return null;
  41.         L2PcInstance player = _participants.get(Rnd.get(_participants.size() - 1));
  42.         _participants.remove(player);
  43.         if (player.isInOlympiadMode()) //need more restricts obviously
  44.             return pickPlayer();
  45.         return player;
  46.     }
  47.    
  48.     protected static int f;
  49.     protected static class Fight
  50.     {
  51.         //configs
  52.         protected static int ListenerSleepTimeSeconds = 5;
  53.         protected static int FightDurationSeconds = 180;
  54.         //
  55.        
  56.         protected final int id = ++f;
  57.         protected final L2PcInstance player1 = pickPlayer();
  58.         protected final L2PcInstance player2 = pickPlayer();
  59.         protected int timePassedSeconds;
  60.        
  61.         {   //validate this fight
  62.             if (player1 == null || player2 == null) //invalid, XXX more checks?
  63.             {
  64.                 if (player1 != null)
  65.                     _participants.add(player1); //back on queue
  66.                 else if (player2 != null)
  67.                     _participants.add(player2); //back on queue
  68.                 Broadcast.announceToOnlinePlayers("Fight " + id + ") didn't met the required criteria, canceling...");
  69.             }
  70.             else //its valid, start it
  71.             {
  72.                 Broadcast.announceToOnlinePlayers("Fight " + id + ") started, " + player1.getName() + " versus " + player2.getName() + "!");
  73.                 //teleport them in and do stuff
  74.                
  75.                 //
  76.                 ThreadPoolManager.getInstance().scheduleGeneral(new FightListener(), ListenerSleepTimeSeconds * 1000);
  77.             }
  78.         }
  79.        
  80.         protected class FightListener implements Runnable
  81.         {
  82.             @Override
  83.             public void run()
  84.             {
  85.                 timePassedSeconds += ListenerSleepTimeSeconds;
  86.                 if (player1 == null && player2 == null)
  87.                     winnerIs(null);
  88.                 else if (player1 == null || player1.isDead() && !player2.isDead())
  89.                     winnerIs(player2);
  90.                 else if (player2 == null || player2.isDead() && !player1.isDead())
  91.                     winnerIs(player1);
  92.                 else if (timePassedSeconds >= FightDurationSeconds)
  93.                     winnerIs(player1.getMaxHp() / player1.getCurrentHp() > player2.getMaxHp() / player2.getCurrentHp() ? player1 : player2);
  94.                 else //no event in the meantime reschedule me
  95.                     ThreadPoolManager.getInstance().scheduleGeneral(this, ListenerSleepTimeSeconds * 1000);
  96.             }
  97.         }
  98.        
  99.         protected void winnerIs(L2PcInstance player)
  100.         {
  101.             if (player == null)
  102.                 Broadcast.announceToOnlinePlayers("Fight " + id + ") no winner!");
  103.             else
  104.                 Broadcast.announceToOnlinePlayers("Fight " + id + ") " + player.getName() + " wins!");
  105.             //reward
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement