SHARE
TWEET

file

a guest Feb 16th, 2020 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package game.content.phantasye.gambling;
  2.  
  3. import core.GameType;
  4. import game.content.dialogue.DialogueChain;
  5. import game.content.dialogueold.DialogueHandler;
  6. import game.item.GameItem;
  7. import game.item.ItemAssistant;
  8. import game.item.ItemDefinition;
  9. import game.npc.CustomNpcComponent;
  10. import game.npc.Npc;
  11. import game.npc.NpcHandler;
  12. import game.player.Player;
  13. import game.type.GameTypeIdentity;
  14. import org.menaphos.action.ActionInvoker;
  15. import org.menaphos.entity.impl.impl.NonPlayableCharacter;
  16. import org.menaphos.entity.impl.item.container.ItemContainer;
  17. import org.menaphos.entity.impl.item.container.impl.DefaultItemContainerImpl;
  18.  
  19. import org.menaphos.entity.impl.item.container.impl.MerchandiseItemContainerImpl;
  20.  
  21. import org.menaphos.model.math.AdjustableNumber;
  22. import org.menaphos.model.math.impl.AdjustableInteger;
  23. import org.menaphos.model.world.location.Location;
  24. import utility.Misc;
  25.  
  26. import java.text.NumberFormat;
  27. import java.util.*;
  28.  
  29. @CustomNpcComponent(identities = @GameTypeIdentity(type = GameType.OSRS, identity = 2713))
  30. public class DiceNPCBase extends Npc implements NonPlayableCharacter {
  31.  
  32.     private static final int COINS = 995;
  33.     private static final int TOKENS = 13204;
  34.  
  35.     private static DiceNPCBase instance = null;
  36.  
  37.     private final AdjustableNumber<Integer> coins;
  38.     private final ActionInvoker actionInvoker;
  39.     private Player player;
  40.     private final List<GameItem> playersWager;
  41.     private final ItemContainer inventory;
  42.  
  43.     private CoinPayoutDispenseChain payoutDispenseChain;
  44.  
  45.     private Timer wageTimer;
  46.  
  47.     private DiceNPCBase(int id, int type) {
  48.         super(-1, type);
  49.         this.actionInvoker = new ActionInvoker();
  50.         this.coins = new AdjustableInteger(0);
  51.         this.playersWager = new ArrayList<>();
  52.         this.inventory = new MerchandiseItemContainerImpl(50);
  53.         this.addItemToInventory(995, 500000000);
  54.     }
  55.  
  56.     public static DiceNPCBase getInstance(int index) {
  57.         if (instance == null)
  58.             instance = new DiceNPCBase(index, 2713);
  59.         return instance;
  60.     }
  61.  
  62.     private void startWagerTimer() {
  63.         final Timer timer = new Timer();
  64.         final TimerTask task = new TimerTask() {
  65.             @Override
  66.             public void run() {
  67.                 cancelWager();
  68.             }
  69.         };
  70.         timer.schedule(task, 60000);
  71.         this.setWageTimer(timer);
  72.     }
  73.  
  74.     private boolean stopWagerTimer() {
  75.         if (wageTimer != null) {
  76.             wageTimer.cancel();
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.  
  82.     private void cancelWager() {
  83.         if (wageTimer != null) {
  84.             wageTimer.cancel();
  85.             if (!playersWager.isEmpty()) {
  86.                 playersWager.stream().forEach(item -> player.addItemToInventory(item.getId(), item.getAmount()));
  87.                 sendMessage(getPlayer().getPlayerName() + " has forfeit and I am now accepting new wagers!");
  88.                 this.reset();
  89.             }
  90.         }
  91.     }
  92.  
  93.     private void reset() {
  94.         playersWager.clear();
  95.         wageTimer.cancel();
  96.         this.setWageTimer(null);
  97.         this.setPlayer(null);
  98.  
  99.     }
  100.  
  101.     private void sendPlayDialog(Player player) {
  102.         player.setDialogueChain(new DialogueChain().npc(
  103.                 this.getDefinition(),
  104.                 DialogueHandler.FacialAnimation.DEFAULT,
  105.                 "Would you like to play some 55x2?",
  106.                 "I'm currently taking wagers up to " + this.formatValue(this.getMaxBid()),
  107.                 "and I do accept Items!")
  108.                 .option((p, option) -> {
  109.                     if (option == 1) {
  110.                         this.setPlayer(player);
  111.                         player.getPA().closeInterfaces(true);
  112.                         player.setDialogueChain(new DialogueChain().npc(
  113.                                 this.getDefinition(),
  114.                                 DialogueHandler.FacialAnimation.DEFAULT,
  115.                                 "I'll give you 1 minute",
  116.                                 "to give me your wager."));
  117.                         this.startWagerTimer();
  118.                     } else {
  119.                         player.getPA().closeInterfaces(true);
  120.                     }
  121.                 }, "Play 55x2", "Yes", "No")).start(player);
  122.     }
  123.  
  124.     private int convertWinningsToTokens(int winnings) {
  125.         if (winnings >= 1000000)
  126.             return winnings / 1000000;
  127.         return 0;
  128.     }
  129.  
  130.     private void roll() {
  131.         int roll = Misc.random(1, 100);
  132.         int maxValue = roll - 55;
  133.         int finalRoll = roll;
  134.         if (maxValue > 0) {
  135.             finalRoll -= Misc.random(1, 20);
  136.         }
  137.         final int modifiedRoll = finalRoll;
  138.         this.performAnimation(11000);
  139.         NpcHandler.getNpcByNpcId(this.getId()).gfx0(2000);
  140.         announceFinalRoll(modifiedRoll);
  141.     }
  142.  
  143.     private void announceFinalRoll(int finalRoll) {
  144.         if (finalRoll > 55) {
  145.             NpcHandler.getNpcByNpcId(getId()).forceChat("It's a " + finalRoll + "! We have a winner!");
  146.             this.payoutDispenseChain = new CoinPayoutDispenseChain(player);
  147.             payout();
  148.         } else {
  149.             NpcHandler.getNpcByNpcId(getId()).forceChat("It's a " + finalRoll + "! Better luck next time!");
  150.             collect();
  151.         }
  152.     }
  153.  
  154.     private void collect() {
  155.         this.reset();
  156.     }
  157.  
  158.     private void payout() {
  159.         playersWager.forEach(item->payPlayer(item.getId(),item.getAmount()));
  160.         this.reset();
  161.     }
  162.  
  163.     private void payPlayer(int id, int amount) {
  164.         final int payout = amount * 2;
  165.         if(!removeItemFromInventory(id,payout)) {
  166.              removeItemFromInventory(COINS,ItemDefinition.getDefinitions()[id].price * amount);
  167.         }
  168.         if (ItemAssistant.getItemAmount(player, id) + payout < Integer.MAX_VALUE && id != COINS) {
  169.             player.addItemToInventory(id, payout);
  170.         } else if(id == COINS){
  171.             payoutDispenseChain.payout(payout);
  172.         } else {
  173.             payoutDispenseChain.payout((ItemDefinition.getDefinitions()[id].price * amount) * 2) ;
  174.         }
  175.     }
  176.  
  177.     private void clean() {
  178.         inventory.contents().clear();
  179.         this.addItemToInventory(COINS, 500000000);
  180.     }
  181.  
  182.  
  183.     private void sendPlayEarlyDialog() {
  184.         player.setDialogueChain(new DialogueChain().npc(
  185.                 this.getDefinition(),
  186.                 DialogueHandler.FacialAnimation.DEFAULT,
  187.                 "Are you ready to play? Your current wager is ",
  188.                 this.formatValue(this.getCurrentWager()))
  189.                 .option((p, option) -> {
  190.                     if (option == 1) {
  191.                         if (!playersWager.isEmpty()) {
  192.                             this.stopWagerTimer();
  193.                             player.getPA().closeInterfaces(true);
  194.                             this.roll();
  195.                         } else {
  196.                             this.cancelWager();
  197.                             player.setDialogueChain(new DialogueChain().npc(
  198.                                     this.getDefinition(),
  199.                                     DialogueHandler.FacialAnimation.DEFAULT,
  200.                                     "You've not placed a wager."));
  201.                             player.getPA().closeInterfaces(true);
  202.                         }
  203.                     } else {
  204.                         player.getPA().closeInterfaces(true);
  205.                     }
  206.                 }, "Wager of: " + this.formatValue(this.getCurrentWager()), "Yes", "No")).start(player);
  207.     }
  208.  
  209.     private void sendBusyDialog(Player player) {
  210.         player.setDialogueChain(new DialogueChain().statement("He appears to be already playing with someone else.")).start(player);
  211.     }
  212.  
  213.     public void talkTo(Player player) {
  214.         if (this.getPlayer() == null) {
  215.             this.sendPlayDialog(player);
  216.         } else if (this.getPlayer() == player) {
  217.             this.sendPlayEarlyDialog();
  218.         } else {
  219.             this.sendBusyDialog(player);
  220.         }
  221.     }
  222.  
  223.     public void announce() {
  224.         if (player == null) {
  225.             this.sendMessage("I am now accepting trades! Use the item(s) you wish to wager on me!");
  226.         }
  227.     }
  228.  
  229.     public boolean acceptTrade(int itemId, int amount, Player player) {
  230.         if (this.player != null) {
  231.             if (this.player == player) {
  232.                 if (!ItemDefinition.getDefinitions()[itemId].untradeableOsrsEco) {
  233.                     if (this.convertToCoins(itemId, amount) && player.removeItemFromInventory(itemId, amount)) {
  234.                         this.playersWager.add(new GameItem(itemId, amount));
  235.                         return this.addItemToInventory(itemId, amount);
  236.                     }
  237.                 } else {
  238.                     this.sendMessage("I can not accept untradable items.");
  239.                 }
  240.             } else {
  241.                 this.sendMessage("I'm currently dicing with " + this.player.getPlayerName() + " please come back in a bit.");
  242.             }
  243.         } else {
  244.             this.talkTo(player);
  245.         }
  246.         return false;
  247.     }
  248.  
  249.     private boolean convertToCoins(int itemId, int amount) {
  250.         final int value = ItemDefinition.getDefinitions()[itemId].price * amount;
  251.         if (value <= this.getMaxBid()) {
  252.             return true;
  253.         } else {
  254.             this.sendMessage("I can't accept an offer that high! My current max trade is: " + this.formatValue(this.getMaxBid()) + " GP.");
  255.         }
  256.         return false;
  257.     }
  258.  
  259.     private int getCurrentWager() {
  260.         if (!playersWager.isEmpty())
  261.             return playersWager.stream().mapToInt(item -> ItemDefinition.getDefinitions()[item.getId()].price * item.getAmount()).sum();
  262.         return 0;
  263.     }
  264.  
  265.     private String formatValue(long value) {
  266.         return NumberFormat.getInstance().format(value) + " GP";
  267.     }
  268.  
  269.     private long getMaxBid() {
  270.         return Arrays.stream(inventory.getReadOnlyContents()).filter(itemSlot -> itemSlot.getId() != -1).mapToInt(item -> ItemDefinition.getDefinitions()[item.getId()].price * item.count()).sum();
  271.  
  272.     }
  273.  
  274.     @Override
  275.     public int getId() {
  276.         return 2713;
  277.     }
  278.  
  279.     @Override
  280.     public boolean addItemToInventory(int id, int amt) {
  281.         return inventory.add(id, amt);
  282.     }
  283.  
  284.     @Override
  285.     public boolean removeItemFromInventory(int id, int amt) {
  286.         System.out.println("REMOVING ID: " + id + " AMOUNT: " + amt);
  287.         return inventory.remove(id, amt);
  288.     }
  289.  
  290.     @Override
  291.     public boolean pickupItem(int i, int i1) {
  292.         return false;
  293.     }
  294.  
  295.     @Override
  296.     public void sendMessage(String s) {
  297.         NpcHandler.getNpcByNpcId(this.getId()).forceChat(s);
  298.     }
  299.  
  300.     @Override
  301.     public void receiveMessage(String s) {
  302.  
  303.     }
  304.  
  305.     @Override
  306.     public boolean hasItem(int i, int i1) {
  307.         return false;
  308.     }
  309.  
  310.     @Override
  311.     public void performAnimation(int i) {
  312.         NpcHandler.getNpcByNpcId(this.getId()).requestAnimation(i);
  313.     }
  314.  
  315.     @Override
  316.     public ActionInvoker getActionInvoker() {
  317.         return actionInvoker;
  318.     }
  319.  
  320.     @Override
  321.     public boolean moveTo(Location location) {
  322.         return false;
  323.     }
  324.  
  325.     @Override
  326.     public Npc copy(int index) {
  327.         return getInstance(index);
  328.     }
  329.  
  330.     public Player getPlayer() {
  331.         return player;
  332.     }
  333.  
  334.     public void setPlayer(Player player) {
  335.         this.player = player;
  336.     }
  337.  
  338.  
  339.     public void setWageTimer(Timer wageTimer) {
  340.         this.wageTimer = wageTimer;
  341.     }
  342. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top