Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/items/interactions/wired/effects/WiredEffectGiveScore.java

218 lines
7.0 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.items.interactions.wired.effects;
import com.eu.habbo.Emulator;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.gameclients.GameClient;
import com.eu.habbo.habbohotel.games.Game;
import com.eu.habbo.habbohotel.items.Item;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredEffect;
import com.eu.habbo.habbohotel.items.interactions.InteractionWiredTrigger;
2022-11-16 16:56:06 +01:00
import com.eu.habbo.habbohotel.items.interactions.wired.WiredSettings;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.habbohotel.rooms.Room;
import com.eu.habbo.habbohotel.rooms.RoomUnit;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.wired.WiredEffectType;
2020-11-16 12:12:24 +01:00
import com.eu.habbo.habbohotel.wired.WiredHandler;
2018-07-06 15:30:00 +02:00
import com.eu.habbo.messages.ClientMessage;
import com.eu.habbo.messages.ServerMessage;
import com.eu.habbo.messages.incoming.wired.WiredSaveException;
2018-07-06 15:30:00 +02:00
import gnu.trove.iterator.TObjectIntIterator;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import gnu.trove.procedure.TObjectProcedure;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
2019-05-26 20:14:53 +02:00
public class WiredEffectGiveScore extends InteractionWiredEffect {
2018-07-06 15:30:00 +02:00
public static final WiredEffectType type = WiredEffectType.GIVE_SCORE;
private int score;
private int count;
2018-09-28 21:25:00 +02:00
private TObjectIntMap<Map.Entry<Integer, Integer>> data = new TObjectIntHashMap<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public WiredEffectGiveScore(ResultSet set, Item baseItem) throws SQLException {
2018-07-06 15:30:00 +02:00
super(set, baseItem);
}
2019-05-26 20:14:53 +02:00
public WiredEffectGiveScore(int id, int userId, Item item, String extradata, int limitedStack, int limitedSells) {
2018-07-06 15:30:00 +02:00
super(id, userId, item, extradata, limitedStack, limitedSells);
}
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(RoomUnit roomUnit, Room room, Object[] stuff) {
2018-07-06 15:30:00 +02:00
Habbo habbo = room.getHabbo(roomUnit);
2019-05-26 20:14:53 +02:00
if (habbo != null && habbo.getHabboInfo().getCurrentGame() != null) {
2018-07-06 15:30:00 +02:00
Game game = room.getGame(habbo.getHabboInfo().getCurrentGame());
2019-05-26 20:14:53 +02:00
if (game == null)
2018-07-06 15:30:00 +02:00
return false;
int gameStartTime = game.getStartTime();
2018-07-06 15:30:00 +02:00
TObjectIntMap<Map.Entry<Integer, Integer>> dataClone = new TObjectIntHashMap<>(this.data);
TObjectIntIterator<Map.Entry<Integer, Integer>> iterator = dataClone.iterator();
for (int i = dataClone.size(); i-- > 0; ) {
2018-07-06 15:30:00 +02:00
iterator.advance();
Map.Entry<Integer, Integer> map = iterator.key();
2019-05-26 20:14:53 +02:00
if (map.getValue() == habbo.getHabboInfo().getId()) {
if (map.getKey() == gameStartTime) {
2019-05-26 20:14:53 +02:00
if (iterator.value() < this.count) {
2018-07-06 15:30:00 +02:00
iterator.setValue(iterator.value() + 1);
habbo.getHabboInfo().getGamePlayer().addScore(this.score, true);
2018-07-06 15:30:00 +02:00
return true;
}
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
iterator.remove();
}
}
}
try {
this.data.put(new AbstractMap.SimpleEntry<>(gameStartTime, habbo.getHabboInfo().getId()), 1);
}
catch(IllegalArgumentException e) {
}
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
if (habbo.getHabboInfo().getGamePlayer() != null) {
habbo.getHabboInfo().getGamePlayer().addScore(this.score, true);
2018-07-06 15:30:00 +02:00
}
return true;
}
return false;
}
@Override
2019-05-26 20:14:53 +02:00
public String getWiredData() {
2020-11-16 12:12:24 +01:00
return WiredHandler.getGsonBuilder().create().toJson(new JsonData(this.score, this.count, this.getDelay()));
2018-07-06 15:30:00 +02:00
}
@Override
2019-05-26 20:14:53 +02:00
public void loadWiredData(ResultSet set, Room room) throws SQLException {
2020-11-16 12:12:24 +01:00
String wiredData = set.getString("wired_data");
2018-07-06 15:30:00 +02:00
2020-11-16 12:12:24 +01:00
if(wiredData.startsWith("{")) {
JsonData data = WiredHandler.getGsonBuilder().create().fromJson(wiredData, JsonData.class);
this.score = data.score;
this.count = data.count;
this.setDelay(data.delay);
}
else {
String[] data = wiredData.split(";");
if (data.length == 3) {
this.score = Integer.valueOf(data[0]);
this.count = Integer.valueOf(data[1]);
this.setDelay(Integer.valueOf(data[2]));
}
this.needsUpdate(true);
2018-07-06 15:30:00 +02:00
}
}
@Override
2019-05-26 20:14:53 +02:00
public void onPickUp() {
2018-07-06 15:30:00 +02:00
this.score = 0;
this.count = 0;
this.setDelay(0);
}
@Override
2019-05-26 20:14:53 +02:00
public WiredEffectType getType() {
2018-07-06 15:30:00 +02:00
return WiredEffectGiveScore.type;
}
@Override
2019-05-26 20:14:53 +02:00
public void serializeWiredData(ServerMessage message, Room room) {
2018-07-06 15:30:00 +02:00
message.appendBoolean(false);
message.appendInt(5);
message.appendInt(0);
message.appendInt(this.getBaseItem().getSpriteId());
message.appendInt(this.getId());
message.appendString("");
message.appendInt(2);
message.appendInt(this.score);
message.appendInt(this.count);
message.appendInt(0);
message.appendInt(this.getType().code);
message.appendInt(this.getDelay());
2019-05-26 20:14:53 +02:00
if (this.requiresTriggeringUser()) {
2018-07-06 15:30:00 +02:00
List<Integer> invalidTriggers = new ArrayList<>();
2019-05-26 20:14:53 +02:00
room.getRoomSpecialTypes().getTriggers(this.getX(), this.getY()).forEach(new TObjectProcedure<InteractionWiredTrigger>() {
2018-07-06 15:30:00 +02:00
@Override
2019-05-26 20:14:53 +02:00
public boolean execute(InteractionWiredTrigger object) {
if (!object.isTriggeredByRoomUnit()) {
2018-07-06 15:30:00 +02:00
invalidTriggers.add(object.getBaseItem().getSpriteId());
}
return true;
}
});
message.appendInt(invalidTriggers.size());
2019-05-26 20:14:53 +02:00
for (Integer i : invalidTriggers) {
2018-07-06 15:30:00 +02:00
message.appendInt(i);
}
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
message.appendInt(0);
}
}
@Override
2022-11-16 16:56:06 +01:00
public boolean saveData(WiredSettings settings, GameClient gameClient) throws WiredSaveException {
if(settings.getIntParams().length < 2) throw new WiredSaveException("Invalid data");
2018-07-06 15:30:00 +02:00
2022-11-16 16:56:06 +01:00
int score = settings.getIntParams()[0];
if(score < 1 || score > 100)
throw new WiredSaveException("Score is invalid");
2022-11-16 16:56:06 +01:00
int timesPerGame = settings.getIntParams()[1];
if(timesPerGame < 1 || timesPerGame > 10)
throw new WiredSaveException("Times per game is invalid");
2022-11-16 16:56:06 +01:00
int delay = settings.getDelay();
if(delay > Emulator.getConfig().getInt("hotel.wired.max_delay", 20))
throw new WiredSaveException("Delay too long");
this.score = score;
this.count = timesPerGame;
this.setDelay(delay);
2018-07-06 15:30:00 +02:00
return true;
}
@Override
2019-05-26 20:14:53 +02:00
public boolean requiresTriggeringUser() {
2018-07-06 15:30:00 +02:00
return true;
}
2020-11-16 12:12:24 +01:00
static class JsonData {
int score;
int count;
int delay;
public JsonData(int score, int count, int delay) {
this.score = score;
this.count = count;
this.delay = delay;
}
}
2018-07-06 15:30:00 +02:00
}