RuneHive-Game
Loading...
Searching...
No Matches
GambleManager.java
Go to the documentation of this file.
1package com.runehive.content.gambling;
2
3import com.runehive.content.gambling.impl.FiftyFive;
4import com.runehive.content.gambling.impl.FlowerPoker;
5import com.runehive.game.world.World;
6import com.runehive.game.world.entity.mob.Direction;
7import com.runehive.game.world.entity.mob.player.Player;
8import com.runehive.game.world.entity.mob.player.PlayerRight;
9import com.runehive.game.world.items.Item;
10import com.runehive.game.world.items.ItemDefinition;
11import com.runehive.game.world.items.containers.ItemContainer;
12import com.runehive.game.world.object.CustomGameObject;
13import com.runehive.game.world.pathfinding.TraversalMap;
14import com.runehive.game.world.position.Boundary;
15import com.runehive.game.world.position.Position;
16import com.runehive.net.packet.out.*;
17import com.runehive.util.Utility;
18
19import java.util.ArrayList;
20import java.util.Objects;
21
22public class GambleManager {
23
24 /**
25 * The main gambling interface
26 */
27 private final int INTERFACE_ID = 44750;
28 /**
29 * Interface id for the player's inventory
30 */
31 private final int INVENTORY_ID = 44775;
32 /**
33 * The config used for selecting the game type
34 */
35 private final int CONFIG = 444;
36
37 public static final Boundary GAMBLING_ZONE = new Boundary(3148, 3476, 3181, 3505);
38
39 /**
40 * The current stage of the gamble
41 */
43
45 return stage;
46 }
47
48 public void setStage(GambleStage stage) {
49 this.stage = stage;
50 }
51
52 /**
53 * The other player within this gamble
54 */
55 protected Player other;
56
57 public Player getOther() {
58 return other;
59 }
60
61 public void setOther(Player other) {
62 this.other = other;
63 }
64
65 /**
66 * Checks if the player has confirmed the gamble
67 */
68 protected boolean confirmed;
69
70 public boolean hasConfirmed() {
71 return confirmed;
72 }
73
74 public void setConfirmed(boolean confirmed) {
75 this.confirmed = confirmed;
76 }
77
78 /**
79 * The gamble type going on
80 */
82
84 return type;
85 }
86
87 public void setType(GambleType type) {
88 this.type = type;
89 }
90
91 /**
92 * The current gamble going on between the two players
93 */
94 protected Gamble game;
95
96 public Gamble getGame() {
97 return game;
98 }
99
100 public void setGame(Gamble game) {
101 this.game = game;
102 }
103
104 /**
105 * The container with gambled items of the player
106 */
108
110 return container;
111 }
112
113 protected ArrayList<CustomGameObject> gameFlowers = new ArrayList<CustomGameObject>();
114
115 public ArrayList<CustomGameObject> getGameFlowers() {
116 return gameFlowers;
117 }
118
119
120 protected ArrayList<Flowers> flowers = new ArrayList<Flowers>();
121
122 public ArrayList<Flowers> getFlowers() {
123 return flowers;
124 }
125
126 /**
127 * Checks if the player is allowed to gamble or not
128 * @param player
129 * @param requiredStage
130 * @return
131 */
132 public boolean canGamble(Player player, GambleStage requiredStage) {
133
134 if(!Boundary.isIn(player, GAMBLING_ZONE)) return false;
135
136 if (Objects.nonNull(requiredStage) && !player.getGambling().getStage().equals(requiredStage)) return false;
137
138 return true;
139 }
140
141 /**
142 * Handles sending a request to another player
143 * @param player
144 * @param other
145 */
146 public void sendRequest(Player player, Player other) {
147
148 if (PlayerRight.isIronman(player)) {
149 player.message("You can not gamble as you are an iron man.");
150 return;
151 }
152
154 player.message(other.getName() + " can not gamble as they are an iron man.");
155 return;
156 }
157 if(player.playTime < 6000) {
158 player.message("You must have at least 1 hour of play time to gamble.");
159 return;
160 }
161
162 Player requested = other.getGambling().getOther();
163
164 if (!Objects.isNull(requested)) {
165 System.out.println("Accept...");
166 acceptRequest(player, other);
167 return;
168 }
169
170 player.message("You've sent a gamble invite to " + Utility.capitalizeSentence(other.getName()) + ".");
171 other.message(Utility.capitalizeSentence(player.getName()) + ":gamblereq:");
172 player.send(new SendRemoveInterface());
173 player.getGambling().setOther(other);
175 System.out.println("send request...");
176 }
177
178 /**
179 * Handles accepting a request
180 * @param player
181 * @param other
182 */
183 public void acceptRequest(Player player, Player other) {
184 if(player == other) return;
185
187
189 player.getGambling().setOther(other);
190
191 if (other.getGambling().getOther() != null && player.getIndex() == other.getGambling().getOther().getIndex()) {
192 System.out.println("accepted request...");
193 open(other, player);
194 open(player, other);
195 }
196 }
197
198 /**
199 * Handles opening the interface
200 * @param player
201 * @param other
202 */
203 public void open(Player player, Player other) {
204 player.getGambling().setConfirmed(false);
206 System.out.println("Open the interface for ["+String.format(player.getUsername())+"] and ["+String.format(other.getUsername())+"]...");
207
209 player.send(new SendItemOnInterface(44770, player.getGambling().getContainer().toArray()));
210 player.send(new SendItemOnInterface(44771, other.getGambling().getContainer().toArray()));
211 player.send(new SendItemOnInterface(INVENTORY_ID + 1, player.inventory.getItems()));
212
213 player.send(new SendString("" + String.format(player.getUsername()), 44768));
214 player.send(new SendString("" + String.format(other.getUsername()), 44769));
215 player.send(new SendConfig(CONFIG, 0));
216 }
217
218 /**
219 * Handles accepting the gamble
220 * @param player
221 */
222 public void accept(Player player) {
223 Player other = player.getGambling().getOther();
224
225 if (other == null) return;
226
227 if(player.getGambling().getType() == null || player.getGambling().getType().equals(GambleType.NONE)) {
228 player.message("You need to select a game first.");
229 return;
230 }
231
232 if (System.currentTimeMillis() - player.getLastModification() < 5_000) {
233 player.message("@red@Something was changed in the last 5 seconds, you cannot accept yet.");
234 return;
235 }
236
237 if (player.getGambling().getType() != other.getGambling().getType()) return;
238
239 if(player.getGambling().hasConfirmed()) return;
240
241 player.getGambling().setConfirmed(true);
242
243 player.message("You have accepted the gamble with "+String.format(other.getUsername())+".");
244 other.message(String.format(player.getUsername()) + " has accepted the gamble.");
245
246 System.out.println("player: " + String.format(player.getUsername()));
247 System.out.println("other: " + String.format(other.getUsername()));
248
249 if (other.getGambling().hasConfirmed()) {
250 Gamble game = getGame(other, player, other.getGambling().getType());
251
252 player.getGambling().setGame(game);
253 other.getGambling().setGame(game);
254
256
257 player.getGambling().setOther(other);
258
259 System.out.println("Both confirmed start the game...");
260
261 start(player);
262 }
263 }
264
265 /**
266 * Checks if the position that was selected is suitable for a game of flower poker
267 * @param player
268 * @param position
269 * @return
270 */
271 public boolean canPlayFlowerPokerAtPositon(Player player, Position position) {
272 //Checks if the spot its checking has a object placed there
273 if (!TraversalMap.isTraversable(new Position(position.getX() + 1, position.getY()), Direction.SOUTH, player.width())) return false;
274
275 boolean canPlay = true;
276 for(int index = 0; index < 5; index++) {
277 if (!TraversalMap.isTraversable(position, Direction.SOUTH, player.width())) {
278 canPlay = false;
279 break;
280 }
281 position = position.create(position.getX(), position.getY() + Direction.SOUTH.getDirectionY());
282 }
283 return canPlay;
284 }
285
286 /**
287 * Handles declining the gamble
288 * @param player
289 */
290 public void decline(Player player) {
291 Player other = player.getGambling().getOther();
292
293 for(Item item : player.getGambling().getContainer()) {
294 if(item == null || item.getId() == -1) continue;
295
296 if(player.inventory.hasCapacityFor(item))
297 player.inventory.add(item);
298 else {
299 player.bank.add(item);
300 player.message("@red@You had no room for the "+item.getAmount()+" x "+item.getDefinition().getName()+", its been sent to your bank.");
301 }
302 }
303
304 player.getGambling().getContainer().clear();
305 player.inventory.refresh();
306 player.bank.refresh();
307
308 if(other != null) {
309
310 for(Item item : other.getGambling().getContainer()) {
311 if(item == null || item.getId() == -1) continue;
312
313 if(other.inventory.hasCapacityFor(item))
314 other.inventory.add(item);
315 else {
316 other.bank.add(item);
317 other.message("@red@You had no room for the "+item.getAmount()+" x "+item.getDefinition().getName()+", its been sent to your bank.");
318 }
319 }
320
321 other.getGambling().getContainer().clear();
322 other.inventory.refresh();
323 other.bank.refresh();
324
325 reset(other);
326 }
327
328 reset(player);
329 }
330
331 /**
332 * Deposit a item into the gamble session
333 * @param player
334 * @param slot
335 */
336 public void deposit(Player player, int itemId, int slot, int amount) {
337 Player other = player.getGambling().getOther();
338
339 if(other == null) return;
340
341 ItemDefinition def = ItemDefinition.get(itemId);
342
343 if (!def.isTradeable()) {
344 player.send(new SendMessage("This is item is untradeable!"));
345 return;
346 }
347
348 if(!player.inventory.contains(itemId)) return;
349
350 if(amount > player.inventory.computeAmountForId(itemId))
351 amount = player.inventory.computeAmountForId(itemId);
352
353 if(amount <= 0) return;
354
355 Item item = new Item(itemId, amount);
356 if(player.getGambling().getContainer().hasCapacityFor(item)) {
357 player.inventory.remove(item, slot);
358 player.getGambling().getContainer().add(item);
359 player.inventory.refresh();
360 player.inventory.refresh(player, INVENTORY_ID + 1);
361 } else
362 player.message("Not enough space.");
363
364 player.setLastModification(System.currentTimeMillis());
365 other.setLastModification(System.currentTimeMillis());
366
367 player.send(new SendItemOnInterface(44770, player.getGambling().getContainer().toArray()));
368 player.send(new SendItemOnInterface(44771, other.getGambling().getContainer().toArray()));
369
370 other.send(new SendItemOnInterface(44770, other.getGambling().getContainer().toArray()));
371 other.send(new SendItemOnInterface(44771, player.getGambling().getContainer().toArray()));
372 }
373
374 /**
375 * Withdraw an item from the gamble session
376 * @param player
377 * @param slot
378 */
379 public void withdraw(Player player, int itemId, int slot, int amount) {
380 Player other = player.getGambling().getOther();
381
382 if(other == null) return;
383
384 Item clickedItem = player.getGambling().getContainer().get(slot);
385
386 if(clickedItem.getId() != itemId) return;
387
388 if(amount > clickedItem.getAmount())
389 amount = clickedItem.getAmount();
390
391 if(!clickedItem.getDefinition().isStackable() && amount > player.inventory.getFreeSlots())
392 amount = player.inventory.getFreeSlots();
393
394 if(amount <= 0) return;
395
396 player.getGambling().getContainer().remove(itemId, amount);
397 player.inventory.add(itemId, amount);
398
399 player.inventory.refresh();
400 player.inventory.refresh(player, INVENTORY_ID + 1);
401
402 player.setLastModification(System.currentTimeMillis());
403 other.setLastModification(System.currentTimeMillis());
404
405 player.send(new SendItemOnInterface(44770, player.getGambling().getContainer().toArray()));
406 player.send(new SendItemOnInterface(44771, other.getGambling().getContainer().toArray()));
407
408 other.send(new SendItemOnInterface(44770, other.getGambling().getContainer().toArray()));
409 other.send(new SendItemOnInterface(44771, player.getGambling().getContainer().toArray()));
410 }
411
412 /**
413 * Get thet game based on the gamble type
414 * @param other
415 * @param player
416 * @param type
417 * @return
418 */
420 switch (type) {
421 case FIFTY_FIVE:
422 return new FiftyFive(player, other);
423 case FLOWER_POKER:
424 return new FlowerPoker(player, other);
425 }
426 return null;
427 }
428
429 /**
430 * Handles starting the gamble
431 * @param player
432 */
433 private void start(Player player) {
434 if (!canGamble(player, GambleStage.SENDING_OFFER)) return;
435
436 Player other = player.getGambling().getOther();
437
438 if (other == null) return;
439
440 if (other.getGambling().hasConfirmed()) {
441 if (player.getGambling().getGame() == null) return;
442
443 if (player.getGambling().getGame().getHost() == null) return;
444
445 player.send(new SendRemoveInterface());
446 other.send(new SendRemoveInterface());
447
449 other.getGambling().setStage(GambleStage.IN_PROGRESS);
450
451 player.setGambleLock(true);
452 other.setGambleLock(true);
453
454 player.getGambling().getGame().gamble();
455 }
456 }
457
458 /**
459 * Handles finishing up a automated gamble
460 * @param host
461 * @param opponent
462 * @param hostScore
463 * @param opponentScore
464 */
465 public void finish(Player host, Player opponent, int hostScore, int opponentScore) {
466 if (host.getGambling().getGame() == null)
467 return;
468
469 if (host.getGambling().getGame().getHost() == null)
470 return;
471
472 if (!World.getPlayers().contains(opponent) && World.getPlayers().contains(host)) {
473 give(host.getGambling().getType(), 'H', host, opponent, false);
474 } else if (World.getPlayers().contains(opponent) && !World.getPlayers().contains(host)) {
475 give(host.getGambling().getType(), 'O', opponent, host, false);
476 } else {
477 if (hostScore == 55) {
478 host.speak("The roll was a draw, rerolling...");
479 opponent.speak("The roll was a draw, rerolling...");
480
483 host.getGambling().getGame().gamble();
484 return;
485 }
486
487 boolean hostWon = hostScore > opponentScore;
488 give(host.getGambling().getType(), hostWon ? 'H' : 'O', hostWon ? host : opponent, hostWon ? opponent : host, false);
489 }
490 }
491
492 /**
493 * Handles giving the players the winnings or returning the items when its a draw
494 * @param gambleType
495 * @param winnerIdentifier
496 * @param winner
497 * @param loser
498 * @param draw
499 */
500 public void give(GambleType gambleType, char winnerIdentifier, Player winner, Player loser, boolean draw) {
501
502 if(draw) {
503
504 winner.speak("It's a draw!");
505 loser.speak("It's a draw!");
506
507 removeFlowers(winner);
508 removeFlowers(loser);
509
512
513 winner.getGambling().getGame().gamble();
514
515 return;
516 } else {
517
518 //Handle giving the winner the items
519 for(Item item : winner.getGambling().getContainer()) {
520 if(item == null || item.getId() == -1) continue;
521
522 if(winner.inventory.hasCapacityFor(item))
523 winner.inventory.add(item);
524 else {
525 winner.inventory.addOrDrop(item);
526 winner.bank.refresh();
527 }
528 }
529
530 for(Item item : loser.getGambling().getContainer()) {
531 if(item == null || item.getId() == -1) continue;
532
533 if(winner.inventory.hasCapacityFor(item))
534 winner.inventory.add(item);
535 else {
536 winner.inventory.addOrDrop(item);
537 winner.bank.refresh();
538 }
539 }
540
541 send(gambleType, winnerIdentifier, winner, loser);
542
543 }
544
545 loser.getGambling().container.clear();
546 winner.getGambling().container.clear();
547
548 reset(loser);
549 reset(winner);
550 }
551
552 /**
553 * Handles sending the message for who won
554 * @param gambleType
555 * @param winnerIdentifier
556 * @param winner
557 * @param loser
558 */
559 private void send(GambleType gambleType, char winnerIdentifier, Player winner, Player loser) {
560 switch(gambleType) {
561 case FIFTY_FIVE -> {
562 if (winnerIdentifier == 'H')
563 winner.speak("The roll was under 55, I have won the 55x2.");
564 else
565 winner.speak("The roll was higher then 55, I have won the 55x2.");
566 }
567 default -> {
568 winner.speak("I have won!");
569 loser.speak("I have lost!");
570 }
571 }
572 }
573
574 /**
575 * Handles setting the different game types
576 * @param player
577 * @param type
578 */
580 Player other = player.getGambling().getOther();
581
582 if(other == null) return;
583
584 player.getGambling().setConfirmed(false);
585 player.getGambling().setType(type);
586 player.send(new SendConfig(CONFIG, type.ordinal()));
587 player.setLastModification(System.currentTimeMillis());
588
589 other.getGambling().setConfirmed(false);
590 other.getGambling().setType(type);
591 other.send(new SendConfig(CONFIG, type.ordinal()));
592 other.setLastModification(System.currentTimeMillis());
593 }
594
595 /**
596 * Handles resetting all the gambling variables for a player
597 * @param player
598 */
599 public void reset(Player player) {
600
601 if(player.getGambling().getStage().equals(GambleStage.NONE))
602 return;
603
604 player.getGambling().setConfirmed(false);
605 player.getGambling().setGame(null);
606 player.getGambling().setType(null);
608 player.setGambleLock(false);
609 player.send(new SendRemoveInterface());
610 player.send(new SendConfig(CONFIG, 0));
611 removeFlowers(player);
612
613 if(player.getGambling().getOther() != null)
614 reset(player.getGambling().getOther());
615
616 player.getGambling().setOther(null);
617 }
618
619 public void removeFlowers(Player player) {
621 flowers.unregister();
622
623 player.getGambling().getGameFlowers().clear();
624 player.getGambling().getFlowers().clear();
625 }
626}
void finish(Player host, Player opponent, int hostScore, int opponentScore)
Handles finishing up a automated gamble.
void send(GambleType gambleType, char winnerIdentifier, Player winner, Player loser)
Handles sending the message for who won.
GambleStage stage
The current stage of the gamble.
void acceptRequest(Player player, Player other)
Handles accepting a request.
Player other
The other player within this gamble.
void give(GambleType gambleType, char winnerIdentifier, Player winner, Player loser, boolean draw)
Handles giving the players the winnings or returning the items when its a draw.
void decline(Player player)
Handles declining the gamble.
ItemContainer container
The container with gambled items of the player.
void deposit(Player player, int itemId, int slot, int amount)
Deposit a item into the gamble session.
boolean confirmed
Checks if the player has confirmed the gamble.
ArrayList< CustomGameObject > gameFlowers
GambleType type
The gamble type going on.
final int INTERFACE_ID
The main gambling interface.
Gamble game
The current gamble going on between the two players.
void reset(Player player)
Handles resetting all the gambling variables for a player.
void accept(Player player)
Handles accepting the gamble.
Gamble getGame(Player other, Player player, GambleType type)
Get thet game based on the gamble type.
void open(Player player, Player other)
Handles opening the interface.
final int INVENTORY_ID
Interface id for the player's inventory.
void handleModeSelection(Player player, GambleType type)
Handles setting the different game types.
boolean canPlayFlowerPokerAtPositon(Player player, Position position)
Checks if the position that was selected is suitable for a game of flower poker.
ArrayList< CustomGameObject > getGameFlowers()
void start(Player player)
Handles starting the gamble.
final int CONFIG
The config used for selecting the game type.
boolean canGamble(Player player, GambleStage requiredStage)
Checks if the player is allowed to gamble or not.
void withdraw(Player player, int itemId, int slot, int amount)
Withdraw an item from the gamble session.
void sendRequest(Player player, Player other)
Handles sending a request to another player.
Represents the game world.
Definition World.java:46
static MobList< Player > getPlayers()
Definition World.java:544
void speak(String forceChat)
Sets the mob's forced chat.
Definition Mob.java:127
This class represents a character controlled by a player.
Definition Player.java:125
String getName()
Gets the name of this entity.
Definition Player.java:774
void setLastModification(long lastModification)
Definition Player.java:142
Represents all of an in-game Item's attributes.
static ItemDefinition get(int id)
Gets an item definition.
The container class that represents an item that can be interacted with.
Definition Item.java:21
final int getId()
Gets the identification of this item.
Definition Item.java:324
final int getAmount()
Gets the quantity of this item.
Definition Item.java:342
ItemDefinition getDefinition()
Gets the item definition for the item identifier.
Definition Item.java:315
An abstraction game representing a group of Items.
final int computeAmountForId(int id)
Computes the total quantity of the Items in this container with id.
boolean remove(Item item)
Attempts to withdraw item from this container.
final Item get(int index)
Gets the Item located on index.
boolean add(Item item)
Attempts to deposit item into this container.
final boolean hasCapacityFor(Item... item)
Determines if this container has the capacity for item.
boolean contains(int id)
Determines if this container contains id.
void clear()
Removes all of the items from this container.
final Item[] toArray()
Returns a shallow copy of the backing array.
void refresh()
Refreshes the bank itemcontainer.
Definition Bank.java:73
void refresh()
Refreshes the players inventory.
void addOrDrop(List< Item > items)
Attempts to deposit an item to the players inventory, if there is no space it'll bank the item instea...
Represents a static game object loaded from the map fs.
Contains traversal data for a set of regions.
static boolean isTraversable(Position from, Direction direction, int size)
Tests whether or not a specified position is traversable in the specified direction.
Created by Daniel on 2017-11-24.
Definition Boundary.java:12
static boolean isIn(Mob mob, Boundary... boundaries)
Definition Boundary.java:49
Represents a single tile on the game world.
Definition Position.java:14
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
static Position create(int x, int y, int z)
Creates a location.
The OutgoingPacket responsible for changing settings on a client.
The OutgoingPacket that opens the inventory with itemcontainer.
The OutgoingPacket that sends a message to a Players chatbox in the client.
The OutgoingPacket that sends a string to a Players itemcontainer in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static String capitalizeSentence(final String string)
Capitalize each letter after .
Definition Utility.java:99
Represents the enumerated directions an entity can walk or face.
static boolean isIronman(Player player)
Checks if the player is an ironman.
An enumerated type defining policies for stackable Items.
STANDARD
The STANDARD policy, items are only stacked if they are defined as stackable in their ItemDefinition ...