RuneHive-Game
Loading...
Searching...
No Matches
VoteService.java
Go to the documentation of this file.
1package com.runehive.game.service;
2
3import com.runehive.content.achievement.AchievementHandler;
4import com.runehive.content.achievement.AchievementKey;
5import com.runehive.game.world.World;
6import com.runehive.game.world.entity.mob.player.Player;
7import com.runehive.game.world.items.Item;
8import org.apache.logging.log4j.LogManager;
9import org.apache.logging.log4j.Logger;
10
11import java.sql.*;
12import java.util.concurrent.TimeUnit;
13
14public final class VoteService {
15
16 private static final Logger logger = LogManager.getLogger(VoteService.class);
17 private static final String CONNECTION_STRING = "jdbc:mysql://173.82.152.23:3306/osroyjs_vote";
18 private static final String USER = "osroyjs_exo1";
19 private static final String PASS = "3AXbU=W7IfzX";
20 private static final Item REWARD = new Item(7478, 1);
21
22 public static void claimReward(Player player) {
23 if (!player.databaseRequest.elapsed(1, TimeUnit.MINUTES)) {
24 player.dialogueFactory.sendStatement("You can only check our database once every 1 minute!").execute();
25 return;
26 }
27
28 if (!player.inventory.hasCapacityFor(REWARD)) {
29 player.dialogueFactory.sendStatement("Please clear up some inventory spaces before doing this!").execute();
30 return;
31 }
32
33 boolean found = false;
34 player.dialogueFactory.sendStatement("Checking request...").execute();
35 player.databaseRequest.reset();
36
37 try (Connection connection = DriverManager.getConnection(CONNECTION_STRING, USER, PASS);
38 PreparedStatement sta = connection.prepareStatement("SELECT * FROM fx_votes WHERE username = ? AND claimed=0 AND callback_date IS NOT NULL", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
39
40 sta.setString(1, player.getName().replace(" ", "_"));
41
42 try (ResultSet rs = sta.executeQuery()) {
43 while (rs.next()) {
44 String timestamp = rs.getTimestamp("callback_date").toString();
45 String ipAddress = rs.getString("ip_address");
46 int siteId = rs.getInt("site_id");
47
48 found = true;
49 player.inventory.add(REWARD);
50 player.totalVotes += 1;
51 logger.info(String.format("[Vote] Vote claimed by %s. (sid: %d, ip: %s, time: %s)", player.getName(), siteId, ipAddress, timestamp));
52 rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
53 rs.updateRow();
54 }
55 }
56
57 if (!found) {
58 player.dialogueFactory.sendStatement("There is no vote reward for you to claim!").execute();
59 } else {
61 player.dialogueFactory.sendStatement("Thank-you for your support, " + player.getName() + "!").execute();
62 World.sendMessage("<col=CF2192>osroyale: <col=" + player.right.getColor() + ">" + player.getName() + " </col>has just voted! They have voted a total of <col=CF2192>" + player.totalVotes + " </col>times.");
63 }
64 } catch (SQLException ex) {
65 logger.error(String.format("Error claiming vote for player=%s", player.getName()), ex);
66 }
67 }
68
69}
static void activate(Player player, AchievementKey achievement)
Activates the achievement for the individual player.
final DialogueFactory execute()
Retrieves the next dialogue in the chain and executes it.
final DialogueFactory sendStatement(String... lines)
Appends a StatementDialogue to the current dialogue chain.
static void claimReward(Player player)
Represents the game world.
Definition World.java:46
static void sendMessage(String... messages)
Sends a global message.
Definition World.java:396
This class represents a character controlled by a player.
Definition Player.java:125
String getName()
Gets the name of this entity.
Definition Player.java:774
The container class that represents an item that can be interacted with.
Definition Item.java:21
boolean add(Item item)
Attempts to deposit item into this container.
final boolean hasCapacityFor(Item... item)
Determines if this container has the capacity for item.
boolean elapsed(long time, TimeUnit unit)