Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/games/InteractionGameTimer.java

360 lines
11 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions.games;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.games.Game;
2019-03-18 02:22:00 +01:00
import com.eu.habbo.habbohotel.games.GameState;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.items.Item;
2018-09-12 18:45:00 +02:00
import com.eu.habbo.habbohotel.permissions.Permission;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.Room;
2019-03-18 02:22:00 +01:00
import com.eu.habbo.habbohotel.rooms.RoomUnit;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.users.HabboItem;
import com.eu.habbo.habbohotel.wired.WiredEffectType;
2019-05-04 22:41:18 +02:00
import com.eu.habbo.habbohotel.wired.WiredHandler;
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.ServerMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
2019-05-26 20:14:53 +02:00
public abstract class InteractionGameTimer extends HabboItem implements Runnable {
2018-07-06 15:30:00 +02:00
private int baseTime = 0;
2019-05-04 22:41:18 +02:00
private int timeNow = 0;
private boolean isRunning = false;
private boolean isPaused = false;
2019-05-24 13:12:22 +02:00
private boolean threadActive = false;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public InteractionGameTimer(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
String[] data = set.getString("extra_data").split("\t");
2019-05-26 20:14:53 +02:00
if (data.length >= 2) {
2018-07-06 15:30:00 +02:00
this.baseTime = Integer.valueOf(data[1]);
2019-05-04 22:41:18 +02:00
this.timeNow = this.baseTime;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
if (data.length >= 1) {
2018-07-06 15:30:00 +02:00
this.setExtradata(data[0]);
}
}
2019-05-26 20:14:53 +02:00
public InteractionGameTimer(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
2018-07-06 15:30:00 +02:00
super(id, userId, item, extradata, limitedStack, limitedSells);
}
2019-05-04 22:41:18 +02:00
public static void endGamesIfLastTimer(Room room) {
boolean gamesActive = false;
for (InteractionGameTimer timer : room.getRoomSpecialTypes().getGameTimers().values()) {
if (timer.isRunning())
gamesActive = true;
}
if (!gamesActive) {
endGames(room);
}
}
public static void endGames(Room room) {
2019-05-05 04:51:27 +02:00
endGames(room, false);
}
public static void endGames(Room room, boolean overrideTriggerWired) {
boolean triggerWired = false;
2019-05-04 22:41:18 +02:00
//end existing games
for (Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
Game game = InteractionGameTimer.getOrCreateGame(room, gameClass);
if (!game.state.equals(GameState.IDLE)) {
2019-05-05 04:51:27 +02:00
triggerWired = true;
2019-05-04 22:41:18 +02:00
game.onEnd();
game.stop();
}
}
2019-05-26 20:14:53 +02:00
if (triggerWired) {
2019-05-05 04:51:27 +02:00
WiredHandler.handle(WiredTriggerType.GAME_ENDS, null, room, new Object[]{});
}
2019-05-04 22:41:18 +02:00
}
2019-05-26 20:14:53 +02:00
public static Game getOrCreateGame(Room room, Class<? extends Game> gameClass) {
Game game = (gameClass.cast(room.getGame(gameClass)));
if (game == null) {
try {
game = gameClass.getDeclaredConstructor(Room.class).newInstance(room);
room.addGame(game);
} catch (Exception e) {
Emulator.getLogging().logErrorLine(e);
}
}
return game;
}
@Override
public void run() {
if (this.needsUpdate() || this.needsDelete()) {
super.run();
}
if (this.getRoomId() == 0) {
this.threadActive = false;
return;
}
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
if (room == null || !this.isRunning || this.isPaused) {
this.threadActive = false;
return;
}
if (this.timeNow > 0) {
this.threadActive = true;
Emulator.getThreading().run(this, 1000);
this.timeNow--;
room.updateItem(this);
} else {
this.isRunning = false;
this.isPaused = false;
this.threadActive = false;
endGamesIfLastTimer(room);
}
}
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp(Room room) {
2018-07-06 15:30:00 +02:00
this.setExtradata("0");
}
@Override
2019-05-26 20:14:53 +02:00
public void onPlace(Room room) {
if (this.baseTime == 0) {
2019-05-04 22:41:18 +02:00
this.baseTime = 30;
this.timeNow = this.baseTime;
}
this.setExtradata(this.timeNow + "\t" + this.baseTime);
2018-07-06 15:30:00 +02:00
room.updateItem(this);
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeExtradata(ServerMessage serverMessage) {
2018-07-06 15:30:00 +02:00
serverMessage.appendInt((this.isLimited() ? 256 : 0));
2019-05-04 22:41:18 +02:00
serverMessage.appendString("" + timeNow);
2018-07-06 15:30:00 +02:00
super.serializeExtradata(serverMessage);
}
2019-03-18 02:22:00 +01:00
@Override
2019-05-26 20:14:53 +02:00
public boolean canWalkOn(RoomUnit roomUnit, Room room, Object[] objects) {
2019-03-18 02:22:00 +01:00
return false;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean isWalkable() {
2019-03-18 02:22:00 +01:00
return false;
}
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public void onClick(GameClient client, Room room, Object[] objects) throws Exception {
if (this.getExtradata().isEmpty()) {
2019-05-04 22:41:18 +02:00
this.setExtradata("0");
2018-07-06 15:30:00 +02:00
}
2019-05-04 22:41:18 +02:00
// if wired triggered it
2019-05-26 20:14:53 +02:00
if (objects.length >= 2 && objects[1] instanceof WiredEffectType && !this.isRunning) {
2019-05-05 04:51:27 +02:00
endGamesIfLastTimer(room);
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
for (Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
2019-05-04 22:41:18 +02:00
Game game = getOrCreateGame(room, gameClass);
2019-05-26 20:14:53 +02:00
if (!game.isRunning) {
2019-05-05 04:51:27 +02:00
game.initialise();
2019-05-04 22:41:18 +02:00
}
}
2019-03-18 02:22:00 +01:00
2019-05-04 22:41:18 +02:00
timeNow = this.baseTime;
this.isRunning = true;
room.updateItem(this);
2019-05-26 20:14:53 +02:00
WiredHandler.handle(WiredTriggerType.GAME_STARTS, null, room, new Object[]{});
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (!this.threadActive) {
2019-05-24 13:12:22 +02:00
this.threadActive = true;
Emulator.getThreading().run(this);
}
2019-05-26 20:14:53 +02:00
} else if (client != null) {
2019-05-04 22:41:18 +02:00
if (!(room.hasRights(client.getHabbo()) || client.getHabbo().hasPermission(Permission.ACC_ANYROOMOWNER)))
return;
int state = 1;
2019-05-26 20:14:53 +02:00
if (objects.length >= 1 && objects[0] instanceof Integer) {
2019-05-04 22:41:18 +02:00
state = (Integer) objects[0];
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
switch (state) {
2018-07-06 15:30:00 +02:00
case 1:
2019-05-26 20:14:53 +02:00
if (this.isRunning) {
2019-05-04 22:41:18 +02:00
this.isPaused = !this.isPaused;
2019-05-05 04:51:27 +02:00
boolean allPaused = this.isPaused;
2019-05-26 20:14:53 +02:00
for (InteractionGameTimer timer : room.getRoomSpecialTypes().getGameTimers().values()) {
if (!timer.isPaused)
2019-05-05 04:51:27 +02:00
allPaused = false;
}
2019-05-26 20:14:53 +02:00
for (Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
2019-05-04 22:41:18 +02:00
Game game = getOrCreateGame(room, gameClass);
2019-05-26 20:14:53 +02:00
if (allPaused) {
2019-05-04 22:41:18 +02:00
game.pause();
2019-05-26 20:14:53 +02:00
} else {
2019-05-04 22:41:18 +02:00
game.unpause();
}
}
2019-05-26 20:14:53 +02:00
if (!this.isPaused) {
2019-05-05 04:51:27 +02:00
this.isRunning = true;
timeNow = this.baseTime;
room.updateItem(this);
2019-05-24 13:12:22 +02:00
2019-05-26 20:14:53 +02:00
if (!this.threadActive) {
2019-05-24 13:12:22 +02:00
this.threadActive = true;
Emulator.getThreading().run(this);
}
2019-05-04 22:41:18 +02:00
}
}
2019-05-26 20:14:53 +02:00
if (!this.isRunning) {
2019-05-05 04:51:27 +02:00
endGamesIfLastTimer(room);
2019-05-26 20:14:53 +02:00
for (Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
2019-05-04 22:41:18 +02:00
Game game = getOrCreateGame(room, gameClass);
game.initialise();
}
2019-05-26 20:14:53 +02:00
WiredHandler.handle(WiredTriggerType.GAME_STARTS, null, room, new Object[]{});
2019-05-04 22:41:18 +02:00
this.isRunning = true;
2019-05-05 04:51:27 +02:00
timeNow = this.baseTime;
2019-05-04 22:41:18 +02:00
room.updateItem(this);
2019-05-24 13:12:22 +02:00
2019-05-26 20:14:53 +02:00
if (!this.threadActive) {
2019-05-24 13:12:22 +02:00
this.threadActive = true;
Emulator.getThreading().run(this);
}
2019-05-04 22:41:18 +02:00
}
2018-07-06 15:30:00 +02:00
break;
case 2:
2019-05-26 20:14:53 +02:00
if (!this.isRunning) {
2019-05-04 22:41:18 +02:00
this.increaseTimer(room);
return;
}
2019-05-26 20:14:53 +02:00
if (this.isPaused) {
2019-05-04 22:41:18 +02:00
this.isPaused = false;
this.isRunning = false;
timeNow = this.baseTime;
room.updateItem(this);
endGamesIfLastTimer(room);
}
break;
2018-07-06 15:30:00 +02:00
case 3:
2019-05-04 22:41:18 +02:00
this.isPaused = false;
this.isRunning = false;
timeNow = this.baseTime;
room.updateItem(this);
boolean gamesActive = false;
for (InteractionGameTimer timer : room.getRoomSpecialTypes().getGameTimers().values()) {
if (timer.isRunning())
gamesActive = true;
}
if (!gamesActive) {
endGames(room);
}
break;
2018-07-06 15:30:00 +02:00
}
}
super.onClick(client, room, objects);
}
2019-03-18 02:22:00 +01:00
@Override
2019-05-26 20:14:53 +02:00
public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception {
2019-03-18 02:22:00 +01:00
}
2019-05-26 20:14:53 +02:00
private void increaseTimer(Room room) {
if (this.isRunning)
2018-07-06 15:30:00 +02:00
return;
this.needsUpdate(true);
2019-05-04 22:41:18 +02:00
2019-05-26 20:14:53 +02:00
switch (this.baseTime) {
case 0:
this.baseTime = 30;
break;
case 30:
this.baseTime = 60;
break;
case 60:
this.baseTime = 120;
break;
case 120:
this.baseTime = 180;
break;
case 180:
this.baseTime = 300;
break;
case 300:
this.baseTime = 600;
break;
2018-07-06 15:30:00 +02:00
//case 600: this.baseTime = 0; break;
default:
this.baseTime = 30;
}
2019-05-04 22:41:18 +02:00
this.timeNow = this.baseTime;
2018-07-06 15:30:00 +02:00
room.updateItem(this);
2019-05-04 22:41:18 +02:00
this.needsUpdate(true);
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public String getDatabaseExtraData() {
2018-07-06 15:30:00 +02:00
return this.getExtradata() + "\t" + this.baseTime;
}
public abstract Class<? extends Game> getGameType();
2018-11-17 14:28:00 +01:00
@Override
2019-05-26 20:14:53 +02:00
public boolean allowWiredResetState() {
2018-11-17 14:28:00 +01:00
return true;
}
2019-05-04 22:41:18 +02:00
public boolean isRunning() {
return isRunning;
}
public void setRunning(boolean running) {
isRunning = running;
}
public int getTimeNow() {
return timeNow;
}
public void setTimeNow(int timeNow) {
this.timeNow = timeNow;
}
2018-07-06 15:30:00 +02:00
}