RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
GambleManager.java
1package com.osroyale.content.gambling;
2
3import com.osroyale.content.gambling.impl.FiftyFive;
4import com.osroyale.content.gambling.impl.FlowerPoker;
5import com.osroyale.game.world.World;
6import com.osroyale.game.world.entity.mob.Direction;
7import com.osroyale.game.world.entity.mob.player.Player;
8import com.osroyale.game.world.entity.mob.player.PlayerRight;
9import com.osroyale.game.world.items.Item;
10import com.osroyale.game.world.items.ItemDefinition;
11import com.osroyale.game.world.items.containers.ItemContainer;
12import com.osroyale.game.world.object.CustomGameObject;
13import com.osroyale.game.world.pathfinding.TraversalMap;
14import com.osroyale.game.world.position.Boundary;
15import com.osroyale.game.world.position.Position;
16import com.osroyale.net.packet.out.*;
17import com.osroyale.util.Utility;
18
19import java.util.ArrayList;
20import java.util.Objects;
21
58
59public class GambleManager {
60
64 private final int INTERFACE_ID = 44750;
68 private final int INVENTORY_ID = 44775;
72 private final int CONFIG = 444;
73
74 public static final Boundary GAMBLING_ZONE = new Boundary(3148, 3476, 3181, 3505);
75
79 protected GambleStage stage = GambleStage.NONE;
80
81 public GambleStage getStage() {
82 return stage;
83 }
84
85 public void setStage(GambleStage stage) {
86 this.stage = stage;
87 }
88
92 protected Player other;
93
94 public Player getOther() {
95 return other;
96 }
97
98 public void setOther(Player other) {
99 this.other = other;
100 }
101
105 protected boolean confirmed;
106
107 public boolean hasConfirmed() {
108 return confirmed;
109 }
110
111 public void setConfirmed(boolean confirmed) {
112 this.confirmed = confirmed;
113 }
114
118 protected GambleType type = GambleType.NONE;
119
120 public GambleType getType() {
121 return type;
122 }
123
124 public void setType(GambleType type) {
125 this.type = type;
126 }
127
131 protected Gamble game;
132
133 public Gamble getGame() {
134 return game;
135 }
136
137 public void setGame(Gamble game) {
138 this.game = game;
139 }
140
145
146 public ItemContainer getContainer() {
147 return container;
148 }
149
150 protected ArrayList<CustomGameObject> gameFlowers = new ArrayList<CustomGameObject>();
151
152 public ArrayList<CustomGameObject> getGameFlowers() {
153 return gameFlowers;
154 }
155
156
157 protected ArrayList<Flowers> flowers = new ArrayList<Flowers>();
158
159 public ArrayList<Flowers> getFlowers() {
160 return flowers;
161 }
162
169 public boolean canGamble(Player player, GambleStage requiredStage) {
170
171 if(!Boundary.isIn(player, GAMBLING_ZONE)) return false;
172
173 if (Objects.nonNull(requiredStage) && !player.getGambling().getStage().equals(requiredStage)) return false;
174
175 return true;
176 }
177
183 public void sendRequest(Player player, Player other) {
184
185 if (PlayerRight.isIronman(player)) {
186 player.message("You can not gamble as you are an iron man.");
187 return;
188 }
189
191 player.message(other.getName() + " can not gamble as they are an iron man.");
192 return;
193 }
194 if(player.playTime < 6000) {
195 player.message("You must have at least 1 hour of play time to gamble.");
196 return;
197 }
198
199 Player requested = other.getGambling().getOther();
200
201 if (!Objects.isNull(requested)) {
202 System.out.println("Accept...");
203 acceptRequest(player, other);
204 return;
205 }
206
207 player.message("You've sent a gamble invite to " + Utility.capitalizeSentence(other.getName()) + ".");
208 other.message(Utility.capitalizeSentence(player.getName()) + ":gamblereq:");
209 player.send(new SendRemoveInterface());
210 player.getGambling().setOther(other);
211 player.getGambling().setStage(GambleStage.SENDING_OFFER);
212 System.out.println("send request...");
213 }
214
220 public void acceptRequest(Player player, Player other) {
221 if(player == other) return;
222
223 if (!canGamble(player, GambleStage.NONE) || !canGamble(other, GambleStage.SENDING_OFFER)) return;
224
225 player.getGambling().setStage(GambleStage.SENDING_OFFER);
226 player.getGambling().setOther(other);
227
228 if (other.getGambling().getOther() != null && player.getIndex() == other.getGambling().getOther().getIndex()) {
229 System.out.println("accepted request...");
230 open(other, player);
231 open(player, other);
232 }
233 }
234
240 public void open(Player player, Player other) {
241 player.getGambling().setConfirmed(false);
242 player.getGambling().setStage(GambleStage.PLACING_BET);
243 System.out.println("Open the interface for ["+String.format(player.getUsername())+"] and ["+String.format(other.getUsername())+"]...");
244
245 player.send(new SendInventoryInterface(INTERFACE_ID, INVENTORY_ID));
246 player.send(new SendItemOnInterface(44770, player.getGambling().getContainer().toArray()));
247 player.send(new SendItemOnInterface(44771, other.getGambling().getContainer().toArray()));
248 player.send(new SendItemOnInterface(INVENTORY_ID + 1, player.inventory.getItems()));
249
250 player.send(new SendString("" + String.format(player.getUsername()), 44768));
251 player.send(new SendString("" + String.format(other.getUsername()), 44769));
252 player.send(new SendConfig(CONFIG, 0));
253 }
254
259 public void accept(Player player) {
260 Player other = player.getGambling().getOther();
261
262 if (other == null) return;
263
264 if(player.getGambling().getType() == null || player.getGambling().getType().equals(GambleType.NONE)) {
265 player.message("You need to select a game first.");
266 return;
267 }
268
269 if (System.currentTimeMillis() - player.getLastModification() < 5_000) {
270 player.message("@red@Something was changed in the last 5 seconds, you cannot accept yet.");
271 return;
272 }
273
274 if (player.getGambling().getType() != other.getGambling().getType()) return;
275
276 if(player.getGambling().hasConfirmed()) return;
277
278 player.getGambling().setConfirmed(true);
279
280 player.message("You have accepted the gamble with "+String.format(other.getUsername())+".");
281 other.message(String.format(player.getUsername()) + " has accepted the gamble.");
282
283 System.out.println("player: " + String.format(player.getUsername()));
284 System.out.println("other: " + String.format(other.getUsername()));
285
286 if (other.getGambling().hasConfirmed()) {
287 Gamble game = getGame(other, player, other.getGambling().getType());
288
289 player.getGambling().setGame(game);
290 other.getGambling().setGame(game);
291
292 player.getGambling().setStage(GambleStage.SENDING_OFFER);
293
294 player.getGambling().setOther(other);
295
296 System.out.println("Both confirmed start the game...");
297
298 start(player);
299 }
300 }
301
308 public boolean canPlayFlowerPokerAtPositon(Player player, Position position) {
309 //Checks if the spot its checking has a object placed there
310 if (!TraversalMap.isTraversable(new Position(position.getX() + 1, position.getY()), Direction.SOUTH, player.width())) return false;
311
312 boolean canPlay = true;
313 for(int index = 0; index < 5; index++) {
314 if (!TraversalMap.isTraversable(position, Direction.SOUTH, player.width())) {
315 canPlay = false;
316 break;
317 }
318 position = position.create(position.getX(), position.getY() + Direction.SOUTH.getDirectionY());
319 }
320 return canPlay;
321 }
322
327 public void decline(Player player) {
328 Player other = player.getGambling().getOther();
329
330 for(Item item : player.getGambling().getContainer()) {
331 if(item == null || item.getId() == -1) continue;
332
333 if(player.inventory.hasCapacityFor(item))
334 player.inventory.add(item);
335 else {
336 player.bank.add(item);
337 player.message("@red@You had no room for the "+item.getAmount()+" x "+item.getDefinition().getName()+", its been sent to your bank.");
338 }
339 }
340
341 player.getGambling().getContainer().clear();
342 player.inventory.refresh();
343 player.bank.refresh();
344
345 if(other != null) {
346
347 for(Item item : other.getGambling().getContainer()) {
348 if(item == null || item.getId() == -1) continue;
349
350 if(other.inventory.hasCapacityFor(item))
351 other.inventory.add(item);
352 else {
353 other.bank.add(item);
354 other.message("@red@You had no room for the "+item.getAmount()+" x "+item.getDefinition().getName()+", its been sent to your bank.");
355 }
356 }
357
358 other.getGambling().getContainer().clear();
359 other.inventory.refresh();
360 other.bank.refresh();
361
362 reset(other);
363 }
364
365 reset(player);
366 }
367
373 public void deposit(Player player, int itemId, int slot, int amount) {
374 Player other = player.getGambling().getOther();
375
376 if(other == null) return;
377
378 ItemDefinition def = ItemDefinition.get(itemId);
379
380 if (!def.isTradeable()) {
381 player.send(new SendMessage("This is item is untradeable!"));
382 return;
383 }
384
385 if(!player.inventory.contains(itemId)) return;
386
387 if(amount > player.inventory.computeAmountForId(itemId))
388 amount = player.inventory.computeAmountForId(itemId);
389
390 if(amount <= 0) return;
391
392 Item item = new Item(itemId, amount);
393 if(player.getGambling().getContainer().hasCapacityFor(item)) {
394 player.inventory.remove(item, slot);
395 player.getGambling().getContainer().add(item);
396 player.inventory.refresh();
397 player.inventory.refresh(player, INVENTORY_ID + 1);
398 } else
399 player.message("Not enough space.");
400
401 player.setLastModification(System.currentTimeMillis());
402 other.setLastModification(System.currentTimeMillis());
403
404 player.send(new SendItemOnInterface(44770, player.getGambling().getContainer().toArray()));
405 player.send(new SendItemOnInterface(44771, other.getGambling().getContainer().toArray()));
406
407 other.send(new SendItemOnInterface(44770, other.getGambling().getContainer().toArray()));
408 other.send(new SendItemOnInterface(44771, player.getGambling().getContainer().toArray()));
409 }
410
416 public void withdraw(Player player, int itemId, int slot, int amount) {
417 Player other = player.getGambling().getOther();
418
419 if(other == null) return;
420
421 Item clickedItem = player.getGambling().getContainer().get(slot);
422
423 if(clickedItem.getId() != itemId) return;
424
425 if(amount > clickedItem.getAmount())
426 amount = clickedItem.getAmount();
427
428 if(!clickedItem.getDefinition().isStackable() && amount > player.inventory.getFreeSlots())
429 amount = player.inventory.getFreeSlots();
430
431 if(amount <= 0) return;
432
433 player.getGambling().getContainer().remove(itemId, amount);
434 player.inventory.add(itemId, amount);
435
436 player.inventory.refresh();
437 player.inventory.refresh(player, INVENTORY_ID + 1);
438
439 player.setLastModification(System.currentTimeMillis());
440 other.setLastModification(System.currentTimeMillis());
441
442 player.send(new SendItemOnInterface(44770, player.getGambling().getContainer().toArray()));
443 player.send(new SendItemOnInterface(44771, other.getGambling().getContainer().toArray()));
444
445 other.send(new SendItemOnInterface(44770, other.getGambling().getContainer().toArray()));
446 other.send(new SendItemOnInterface(44771, player.getGambling().getContainer().toArray()));
447 }
448
456 private Gamble getGame(Player other, Player player, GambleType type) {
457 switch (type) {
458 case FIFTY_FIVE:
459 return new FiftyFive(player, other);
460 case FLOWER_POKER:
461 return new FlowerPoker(player, other);
462 }
463 return null;
464 }
465
470 private void start(Player player) {
471 if (!canGamble(player, GambleStage.SENDING_OFFER)) return;
472
473 Player other = player.getGambling().getOther();
474
475 if (other == null) return;
476
477 if (other.getGambling().hasConfirmed()) {
478 if (player.getGambling().getGame() == null) return;
479
480 if (player.getGambling().getGame().getHost() == null) return;
481
482 player.send(new SendRemoveInterface());
483 other.send(new SendRemoveInterface());
484
485 player.getGambling().setStage(GambleStage.IN_PROGRESS);
486 other.getGambling().setStage(GambleStage.IN_PROGRESS);
487
488 player.setGambleLock(true);
489 other.setGambleLock(true);
490
491 player.getGambling().getGame().gamble();
492 }
493 }
494
502 public void finish(Player host, Player opponent, int hostScore, int opponentScore) {
503 if (host.getGambling().getGame() == null)
504 return;
505
506 if (host.getGambling().getGame().getHost() == null)
507 return;
508
509 if (!World.getPlayers().contains(opponent) && World.getPlayers().contains(host)) {
510 give(host.getGambling().getType(), 'H', host, opponent, false);
511 } else if (World.getPlayers().contains(opponent) && !World.getPlayers().contains(host)) {
512 give(host.getGambling().getType(), 'O', opponent, host, false);
513 } else {
514 if (hostScore == 55) {
515 host.speak("The roll was a draw, rerolling...");
516 opponent.speak("The roll was a draw, rerolling...");
517
518 host.getGambling().setStage(GambleStage.IN_PROGRESS);
519 opponent.getGambling().setStage(GambleStage.IN_PROGRESS);
520 host.getGambling().getGame().gamble();
521 return;
522 }
523
524 boolean hostWon = hostScore > opponentScore;
525 give(host.getGambling().getType(), hostWon ? 'H' : 'O', hostWon ? host : opponent, hostWon ? opponent : host, false);
526 }
527 }
528
537 public void give(GambleType gambleType, char winnerIdentifier, Player winner, Player loser, boolean draw) {
538
539 if(draw) {
540
541 winner.speak("It's a draw!");
542 loser.speak("It's a draw!");
543
544 removeFlowers(winner);
545 removeFlowers(loser);
546
547 winner.getGambling().setStage(GambleStage.IN_PROGRESS);
548 loser.getGambling().setStage(GambleStage.IN_PROGRESS);
549
550 winner.getGambling().getGame().gamble();
551
552 return;
553 } else {
554
555 //Handle giving the winner the items
556 for(Item item : winner.getGambling().getContainer()) {
557 if(item == null || item.getId() == -1) continue;
558
559 if(winner.inventory.hasCapacityFor(item))
560 winner.inventory.add(item);
561 else {
562 winner.inventory.addOrDrop(item);
563 winner.bank.refresh();
564 }
565 }
566
567 for(Item item : loser.getGambling().getContainer()) {
568 if(item == null || item.getId() == -1) continue;
569
570 if(winner.inventory.hasCapacityFor(item))
571 winner.inventory.add(item);
572 else {
573 winner.inventory.addOrDrop(item);
574 winner.bank.refresh();
575 }
576 }
577
578 send(gambleType, winnerIdentifier, winner, loser);
579
580 }
581
582 loser.getGambling().container.clear();
583 winner.getGambling().container.clear();
584
585 reset(loser);
586 reset(winner);
587 }
588
596 private void send(GambleType gambleType, char winnerIdentifier, Player winner, Player loser) {
597 switch(gambleType) {
598 case FIFTY_FIVE -> {
599 if (winnerIdentifier == 'H')
600 winner.speak("The roll was under 55, I have won the 55x2.");
601 else
602 winner.speak("The roll was higher then 55, I have won the 55x2.");
603 }
604 default -> {
605 winner.speak("I have won!");
606 loser.speak("I have lost!");
607 }
608 }
609 }
610
617 Player other = player.getGambling().getOther();
618
619 if(other == null) return;
620
621 player.getGambling().setConfirmed(false);
622 player.getGambling().setType(type);
623 player.send(new SendConfig(CONFIG, type.ordinal()));
624 player.setLastModification(System.currentTimeMillis());
625
626 other.getGambling().setConfirmed(false);
627 other.getGambling().setType(type);
628 other.send(new SendConfig(CONFIG, type.ordinal()));
629 other.setLastModification(System.currentTimeMillis());
630 }
631
636 public void reset(Player player) {
637
638 if(player.getGambling().getStage().equals(GambleStage.NONE))
639 return;
640
641 player.getGambling().setConfirmed(false);
642 player.getGambling().setGame(null);
643 player.getGambling().setType(null);
644 player.getGambling().setStage(GambleStage.NONE);
645 player.setGambleLock(false);
646 player.send(new SendRemoveInterface());
647 player.send(new SendConfig(CONFIG, 0));
648 removeFlowers(player);
649
650 if(player.getGambling().getOther() != null)
651 reset(player.getGambling().getOther());
652
653 player.getGambling().setOther(null);
654 }
655
656 public void removeFlowers(Player player) {
657 for(CustomGameObject flowers : player.getGambling().getGameFlowers())
658 flowers.unregister();
659
660 player.getGambling().getGameFlowers().clear();
661 player.getGambling().getFlowers().clear();
662 }
663}
void open(Player player, Player other)
void deposit(Player player, int itemId, int slot, int amount)
boolean canGamble(Player player, GambleStage requiredStage)
void withdraw(Player player, int itemId, int slot, int amount)
void acceptRequest(Player player, Player other)
void give(GambleType gambleType, char winnerIdentifier, Player winner, Player loser, boolean draw)
void handleModeSelection(Player player, GambleType type)
boolean canPlayFlowerPokerAtPositon(Player player, Position position)
void sendRequest(Player player, Player other)
void finish(Player host, Player opponent, int hostScore, int opponentScore)
void speak(String forceChat)
Definition Mob.java:164
static boolean isTraversable(Position from, Direction direction, int size)
static Position create(int x, int y, int z)
static String capitalizeSentence(final String string)
Definition Utility.java:136