2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.messages.incoming.polls;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.habbohotel.polls.Poll;
|
|
|
|
import com.eu.habbo.habbohotel.users.HabboBadge;
|
|
|
|
import com.eu.habbo.messages.incoming.MessageHandler;
|
|
|
|
import com.eu.habbo.messages.outgoing.users.AddUserBadgeComposer;
|
2019-05-26 20:14:53 +02:00
|
|
|
import com.eu.habbo.messages.outgoing.wired.WiredRewardAlertComposer;
|
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.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public class AnswerPollEvent extends MessageHandler {
|
2020-05-04 22:24:09 +02:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(AnswerPollEvent.class);
|
|
|
|
|
2018-07-06 15:30:00 +02:00
|
|
|
@Override
|
2019-05-26 20:14:53 +02:00
|
|
|
public void handle() throws Exception {
|
2018-07-06 15:30:00 +02:00
|
|
|
int pollId = this.packet.readInt();
|
|
|
|
int questionId = this.packet.readInt();
|
|
|
|
int count = this.packet.readInt();
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
StringBuilder answer = new StringBuilder();
|
2019-05-26 20:14:53 +02:00
|
|
|
for (int i = 0; i < count; i++) {
|
2019-03-18 02:22:00 +01:00
|
|
|
answer.append(":").append(this.packet.readString());
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (pollId == 0 && questionId <= 0) {
|
2019-03-18 02:22:00 +01:00
|
|
|
this.client.getHabbo().getHabboInfo().getCurrentRoom().handleWordQuiz(this.client.getHabbo(), answer.toString());
|
2018-07-06 15:30:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-18 02:22:00 +01:00
|
|
|
answer = new StringBuilder(answer.substring(1));
|
2018-07-06 15:30:00 +02:00
|
|
|
|
|
|
|
Poll poll = Emulator.getGameEnvironment().getPollManager().getPoll(pollId);
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (poll != null) {
|
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO polls_answers(poll_id, user_id, question_id, answer) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE answer=VALUES(answer)")) {
|
2018-07-06 15:30:00 +02:00
|
|
|
statement.setInt(1, pollId);
|
|
|
|
statement.setInt(2, this.client.getHabbo().getHabboInfo().getId());
|
|
|
|
statement.setInt(3, questionId);
|
2019-03-18 02:22:00 +01:00
|
|
|
statement.setString(4, answer.toString());
|
2018-07-06 15:30:00 +02:00
|
|
|
statement.execute();
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (SQLException e) {
|
2020-05-04 22:24:09 +02:00
|
|
|
LOGGER.error("Caught SQL exception", e);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (poll.lastQuestionId == questionId) {
|
|
|
|
if (poll.badgeReward.length() > 0) {
|
|
|
|
if (this.client.getHabbo().getInventory().getBadgesComponent().getBadge(poll.badgeReward) == null) {
|
2018-12-22 11:39:00 +01:00
|
|
|
HabboBadge badge = new HabboBadge(0, poll.badgeReward, 0, this.client.getHabbo());
|
2018-07-06 15:30:00 +02:00
|
|
|
Emulator.getThreading().run(badge);
|
|
|
|
this.client.getHabbo().getInventory().getBadgesComponent().addBadge(badge);
|
|
|
|
this.client.sendResponse(new AddUserBadgeComposer(badge));
|
|
|
|
this.client.sendResponse(new WiredRewardAlertComposer(WiredRewardAlertComposer.REWARD_RECEIVED_BADGE));
|
2019-05-26 20:14:53 +02:00
|
|
|
} else {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.client.sendResponse(new WiredRewardAlertComposer(WiredRewardAlertComposer.REWARD_ALREADY_RECEIVED));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|