forked from Quackster/Kepler
-
Notifications
You must be signed in to change notification settings - Fork 1
Started work on polls #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Webbanditten
wants to merge
13
commits into
master
Choose a base branch
from
feature/polls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
12be54f
Started work on polls
Webbanditten 9895609
feature: Continue to explore polls
Webbanditten 7f838cd
Merge branch 'master' into feature/polls
Webbanditten f431d36
feature: Continue exploration of polls
Webbanditten 59acf3f
Created classes for Poll system
Webbanditten e9a5c63
Started work on polls dao
Webbanditten adc8a99
Started work on implementation of polls
Webbanditten 3587a31
Started adding SQL for polls
Webbanditten 77413bd
Prompting for poll now works
Webbanditten 856705c
Added text field to poll question
Webbanditten e348ac5
Presenting polls to the user now works
Webbanditten 581eb70
Fixed offline messenger requests
Webbanditten 1911e52
feat: More work on polls
Webbanditten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
239 changes: 239 additions & 0 deletions
239
Kepler-Server/src/main/java/org/alexdev/kepler/dao/mysql/PollDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
Kepler-Server/src/main/java/org/alexdev/kepler/game/polls/Poll.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| 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) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())); | ||
| } | ||
|
|
||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
Kepler-Server/src/main/java/org/alexdev/kepler/game/polls/PollAnswer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
Kepler-Server/src/main/java/org/alexdev/kepler/game/polls/PollOffer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or