RuneHive-Game
Loading...
Searching...
No Matches
LobbyManager.java
Go to the documentation of this file.
1package com.runehive.content.activity.lobby;
2
3import com.runehive.game.world.entity.mob.player.Player;
4
5import java.util.Deque;
6import java.util.Iterator;
7import java.util.LinkedList;
8
9public abstract class LobbyManager {
10 private final Deque<LobbyNode> active = new LinkedList<>();
11
12 private final int playerCapacity;
13 private final int gameCapacity;
14 private final int lobbyCooldown;
15 private final int gameCooldown;
16 private final int minimumRequired;
17
19 this.playerCapacity = playerCapacity;
20 this.gameCapacity = gameCapacity;
21 this.lobbyCooldown = lobbyCooldown;
22 this.gameCooldown = gameCooldown;
23 this.minimumRequired = minimumRequired;
24 }
25
26 public final void enter(Player player) {
28
29 if (lobby == null) {
30 return;
31 }
32
33 if (lobby.getActiveSize() >= playerCapacity) {
35 return;
36 }
37
38 lobby.addActivity(player, lobby.createActivity(player));
39
40 /* reset lobby cooldown since first player just joined */
41 if (minimumRequired > -1 && lobby.getActiveSize() == 1) {
42 lobby.cooldown(lobbyCooldown);
43 }
44
45 onEnter(player);
46 }
47
48 public final void leave(Player player) {
49 LobbyNode lobby = findNode(player);
50
51 if (lobby == null) {
52 return;
53 }
54
55 lobby.removeActivity(player);
56 onLeave(player);
57 }
58
59 public final void sequenceActive() {
60 Iterator<LobbyNode> iterator = active.iterator();
61
62 while (iterator.hasNext()) {
63 LobbyNode node = iterator.next();
64
65 if (node.finished()) {
66 node.finish();
67 node.removeAll();
68 node.cleanup();
69 iterator.remove();
70 continue;
71 }
72
73 node.sequence();
74 }
75 }
76
77 protected abstract LobbyNode createLobby();
78
79 protected void onEnter(Player player) {
80 /* do nothing by default */
81 }
82
83 protected void onLeave(Player player) {
84 /* do nothing by default */
85 }
86
87 protected void exceededGameCapacity(Player player) {
88 /* do nothing by default */
89 }
90
91 protected void exceededPlayerCapacity(Player player) {
92 /* do nothing by default */
93 }
94
95 private LobbyNode findNode(Player player) {
96 for (LobbyNode node : active) {
97 if (node.contains(player)) {
98 return node;
99 }
100 }
101 return null;
102 }
103
105 for (LobbyNode node : active) {
106 if (node.inLobby()) {
107 return node;
108 }
109 }
110
111 if (active.size() >= 1 + gameCapacity) {
112 return null;
113 }
114
116 active.addFirst(activity);
117 activity.lobby = true;
118 return activity;
119 }
120
121 public boolean canStart(LobbyNode node) {
122 if (!node.lobby) {
123 return false;
124 }
125
126 int activeGames = 0;
127 for (LobbyNode activity : active) {
128 if (!activity.lobby)
129 activeGames++;
130 }
131
132 if (activeGames >= gameCapacity) {
134 node.forEachActivity((mob, activity) -> {
135 if (mob.isPlayer()) {
136 exceededGameCapacity(mob.getPlayer());
137 }
138 });
139 return false;
140 }
141
142 if (minimumRequired > node.getActiveSize()) {
144 node.groupMessage("This activity requires at least " + minimumRequired + " players to start.");
145 return false;
146 }
147
148 return true;
149 }
150
151 public int getGameCapacity() {
152 return gameCapacity;
153 }
154
155 public int getPlayerCapacity() {
156 return playerCapacity;
157 }
158
159 public int getMinimumRequired() {
160 return minimumRequired;
161 }
162
163 public int getGameCooldown() {
164 return gameCooldown;
165 }
166
167 public int getLobbyCooldown() {
168 return lobbyCooldown;
169 }
170
171}
final int cooldown
The sequencing cooldown.
Definition Activity.java:43
void sequence()
Sequences the activity.
final void groupMessage(String message)
Sends a message to all the players in the group.
void forEachActivity(BiConsumer< Mob, Activity > activity)
Loops through all the activities.
void cleanup()
Cleans up the activity when finished.
int getActiveSize()
Gets the size of the activities in this group.
LobbyManager(int gameCapacity, int playerCapacity, int minimumRequired, int lobbyCooldown, int gameCooldown)
This class represents a character controlled by a player.
Definition Player.java:125