Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/games/Game.java

283 lines
9.4 KiB
Java
Raw Normal View History

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;
2019-05-04 22:41:18 +02:00
import com.eu.habbo.habbohotel.items.interactions.games.InteractionGameTimer;
2018-09-28 21:25:00 +02:00
import com.eu.habbo.habbohotel.items.interactions.wired.extra.WiredBlob;
2019-04-22 01:42:00 +02:00
import com.eu.habbo.habbohotel.items.interactions.wired.triggers.WiredTriggerTeamLoses;
import com.eu.habbo.habbohotel.items.interactions.wired.triggers.WiredTriggerTeamWins;
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;
2019-05-26 20:14:53 +02:00
public abstract class Game implements Runnable {
2018-07-08 23:32:00 +02:00
2018-09-28 21:25:00 +02:00
protected final THashMap<GameTeamColors, GameTeam> teams = new THashMap<>();
2019-03-18 02:22:00 +01:00
protected final Room room;
2019-05-26 20:14:53 +02:00
private final Class<? extends GameTeam> gameTeamClazz;
private final Class<? extends GamePlayer> gamePlayerClazz;
2019-05-05 04:51:27 +02:00
private final boolean countsAchievements;
public boolean isRunning;
2019-03-18 02:22:00 +01:00
public GameState state = GameState.IDLE;
2019-05-26 20:14:53 +02:00
private int startTime;
private int endTime;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public Game(Class<? extends GameTeam> gameTeamClazz, Class<? extends GamePlayer> gamePlayerClazz, Room room, boolean countsAchievements) {
2018-07-06 15:30:00 +02:00
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
2019-05-26 20:14:53 +02:00
public boolean addHabbo(Habbo habbo, GameTeamColors teamColor) {
try {
if (habbo != null) {
if (Emulator.getPluginManager().isRegistered(GameHabboJoinEvent.class, true)) {
2018-07-06 15:30:00 +02:00
Event gameHabboJoinEvent = new GameHabboJoinEvent(this, habbo);
Emulator.getPluginManager().fireEvent(gameHabboJoinEvent);
2019-05-26 20:14:53 +02:00
if (gameHabboJoinEvent.isCancelled())
2018-07-06 15:30:00 +02:00
return false;
}
2019-05-26 20:14:53 +02:00
synchronized (this.teams) {
2018-07-06 15:30:00 +02:00
GameTeam team = this.getTeam(teamColor);
2019-05-26 20:14:53 +02:00
if (team == null) {
2018-07-06 15:30:00 +02:00
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;
}
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
Emulator.getLogging().logErrorLine(e);
}
return false;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void removeHabbo(Habbo habbo) {
if (habbo != null) {
if (Emulator.getPluginManager().isRegistered(GameHabboLeaveEvent.class, true)) {
2018-07-06 15:30:00 +02:00
Event gameHabboLeaveEvent = new GameHabboLeaveEvent(this, habbo);
Emulator.getPluginManager().fireEvent(gameHabboLeaveEvent);
2019-05-26 20:14:53 +02:00
if (gameHabboLeaveEvent.isCancelled())
2018-07-06 15:30:00 +02:00
return;
}
GameTeam team = this.getTeamForHabbo(habbo);
2019-05-26 20:14:53 +02:00
if (team != null && team.isMember(habbo)) {
2018-07-06 15:30:00 +02:00
team.removeMember(habbo.getHabboInfo().getGamePlayer());
habbo.getHabboInfo().getGamePlayer().reset();
habbo.getHabboInfo().setCurrentGame(null);
habbo.getHabboInfo().setGamePlayer(null);
2019-05-26 20:14:53 +02:00
if (this.countsAchievements && this.endTime > this.startTime) {
2018-07-06 15:30:00 +02:00
AchievementManager.progressAchievement(habbo, Emulator.getGameEnvironment().getAchievementManager().getAchievement("GamePlayed"));
}
}
}
2019-05-05 04:51:27 +02:00
/*
2018-07-06 15:30:00 +02:00
boolean deleteGame = true;
for (GameTeam team : this.teams.values())
{
if (team.getMembers().size() > 0 )
{
deleteGame = false;
break;
}
}
if (deleteGame)
{
2019-03-18 02:22:00 +01:00
this.room.deleteGame(this);
2018-07-06 15:30:00 +02:00
}
2019-05-05 04:51:27 +02:00
*/
2018-07-06 15:30:00 +02:00
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void start() {
this.isRunning = false;
2019-03-18 02:22:00 +01:00
this.state = GameState.RUNNING;
2018-07-06 15:30:00 +02:00
this.startTime = Emulator.getIntUnixTimestamp();
2019-05-26 20:14:53 +02:00
if (Emulator.getPluginManager().isRegistered(GameStartedEvent.class, true)) {
2018-07-06 15:30:00 +02:00
Event gameStartedEvent = new GameStartedEvent(this);
Emulator.getPluginManager().fireEvent(gameStartedEvent);
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(WiredBlob.class)) {
2018-09-28 21:25:00 +02:00
item.setExtradata("0");
2019-03-18 02:22:00 +01:00
this.room.updateItem(item);
2018-09-28 21:25:00 +02:00
}
2018-07-06 15:30:00 +02:00
}
2019-05-04 22:41:18 +02:00
public void onEnd() {
2019-05-05 04:51:27 +02:00
this.endTime = Emulator.getIntUnixTimestamp();
2019-05-04 22:41:18 +02:00
this.saveScores();
2019-05-26 23:28:35 +02:00
int totalPointsGained = this.teams.values().stream().mapToInt(GameTeam::getTotalScore).sum();
Habbo roomOwner = Emulator.getGameEnvironment().getHabboManager().getHabbo(this.room.getOwnerId());
if (roomOwner != null) {
AchievementManager.progressAchievement(roomOwner, Emulator.getGameEnvironment().getAchievementManager().getAchievement("GameAuthorExperience"), totalPointsGained);
}
2019-05-04 22:41:18 +02:00
GameTeam winningTeam = null;
2019-05-26 20:14:53 +02:00
for (GameTeam team : this.teams.values()) {
if (winningTeam == null || team.getTotalScore() > winningTeam.getTotalScore()) {
2019-05-04 22:41:18 +02:00
winningTeam = team;
}
}
2019-05-26 20:14:53 +02:00
if (winningTeam != null) {
for (GamePlayer player : winningTeam.getMembers()) {
2019-05-04 22:41:18 +02:00
WiredHandler.handleCustomTrigger(WiredTriggerTeamWins.class, player.getHabbo().getRoomUnit(), this.room, new Object[]{this});
2019-05-26 23:28:35 +02:00
Habbo winner = player.getHabbo();
if (winner != null) {
AchievementManager.progressAchievement(roomOwner, Emulator.getGameEnvironment().getAchievementManager().getAchievement("GamePlayerExperience"));
}
2019-05-04 22:41:18 +02:00
}
2019-05-26 20:14:53 +02:00
for (GameTeam team : this.teams.values()) {
2019-05-04 22:41:18 +02:00
if (team == winningTeam) continue;
2019-05-26 20:14:53 +02:00
for (GamePlayer player : winningTeam.getMembers()) {
2019-05-04 22:41:18 +02:00
WiredHandler.handleCustomTrigger(WiredTriggerTeamLoses.class, player.getHabbo().getRoomUnit(), this.room, new Object[]{this});
}
}
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) {
2019-05-04 22:41:18 +02:00
this.room.updateItem(item);
}
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public abstract void run();
2019-05-26 20:14:53 +02:00
public void pause() {
if (this.state.equals(GameState.RUNNING)) {
2019-03-18 02:22:00 +01:00
this.state = GameState.PAUSED;
}
}
2019-05-26 20:14:53 +02:00
public void unpause() {
if (this.state.equals(GameState.PAUSED)) {
2019-03-18 02:22:00 +01:00
this.state = GameState.RUNNING;
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void stop() {
2019-03-18 02:22:00 +01:00
this.state = GameState.IDLE;
2018-07-06 15:30:00 +02:00
2019-05-04 22:41:18 +02:00
boolean gamesActive = false;
2019-05-26 20:14:53 +02:00
for (HabboItem timer : room.getFloorItems()) {
if (timer instanceof InteractionGameTimer) {
if (((InteractionGameTimer) timer).isRunning())
2019-05-04 22:41:18 +02:00
gamesActive = true;
2019-04-22 01:42:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
if (gamesActive) {
2019-05-04 22:41:18 +02:00
return;
2019-04-22 01:42:00 +02:00
}
2019-05-26 20:14:53 +02:00
if (Emulator.getPluginManager().isRegistered(GameStoppedEvent.class, true)) {
2018-07-06 15:30:00 +02:00
Event gameStoppedEvent = new GameStoppedEvent(this);
Emulator.getPluginManager().fireEvent(gameStoppedEvent);
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
private void saveScores() {
if (this.room == null)
2018-07-06 15:30:00 +02:00
return;
2019-05-26 20:14:53 +02:00
for (Map.Entry<GameTeamColors, GameTeam> teamEntry : this.teams.entrySet()) {
2018-07-06 15:30:00 +02:00
Emulator.getThreading().run(new SaveScoreForTeam(teamEntry.getValue(), this));
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public GameTeam getTeamForHabbo(Habbo habbo) {
if (habbo != null) {
synchronized (this.teams) {
for (GameTeam team : this.teams.values()) {
if (team.isMember(habbo)) {
2018-07-06 15:30:00 +02:00
return team;
}
}
}
}
return null;
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public GameTeam getTeam(GameTeamColors teamColor) {
synchronized (this.teams) {
2018-07-06 15:30:00 +02:00
return this.teams.get(teamColor);
}
}
2018-07-08 23:32:00 +02:00
2019-05-26 20:14:53 +02:00
public void addTeam(GameTeam team) {
synchronized (this.teams) {
2018-07-06 15:30:00 +02:00
this.teams.put(team.teamColor, team);
}
}
2019-05-26 20:14:53 +02:00
public Room getRoom() {
2018-07-06 15:30:00 +02:00
return this.room;
}
2019-05-26 20:14:53 +02:00
public int getStartTime() {
2018-07-06 15:30:00 +02:00
return this.startTime;
}
2019-05-05 04:51:27 +02:00
public Class<? extends GameTeam> getGameTeamClass() {
return gameTeamClazz;
}
2018-07-08 23:32:00 +02:00
2019-05-05 04:51:27 +02:00
public Class<? extends GamePlayer> getGamePlayerClass() {
return gamePlayerClazz;
2018-07-06 15:30:00 +02:00
}
2019-05-05 04:51:27 +02:00
public THashMap<GameTeamColors, GameTeam> getTeams() {
return teams;
}
2018-07-08 23:32:00 +02:00
2019-05-05 04:51:27 +02:00
public boolean isCountsAchievements() {
return countsAchievements;
}
public GameState getState() {
return state;
2018-07-06 15:30:00 +02:00
}
}