mirror of
https://git.krews.org/morningstar/Arcturus-Community.git
synced 2024-07-02 13:35:33 +02:00
99 lines
3.2 KiB
Java
99 lines
3.2 KiB
Java
|
package com.eu.habbo.habbohotel.polls;
|
||
|
|
||
|
import com.eu.habbo.Emulator;
|
||
|
import com.eu.habbo.habbohotel.users.Habbo;
|
||
|
import gnu.trove.map.hash.THashMap;
|
||
|
|
||
|
import java.sql.*;
|
||
|
|
||
|
public class PollManager
|
||
|
{
|
||
|
private final THashMap<Integer, Poll> activePolls = new THashMap<Integer, Poll>();
|
||
|
|
||
|
public PollManager()
|
||
|
{
|
||
|
this.loadPolls();
|
||
|
}
|
||
|
|
||
|
public void loadPolls()
|
||
|
{
|
||
|
synchronized (this.activePolls)
|
||
|
{
|
||
|
this.activePolls.clear();
|
||
|
|
||
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection())
|
||
|
{
|
||
|
try (Statement statement = connection.createStatement())
|
||
|
{
|
||
|
try (ResultSet set = statement.executeQuery("SELECT * FROM polls"))
|
||
|
{
|
||
|
while (set.next())
|
||
|
{
|
||
|
this.activePolls.put(set.getInt("id"), new Poll(set));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try (ResultSet set = statement.executeQuery("SELECT * FROM polls_questions ORDER BY parent_id, `order` ASC"))
|
||
|
{
|
||
|
while (set.next())
|
||
|
{
|
||
|
Poll poll = this.getPoll(set.getInt("poll_id"));
|
||
|
|
||
|
if (poll != null)
|
||
|
{
|
||
|
PollQuestion question = new PollQuestion(set);
|
||
|
|
||
|
if (set.getInt("parent_id") <= 0)
|
||
|
{
|
||
|
poll.addQuestion(question);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
PollQuestion parentQuestion = poll.getQuestion(set.getInt("parent_id"));
|
||
|
|
||
|
if (parentQuestion != null)
|
||
|
{
|
||
|
parentQuestion.addSubQuestion(question);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
poll.lastQuestionId = question.getId();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (SQLException e)
|
||
|
{
|
||
|
Emulator.getLogging().logSQLException(e);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Poll getPoll(int pollId)
|
||
|
{
|
||
|
return this.activePolls.get(pollId);
|
||
|
}
|
||
|
|
||
|
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())
|
||
|
{
|
||
|
while (set.next())
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (SQLException e)
|
||
|
{
|
||
|
Emulator.getLogging().logSQLException(e);
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|