2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.habbohotel.games;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.habbohotel.achievements.AchievementManager;
|
|
|
|
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredHighscore;
|
2018-09-28 21:25:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredBlob;
|
2018-07-06 15:30:00 +02:00
|
|
|
import com.eu.habbo.habbohotel.rooms.Room;
|
|
|
|
import com.eu.habbo.habbohotel.users.Habbo;
|
|
|
|
import com.eu.habbo.habbohotel.users.HabboItem;
|
|
|
|
import com.eu.habbo.habbohotel.wired.WiredHandler;
|
|
|
|
import com.eu.habbo.habbohotel.wired.WiredTriggerType;
|
|
|
|
import com.eu.habbo.plugin.Event;
|
|
|
|
import com.eu.habbo.plugin.events.games.GameHabboJoinEvent;
|
|
|
|
import com.eu.habbo.plugin.events.games.GameHabboLeaveEvent;
|
|
|
|
import com.eu.habbo.plugin.events.games.GameStartedEvent;
|
|
|
|
import com.eu.habbo.plugin.events.games.GameStoppedEvent;
|
|
|
|
import com.eu.habbo.threading.runnables.SaveScoreForTeam;
|
|
|
|
import gnu.trove.map.hash.THashMap;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public abstract class Game implements Runnable
|
|
|
|
{
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public final Class<? extends GameTeam> gameTeamClazz;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public final Class<? extends GamePlayer> gamePlayerClazz;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-09-28 21:25:00 +02:00
|
|
|
protected final THashMap<GameTeamColors, GameTeam> teams = new THashMap<>();
|
2018-07-06 15:30:00 +02:00
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
protected Room room;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
protected boolean countsAchievements;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
protected int startTime;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
protected int endTime;
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean isRunning;
|
|
|
|
|
|
|
|
public Game(Class<? extends GameTeam> gameTeamClazz, Class<? extends GamePlayer> gamePlayerClazz, Room room, boolean countsAchievements)
|
|
|
|
{
|
|
|
|
this.gameTeamClazz = gameTeamClazz;
|
|
|
|
this.gamePlayerClazz = gamePlayerClazz;
|
|
|
|
this.room = room;
|
|
|
|
this.countsAchievements = countsAchievements;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public abstract void initialise();
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean addHabbo(Habbo habbo, GameTeamColors teamColor)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (habbo != null)
|
|
|
|
{
|
|
|
|
if(Emulator.getPluginManager().isRegistered(GameHabboJoinEvent.class, true))
|
|
|
|
{
|
|
|
|
Event gameHabboJoinEvent = new GameHabboJoinEvent(this, habbo);
|
|
|
|
Emulator.getPluginManager().fireEvent(gameHabboJoinEvent);
|
|
|
|
if(gameHabboJoinEvent.isCancelled())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this.teams)
|
|
|
|
{
|
|
|
|
GameTeam team = this.getTeam(teamColor);
|
|
|
|
if (team == null)
|
|
|
|
{
|
|
|
|
team = this.gameTeamClazz.getDeclaredConstructor(GameTeamColors.class).newInstance(teamColor);
|
|
|
|
this.addTeam(team);
|
|
|
|
}
|
|
|
|
|
|
|
|
GamePlayer player = this.gamePlayerClazz.getDeclaredConstructor(Habbo.class, GameTeamColors.class).newInstance(habbo, teamColor);
|
|
|
|
team.addMember(player);
|
|
|
|
habbo.getHabboInfo().setCurrentGame(this.getClass());
|
|
|
|
habbo.getHabboInfo().setGamePlayer(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Emulator.getLogging().logErrorLine(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void removeHabbo(Habbo habbo)
|
|
|
|
{
|
|
|
|
if (habbo != null)
|
|
|
|
{
|
|
|
|
if(Emulator.getPluginManager().isRegistered(GameHabboLeaveEvent.class, true))
|
|
|
|
{
|
|
|
|
Event gameHabboLeaveEvent = new GameHabboLeaveEvent(this, habbo);
|
|
|
|
Emulator.getPluginManager().fireEvent(gameHabboLeaveEvent);
|
|
|
|
if(gameHabboLeaveEvent.isCancelled())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GameTeam team = this.getTeamForHabbo(habbo);
|
|
|
|
if (team != null && team.isMember(habbo))
|
|
|
|
{
|
|
|
|
team.removeMember(habbo.getHabboInfo().getGamePlayer());
|
|
|
|
habbo.getHabboInfo().getGamePlayer().reset();
|
|
|
|
habbo.getHabboInfo().setCurrentGame(null);
|
|
|
|
habbo.getHabboInfo().setGamePlayer(null);
|
|
|
|
|
|
|
|
if(this.countsAchievements && this.endTime > this.startTime)
|
|
|
|
{
|
|
|
|
AchievementManager.progressAchievement(habbo, Emulator.getGameEnvironment().getAchievementManager().getAchievement("GamePlayed"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean deleteGame = true;
|
|
|
|
for (GameTeam team : this.teams.values())
|
|
|
|
{
|
|
|
|
if (team.getMembers().size() > 0 )
|
|
|
|
{
|
|
|
|
deleteGame = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deleteGame)
|
|
|
|
{
|
|
|
|
room.deleteGame(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void start()
|
|
|
|
{
|
|
|
|
this.isRunning = true;
|
|
|
|
this.startTime = Emulator.getIntUnixTimestamp();
|
|
|
|
|
|
|
|
if(Emulator.getPluginManager().isRegistered(GameStartedEvent.class, true))
|
|
|
|
{
|
|
|
|
Event gameStartedEvent = new GameStartedEvent(this);
|
|
|
|
Emulator.getPluginManager().fireEvent(gameStartedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
WiredHandler.handle(WiredTriggerType.GAME_STARTS, null, this.room, new Object[]{this});
|
2018-09-28 21:25:00 +02:00
|
|
|
|
|
|
|
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(WiredBlob.class))
|
|
|
|
{
|
|
|
|
item.setExtradata("0");
|
|
|
|
room.updateItem(item);
|
|
|
|
}
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public abstract void run();
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void stop()
|
|
|
|
{
|
|
|
|
this.isRunning = false;
|
|
|
|
this.endTime = Emulator.getIntUnixTimestamp();
|
|
|
|
|
|
|
|
this.saveScores();
|
|
|
|
|
|
|
|
if(Emulator.getPluginManager().isRegistered(GameStoppedEvent.class, true))
|
|
|
|
{
|
|
|
|
Event gameStoppedEvent = new GameStoppedEvent(this);
|
|
|
|
Emulator.getPluginManager().fireEvent(gameStoppedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
WiredHandler.handle(WiredTriggerType.GAME_ENDS, null, this.room, new Object[]{this});
|
|
|
|
|
|
|
|
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class))
|
|
|
|
{
|
|
|
|
this.room.updateItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
private void saveScores()
|
|
|
|
{
|
|
|
|
if(this.room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(Map.Entry<GameTeamColors, GameTeam> teamEntry : this.teams.entrySet())
|
|
|
|
{
|
|
|
|
Emulator.getThreading().run(new SaveScoreForTeam(teamEntry.getValue(), this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public GameTeam getTeamForHabbo(Habbo habbo)
|
|
|
|
{
|
|
|
|
if(habbo != null)
|
|
|
|
{
|
|
|
|
synchronized (this.teams)
|
|
|
|
{
|
|
|
|
for (GameTeam team : this.teams.values())
|
|
|
|
{
|
|
|
|
if (team.isMember(habbo))
|
|
|
|
{
|
|
|
|
return team;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public GameTeam getTeam(GameTeamColors teamColor)
|
|
|
|
{
|
|
|
|
synchronized (this.teams)
|
|
|
|
{
|
|
|
|
return this.teams.get(teamColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void addTeam(GameTeam team)
|
|
|
|
{
|
|
|
|
synchronized (this.teams)
|
|
|
|
{
|
|
|
|
this.teams.put(team.teamColor, team);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public Room getRoom()
|
|
|
|
{
|
|
|
|
return this.room;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public int getStartTime()
|
|
|
|
{
|
|
|
|
return this.startTime;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public int getEndTime()
|
|
|
|
{
|
|
|
|
return this.endTime;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
public void addTime(int time)
|
|
|
|
{
|
|
|
|
this.endTime += time;
|
|
|
|
}
|
|
|
|
}
|