RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
LobbyManager.java
1package com.osroyale.content.activity.lobby;
2
3import com.osroyale.game.world.entity.mob.player.Player;
4
5import java.util.Deque;
6import java.util.Iterator;
7import java.util.LinkedList;
8
43
44public abstract class LobbyManager {
45 private final Deque<LobbyNode> active = new LinkedList<>();
46
47 private final int playerCapacity;
48 private final int gameCapacity;
49 private final int lobbyCooldown;
50 private final int gameCooldown;
51 private final int minimumRequired;
52
53 protected LobbyManager(int gameCapacity, int playerCapacity, int minimumRequired, int lobbyCooldown, int gameCooldown) {
54 this.playerCapacity = playerCapacity;
55 this.gameCapacity = gameCapacity;
56 this.lobbyCooldown = lobbyCooldown;
57 this.gameCooldown = gameCooldown;
58 this.minimumRequired = minimumRequired;
59 }
60
61 public final void enter(Player player) {
62 LobbyNode lobby = nextLobby();
63
64 if (lobby == null) {
65 return;
66 }
67
68 if (lobby.getActiveSize() >= playerCapacity) {
69 exceededPlayerCapacity(player);
70 return;
71 }
72
73 lobby.addActivity(player, lobby.createActivity(player));
74
75 /* reset lobby cooldown since first player just joined */
76 if (minimumRequired > -1 && lobby.getActiveSize() == 1) {
77 lobby.cooldown(lobbyCooldown);
78 }
79
80 onEnter(player);
81 }
82
83 public final void leave(Player player) {
84 LobbyNode lobby = findNode(player);
85
86 if (lobby == null) {
87 return;
88 }
89
90 lobby.removeActivity(player);
91 onLeave(player);
92 }
93
94 public final void sequenceActive() {
95 Iterator<LobbyNode> iterator = active.iterator();
96
97 while (iterator.hasNext()) {
98 LobbyNode node = iterator.next();
99
100 if (node.finished()) {
101 node.finish();
102 node.removeAll();
103 node.cleanup();
104 iterator.remove();
105 continue;
106 }
107
108 node.sequence();
109 }
110 }
111
112 protected abstract LobbyNode createLobby();
113
114 protected void onEnter(Player player) {
115 /* do nothing by default */
116 }
117
118 protected void onLeave(Player player) {
119 /* do nothing by default */
120 }
121
122 protected void exceededGameCapacity(Player player) {
123 /* do nothing by default */
124 }
125
126 protected void exceededPlayerCapacity(Player player) {
127 /* do nothing by default */
128 }
129
130 private LobbyNode findNode(Player player) {
131 for (LobbyNode node : active) {
132 if (node.contains(player)) {
133 return node;
134 }
135 }
136 return null;
137 }
138
139 private LobbyNode nextLobby() {
140 for (LobbyNode node : active) {
141 if (node.inLobby()) {
142 return node;
143 }
144 }
145
146 if (active.size() >= 1 + gameCapacity) {
147 return null;
148 }
149
150 LobbyNode activity = createLobby();
151 active.addFirst(activity);
152 activity.lobby = true;
153 return activity;
154 }
155
156 public boolean canStart(LobbyNode node) {
157 if (!node.lobby) {
158 return false;
159 }
160
161 int activeGames = 0;
162 for (LobbyNode activity : active) {
163 if (!activity.lobby)
164 activeGames++;
165 }
166
167 if (activeGames >= gameCapacity) {
168 node.cooldown(lobbyCooldown);
169 node.forEachActivity((mob, activity) -> {
170 if (mob.isPlayer()) {
171 exceededGameCapacity(mob.getPlayer());
172 }
173 });
174 return false;
175 }
176
177 if (minimumRequired > node.getActiveSize()) {
178 node.cooldown(lobbyCooldown);
179 node.groupMessage("This activity requires at least " + minimumRequired + " players to start.");
180 return false;
181 }
182
183 return true;
184 }
185
186 public int getGameCapacity() {
187 return gameCapacity;
188 }
189
190 public int getPlayerCapacity() {
191 return playerCapacity;
192 }
193
194 public int getMinimumRequired() {
195 return minimumRequired;
196 }
197
198 public int getGameCooldown() {
199 return gameCooldown;
200 }
201
202 public int getLobbyCooldown() {
203 return lobbyCooldown;
204 }
205
206}
void forEachActivity(BiConsumer< Mob, Activity > activity)