Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package blizzard.aepvp.model.controlers;
- import java.sql.ResultSet;
- import java.util.StringTokenizer;
- import java.util.stream.Stream;
- import blizzard.aepvp.authentication.PassportManager;
- import blizzard.aepvp.authentication.PlayerPassport;
- import gnu.trove.map.hash.TIntObjectHashMap;
- import gnu.trove.set.hash.TIntHashSet;
- import l2.ae.pvp.Debug;
- import l2.ae.pvp.L2DatabaseFactory;
- import l2.ae.pvp.gameserver.Shutdown;
- import l2.ae.pvp.gameserver.Shutdown.Savable;
- import l2.ae.pvp.gameserver.cache.HtmCache;
- import l2.ae.pvp.gameserver.idfactory.IdFactory;
- import l2.ae.pvp.gameserver.model.IBypassHandler;
- import l2.ae.pvp.gameserver.model.actor.instance.L2PcInstance;
- import l2.ae.pvp.gameserver.network.clientpackets.RequestBypassToServer;
- import l2.ae.pvp.gameserver.network.serverpackets.NpcHtmlMessage;
- public class FAQController implements IBypassHandler, Savable
- {
- private static final int STATUS_NOT_APPROVED = 0;
- private static final int STATUS_APPROVED = 1;
- private static final int STATUS_DELETED = -1;
- private static final int STATUS_PINNED = 2;
- private static final int TOPICS_PER_PAGE = 4;
- private static String SELECT_FAQ = "SELECT * FROM server_faq WHERE status > -1";
- private static String topic_header;
- private final TIntObjectHashMap<FAQTopic> _topics = new TIntObjectHashMap<>();
- private FAQController()
- {
- RequestBypassToServer.register(this);
- Shutdown.addShutdownHook(this);
- reloadHtml();
- load();
- }
- public void reloadHtml()
- {
- topic_header = HtmCache.getInstance().getHtm("data/html/aepvp/cubic/faq/topic_header.htm");
- }
- private void load()
- {
- try (final var con = L2DatabaseFactory.getConnectionS();
- final var st = con.createStatement();
- final var rs = st.executeQuery(SELECT_FAQ))
- {
- while (rs.next())
- {
- final var faqTopic = new FAQTopic(rs);
- _topics.put(faqTopic.getTopicId(), faqTopic);
- }
- try (final var st2 = con.createStatement();
- final var rs2 = st2.executeQuery("SELECT * FROM server_faq_likes"))
- {
- while (rs2.next())
- {
- final int playerId = rs2.getInt("player_id");
- final int topicId = rs2.getInt("topic_id");
- final var topic = _topics.get(topicId);
- if (topic != null)
- topic.addLike(playerId);
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public boolean handleBypass(L2PcInstance player, String cmd)
- {
- if (cmd.startsWith("faq_main"))
- {
- final StringTokenizer st = new StringTokenizer(cmd);
- st.nextToken();
- if (st.hasMoreTokens())
- {
- final int page = Integer.parseInt(st.nextToken());
- final String search = st.hasMoreTokens() ? st.nextToken() : null;
- renderMain(player, search, page);
- }
- else
- renderMain(player, null);
- return true;
- }
- else if (cmd.startsWith("faq_open"))
- {
- if (cmd.length() < 9)
- renderMain(player, null);
- else
- {
- final int topicId = Integer.parseInt(cmd.substring(9));
- openTopic(player, topicId);
- }
- }
- else if (cmd.startsWith("faq_search"))
- {
- if (cmd.length() < 12)
- renderMain(player, null);
- else
- {
- final String search = cmd.substring(11);
- renderMain(player, search);
- }
- return true;
- }
- else if (cmd.startsWith("faq_topic"))
- {
- Debug.append("FAQQQQ");
- return true;
- }
- else if (cmd.startsWith("faq_delete"))
- {
- if (cmd.length() < 12)
- renderMain(player, null);
- else
- {
- final int topicId = Integer.parseInt(cmd.substring(11));
- deleteTopic(topicId);
- renderMain(player, null);
- }
- return true;
- }
- else if (cmd.startsWith("faq_like"))
- {
- if (cmd.length() < 10)
- renderMain(player, null);
- else
- {
- final int topicId = Integer.parseInt(cmd.substring(9));
- likeTopic(player, topicId);
- }
- return true;
- }
- else if (cmd.startsWith("faq_edit_content"))
- {
- final StringTokenizer st = new StringTokenizer(cmd);
- st.nextToken();
- final int topicId = Integer.parseInt(st.nextToken());
- final String content = st.nextToken("\t").substring(1).replace("\r\n", "<br1>");
- editTopicContent(player, topicId, content);
- return true;
- }
- else if (cmd.startsWith("faq_edit_title"))
- {
- final StringTokenizer st = new StringTokenizer(cmd);
- st.nextToken();
- final int topicId = Integer.parseInt(st.nextToken());
- final String content = st.nextToken("\t").strip().replace("\r\n", "");
- editTopicTitle(player, topicId, content);
- return true;
- }
- else if (cmd.startsWith("faq_edit_icon"))
- {
- final StringTokenizer st = new StringTokenizer(cmd);
- st.nextToken();
- final int topicId = Integer.parseInt(st.nextToken());
- final int ic = Integer.parseInt(st.nextToken());
- final String icon = st.nextToken("\t").strip();
- editTopicIcon(player, topicId, icon, ic);
- return true;
- }
- else if (cmd.startsWith("faq_edit"))
- {
- if (cmd.length() < 10)
- renderMain(player, null);
- else
- {
- final int topicId = Integer.parseInt(cmd.substring(9));
- editTopic(player, topicId);
- }
- return true;
- }
- else if (cmd.startsWith("faq_stat"))
- {
- final StringTokenizer st = new StringTokenizer(cmd);
- st.nextToken();
- if (st.hasMoreTokens())
- {
- final int topicId = Integer.parseInt(st.nextToken());
- final int status = Integer.parseInt(st.nextToken());
- editTopicStatus(player, topicId, status);
- }
- return true;
- }
- else if (cmd.startsWith("faq_create_main"))
- {
- createMain(player);
- return true;
- }
- else if (cmd.startsWith("faq_create"))
- {
- final StringTokenizer st = new StringTokenizer(cmd);
- st.nextToken();
- if (st.hasMoreTokens())
- {
- final String title = st.nextToken("%-%").strip().replace("\r\n", "");
- if (st.hasMoreTokens())
- {
- final String cont = st.nextToken("%-%").strip().replace("\r\n", "<br1>");
- createTopic(player, title, cont);
- }
- }
- return true;
- }
- return false;
- }
- private void createMain(final L2PcInstance player)
- {
- final var html = new NpcHtmlMessage("data/html/aepvp/cubic/faq/create.htm");
- player.sendPacket(html);
- }
- private void createTopic(final L2PcInstance player, final String title, final String content)
- {
- if (!player.isGM())
- {
- int counter = 0;
- for (final var topic : _topics.valueCollection())
- {
- if (topic._status == STATUS_NOT_APPROVED && topic._author == player.getPassport() && counter++ > 2)
- {
- player.sendMessage("You have already submitted 3 or more topics that await approval by a GM. Please wait.");
- return;
- }
- }
- if (player.getLevel() < 90)
- {
- player.sendMessage("You must be at least Lv. 90 to create a FAQ.");
- return;
- }
- }
- final var topic = new FAQTopic(player.getPassport(), title, content);
- _topics.put(topic.getTopicId(), topic);
- openTopic(player, topic.getTopicId());
- }
- private void editTopicStatus(final L2PcInstance player, final int topicId, final int status)
- {
- if (player.isGM())
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- {
- if (status == STATUS_APPROVED)
- topic.toggleActive();
- else
- topic.togglePinned();
- }
- }
- editTopic(player, topicId);
- }
- private void editTopicIcon(final L2PcInstance player, final int topicId, final String icon, final int ic)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- {
- topic.updateIcon(icon, ic == 1);
- }
- editTopic(player, topicId);
- }
- private void editTopicTitle(final L2PcInstance player, final int topicId, final String title)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- topic.updateTitle(title);
- editTopic(player, topicId);
- }
- private void editTopicContent(final L2PcInstance player, final int topicId, final String content)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- topic.updateContent(content);
- editTopic(player, topicId);
- }
- private void editTopic(final L2PcInstance player, final int topicId)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- {
- final var npcHtml = new NpcHtmlMessage();
- final var htmlStr = topic.render(HtmCache.getInstance().getHtm("data/html/aepvp/cubic/faq/edit.htm"), player);
- npcHtml.setHtml(htmlStr);
- npcHtml.replace("%id%", String.valueOf(topicId));
- npcHtml.replace("%appr%", topic.isActive() ? "Hide" : "Approve");
- npcHtml.replace("%pin%", topic.isPinned() ? "Unpin" : "Pin");
- player.sendPacket(npcHtml);
- }
- }
- private void openTopic(final L2PcInstance player, final int topicId)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- {
- final var npcHtml = topic.renderTopic(player);
- player.sendPacket(npcHtml);
- }
- }
- private void renderMain(final L2PcInstance player, final String search)
- {
- renderMain(player, search, 0);
- }
- private void renderMain(final L2PcInstance player, final String search, final int page)
- {
- final var npcHtml = new NpcHtmlMessage();
- npcHtml.setFile("data/html/aepvp/cubic/faq/main.htm");
- final StringBuilder sb = new StringBuilder(2048);
- final var topicsStream = getTopics(search);
- final var topics = topicsStream.filter((topic) -> {return topic.isRenderableFor(player);}).toArray(FAQTopic[]::new);
- final var topicsLen = topics.length;
- final int pages = topicsLen < TOPICS_PER_PAGE ? 1 : topicsLen / TOPICS_PER_PAGE + ((topicsLen % TOPICS_PER_PAGE) > 0 ? 1 : 0);
- for (int i = 0; i < TOPICS_PER_PAGE; i++)
- {
- final int indx = TOPICS_PER_PAGE * page + i;
- if (indx < topicsLen)
- {
- final var topic = topics[indx];
- topic.renderHeader(sb, player);
- }
- else
- sb.append("<img height=89/>");
- }
- npcHtml.replace("%topic_headers%", sb.toString());
- sb.setLength(0);
- for (int i = 0; i < pages; i++)
- {
- if (page == i)
- sb.append(String.format("<td align=center>Page %d</td>", i+1));
- else
- sb.append(String.format("<td align=center><a action=\"bypass faq_main %d %s\">Page %d</a></td>", i, search == null ? "" : search, i + 1));
- }
- npcHtml.replace("%pages1%", sb.toString());
- player.sendPacket(npcHtml);
- }
- private Stream<FAQTopic> getTopics(final String search)
- {
- final var topicStream = getTopicsStream();
- if (search == null)
- return topicStream;
- else
- return topicStream.filter((topic) -> { return topic.matchesSearch(search); });
- }
- private Stream<FAQTopic> getTopicsStream()
- {
- return _topics.valueCollection().stream().sorted();
- }
- private void likeTopic(final L2PcInstance player, final int topicId)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- {
- topic.tryLike(player);
- }
- openTopic(player, topic.getTopicId());
- }
- private void deleteTopic(final int topicId)
- {
- final var topic = _topics.get(topicId);
- if (topic != null)
- {
- topic.delete();
- }
- }
- public static class FAQTopic implements Comparable<FAQTopic>
- {
- private final TIntHashSet _likedNew = new TIntHashSet();
- private final TIntHashSet _liked = new TIntHashSet();
- private static final String nullIcon = "L2UI_ct1.SystemMenuWnd.SystemMenuWnd_df_Help";
- private final int _topicId;
- private final PlayerPassport _author;
- private String _title;
- private String _content;
- private String _icon_a;
- private String _icon_b;
- // private int _likes;
- private int _status;
- private boolean _updated;
- private boolean _new;
- public boolean matchesSearch(final String search)
- {
- final String author = _author.getPlayerName();
- return author.contains(search) ||
- _title.contains(search) ||
- _content.contains(search)||
- author.toLowerCase().contains(search.toLowerCase()) ||
- _title.toLowerCase().contains(search.toLowerCase()) ||
- _content.toLowerCase().contains(search.toLowerCase())
- ;
- }
- public FAQTopic(final PlayerPassport author, final String title, final String content)
- {
- _topicId = IdFactory.getInstance().getNextId();
- _author = author;
- _title = title;
- _content = content;
- _new = true;
- }
- public FAQTopic(final ResultSet rs) throws Exception
- {
- _topicId = rs.getInt("topic_id");
- final int authorId = rs.getInt("author_id");
- _author = PassportManager.getInstance().fetch(authorId);
- _title = rs.getString("title");
- _content = rs.getString("content");
- _icon_a = rs.getString("icon_a");
- _icon_b = rs.getString("icon_b");
- _status = rs.getInt("status");
- }
- public void addLike(final int playerId)
- {
- _liked.add(playerId);
- }
- public boolean tryLike(final L2PcInstance player)
- {
- final var playerId = player.getObjectId();
- if (!player.isGM())
- {
- if (_liked.contains(playerId))
- return false;
- }
- _liked.add(playerId);
- _likedNew.add(playerId);
- return true;
- }
- public boolean hasLike(final int playerId)
- {
- return _liked.contains(playerId);
- }
- public void setStatus(final int status)
- {
- _status = status;
- }
- public boolean isRenderableFor(final L2PcInstance player)
- {
- return notDeleted() && (isActive() || player.getName().equals(_author) || player.isGM());
- }
- public boolean isActive()
- {
- return _status > STATUS_NOT_APPROVED;
- }
- public boolean notDeleted()
- {
- return _status > STATUS_DELETED;
- }
- public int getTopicId()
- {
- return _topicId;
- }
- public TIntHashSet getNewLikes()
- {
- return _likedNew;
- }
- public int getLikes()
- {
- return _liked.size();
- }
- public int getRankingLikes()
- {
- if (_status == STATUS_PINNED)
- return getLikes() + 10000;
- return getLikes();
- }
- public void updateTitle(final String title)
- {
- _title = title;
- _updated = true;
- }
- public void updateContent(final String content)
- {
- _content = content;
- _updated = true;
- }
- public void togglePinned()
- {
- if (_status == 2)
- _status = 1;
- else
- _status = 2;
- _updated = true;
- }
- public void toggleActive()
- {
- if (_status < 1)
- _status = 1;
- else
- _status = 0;
- _updated = true;
- }
- public boolean isPinned()
- {
- return _status == STATUS_PINNED;
- }
- public boolean isNew()
- {
- return _new;
- }
- public void renderHeader(final StringBuilder sb, final L2PcInstance viewer)
- {
- String topicHeader = render(topic_header, viewer);
- if (_status == 2)
- topicHeader = topicHeader.replace("_Info", "_HeroConfirm");
- sb.append(topicHeader);
- }
- public void renderTopic(final StringBuilder sb)
- {
- }
- public String render(final String render, final L2PcInstance viewer)
- {
- final String gmTemplate = String.format("<td align=center><table width=100%%><tr><td><a action=\"bypass faq_delete %d\" msg=\"You want to delete topic %s?\"><font color=\"c43c3c\">Delete</font></a> </td><td><a action=\"bypass faq_edit %d\">Edit</a></td></tr></table></td>", _topicId, _title, _topicId);
- return render.replace("%title%", _title).
- replace("%likes%", String.valueOf(getLikes())).
- replace("%topic_id%", String.valueOf(_topicId)).
- replace("%icon_a%", _icon_a == null ? nullIcon : String.valueOf(_icon_a)).
- replace("%icon_b%", _icon_b == null ? nullIcon : String.valueOf(_icon_b)).
- replace("%gm%", viewer.isGM() ? gmTemplate : "").
- replace(" Likes", isPinned() ? " Likes - <font color=\"c43c3c\">PINNED</font>" : !isActive() ? " Likes - <font color=\"LEVEL\">UNLISTED</font>" : " Likes").
- replace("%content%", _content).
- replace("%ilike%", hasLike(viewer.getObjectId()) ? "" : "<tr></tr><tr><td align=center><a action=\"bypass faq_like %id%\">I Like this!</a></td></tr>").
- replace("%id%", String.valueOf(_topicId)).
- replace("%author%", _author.getPlayerName());
- }
- public NpcHtmlMessage renderTopic(final L2PcInstance viewer)
- {
- final var html = HtmCache.getInstance().getHtm("data/html/aepvp/cubic/faq/topic.htm");
- final var npcHtml = new NpcHtmlMessage();
- final String renderHtm = render(html, viewer);
- npcHtml.setHtml(renderHtm);
- return npcHtml;
- }
- public void updateIcon(final String icon, final boolean ic)
- {
- if (ic)
- _icon_a = icon;
- else
- _icon_b = icon;
- _updated = true;
- }
- public void activate()
- {
- _status = STATUS_APPROVED;
- _updated = true;
- }
- public void delete()
- {
- _status = STATUS_DELETED;
- _updated = true;
- }
- public boolean isUpdated()
- {
- return _updated;
- }
- @Override
- public int compareTo(FAQTopic other)
- {
- return other.getRankingLikes() - getRankingLikes();
- }
- @Override
- public String toString()
- {
- return _title + " By " + _author;
- }
- }
- private static class InstanceHolder
- {
- private static final FAQController _instance = new FAQController();
- }
- public static FAQController getInstance()
- {
- return InstanceHolder._instance;
- }
- @Override
- public void exception(Exception e)
- {
- e.printStackTrace();
- }
- @Override
- public void store()
- {
- try (final var con = L2DatabaseFactory.getConnectionS();
- final var psu = con.prepareStatement("UPDATE server_faq SET author_id = ?, title = ?, content = ?, icon_a = ?, icon_b = ?, status = ? WHERE topic_id = ?");
- final var psn = con.prepareStatement("INSERT INTO server_faq (topic_id, author_id, title, content, icon_a, icon_b, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
- final var psl = con.prepareStatement("INSERT INTO server_faq_likes (topic_id, player_id) VALUES (?, ?)");)
- {
- con.setAutoCommit(false);
- for (final var faqTopic : _topics.valueCollection())
- {
- if (faqTopic.isNew())
- {
- psn.setInt(1, faqTopic.getTopicId());
- psn.setInt(2, faqTopic._author.getObjectId());
- psn.setString(3, faqTopic._title);
- psn.setString(4, faqTopic._content);
- psn.setString(5, faqTopic._icon_a);
- psn.setString(6, faqTopic._icon_b);
- psn.setInt(7, faqTopic._status);
- psn.addBatch();
- }
- else if (faqTopic.isUpdated())
- {
- psu.setInt(1, faqTopic._author.getObjectId());
- psu.setString(2, faqTopic._title);
- psu.setString(3, faqTopic._content);
- psu.setString(4, faqTopic._icon_a);
- psu.setString(5, faqTopic._icon_b);
- psu.setInt(6, faqTopic._status);
- psu.setInt(7, faqTopic._topicId);
- psu.addBatch();
- }
- final var newLikes = faqTopic.getNewLikes();
- if (!newLikes.isEmpty())
- {
- for (final var playerId : newLikes._set) if (playerId > 0)
- {
- psl.setInt(1, faqTopic.getTopicId());
- psl.setInt(2, playerId);
- psl.addBatch();
- }
- }
- }
- final int inserted = psn.executeBatch().length;
- final int updated = psu.executeBatch().length;
- final int insertdl = psl.executeBatch().length;
- con.commit();
- System.err.println("FAQController: Inserted " + inserted + " new FAQ Topics!");
- System.err.println("FAQController: Updated " + updated + " old FAQ Topics!");
- System.err.println("FAQController: Inserted " + insertdl + " new FAQ Likes!");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement