Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 239 additions & 0 deletions Kepler-Server/src/main/java/org/alexdev/kepler/dao/mysql/PollDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package org.alexdev.kepler.dao.mysql;

import org.alexdev.kepler.dao.Storage;
import org.alexdev.kepler.game.polls.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PollDao {

public static Poll getPoll(int pollId) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

Poll poll = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM polls WHERE id = ? limit 1;", sqlConnection);
preparedStatement.setInt(1, pollId);
resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
poll = new Poll(
resultSet.getInt("id"),
resultSet.getString("headline"),
resultSet.getString("thank_you"),
resultSet.getString("description")
);
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}

return poll;
}

public static List<PollQuestion> getPollQuestions(int pollId) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

List<PollQuestion> questions = new ArrayList<>();

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM polls_questions WHERE poll_id = ?", sqlConnection);
preparedStatement.setInt(1, pollId);
resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
questions.add(new PollQuestion(
resultSet.getInt("id"),
resultSet.getInt("poll_id"),
PollQuestionType.valueOf(resultSet.getString("type")),
resultSet.getString("text"),
resultSet.getInt("min_select"),
resultSet.getInt("max_select")
));
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}

return questions;
}

public static List<PollQuestionOption> getPollQuestionOptions(int questionId) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

List<PollQuestionOption> questionOptions = new ArrayList<>();

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM polls_questions_options WHERE poll_question_id = ?", sqlConnection);
preparedStatement.setInt(1, questionId);
resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
questionOptions.add(new PollQuestionOption(
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getInt("poll_question_id")
));
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}

return questionOptions;
}

public static List<PollTrigger> getPollTriggers(int userId) {
List<PollTrigger> triggers = new ArrayList<>();

Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT\n" +
"pt.id as trigger_id,\n" +
"pt.room,\n" +
"pt.time_from,\n" +
"pt.time_to,\n" +
"p.id as poll_id,\n" +
"p.headline,\n" +
"p.thank_you,\n" +
"p.description\n" +
"FROM polls_triggers as pt\n" +
"LEFT JOIN polls as p on pt.poll_id = p.id\n" +
"WHERE p.id NOT IN (SELECT po.poll_id FROM polls_offers as po where po.user_id = ?);", sqlConnection);
preparedStatement.setInt(1, userId);
resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
PollTrigger trigger = new PollTrigger(
resultSet.getInt("trigger_id"),
resultSet.getInt("poll_id"),
resultSet.getInt("room"),
resultSet.getInt("time_from"),
resultSet.getInt("time_to"),
new Poll(
resultSet.getInt("poll_id"),
resultSet.getString("headline"),
resultSet.getString("thank_you"),
resultSet.getString("description")
)
);
triggers.add(trigger);
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}

return triggers;
}

public static void addAnswer(PollAnswer answer) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("INSERT INTO polls_answers (poll_question_id, value) VALUES (?, ?)", sqlConnection);

preparedStatement.setInt(1, answer.getPollQuestionId());
preparedStatement.setString(2, answer.getValue());
preparedStatement.execute();

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}

public static void addOffer(int pollId, int userId) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("INSERT INTO polls_offers (poll_Id, user_id) VALUES (?, ?)", sqlConnection);

preparedStatement.setInt(1, pollId);
preparedStatement.setInt(2, userId);
preparedStatement.execute();

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}


public static PollQuestion getQuestion(int questionId) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

PollQuestion question = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM polls_questions WHERE id = ? limit 1;", sqlConnection);
preparedStatement.setInt(1, questionId);
resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
question = new PollQuestion(
resultSet.getInt("id"),
resultSet.getInt("poll_id"),
PollQuestionType.valueOf(resultSet.getString("type")),
resultSet.getString("text"),
resultSet.getInt("min_select"),
resultSet.getInt("max_select")
);
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}

return question;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.alexdev.kepler.game.polls;

import org.alexdev.kepler.dao.mysql.PollDao;
import org.alexdev.kepler.game.player.Player;
import org.alexdev.kepler.messages.outgoing.poll.POLL_OFFER;
import org.alexdev.kepler.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class Poll {
private int id;
private String headline;
private String thankYou;
private String description;

public Poll(int id, String headline, String thankYou, String description) {
this.id = id;
this.headline = headline;
this.thankYou = thankYou;
this.description = description;
}

public int getId() {
return id;
}
public String getHeadline() {
return headline;
}
public String getThankYou() {
return thankYou;
}
public String getDescription() {
return description;
}

public List<PollQuestion> getQuestions() {
List<PollQuestion> questions = PollDao.getPollQuestions(this.id);
for (PollQuestion question : questions) {
question.addOptions(PollDao.getPollQuestionOptions(question.getId()));
}
return questions;
}

public static void sendAvailablePoll(Player player) {

List<PollTrigger> triggers = PollDao.getPollTriggers(player.getDetails().getId());
if(triggers.isEmpty()) return;

List<PollTrigger> actualTriggers = new ArrayList<>();

for (PollTrigger trigger : triggers) {
boolean hasTimeWindow = trigger.getTimeFrom() != 0 && trigger.getTimeTo() != 0;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or

boolean isWithinTimeWindow = !hasTimeWindow || !(DateUtil.getCurrentTimeSeconds() >= trigger.getTimeFrom() && DateUtil.getCurrentTimeSeconds() <= trigger.getTimeTo());
if(isWithinTimeWindow) {
if((player.getRoomUser() != null && player.getRoomUser().getRoom() != null) && trigger.getRoomId() == player.getRoomUser().getRoom().getId()) {
actualTriggers.add(trigger);
} else if(trigger.getRoomId() == 0) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if it can fit in the other if statement

actualTriggers.add(trigger);
}
}
}

if(!actualTriggers.isEmpty()) {
PollTrigger pollTrigger = actualTriggers.get(0);
player.send(new POLL_OFFER(pollTrigger.getPoll().getId(), pollTrigger.getPoll().getDescription()));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.alexdev.kepler.game.polls;


public class PollAnswer {
private int id;
private int pollQuestionId;
private String value;

public PollAnswer(int pollQuestionId, String value) {
this.pollQuestionId = pollQuestionId;
this.value = value;
}

public PollAnswer(int id, int pollQuestionId, String value) {
this.id = id;
this.pollQuestionId = pollQuestionId;
this.value = value;
}

public int getId() {
return id;
}

public int getPollQuestionId() {
return pollQuestionId;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.alexdev.kepler.game.polls;

public class PollOffer {
private int id;
private int pollId;
private int userId;

public PollOffer(int id, int pollId, int userId) {
this.id = id;
this.pollId = pollId;
this.userId = userId;
}

public int getId() {
return id;
}

public int getPollId() {
return pollId;
}

public int getUserId() {
return userId;
}
}
Loading