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

313 lines
8.7 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;
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;
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
2019-03-18 02:22:00 +01:00
protected final Room room;
2018-07-06 15:30:00 +02:00
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
protected final boolean countsAchievements;
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 int startTime;
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
protected int pauseTime;
2018-07-06 15:30:00 +02:00
protected int endTime;
2018-07-08 23:32:00 +02:00
2019-03-18 02:22:00 +01:00
public GameState state = GameState.IDLE;
2018-07-06 15:30:00 +02:00
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)
{
2019-03-18 02:22:00 +01:00
this.room.deleteGame(this);
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 void start()
{
2019-03-18 02:22:00 +01:00
this.state = GameState.RUNNING;
2018-07-06 15:30:00 +02:00
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");
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
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public abstract void run();
2019-03-18 02:22:00 +01:00
public void pause()
{
if (this.state.equals(GameState.RUNNING))
{
this.state = GameState.PAUSED;
this.pauseTime = Emulator.getIntUnixTimestamp();
}
}
public void unpause()
{
if (this.state.equals(GameState.PAUSED))
{
this.state = GameState.RUNNING;
this.endTime = Emulator.getIntUnixTimestamp() + (this.endTime - this.pauseTime);
}
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +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
this.endTime = Emulator.getIntUnixTimestamp();
this.saveScores();
2019-04-22 01:42:00 +02:00
GameTeam winningTeam = null;
for (GameTeam team : this.teams.values())
{
if (winningTeam == null || team.getTotalScore() > winningTeam.getTotalScore())
{
winningTeam = team;
}
}
if (winningTeam != null)
{
for (GamePlayer player : winningTeam.getMembers())
{
WiredHandler.handleCustomTrigger(WiredTriggerTeamWins.class, player.getHabbo().getRoomUnit(), this.room, new Object[]{this});
}
for (GameTeam team : this.teams.values())
{
if (team == winningTeam) continue;
for (GamePlayer player : winningTeam.getMembers())
{
WiredHandler.handleCustomTrigger(WiredTriggerTeamLoses.class, player.getHabbo().getRoomUnit(), this.room, new Object[]{this});
}
}
}
2018-07-06 15:30:00 +02:00
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;
}
}