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

378 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;
2019-05-04 22:41:18 +02:00
import com.eu.habbo.habbohotel.games.battlebanzai.BattleBanzaiGame;
import com.eu.habbo.habbohotel.games.freeze.FreezeGame;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.games.wired.WiredGame;
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;
2019-05-04 22:41:18 +02:00
import java.lang.reflect.InvocationTargetException;
2018-07-06 15:30:00 +02:00
import java.sql.ResultSet;
import java.sql.SQLException;
2019-05-04 22:41:18 +02:00
import java.util.ArrayList;
import java.util.Map;
2018-07-06 15:30:00 +02:00
2019-05-04 22:41:18 +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-04-22 01:42:00 +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");
if (data.length >= 2)
{
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
}
if (data.length >= 1)
{
this.setExtradata(data[0]);
}
}
2019-04-22 01:42:00 +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
@Override
public void run() {
if(this.needsUpdate() || this.needsDelete()) {
super.run();
}
2019-05-24 13:12:22 +02:00
if(this.getRoomId() == 0) {
this.threadActive = false;
2019-05-05 04:51:27 +02:00
return;
2019-05-24 13:12:22 +02:00
}
2019-05-05 04:51:27 +02:00
2019-05-04 22:41:18 +02:00
Room room = Emulator.getGameEnvironment().getRoomManager().getRoom(this.getRoomId());
2019-05-24 13:12:22 +02:00
if(room == null || !this.isRunning || this.isPaused) {
this.threadActive = false;
2019-05-04 22:41:18 +02:00
return;
2019-05-24 13:12:22 +02:00
}
2019-05-04 22:41:18 +02:00
if(this.timeNow > 0) {
2019-05-24 13:12:22 +02:00
this.threadActive = true;
2019-05-04 22:41:18 +02:00
Emulator.getThreading().run(this, 1000);
this.timeNow--;
room.updateItem(this);
}
else {
this.isRunning = false;
this.isPaused = false;
2019-05-24 13:12:22 +02:00
this.threadActive = false;
2019-05-04 22:41:18 +02:00
endGamesIfLastTimer(room);
}
}
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-05 04:51:27 +02:00
if(triggerWired) {
WiredHandler.handle(WiredTriggerType.GAME_ENDS, null, room, new Object[]{});
}
2019-05-04 22:41:18 +02:00
}
2018-07-06 15:30:00 +02:00
@Override
public void onPickUp(Room room)
{
this.setExtradata("0");
}
@Override
public void onPlace(Room room)
{
2019-05-04 22:41:18 +02:00
if(this.baseTime == 0) {
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
public void serializeExtradata(ServerMessage serverMessage)
{
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
public boolean canWalkOn(RoomUnit roomUnit, Room room, Object[] objects)
{
return false;
}
@Override
public boolean isWalkable()
{
return false;
}
2018-07-06 15:30:00 +02:00
@Override
public void onClick(GameClient client, Room room, Object[] objects) throws Exception
{
2019-05-04 22:41:18 +02:00
if(this.getExtradata().isEmpty())
2018-07-06 15:30:00 +02:00
{
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
if (objects.length >= 2 && objects[1] instanceof WiredEffectType && !this.isRunning)
2018-07-06 15:30:00 +02:00
{
2019-05-05 04:51:27 +02:00
endGamesIfLastTimer(room);
2018-07-06 15:30:00 +02:00
2019-05-04 22:41:18 +02:00
for(Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
Game game = getOrCreateGame(room, gameClass);
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);
WiredHandler.handle(WiredTriggerType.GAME_STARTS, null, room, new Object[] { });
2018-07-06 15:30:00 +02:00
2019-05-24 13:12:22 +02:00
if(!this.threadActive) {
this.threadActive = true;
Emulator.getThreading().run(this);
}
2019-05-04 22:41:18 +02:00
}
else if(client != null)
2018-07-06 15:30:00 +02:00
{
2019-05-04 22:41:18 +02:00
if (!(room.hasRights(client.getHabbo()) || client.getHabbo().hasPermission(Permission.ACC_ANYROOMOWNER)))
return;
int state = 1;
if(objects.length >= 1 && objects[0] instanceof Integer) {
state = (Integer) objects[0];
}
2018-07-06 15:30:00 +02:00
switch (state)
{
case 1:
2019-05-04 22:41:18 +02:00
if(this.isRunning) {
this.isPaused = !this.isPaused;
2019-05-05 04:51:27 +02:00
boolean allPaused = this.isPaused;
for(InteractionGameTimer timer : room.getRoomSpecialTypes().getGameTimers().values()) {
if(!timer.isPaused)
allPaused = false;
}
2019-05-04 22:41:18 +02:00
for(Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
Game game = getOrCreateGame(room, gameClass);
2019-05-05 04:51:27 +02:00
if(allPaused) {
2019-05-04 22:41:18 +02:00
game.pause();
}
else {
game.unpause();
}
}
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
if(!this.threadActive) {
this.threadActive = true;
Emulator.getThreading().run(this);
}
2019-05-04 22:41:18 +02:00
}
}
if(!this.isRunning) {
2019-05-05 04:51:27 +02:00
endGamesIfLastTimer(room);
2019-05-04 22:41:18 +02:00
for(Class<? extends Game> gameClass : Emulator.getGameEnvironment().getRoomManager().getGameTypes()) {
Game game = getOrCreateGame(room, gameClass);
game.initialise();
}
WiredHandler.handle(WiredTriggerType.GAME_STARTS, null, room, new Object[] { });
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
if(!this.threadActive) {
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-04 22:41:18 +02:00
if(!this.isRunning) {
this.increaseTimer(room);
return;
}
if(this.isPaused) {
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
public void onWalk(RoomUnit roomUnit, Room room, Object[] objects) throws Exception
{
}
2019-05-04 22:41:18 +02:00
public static Game getOrCreateGame(Room room, Class<? extends Game> gameClass)
2019-03-18 02:22:00 +01:00
{
2019-05-04 22:41:18 +02:00
Game game = (gameClass.cast(room.getGame(gameClass)));
2019-03-18 02:22:00 +01:00
2019-05-04 22:41:18 +02:00
if (game == null) {
try {
game = gameClass.getDeclaredConstructor(Room.class).newInstance(room);
2019-03-18 02:22:00 +01:00
room.addGame(game);
2019-05-04 22:41:18 +02:00
} catch (Exception e) {
Emulator.getLogging().logErrorLine(e);
2019-03-18 02:22:00 +01:00
}
}
return game;
}
2018-07-06 15:30:00 +02:00
private void increaseTimer(Room room)
{
2019-05-04 22:41:18 +02:00
if(this.isRunning)
2018-07-06 15:30:00 +02:00
return;
this.needsUpdate(true);
2019-05-04 22:41:18 +02:00
2018-07-06 15:30:00 +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;
//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
public String getDatabaseExtraData()
{
return this.getExtradata() + "\t" + this.baseTime;
}
public abstract Class<? extends Game> getGameType();
2018-11-17 14:28:00 +01:00
@Override
public boolean allowWiredResetState()
{
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
}