· 5 years ago · Oct 08, 2020, 09:20 AM
1/*
2 * Copyright (C) 2014-2015 Vote Rewarding System
3 *
4 * This file is part of Vote Rewarding System.
5 *
6 * Vote Rewarding System is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Vote Rewarding System is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19package fandc.votingengine;
20
21import java.io.BufferedReader;
22import java.io.InputStreamReader;
23import java.net.HttpURLConnection;
24import java.net.URL;
25
26import l2f.gameserver.Config;
27import l2f.gameserver.model.Player;
28import l2f.gameserver.utils.ItemFunctions;
29
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import fandc.votingengine.VotingSettings.MessageType;
34
35/**
36 * @author UnAfraid
37 */
38public class VotingRewardTask
39{
40 private static final Logger _log = LoggerFactory.getLogger(VotingRewardTask.class);
41 // Constants
42 private static final String TOPZONE_API_URL = "https://api.l2topzone.com/v1/vote?token=%s&ip=%s";
43 private static final String HOPZONE_API_URL = "https://api.hopzone.net/lineage2/vote?token=%s&ip_address=%s";
44
45 public static void checkReward(Player player)
46 {
47 final long timeRemaining = VotingRewardCache.getInstance().getLastVotedTime(player);
48 // Check if player voted
49 if (timeRemaining <= 0)
50 {
51 for (String zone : VotingSettings.getInstance().getZones().keySet())
52 {
53 if (!isVotter(zone, player.getIP()))
54 {
55 String msg = VotingSettings.getInstance().getMessage(MessageType.ON_NOT_VOTED);
56 if (msg != null)
57 {
58 msg = msg.replaceAll("%zoneName%", zone);
59 player.sendMessage(msg);
60 }
61 return;
62 }
63 }
64 // Give him reward
65 giveReward(player);
66
67 // Mark down this reward as given
68 VotingRewardCache.getInstance().markAsVotted(player);
69
70 if (Config.ENABLE_PLAYER_COUNTERS)
71 player.getCounters().timesVoted++;
72
73 // Send message to player
74 final String msg = VotingSettings.getInstance().getMessage(MessageType.ON_SUCCESS);
75 if (msg != null)
76 player.sendMessage(msg);
77 }
78 else
79 VotingRewardAPI.sendReEnterMessage(timeRemaining, player);
80 }
81
82 private static void giveReward(Player activeChar)
83 {
84 if (activeChar == null)
85 return;
86 for (RewardItem item : VotingSettings.getInstance().getDroplist().calculateDrops())
87 ItemFunctions.addItem(activeChar, item.getId(), item.getCount(), true, "VotingReward");
88 if (VotingSettings.getInstance().getColor() != null)
89 activeChar.setNameColor(Integer.decode("0x" + VotingSettings.getInstance().getColor()));
90 }
91
92 private static final boolean isVotter(String zoneName, String ip)
93 {
94 try
95 {
96 String url = null;
97 String urlAgent = null;
98 final String apiKey = VotingSettings.getInstance().getAPIKey(zoneName);
99 //final int serverId = VotingSettings.getInstance().getServerId(zoneName);
100 // api key is not set return true to skip check.
101 if (apiKey == null || apiKey.isEmpty())
102 return true;
103
104 if (zoneName.equals("Topzone"))
105 {
106 url = String.format(TOPZONE_API_URL, apiKey, ip);
107 urlAgent = "L2TopZone";
108 }
109 else if (zoneName.equals("Hopzone"))
110 {
111 url = String.format(HOPZONE_API_URL, apiKey, ip);
112 urlAgent = "L2HopZone";
113 }
114
115 final URL obj = new URL(url);
116 final HttpURLConnection con = (HttpURLConnection) obj.openConnection();
117 // add request header
118 con.setRequestProperty("User-Agent", urlAgent);
119 con.setConnectTimeout(5 * 1000);
120 final int responseCode = con.getResponseCode();
121 if (responseCode == 200) // OK
122 {
123 final StringBuilder sb = new StringBuilder();
124 try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())))
125 {
126 String inputLine;
127 while ((inputLine = in.readLine()) != null)
128 sb.append(inputLine);
129 }
130 if (zoneName.equals("Topzone")){
131 System.out.println(parseTopzone(sb));
132 return parseTopzone(sb);}
133 else if (zoneName.equals("Hopzone"))
134 return parseHopzone(sb);
135 }
136 }
137 catch (Exception e)
138 {
139 _log.error("Failed to establish connection with voting provider", e);
140 }
141 return false;
142 }
143
144 private static boolean parseHopzone(StringBuilder sb)
145 {
146 for (String s : sb.toString().split(","))
147 {
148 if (s == null)
149 continue;
150 if (s.contains("voted"))
151 return s.split(":")[1].equals("true");
152 }
153 return false;
154 }
155
156 private static boolean parseTopzone(StringBuilder sb)
157 {
158 for (String s : sb.toString().split("(,)|\\{"))
159 {
160 if (s == null)
161 continue;
162
163 if (s.contains("isVoted"))
164 return s.split(":")[1].equals("true");
165 }
166 return false;
167 }
168}