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;
|
2020-10-31 03:11:33 +01:00
|
|
|
import com.eu.habbo.habbohotel.games.wired.WiredGame;
|
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;
|
2021-02-04 19:59:16 +01:00
|
|
|
import com.eu.habbo.threading.runnables.games.GameTimer;
|
2020-05-04 22:24:09 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
2019-07-30 12:52:29 +02:00
|
|
|
import java.util.Arrays;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
public class InteractionGameTimer extends HabboItem implements Runnable {
|
2020-05-04 22:24:09 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(InteractionGameTimer.class);
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
private int[] TIMER_INTERVAL_STEPS = new int[] { 30, 60, 120, 180, 300, 600 };
|
|
|
|
|
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-06-03 22:49:17 +02:00
|
|
|
public enum InteractionGameTimerAction {
|
|
|
|
START_STOP(1),
|
|
|
|
INCREASE_TIME(2);
|
|
|
|
|
|
|
|
private int action;
|
|
|
|
|
|
|
|
InteractionGameTimerAction(int action) {
|
|
|
|
this.action = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getAction() {
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static InteractionGameTimerAction getByAction(int action) {
|
|
|
|
if (action == 1) return START_STOP;
|
|
|
|
if (action == 2) return INCREASE_TIME;
|
|
|
|
|
|
|
|
return START_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
parseCustomParams(baseItem);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
try {
|
|
|
|
String[] data = set.getString("extra_data").split("\t");
|
|
|
|
|
|
|
|
if (data.length >= 2) {
|
2021-02-04 19:59:16 +01:00
|
|
|
this.baseTime = Integer.parseInt(data[1]);
|
2019-06-03 22:49:17 +02:00
|
|
|
this.timeNow = this.baseTime;
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
if (data.length >= 1) {
|
|
|
|
this.setExtradata(data[0] + "\t0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
this.baseTime = TIMER_INTERVAL_STEPS[0];
|
|
|
|
this.timeNow = this.baseTime;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-03 22:49:17 +02:00
|
|
|
|
|
|
|
parseCustomParams(item);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 12:52:29 +02:00
|
|
|
private void parseCustomParams(Item baseItem) {
|
2019-06-03 22:49:17 +02:00
|
|
|
try {
|
2019-07-30 12:52:29 +02:00
|
|
|
TIMER_INTERVAL_STEPS = Arrays.stream(baseItem.getCustomParams().split(","))
|
|
|
|
.mapToInt(s -> {
|
|
|
|
try {
|
|
|
|
return Integer.parseInt(s);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}).toArray();
|
|
|
|
} catch (Exception e) {
|
2020-05-04 22:24:09 +02:00
|
|
|
LOGGER.error("Caught exception", e);
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
public void endGame(Room room) {
|
2020-10-31 03:11:33 +01:00
|
|
|
endGame(room, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void endGame(Room room, boolean isStart) {
|
2019-06-03 22:49:17 +02:00
|
|
|
this.isRunning = false;
|
|
|
|
this.isPaused = false;
|
2019-05-05 04:51:27 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
for (Game game : room.getGames()) {
|
2020-10-31 03:11:33 +01:00
|
|
|
if (!game.getState().equals(GameState.IDLE) && !(isStart && game instanceof WiredGame)) {
|
2019-05-04 22:41:18 +02:00
|
|
|
game.onEnd();
|
|
|
|
game.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
private void createNewGame(Room room) {
|
|
|
|
for(Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
|
|
|
|
Game existingGame = room.getGame(gameClass);
|
2019-05-26 20:14:53 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
if (existingGame != null) {
|
|
|
|
existingGame.initialise();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Game game = gameClass.getDeclaredConstructor(Room.class).newInstance(room);
|
|
|
|
room.addGame(game);
|
|
|
|
game.initialise();
|
|
|
|
} catch (Exception e) {
|
2020-05-04 22:24:09 +02:00
|
|
|
LOGGER.error("Caught exception", e);
|
2019-06-03 22:49:17 +02:00
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-03 22:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void pause(Room room) {
|
|
|
|
for (Game game : room.getGames()) {
|
|
|
|
game.pause();
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
private void unpause(Room room) {
|
|
|
|
for (Game game : room.getGames()) {
|
|
|
|
game.unpause();
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (this.needsUpdate() || this.needsDelete()) {
|
|
|
|
super.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public void onPickUp(Room room) {
|
2019-06-03 22:49:17 +02:00
|
|
|
this.endGame(room);
|
|
|
|
|
|
|
|
this.setExtradata(this.baseTime + "\t" + this.baseTime);
|
|
|
|
this.needsUpdate(true);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public void onPlace(Room room) {
|
2019-06-03 22:49:17 +02:00
|
|
|
if (this.baseTime < this.TIMER_INTERVAL_STEPS[0]) {
|
|
|
|
this.baseTime = this.TIMER_INTERVAL_STEPS[0];
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
this.timeNow = this.baseTime;
|
|
|
|
|
2019-05-04 22:41:18 +02:00
|
|
|
this.setExtradata(this.timeNow + "\t" + this.baseTime);
|
2018-07-06 15:30:00 +02:00
|
|
|
room.updateItem(this);
|
2019-06-03 22:49:17 +02:00
|
|
|
this.needsUpdate(true);
|
2019-05-26 21:33:51 +02:00
|
|
|
|
|
|
|
super.onPlace(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-06-03 22:49:17 +02:00
|
|
|
this.setExtradata("0\t" + this.TIMER_INTERVAL_STEPS[0]);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 22:41:18 +02:00
|
|
|
// if wired triggered it
|
2019-06-03 22:49:17 +02:00
|
|
|
if (objects.length >= 2 && objects[1] instanceof WiredEffectType) {
|
|
|
|
if(!(!this.isRunning || this.isPaused))
|
|
|
|
return;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
boolean wasPaused = this.isPaused;
|
2020-10-31 03:11:33 +01:00
|
|
|
this.endGame(room, true);
|
2019-06-03 22:49:17 +02:00
|
|
|
|
|
|
|
if(wasPaused) {
|
|
|
|
WiredHandler.handle(WiredTriggerType.GAME_ENDS, null, room, new Object[]{});
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
this.createNewGame(room);
|
|
|
|
|
|
|
|
this.timeNow = this.baseTime;
|
2019-05-04 22:41:18 +02:00
|
|
|
this.isRunning = true;
|
2019-06-03 22:49:17 +02:00
|
|
|
this.isPaused = false;
|
|
|
|
|
2019-05-04 22:41:18 +02:00
|
|
|
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;
|
2021-02-04 19:59:16 +01:00
|
|
|
Emulator.getThreading().run(new GameTimer(this), 1000);
|
2019-05-24 13:12:22 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
InteractionGameTimerAction state = InteractionGameTimerAction.START_STOP;
|
2019-05-04 22:41:18 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (objects.length >= 1 && objects[0] instanceof Integer) {
|
2019-06-03 22:49:17 +02:00
|
|
|
state = InteractionGameTimerAction.getByAction((int) objects[0]);
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
switch (state) {
|
2019-06-03 22:49:17 +02:00
|
|
|
case START_STOP:
|
|
|
|
if (this.isRunning) { // a game has been started
|
2019-05-04 22:41:18 +02:00
|
|
|
this.isPaused = !this.isPaused;
|
2019-06-03 22:49:17 +02:00
|
|
|
if (this.isPaused) {
|
|
|
|
this.pause(room);
|
|
|
|
} else {
|
|
|
|
this.unpause(room);
|
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;
|
2021-02-04 19:59:16 +01:00
|
|
|
Emulator.getThreading().run(new GameTimer(this));
|
2019-05-24 13:12:22 +02:00
|
|
|
}
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
2019-06-03 22:49:17 +02:00
|
|
|
} else {
|
|
|
|
this.isPaused = false;
|
2019-05-04 22:41:18 +02:00
|
|
|
this.isRunning = true;
|
2019-06-03 22:49:17 +02:00
|
|
|
this.timeNow = this.baseTime;
|
2019-05-04 22:41:18 +02:00
|
|
|
room.updateItem(this);
|
2019-05-24 13:12:22 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
this.createNewGame(room);
|
|
|
|
WiredHandler.handle(WiredTriggerType.GAME_STARTS, null, room, new Object[]{this});
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (!this.threadActive) {
|
2019-05-24 13:12:22 +02:00
|
|
|
this.threadActive = true;
|
2021-02-04 19:59:16 +01:00
|
|
|
Emulator.getThreading().run(new GameTimer(this), 1000);
|
2019-05-24 13:12:22 +02:00
|
|
|
}
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
2019-06-03 22:49:17 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
case INCREASE_TIME:
|
2019-05-26 20:14:53 +02:00
|
|
|
if (!this.isRunning) {
|
2019-05-04 22:41:18 +02:00
|
|
|
this.increaseTimer(room);
|
2019-06-03 22:49:17 +02:00
|
|
|
} else if (this.isPaused) {
|
|
|
|
this.endGame(room);
|
|
|
|
this.increaseTimer(room);
|
|
|
|
WiredHandler.handle(WiredTriggerType.GAME_ENDS, null, room, new Object[]{});
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
int baseTime = -1;
|
2019-05-04 22:41:18 +02:00
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
if (this.timeNow != this.baseTime) {
|
|
|
|
baseTime = this.baseTime;
|
|
|
|
} else {
|
|
|
|
for (int step : this.TIMER_INTERVAL_STEPS) {
|
|
|
|
if (this.timeNow < step) {
|
|
|
|
baseTime = step;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseTime == -1) baseTime = this.TIMER_INTERVAL_STEPS[0];
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 22:49:17 +02:00
|
|
|
this.baseTime = baseTime;
|
|
|
|
this.setExtradata(this.timeNow + "\t" + this.baseTime);
|
|
|
|
|
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() {
|
2019-06-03 22:49:17 +02:00
|
|
|
return this.getExtradata();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-02-04 19:59:16 +01:00
|
|
|
return this.isRunning;
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setRunning(boolean running) {
|
2021-02-04 19:59:16 +01:00
|
|
|
this.isRunning = running;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setThreadActive(boolean threadActive) {
|
|
|
|
this.threadActive = threadActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isPaused() {
|
|
|
|
return this.isPaused;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reduceTime() {
|
|
|
|
this.timeNow--;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTimeNow() {
|
|
|
|
return this.timeNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTimeNow(int timeNow) {
|
|
|
|
this.timeNow = timeNow;
|
2019-05-04 22:41:18 +02:00
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|