U3Games

Games | Desarrollo & Soporte => L2 | Sección de Servidores => Lineage => L2 | Implementaciones => Mensaje iniciado por: Swarlog en Ago 03, 2025, 01:38 AM

Título: Vote Reward H5 by CIAurelius
Publicado por: Swarlog en Ago 03, 2025, 01:38 AM
Buenas a tod@s, nuevo código de votos en las webs (HOPZONE y TOPZONE).

Hopzone.java
-------------------------------------------------------------------------------------------------
package com.l2jserver.gameserver.votereward;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import static com.l2jserver.gameserver.config.Configuration.voteSystem;

/**
 * Hopzone Vote System.
 * @author CIAurelius
 */

public class Hopzone extends VoteSystem
{
public Hopzone(int votesDiff, boolean allowReport, int boxes, Map<Integer, Integer> rewards, int checkMins)
{
super(votesDiff, allowReport, boxes, rewards, checkMins);
}

public void run()
{
reward();
}

public int getVotes()
{
int votes = 1;

try
{
URLConnection con = new URL(voteSystem().hopzoneServerLink()).openConnection();
con.addRequestProperty("User-Agent", "Mozilla/5.0");

BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

String line;
while ((line = br.readLine()) != null)
{
if (line.contains("Total Votes"))
{
String inputLine = line.split(">")[2].replace("</span", "");
votes = Integer.parseInt(inputLine);
}
}
br.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error while getting server vote count from " + getSiteName() + ".");
}
return votes;
}

public String getSiteName()
{
return "[Hopzone]";
}
}

Topzone.java
-------------------------------------------------------------------------------------------------
package com.l2jserver.gameserver.votereward;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import static com.l2jserver.gameserver.config.Configuration.voteSystem;

/**
 * Topzone Vote System.
 * @author CIAurelius
 */

public class Topzone extends VoteSystem
{
public Topzone(int votesDiff, boolean allowReport, int boxes, Map<Integer, Integer> rewards, int checkMins)
{
super(votesDiff, allowReport, boxes, rewards, checkMins);
}

public void run()
{
reward();
}

public int getVotes()
{
int votes = -1;

try
{
URLConnection con = new URL(voteSystem().topzoneServerLink()).openConnection();
con.addRequestProperty("User-Agent", "Mozilla/5.0");

            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

String line;
StringBuilder builder = new StringBuilder();
if ((line = br.readLine()) != null)
{
if (line.contains("fa fa-fw fa-lg fa-thumbs-up"))
{
String inputLine = line.split("fa fa-fw fa-lg fa-thumbs-up")[1].replace("\"></i>", "");
for (int i = 0; i < inputLine.length(); i++) {
char c = inputLine.charAt(i);
if (c == '<') {
break;
}
builder.append(c);
}
votes = Integer.parseInt(builder.toString());
}
}
br.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error while getting server vote count from " + getSiteName() + ".");
}
return votes;
}

public String getSiteName()
{
return "[Topzone]";
}
}
VoteSystem.java
-------------------------------------------------------------------------------------------------
package com.l2jserver.gameserver.votereward;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import static com.l2jserver.gameserver.config.Configuration.voteSystem;

import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.util.Broadcast;

/**
 * Vote System.
 * @author CIAurelius
 */

