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

308 lines
11 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-07-30 12:45:39 +02:00
import com.eu.habbo.habbohotel.wired.highscores.WiredHighscoreDataEntry;
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;
2020-02-02 10:06:44 +01:00
import com.eu.habbo.messages.outgoing.guides.GuideSessionPartnerIsPlayingComposer;
2018-07-06 15:30:00 +02:00
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;
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.util.Map;
2019-07-30 12:45:39 +02:00
import java.util.stream.Collectors;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public abstract class Game implements Runnable {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(Game.class);
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);
}
2020-02-02 10:06:44 +01:00
habbo.getClient().sendResponse(new GuideSessionPartnerIsPlayingComposer(true));
2018-07-06 15:30:00 +02:00
return true;
}
2020-06-02 11:35:46 +02:00
} catch (Exception e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught exception", e);
2018-07-06 15:30:00 +02:00
}
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)) {
2020-02-08 17:17:02 +01:00
if (habbo.getHabboInfo().getGamePlayer() != null) {
team.removeMember(habbo.getHabboInfo().getGamePlayer());
if (habbo.getHabboInfo().getGamePlayer() != null) {
habbo.getHabboInfo().getGamePlayer().reset();
}
2020-02-08 17:17:02 +01:00
}
2018-07-06 15:30:00 +02:00
habbo.getHabboInfo().setCurrentGame(null);
habbo.getHabboInfo().setGamePlayer(null);
2020-02-02 10:06:44 +01:00
habbo.getClient().sendResponse(new GuideSessionPartnerIsPlayingComposer(false));
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"));
}
}
}
}
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)) {
2020-02-05 20:30:11 +01:00
((WiredBlob) item).onGameStart(this.room);
2018-09-28 21:25:00 +02:00
}
2020-07-02 23:01:05 +02:00
for (GameTeam team : this.teams.values()) {
team.resetScores();
}
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;
2020-07-02 23:01:05 +02:00
if(totalPointsGained > 0) {
for (GameTeam team : this.teams.values()) {
if (winningTeam == null || team.getTotalScore() > winningTeam.getTotalScore()) {
winningTeam = team;
}
2019-05-04 22:41:18 +02:00
}
}
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-07-30 21:05:29 +02:00
if (winningTeam.getMembers().size() > 0) {
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) {
Emulator.getGameEnvironment().getItemManager().getHighscoreManager().addHighscoreData(new WiredHighscoreDataEntry(item.getId(), winningTeam.getMembers().stream().map(m -> m.getHabbo().getHabboInfo().getId()).collect(Collectors.toList()), winningTeam.getTotalScore(), true, Emulator.getIntUnixTimestamp()));
}
2019-07-30 12:45:39 +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-07-30 21:05:29 +02:00
for (GamePlayer player : team.getMembers()) {
2019-05-04 22:41:18 +02:00
WiredHandler.handleCustomTrigger(WiredTriggerTeamLoses.class, player.getHabbo().getRoomUnit(), this.room, new Object[]{this});
}
2019-07-30 12:45:39 +02:00
2020-07-02 23:01:05 +02:00
if (team.getMembers().size() > 0 && team.getTotalScore() > 0) {
2019-07-30 21:05:29 +02:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) {
Emulator.getGameEnvironment().getItemManager().getHighscoreManager().addHighscoreData(new WiredHighscoreDataEntry(item.getId(), team.getMembers().stream().map(m -> m.getHabbo().getHabboInfo().getId()).collect(Collectors.toList()), team.getTotalScore(), false, Emulator.getIntUnixTimestamp()));
}
2019-07-30 12:45:39 +02:00
}
2019-05-04 22:41:18 +02:00
}
}
2019-05-26 20:14:53 +02:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(InteractionWiredHighscore.class)) {
2019-07-30 12:45:39 +02:00
((InteractionWiredHighscore) item).reloadData();
2019-05-04 22:41:18 +02:00
this.room.updateItem(item);
}
2020-02-05 20:30:11 +01:00
for (HabboItem item : this.room.getRoomSpecialTypes().getItemsOfType(WiredBlob.class)) {
((WiredBlob) item).onGameEnd(this.room);
}
2019-05-04 22:41:18 +02:00
}
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);
}
}
public void dispose() {
for (GameTeam team : this.teams.values()) {
team.clearMembers();
}
this.teams.clear();
this.stop();
}
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;
THashMap<GameTeamColors, GameTeam> teamsCopy = new THashMap<>();
teamsCopy.putAll(this.teams);
for (Map.Entry<GameTeamColors, GameTeam> teamEntry : teamsCopy.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
}
}