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

94 lines
2.5 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.polls;
import com.eu.habbo.messages.ISerialize;
import com.eu.habbo.messages.ServerMessage;
import gnu.trove.map.hash.THashMap;
2018-09-28 21:25:00 +02:00
2018-07-06 15:30:00 +02:00
import java.sql.ResultSet;
import java.sql.SQLException;
2018-09-28 21:25:00 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public class PollQuestion implements ISerialize, Comparable<PollQuestion> {
2018-12-22 11:39:00 +01:00
public final int id;
public final int parentId;
public final int type;
public final String question;
public final THashMap<Integer, String[]> options;
public final int minSelections;
public final int order;
2018-07-06 15:30:00 +02:00
private ArrayList<PollQuestion> subQuestions;
2019-05-26 20:14:53 +02:00
public PollQuestion(ResultSet set) throws SQLException {
2018-07-06 15:30:00 +02:00
this.id = set.getInt("id");
this.parentId = set.getInt("parent_id");
this.type = set.getInt("type");
this.question = set.getString("question");
this.minSelections = set.getInt("min_selections");
this.order = set.getInt("order");
2018-09-28 21:25:00 +02:00
this.options = new THashMap<>();
this.subQuestions = new ArrayList<>();
2018-07-06 15:30:00 +02:00
String opts = set.getString("options");
2019-05-26 20:14:53 +02:00
if (this.type == 1 || this.type == 2) {
for (int i = 0; i < opts.split(";").length; i++) {
2018-07-06 15:30:00 +02:00
this.options.put(i, new String[]{opts.split(";")[i].split(":")[0], opts.split(";")[i].split(":")[1]});
}
}
}
2019-05-26 20:14:53 +02:00
public void addSubQuestion(PollQuestion pollQuestion) {
2018-07-06 15:30:00 +02:00
this.subQuestions.add(pollQuestion);
}
@Override
2019-05-26 20:14:53 +02:00
public void serialize(ServerMessage message) {
2018-07-06 15:30:00 +02:00
message.appendInt(this.id);
message.appendInt(this.order);
message.appendInt(this.type);
message.appendString(this.question);
message.appendInt(this.minSelections);
message.appendInt(0);
message.appendInt(this.options.size());
2019-05-26 20:14:53 +02:00
if (this.type == 1 || this.type == 2) {
for (Map.Entry<Integer, String[]> set : this.options.entrySet()) {
2018-07-06 15:30:00 +02:00
message.appendString(set.getValue()[0]);
message.appendString(set.getValue()[1]);
message.appendInt(set.getKey());
}
}
2019-05-26 20:14:53 +02:00
if (this.parentId <= 0) {
2018-07-06 15:30:00 +02:00
Collections.sort(this.subQuestions);
message.appendInt(this.subQuestions.size());
2019-05-26 20:14:53 +02:00
for (PollQuestion q : this.subQuestions) {
2018-07-06 15:30:00 +02:00
q.serialize(message);
}
}
}
@Override
2019-05-26 20:14:53 +02:00
public int compareTo(PollQuestion o) {
2018-07-06 15:30:00 +02:00
return this.order - o.order;
}
}