public abstract class VoteSystem implements Runnable
{
private static List<VoteSystem> voteSystems = new ArrayList<>();
private static Logger _log = Logger.getLogger(VoteSystem.class.getName());
protected int votesDiff;
protected boolean allowReport;
protected int boxes;
protected Map<Integer, Integer> rewards;
protected int checkMins;
protected int lastVotes = 0;
private final Map<String, Integer> playerIps = new HashMap<>();

public static void initializeHopzone()
{
if (voteSystem().hopzoneVoteReward())
{
_log.info("Hopzone: Loaded.");
voteSystems.add(new Hopzone(voteSystem().hopzoneVotesDifference(), voteSystem().hopzoneGameServerReport(), voteSystem().hopzoneDualBoxesAllowed(), voteSystem().hopzoneReward(), voteSystem().hopzoneRewardCheckTime()));
}
}

public static void initializeTopzone()
{
if (voteSystem().topzoneVoteReward())
{
_log.info("Topzone: Loaded.");
voteSystems.add(new Topzone(voteSystem().topzoneVotesDifference(), voteSystem().topzoneGameServerReport(), voteSystem().topzoneDualBoxesAllowed(), voteSystem().topzoneReward(), voteSystem().topzoneRewardCheckTime()));
}
}

public static VoteSystem getVoteSystem(String name)
{
for (VoteSystem vs : voteSystems)
{
if (vs.getSiteName().equals(name))
{
return vs;
}
}
return null;
}

public VoteSystem(int votesDiff, boolean allowReport, int boxes, Map<Integer, Integer> rewards, int checkMins)
{
this.votesDiff = votesDiff;
this.allowReport = allowReport;
this.boxes = boxes;
this.rewards = rewards;
this.checkMins = checkMins;

ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(this, checkMins * 1000 * 60, checkMins * 1000 * 60);
}

protected void reward()
{
int currentVotes = getVotes();
if (currentVotes == -1)
{
System.out.println("There was a problem on getting server votes.");
return;
}
if (this.lastVotes == 0)
{
this.lastVotes = currentVotes;
announce(getSiteName() + ": Current vote count is " + currentVotes + ".");
announce(getSiteName() + ": We need " + ((this.lastVotes + this.votesDiff) - currentVotes) + " vote(s) for reward.");
if (this.allowReport)
{
System.out.println("Server votes on " + getSiteName() + ": " + currentVotes);
System.out.println("Votes needed for reward: " + ((this.lastVotes + this.votesDiff) - currentVotes));
}
return;
}
if (currentVotes >= (this.lastVotes + this.votesDiff))
{
Collection<L2PcInstance> pls = L2World.getInstance().getPlayers();
if (this.allowReport)
{
System.out.println("Server votes on " + getSiteName() + ": " + currentVotes);
System.out.println("Votes needed for next reward: " + ((currentVotes + this.votesDiff) - currentVotes));
}
announce(getSiteName() + ": Everyone has been rewarded.");
announce(getSiteName() + ": Current vote count is " + currentVotes + ".");
announce(getSiteName() + ": We need " + this.votesDiff + " vote(s) for next reward.");
for (L2PcInstance p : pls)
{
if ((p.getClient() != null) && (!p.getClient().isDetached()))
{
boolean canReward = false;
String pIp = p.getClient().getConnection().getInetAddress().getHostAddress();
int count;
if (this.playerIps.containsKey(pIp))
{
count = this.playerIps.get(pIp).intValue();
if (count < this.boxes)
{
this.playerIps.remove(pIp);
this.playerIps.put(pIp, Integer.valueOf(count + 1));
canReward = true;
}
}
else
{
canReward = true;
this.playerIps.put(pIp, Integer.valueOf(1));
}
if (canReward)
{
for (int i : rewards.keySet())
{
p.addItem("Vote reward.", i, this.rewards.get(Integer.valueOf(i)).intValue(), p, true);
}
}
else
{
p.sendMessage("Already " + this.boxes + " character(s) of your ip have been rewarded, so this character won't be rewarded.");
}
}
}
this.playerIps.clear();

this.lastVotes = currentVotes;
}
else
{
if (this.allowReport)
{
System.out.println("Server votes on " + getSiteName() + ": " + currentVotes);
System.out.println("Votes needed for next reward: " + ((this.lastVotes + this.votesDiff) - currentVotes));
}
announce(getSiteName() + ": Current vote count is " + currentVotes + ".");
announce(getSiteName() + ": We need " + ((this.lastVotes + this.votesDiff) - currentVotes) + " vote(s) for reward.");
}
}

private static void announce(String msg)
{
CreatureSay cs = new CreatureSay(0, 18, "", msg);
Broadcast.toAllOnlinePlayers(cs);
}

public abstract int getVotes();

public abstract String getSiteName();
}

Configuration.java
-------------------------------------------------------------------------------------------------
private static final VoteSystemConfiguration voteSystem = ConfigFactory.create(VoteSystemConfiguration.class);

public static VoteSystemConfiguration voteSystem() {
return voteSystem;
}

VoteSystemConfiguration.java
-------------------------------------------------------------------------------------------------
package com.l2jserver.gameserver.config;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.aeonbits.owner.Config.HotReloadType.ASYNC;
import static org.aeonbits.owner.Config.LoadType.MERGE;

import java.util.Map;

import org.aeonbits.owner.Reloadable;
import org.aeonbits.owner.Config.HotReload;
import org.aeonbits.owner.Config.LoadPolicy;
import org.aeonbits.owner.Config.Sources;

import com.l2jserver.gameserver.config.converter.MapIntegerIntegerConverter;

/**
 * Vote System Configuration.
 * @author CIAurelius
 */
