Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/polls/PollManager.java

79 lines
2.9 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.polls;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo;
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.sql.*;
2019-05-26 20:14:53 +02:00
public class PollManager {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(PollManager.class);
2018-09-28 21:25:00 +02:00
private final THashMap<Integer, Poll> activePolls = new THashMap<>();
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public PollManager() {
2018-07-06 15:30:00 +02:00
this.loadPolls();
}
2019-05-26 20:14:53 +02:00
public static boolean donePoll(Habbo habbo, int pollId) {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT NULL FROM polls_answers WHERE poll_id = ? AND user_id = ? LIMIT 1")) {
statement.setInt(1, pollId);
statement.setInt(2, habbo.getHabboInfo().getId());
try (ResultSet set = statement.executeQuery()) {
if (set.isBeforeFirst()) {
return true;
}
}
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2019-05-26 20:14:53 +02:00
}
return false;
}
2018-12-22 11:39:00 +01:00
2019-05-26 20:14:53 +02:00
public void loadPolls() {
synchronized (this.activePolls) {
2018-07-06 15:30:00 +02:00
this.activePolls.clear();
2019-05-26 20:14:53 +02:00
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet set = statement.executeQuery("SELECT * FROM polls")) {
while (set.next()) {
2018-07-06 15:30:00 +02:00
this.activePolls.put(set.getInt("id"), new Poll(set));
}
}
2019-05-26 20:14:53 +02:00
try (ResultSet set = statement.executeQuery("SELECT * FROM polls_questions ORDER BY parent_id, `order` ASC")) {
while (set.next()) {
2018-07-06 15:30:00 +02:00
Poll poll = this.getPoll(set.getInt("poll_id"));
2019-05-26 20:14:53 +02:00
if (poll != null) {
2018-07-06 15:30:00 +02:00
PollQuestion question = new PollQuestion(set);
2019-05-26 20:14:53 +02:00
if (set.getInt("parent_id") <= 0) {
2018-07-06 15:30:00 +02:00
poll.addQuestion(question);
2019-05-26 20:14:53 +02:00
} else {
2018-07-06 15:30:00 +02:00
PollQuestion parentQuestion = poll.getQuestion(set.getInt("parent_id"));
2019-05-26 20:14:53 +02:00
if (parentQuestion != null) {
2018-07-06 15:30:00 +02:00
parentQuestion.addSubQuestion(question);
}
}
2018-12-22 11:39:00 +01:00
poll.lastQuestionId = question.id;
2018-07-06 15:30:00 +02:00
}
}
}
}
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
public Poll getPoll(int pollId) {
2018-07-06 15:30:00 +02:00
return this.activePolls.get(pollId);
}
}