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.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;
|
|
|
|
import com.eu.habbo.messages.ServerMessage;
|
|
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
public class InteractionGameTimer extends HabboItem
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
|
|
|
private int baseTime = 0;
|
2019-03-18 02:22:00 +01:00
|
|
|
private int lastToggle = 0;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
protected InteractionGameTimer(ResultSet set, Item baseItem) throws SQLException
|
|
|
|
{
|
|
|
|
super(set, baseItem);
|
|
|
|
|
|
|
|
String[] data = set.getString("extra_data").split("\t");
|
|
|
|
|
|
|
|
if (data.length >= 2)
|
|
|
|
{
|
|
|
|
this.baseTime = Integer.valueOf(data[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.length >= 1)
|
|
|
|
{
|
|
|
|
this.setExtradata(data[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected InteractionGameTimer(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells)
|
|
|
|
{
|
|
|
|
super(id, userId, item, extradata, limitedStack, limitedSells);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPickUp(Room room)
|
|
|
|
{
|
|
|
|
this.setExtradata("0");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPlace(Room room)
|
|
|
|
{
|
|
|
|
this.baseTime = 30;
|
|
|
|
this.setExtradata("30");
|
|
|
|
room.updateItem(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serializeExtradata(ServerMessage serverMessage)
|
|
|
|
{
|
|
|
|
serverMessage.appendInt((this.isLimited() ? 256 : 0));
|
|
|
|
serverMessage.appendString(this.getExtradata());
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (client != null)
|
|
|
|
{
|
2018-09-12 18:45:00 +02:00
|
|
|
if (!(room.hasRights(client.getHabbo()) || client.getHabbo().hasPermission(Permission.ACC_ANYROOMOWNER)))
|
2018-07-06 15:30:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if (client == null)
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
int now = Emulator.getIntUnixTimestamp();
|
|
|
|
if (now - this.lastToggle < 3) return;
|
|
|
|
this.lastToggle = now;
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if(this.getExtradata().isEmpty())
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
this.setExtradata("0");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
Game game = this.getOrCreateGame(room);
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
if ((objects.length >= 2 && objects[1] instanceof WiredEffectType))
|
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
if (game.state.equals(GameState.RUNNING))
|
2018-07-06 15:30:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(objects.length >= 1 && objects[0] instanceof Integer && client != null)
|
|
|
|
{
|
|
|
|
int state = (Integer)objects[0];
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
this.startGame(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
this.increaseTimer(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
this.stopGame(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if (game != null && game.state.equals(GameState.IDLE))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
|
|
|
this.startGame(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private Game getOrCreateGame(Room room)
|
|
|
|
{
|
|
|
|
Game game = (this.getGameType().cast(room.getGame(this.getGameType())));
|
|
|
|
|
|
|
|
if (game == null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
game = this.getGameType().getDeclaredConstructor(Room.class).newInstance(room);
|
|
|
|
room.addGame(game);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
private void startGame(Room room)
|
|
|
|
{
|
|
|
|
this.needsUpdate(true);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
room.updateItem(this);
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
Game game = this.getOrCreateGame(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if (game.state.equals(GameState.IDLE))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
this.setExtradata(this.baseTime + "");
|
|
|
|
game.initialise();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
else if (game.state.equals(GameState.PAUSED))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
game.unpause();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
else if (game.state.equals(GameState.RUNNING))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
game.pause();
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
|
|
|
|
//}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logErrorLine(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopGame(Room room)
|
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
this.setExtradata(this.baseTime + "");
|
2018-07-06 15:30:00 +02:00
|
|
|
this.needsUpdate(true);
|
2019-03-18 02:22:00 +01:00
|
|
|
Game game = this.getOrCreateGame(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if(game != null && game.state != GameState.IDLE)
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
|
|
|
game.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
room.updateItem(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void increaseTimer(Room room)
|
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
Game game = this.getOrCreateGame(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
if (game == null) return;
|
|
|
|
if (game.state.equals(GameState.PAUSED))
|
2018-07-06 15:30:00 +02:00
|
|
|
{
|
2019-03-18 02:22:00 +01:00
|
|
|
stopGame(room);
|
2018-07-06 15:30:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-03-18 02:22:00 +01:00
|
|
|
if (game.state.equals(GameState.RUNNING)) return;
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
this.needsUpdate(true);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setExtradata(this.baseTime + "");
|
|
|
|
|
|
|
|
room.updateItem(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDatabaseExtraData()
|
|
|
|
{
|
|
|
|
return this.getExtradata() + "\t" + this.baseTime;
|
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
public Class<? extends Game> getGameType()
|
|
|
|
{
|
|
|
|
return WiredGame.class;
|
|
|
|
}
|
2018-11-17 14:28:00 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean allowWiredResetState()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|