@Sources({
"file:${L2J_HOME}/custom/game/config/votesystem.properties",
"file:./config/votesystem.properties",
"classpath:config/votesystem.properties"
})
@LoadPolicy(MERGE)
@HotReload(value = 5, unit = MINUTES, type = ASYNC)
public interface VoteSystemConfiguration extends Reloadable
{
@Key("HopzoneVotesDifference")
Integer hopzoneVotesDifference();

@Key("TopzoneVotesDifference")
Integer topzoneVotesDifference();

@Key("HopzoneRewardCheckTime")
Integer hopzoneRewardCheckTime();

@Key("TopzoneRewardCheckTime")
Integer topzoneRewardCheckTime();

@Key("HopzoneDualboxesAllowed")
Integer hopzoneDualBoxesAllowed();

@Key("TopzoneDualboxesAllowed")
Integer topzoneDualBoxesAllowed();

@Key("HopzoneReward")
@ConverterClass(MapIntegerIntegerConverter.class)
Map<Integer, Integer> hopzoneReward();

@Key("TopzoneReward")
@ConverterClass(MapIntegerIntegerConverter.class)
Map<Integer, Integer> topzoneReward();

@Key("HopzoneServerLink")
String hopzoneServerLink();

@Key("AllowHopzoneVoteReward")
Boolean hopzoneVoteReward();

@Key("TopzoneServerLink")
String topzoneServerLink();

@Key("AllowTopzoneVoteReward")
Boolean topzoneVoteReward();

@Key("AllowHopzoneGameServerReport")
Boolean hopzoneGameServerReport();

@Key("AllowTopzoneGameServerReport")
Boolean topzoneGameServerReport();
}

GameServer.java
-------------------------------------------------------------------------------------------------
import static com.l2jserver.gameserver.config.Configuration.voteSystem;

printSection("Customs");
if ((voteSystem().hopzoneVoteReward()) && (voteSystem().topzoneVoteReward())){
VoteSystem.initializeHopzone();
VoteSystem.initializeTopzone();
}
else if (voteSystem().hopzoneVoteReward()){
VoteSystem.initializeHopzone();
}
else if (voteSystem().topzoneVoteReward()){
VoteSystem.initializeTopzone();
}

VoteReward.properties
-------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# L2JVoteReward - non-retail-like systems that have been integrated into the L2J project.
# Be warned that there may be no support for these custom beyond the original author's assistance.

# ---------------------------------------------------------------------------
# Vote Reward System
# ---------------------------------------------------------------------------

# Vote reward for Hopzone.
AllowHopzoneVoteReward = True
# e.g. VoteHtmlPatch = https://l2.hopzone.net/lineage2/details/93067/L2-DAMAGE.html
# Vote reward server link. (IMPORTANT!!!! Always add .html at the end of the link)
HopzoneServerLink = https://l2.hopzone.net/lineage2/details/93067/L2-DAMAGE.html
# Votes for next reward needed.
HopzoneVotesDifference = 10
# Minutes between rewards.
# Eg. You put 5 it checks every 5 minutes for reward.
HopzoneRewardCheckTime = 5
# Reward(s).
HopzoneReward = 3481,5;
# Topzone reward max dual boxes reward.
# For example if you put 2 and someone has 3 boxes open 2 will be rewarded.
HopzoneDualboxesAllowed = 1
# Game server console report.
# If set to true, game server console will get a report of
# current vote count, votes needed for next reward and votes needed for first page.
AllowHopzoneGameServerReport = True

# Vote reward for Topzone.
AllowTopzoneVoteReward = True
# e.g. VoteHtmlPatch = http://l2topzone.com/tv.php?id=6084.html
# Vote reward server link. (IMPORTANT!!!! Always add .html at the end of the link)
TopzoneServerLink = http://l2topzone.com/tv.php?id=6084.html
# Votes for next reward needed.
TopzoneVotesDifference = 10
# Minutes between rewards.
# Eg. You put 7 it checks every 7 minutes for reward.
TopzoneRewardCheckTime = 7
# Reward(s).
TopzoneReward = 3481,5;
# Topzone reward max dual boxes reward.
# For example if you put 2 and someone has 3 boxes open 2 will be rewarded.
TopzoneDualboxesAllowed = 1
# Game server console report.
# If set to true, game server console will get a report of
# current vote count, votes needed for next reward and votes needed for first page.
AllowTopzoneGameServerReport = True

gameserver.java

+ VoteSystem.initializeHopzone();
+ VoteSystem.initializeTopzone();