Skip to content

Instantly share code, notes, and snippets.

@Elfocrash
Last active January 14, 2024 01:42
Show Gist options
  • Save Elfocrash/a5ba37666dd9c4d3f9a9ec022301930c to your computer and use it in GitHub Desktop.
Save Elfocrash/a5ba37666dd9c4d3f9a9ec022301930c to your computer and use it in GitHub Desktop.
Index: java/net/sf/l2j/gameserver/votereward/VoteApiService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/VoteApiService.java (date 1543688983838)
+++ java/net/sf/l2j/gameserver/votereward/VoteApiService.java (date 1543688983838)
@@ -0,0 +1,50 @@
+package net.sf.l2j.gameserver.votereward;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class VoteApiService {
+ private static final Logger LOGGER = Logger.getLogger(VoteApiService.class.getName());
+
+ public static String getApiResponse(String endpoint)
+ {
+ StringBuilder stringBuilder = new StringBuilder();
+
+ try
+ {
+ URL url = new URL(endpoint);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.addRequestProperty("User-Agent", "Mozilla/4.76");
+ connection.setRequestMethod("GET");
+
+ connection.setReadTimeout(5*1000);
+ connection.connect();
+
+ if(connection.getResponseCode() != 200){
+ LOGGER.log(Level.WARNING, "VoteApiService::getApiResponse returned " + connection.getResponseCode());
+ connection.disconnect();
+ return "";
+ }
+
+ try(BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())))
+ {
+ String line;
+ while ((line = reader.readLine()) != null)
+ {
+ stringBuilder.append(line).append("\n");
+ }
+ }
+ connection.disconnect();
+ return stringBuilder.toString();
+ }
+ catch (Exception e)
+ {
+ LOGGER.log(Level.SEVERE, "Something went wrong in VoteBase::getApiResponse", e);
+ return "";
+ }
+ }
+}
Index: java/net/sf/l2j/gameserver/model/actor/instance/Player.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/model/actor/instance/Player.java (revision 41ba208a948216d9ac1b6fbe513583d77ae24411)
+++ java/net/sf/l2j/gameserver/model/actor/instance/Player.java (date 1543687951509)
@@ -22,6 +22,7 @@
import java.util.stream.Collectors;
import net.sf.l2j.commons.concurrent.ThreadPool;
+import net.sf.l2j.commons.lang.StringUtil;
import net.sf.l2j.commons.math.MathUtil;
import net.sf.l2j.commons.random.Rnd;
@@ -240,6 +241,9 @@
import net.sf.l2j.gameserver.templates.skills.L2EffectType;
import net.sf.l2j.gameserver.templates.skills.L2SkillType;
import net.sf.l2j.gameserver.util.Broadcast;
+import net.sf.l2j.gameserver.votereward.VoteDao;
+import net.sf.l2j.gameserver.votereward.VoteSite;
+import net.sf.l2j.gameserver.votereward.VotedRecord;
/**
* This class represents a player in the world.<br>
@@ -247,7 +251,7 @@
*/
public final class Player extends Playable
{
- public enum StoreType
+ public enum StoreType
{
NONE(0),
SELL(1),
@@ -10127,4 +10131,64 @@
}
}
}
+
+ private boolean _isVoting;
+
+ public boolean isVoting() {
+ return _isVoting;
+ }
+
+ public void setIsVoting(boolean isVoting){
+ _isVoting = isVoting;
+ }
+
+ private Map<VoteSite, Long> _lastVoteTimestamps = new HashMap<VoteSite, Long>() {{
+ put(VoteSite.HOPZONE, -1L);
+ put(VoteSite.TOPZONE, -1L);
+ put(VoteSite.NETWORK, -1L);
+ }};
+
+ public long getLastVotedTimestamp(VoteSite voteSite){
+ return _lastVoteTimestamps.get(voteSite);
+ }
+
+ public void setLastVotedTimestamp(VoteSite voteSite, long timestamp){
+ _lastVoteTimestamps.put(voteSite, timestamp);
+ }
+
+ public boolean isEligibleToVote(VoteSite voteSite){
+ if(getLastVotedTimestamp(voteSite) == -1){
+ return VoteDao.canVoteOnSite(this, voteSite);
+ }
+
+ return (getLastVotedTimestamp(voteSite) + 43200000) < System.currentTimeMillis();
+ }
+
+ public String getVoteCountdown(VoteSite voteSite)
+ {
+ if(getLastVotedTimestamp(voteSite) == -1){
+ if(VoteDao.canVoteOnSite(this, voteSite))
+ return "You can vote now";
+ }
+
+ long youCanVote = getLastVotedTimestamp(voteSite) - (System.currentTimeMillis() - 43200000);
+ if(youCanVote <= 0)
+ return "You can vote now";
+ return StringUtil.convertLongToCountdown(youCanVote);
+ }
+
+ public String getIpAddress(){
+ return getClient().getConnection().getInetAddress().getHostAddress();
+ }
}
\ No newline at end of file
Index: java/net/sf/l2j/gameserver/votereward/VotedRecord.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/VotedRecord.java (date 1543685402185)
+++ java/net/sf/l2j/gameserver/votereward/VotedRecord.java (date 1543685402185)
@@ -0,0 +1,33 @@
+package net.sf.l2j.gameserver.votereward;
+
+public class VotedRecord {
+
+ private final String _accountName;
+ private final String _ipAddress;
+ private final long _dateTimeVoted;
+ private final String _voteSiteName;
+
+ public VotedRecord(String accountName, String ipAddress, long dateTimeVoted, String voteSiteName) {
+ _accountName = accountName;
+ _ipAddress = ipAddress;
+ _dateTimeVoted = dateTimeVoted;
+
+ _voteSiteName = voteSiteName;
+ }
+
+ public long getDateTimeVoted() {
+ return _dateTimeVoted;
+ }
+
+ public String getIpAddress() {
+ return _ipAddress;
+ }
+
+ public String getAccountName() {
+ return _accountName;
+ }
+
+ public String getVoteSiteName() {
+ return _voteSiteName;
+ }
+}
Index: java/net/sf/l2j/gameserver/votereward/VoteSiteInfo.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/VoteSiteInfo.java (date 1543687132877)
+++ java/net/sf/l2j/gameserver/votereward/VoteSiteInfo.java (date 1543687132877)
@@ -0,0 +1,13 @@
+package net.sf.l2j.gameserver.votereward;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface VoteSiteInfo {
+ VoteSite voteSite();
+ String apiKey();
+}
Index: java/net/sf/l2j/gameserver/votereward/NetworkVoteReward.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/NetworkVoteReward.java (date 1543687373567)
+++ java/net/sf/l2j/gameserver/votereward/NetworkVoteReward.java (date 1543687373567)
@@ -0,0 +1,29 @@
+package net.sf.l2j.gameserver.votereward;
+
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+
+@VoteSiteInfo(voteSite = VoteSite.NETWORK, apiKey = "NetworkApiKey")
+public class NetworkVoteReward extends VoteRewardSite {
+
+ @Override
+ protected String getEndpoint(Player player) {
+ return String.format("https://l2network.eu/index.php?a=in&u=L2BattleRoyale&ipc=%s", player.getIpAddress());
+ }
+
+ @Override
+ protected boolean hasVoted(Player player) {
+ String serverResponse = VoteApiService.getApiResponse(getEndpoint(player));
+
+ if(serverResponse.length() == 0){
+ player.sendMessage("Something went wrong with this request. Report it to the administrator.");
+ return false;
+ }
+
+ try{
+ return Integer.parseInt(serverResponse.trim()) == 1;
+ }catch (Exception e){
+ player.sendMessage("Something went wrong with this request. Report it to the administrator.");
+ return false;
+ }
+ }
+}
Index: java/net/sf/l2j/gameserver/votereward/VoteRewardSite.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/VoteRewardSite.java (date 1543689055030)
+++ java/net/sf/l2j/gameserver/votereward/VoteRewardSite.java (date 1543689055030)
@@ -0,0 +1,97 @@
+package net.sf.l2j.gameserver.votereward;
+
+import net.sf.l2j.commons.concurrent.ThreadPool;
+import net.sf.l2j.gameserver.model.World;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public abstract class VoteRewardSite {
+
+ private static final Logger LOGGER = Logger.getLogger(VoteRewardSite.class.getName());
+
+ protected abstract String getEndpoint(Player player);
+
+ public void checkVoteReward(Player player){
+ try
+ {
+ if(player.isVoting())
+ {
+ player.sendMessage("You are already voting.");
+ player.sendPacket(ActionFailed.STATIC_PACKET);
+ return;
+ }
+
+ if(World.getInstance().getPlayers().stream().filter(Player::isVoting).count() >= 5){
+ player.sendMessage("Many people are voting currently. Try again in a couple of seconds.");
+ player.sendPacket(ActionFailed.STATIC_PACKET);
+ return;
+ }
+
+ player.setIsVoting(true);
+
+ if(!player.isEligibleToVote(getVoteSiteInfo().voteSite()))
+ {
+ player.setIsVoting(false);
+ player.sendMessage("You can't vote for us yet.");
+ player.sendPacket(ActionFailed.STATIC_PACKET);
+ return;
+ }
+
+ ThreadPool.execute(() -> {
+
+ try{
+ if(!hasVoted(player)){
+ player.setIsVoting(false);
+ player.sendMessage(String.format("You haven't voted yet. Head to %s and vote for us.", getVoteSiteInfo().voteSite().getName()));
+ player.sendPacket(ActionFailed.STATIC_PACKET);
+ return;
+ }
+
+ long dateTimevoted = System.currentTimeMillis();
+
+ VotedRecord votedRecord = new VotedRecord(player.getAccountName(), player.getIpAddress(), dateTimevoted, getVoteSiteInfo().voteSite().getName());
+
+ VoteDao.addVotedRecord(votedRecord);
+ player.setLastVotedTimestamp(getVoteSiteInfo().voteSite(), dateTimevoted);
+ reward(player);
+ player.setIsVoting(false);
+ }catch (Exception e){
+ handleExceptionForVoteAttempt(player, e);
+ }
+
+ });
+
+ }catch (Exception e){
+ handleExceptionForVoteAttempt(player, e);
+ }
+ }
+
+ protected abstract boolean hasVoted(Player player);
+
+ protected void reward(Player player){
+ player.sendMessage("Thanks for voting! Here's your reward.");
+ player.addAdena("votereward", 5000, null,true);
+ }
+
+ private VoteSiteInfo getVoteSiteInfo() {
+ return getClass().getAnnotation(VoteSiteInfo.class);
+ }
+
+ String getApiKey(){
+ return getVoteSiteInfo().apiKey();
+ }
+
+ private void handleExceptionForVoteAttempt(Player player, Exception e) {
+ player.setIsVoting(false);
+ player.sendPacket(ActionFailed.STATIC_PACKET);
+ LOGGER.log(Level.WARNING, "There was an error during a vote attempt", e);
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getAnnotation(VoteSiteInfo.class).voteSite().getName();
+ }
+}
Index: java/net/sf/l2j/gameserver/votereward/TopzoneVoteReward.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/TopzoneVoteReward.java (date 1543687498629)
+++ java/net/sf/l2j/gameserver/votereward/TopzoneVoteReward.java (date 1543687498629)
@@ -0,0 +1,32 @@
+package net.sf.l2j.gameserver.votereward;
+
+import com.google.gson.*;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+
+@VoteSiteInfo(voteSite = VoteSite.TOPZONE, apiKey = "TopzoneApiKey")
+public class TopzoneVoteReward extends VoteRewardSite {
+
+ @Override
+ protected String getEndpoint(Player player) {
+ return String.format("https://api.l2topzone.com/v1/vote?token=%s&ip=%s", getApiKey(), player.getIpAddress());
+ }
+
+ @Override
+ protected boolean hasVoted(Player player) {
+ String serverResponse = VoteApiService.getApiResponse(getEndpoint(player));
+ if(serverResponse.length() == 0){
+ player.sendMessage("Something went wrong with this request. Report it to the administrator.");
+ return false;
+ }
+ JsonElement jelement = new JsonParser().parse(serverResponse);
+ JsonObject topObject = jelement.getAsJsonObject();
+ JsonPrimitive isOkPrimitive = topObject.getAsJsonPrimitive("ok");
+ if(!isOkPrimitive.getAsBoolean()){
+ return false;
+ }
+
+ JsonObject votedObject = topObject.getAsJsonObject("result");
+ JsonPrimitive isVotedObject = votedObject.getAsJsonPrimitive("isVoted");
+ return isVotedObject.getAsBoolean();
+ }
+}
Index: java/net/sf/l2j/gameserver/handler/IVoicedCommandHandler.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/handler/IVoicedCommandHandler.java (date 1543688336103)
+++ java/net/sf/l2j/gameserver/handler/IVoicedCommandHandler.java (date 1543688336103)
@@ -0,0 +1,8 @@
+package net.sf.l2j.gameserver.handler;
+
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+
+public interface IVoicedCommandHandler {
+ void useVoicedCommand(Player activeChar);
+ String[] getVoicedCommands();
+}
Index: java/net/sf/l2j/gameserver/votereward/HopzoneVoteReward.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/HopzoneVoteReward.java (date 1543689067047)
+++ java/net/sf/l2j/gameserver/votereward/HopzoneVoteReward.java (date 1543689067047)
@@ -0,0 +1,29 @@
+package net.sf.l2j.gameserver.votereward;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+
+@VoteSiteInfo(voteSite = VoteSite.HOPZONE, apiKey = "HopzoneApiKey")
+public class HopzoneVoteReward extends VoteRewardSite {
+
+ @Override
+ protected String getEndpoint(Player player) {
+ return String.format("https://api.hopzone.net/lineage2/vote?token=%s&ip_address=%s", getApiKey(), player.getIpAddress());
+ }
+
+ @Override
+ protected boolean hasVoted(Player player) {
+ String serverResponse = VoteApiService.getApiResponse(getEndpoint(player));
+ if(serverResponse.length() == 0){
+ player.sendMessage("Something went wrong with this request. Report it to the administrator.");
+ return false;
+ }
+ JsonElement jelement = new JsonParser().parse(serverResponse);
+ JsonObject topObject = jelement.getAsJsonObject();
+ JsonPrimitive votedObject = topObject.getAsJsonPrimitive("voted");
+ return votedObject.getAsBoolean();
+ }
+}
Index: java/net/sf/l2j/gameserver/handler/VoicedCommandHandler.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/handler/VoicedCommandHandler.java (date 1543688706143)
+++ java/net/sf/l2j/gameserver/handler/VoicedCommandHandler.java (date 1543688706143)
@@ -0,0 +1,46 @@
+package net.sf.l2j.gameserver.handler;
+
+import net.sf.l2j.gameserver.handler.voicecommandhandlers.VoteHopzoneCommandHandler;
+import net.sf.l2j.gameserver.handler.voicecommandhandlers.VoteNetworkCommandHandler;
+import net.sf.l2j.gameserver.handler.voicecommandhandlers.VoteTopzoneCommandHandler;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+public final class VoicedCommandHandler
+{
+ private static final Map<Integer, IVoicedCommandHandler> VOICED_COMMANDS = new HashMap<>();
+
+ private VoicedCommandHandler()
+ {
+ registerVoicedCommand(new VoteHopzoneCommandHandler());
+ registerVoicedCommand(new VoteTopzoneCommandHandler());
+ registerVoicedCommand(new VoteNetworkCommandHandler());
+ }
+
+ private void registerVoicedCommand(IVoicedCommandHandler voicedCommand)
+ {
+ Arrays.stream(voicedCommand.getVoicedCommands()).forEach(v -> VOICED_COMMANDS.put(v.intern().hashCode(), voicedCommand));
+ }
+
+ public IVoicedCommandHandler getVoicedCommand(String voicedCommand)
+ {
+ return VOICED_COMMANDS.get(voicedCommand.hashCode());
+ }
+
+ public int size()
+ {
+ return VOICED_COMMANDS.size();
+ }
+
+ public static final VoicedCommandHandler getInstance()
+ {
+ return SingletonHolder.INSTANCE;
+ }
+
+ private static final class SingletonHolder
+ {
+ private static final VoicedCommandHandler INSTANCE = new VoicedCommandHandler();
+ }
+}
\ No newline at end of file
Index: java/net/sf/l2j/gameserver/handler/chathandlers/ChatAll.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/handler/chathandlers/ChatAll.java (revision 41ba208a948216d9ac1b6fbe513583d77ae24411)
+++ java/net/sf/l2j/gameserver/handler/chathandlers/ChatAll.java (date 1543688775219)
@@ -1,6 +1,8 @@
package net.sf.l2j.gameserver.handler.chathandlers;
import net.sf.l2j.gameserver.handler.IChatHandler;
+import net.sf.l2j.gameserver.handler.IVoicedCommandHandler;
+import net.sf.l2j.gameserver.handler.VoicedCommandHandler;
import net.sf.l2j.gameserver.model.BlockList;
import net.sf.l2j.gameserver.model.actor.instance.Player;
import net.sf.l2j.gameserver.network.FloodProtectors;
@@ -19,6 +21,14 @@
{
if (!FloodProtectors.performAction(activeChar.getClient(), Action.GLOBAL_CHAT))
return;
+
+ if (text.startsWith(".")){
+ final IVoicedCommandHandler voicedCommand = VoicedCommandHandler.getInstance().getVoicedCommand(text.substring(1).toLowerCase());
+ if (voicedCommand != null){
+ voicedCommand.useVoicedCommand(activeChar);
+ return;
+ }
+ }
final CreatureSay cs = new CreatureSay(activeChar.getObjectId(), type, activeChar.getName(), text);
for (Player player : activeChar.getKnownTypeInRadius(Player.class, 1250))
Index: java/net/sf/l2j/commons/lang/StringUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/commons/lang/StringUtil.java (revision 41ba208a948216d9ac1b6fbe513583d77ae24411)
+++ java/net/sf/l2j/commons/lang/StringUtil.java (date 1543687942252)
@@ -5,6 +5,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
+import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -171,6 +172,15 @@
return result;
}
+
+ public static String convertLongToCountdown(long timestamp)
+ {
+ long hours = TimeUnit.MILLISECONDS.toHours(timestamp);
+ long minutes = TimeUnit.MILLISECONDS.toMinutes(timestamp) -
+ TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timestamp));
+
+ return hours > 0 ? hours + " hour(s)" : minutes + " minutes(s)";
+ }
/**
* Format a {@link String} to delete its extension ("castles.xml" > "castles"), if any.
Index: java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteTopzoneCommandHandler.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteTopzoneCommandHandler.java (date 1543688659582)
+++ java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteTopzoneCommandHandler.java (date 1543688659582)
@@ -0,0 +1,28 @@
+package net.sf.l2j.gameserver.handler.voicecommandhandlers;
+
+import net.sf.l2j.gameserver.handler.IVoicedCommandHandler;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+import net.sf.l2j.gameserver.votereward.TopzoneVoteReward;
+import net.sf.l2j.gameserver.votereward.VoteSite;
+
+public class VoteTopzoneCommandHandler implements IVoicedCommandHandler
+{
+ private static final String[] VOICED_COMMANDS = {"votetop"};
+
+ @Override
+ public void useVoicedCommand(Player activeChar)
+ {
+ if(activeChar.isEligibleToVote(VoteSite.TOPZONE)){
+ TopzoneVoteReward rewardSite = new TopzoneVoteReward();
+ rewardSite.checkVoteReward(activeChar);
+ return;
+ }
+ activeChar.sendMessage("Available in " + activeChar.getVoteCountdown(VoteSite.TOPZONE));
+ }
+
+ @Override
+ public String[] getVoicedCommands()
+ {
+ return VOICED_COMMANDS;
+ }
+}
\ No newline at end of file
Index: java/net/sf/l2j/gameserver/GameServer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/GameServer.java (revision 41ba208a948216d9ac1b6fbe513583d77ae24411)
+++ java/net/sf/l2j/gameserver/GameServer.java (date 1542553128220)
@@ -62,11 +62,7 @@
import net.sf.l2j.gameserver.data.xml.TeleportLocationData;
import net.sf.l2j.gameserver.data.xml.WalkerRouteData;
import net.sf.l2j.gameserver.geoengine.GeoEngine;
-import net.sf.l2j.gameserver.handler.AdminCommandHandler;
-import net.sf.l2j.gameserver.handler.ChatHandler;
-import net.sf.l2j.gameserver.handler.ItemHandler;
-import net.sf.l2j.gameserver.handler.SkillHandler;
-import net.sf.l2j.gameserver.handler.UserCommandHandler;
+import net.sf.l2j.gameserver.handler.*;
import net.sf.l2j.gameserver.idfactory.IdFactory;
import net.sf.l2j.gameserver.instancemanager.AuctionManager;
import net.sf.l2j.gameserver.instancemanager.AutoSpawnManager;
@@ -277,6 +273,7 @@
LOGGER.info("Loaded {} item handlers.", ItemHandler.getInstance().size());
LOGGER.info("Loaded {} skill handlers.", SkillHandler.getInstance().size());
LOGGER.info("Loaded {} user command handlers.", UserCommandHandler.getInstance().size());
+ LOGGER.info("Loaded {} vote command handlers.", VoicedCommandHandler.getInstance().size());
StringUtil.printSection("System");
Runtime.getRuntime().addShutdownHook(Shutdown.getInstance());
Index: java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteNetworkCommandHandler.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteNetworkCommandHandler.java (date 1543688666756)
+++ java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteNetworkCommandHandler.java (date 1543688666756)
@@ -0,0 +1,28 @@
+package net.sf.l2j.gameserver.handler.voicecommandhandlers;
+
+import net.sf.l2j.gameserver.handler.IVoicedCommandHandler;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+import net.sf.l2j.gameserver.votereward.NetworkVoteReward;
+import net.sf.l2j.gameserver.votereward.VoteSite;
+
+public class VoteNetworkCommandHandler implements IVoicedCommandHandler
+{
+ private static final String[] VOICED_COMMANDS = {"votenet"};
+
+ @Override
+ public void useVoicedCommand(Player activeChar)
+ {
+ if(activeChar.isEligibleToVote(VoteSite.NETWORK)){
+ NetworkVoteReward rewardSite = new NetworkVoteReward();
+ rewardSite.checkVoteReward(activeChar);
+ return;
+ }
+ activeChar.sendMessage("Available in " + activeChar.getVoteCountdown(VoteSite.NETWORK));
+ }
+
+ @Override
+ public String[] getVoicedCommands()
+ {
+ return VOICED_COMMANDS;
+ }
+}
\ No newline at end of file
Index: java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteHopzoneCommandHandler.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteHopzoneCommandHandler.java (date 1543688674182)
+++ java/net/sf/l2j/gameserver/handler/voicecommandhandlers/VoteHopzoneCommandHandler.java (date 1543688674182)
@@ -0,0 +1,28 @@
+package net.sf.l2j.gameserver.handler.voicecommandhandlers;
+
+import net.sf.l2j.gameserver.handler.IVoicedCommandHandler;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+import net.sf.l2j.gameserver.votereward.HopzoneVoteReward;
+import net.sf.l2j.gameserver.votereward.VoteSite;
+
+public class VoteHopzoneCommandHandler implements IVoicedCommandHandler
+{
+ private static final String[] VOICED_COMMANDS = {"votehop"};
+
+ @Override
+ public void useVoicedCommand(Player activeChar)
+ {
+ if(activeChar.isEligibleToVote(VoteSite.HOPZONE)){
+ HopzoneVoteReward rewardSite = new HopzoneVoteReward();
+ rewardSite.checkVoteReward(activeChar);
+ return;
+ }
+ activeChar.sendMessage("Available in " + activeChar.getVoteCountdown(VoteSite.HOPZONE));
+ }
+
+ @Override
+ public String[] getVoicedCommands()
+ {
+ return VOICED_COMMANDS;
+ }
+}
\ No newline at end of file
Index: java/net/sf/l2j/gameserver/votereward/VoteSite.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/VoteSite.java (date 1543686071235)
+++ java/net/sf/l2j/gameserver/votereward/VoteSite.java (date 1543686071235)
@@ -0,0 +1,19 @@
+package net.sf.l2j.gameserver.votereward;
+
+public enum VoteSite {
+
+ HOPZONE("Hopzone"),
+ TOPZONE("Topzone"),
+ NETWORK("L2Network");
+
+ private final String _name;
+
+ VoteSite(String name)
+ {
+ _name = name;
+ }
+
+ public String getName(){
+ return _name;
+ }
+}
Index: java/net/sf/l2j/gameserver/votereward/VoteDao.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- java/net/sf/l2j/gameserver/votereward/VoteDao.java (date 1543688978311)
+++ java/net/sf/l2j/gameserver/votereward/VoteDao.java (date 1543688978311)
@@ -0,0 +1,64 @@
+package net.sf.l2j.gameserver.votereward;
+
+import net.sf.l2j.L2DatabaseFactory;
+import net.sf.l2j.gameserver.model.actor.instance.Player;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class VoteDao {
+
+ public static final Logger LOGGER = Logger.getLogger(VoteDao.class.getName());
+
+ public static void addVotedRecord(VotedRecord votedRecord) {
+ try (Connection con = L2DatabaseFactory.getInstance().getConnection())
+ {
+ PreparedStatement ps = con.prepareStatement("INSERT INTO votedrecords (accountname, ipaddress, datetimevoted, votesitename) VALUES (?,?,?,?);");
+ ps.setString(1, votedRecord.getAccountName());
+ ps.setString(2, votedRecord.getIpAddress());
+ ps.setLong(3, votedRecord.getDateTimeVoted());
+ ps.setString(4, votedRecord.getVoteSiteName());
+ ps.execute();
+ ps.close();
+ }
+ catch (SQLException e)
+ {
+ LOGGER.log(Level.SEVERE, "Couldn't add voted record", e);
+ }
+ }
+
+ public static boolean canVoteOnSite(Player player, VoteSite voteSite) {
+ try (Connection con = L2DatabaseFactory.getInstance().getConnection())
+ {
+ PreparedStatement ps = con.prepareStatement("select datetimevoted from votedrecords " +
+ "where (accountname = ? or ipaddress = ?) and votesitename = ? and datetimevoted + ? > ? " +
+ "order by datetimevoted " +
+ "desc limit 1");
+
+ ps.setString(1, player.getAccountName());
+ ps.setString(2, player.getIpAddress());
+ ps.setString(3, voteSite.getName());
+ ps.setLong(4, 43200000);
+ ps.setLong(5, System.currentTimeMillis());
+ ResultSet rs = ps.executeQuery();
+
+ if (!rs.next())
+ {
+ return true;
+ }
+ player.setLastVotedTimestamp(voteSite, rs.getLong("datetimevoted"));
+ rs.close();
+ ps.close();
+ }
+ catch (SQLException e)
+ {
+ LOGGER.log(Level.SEVERE, "Couldn't check if user can vote", e);
+ }
+
+ return false;
+ }
+}
----------------------------
SQL Table
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `votedrecords`;
CREATE TABLE `votedrecords` (
`accountname` varchar(45) NOT NULL,
`ipaddress` varchar(100) NOT NULL,
`datetimevoted` bigint(20) NOT NULL,
`votesitename` varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment