Initializing Motivote (ALL BASES!!!)
Motivote will require your server's API key in order to function, this can be obtained in the server control panel.
You can start by initializing the AuthService by supplying Motivote with your API key in the main(String[]) method of your application.
Code:
public static void main(String[] args) throws Exception {
AuthService.setProvider(new Motivote("API_KEY_HERE"));
// other important code below.
}
Note: you only have to add AuthService.setProvider(new Motivote("API_KEY_HERE")) at the top of your EXISTING main method!
Creating an Auth or Redeem Command
Now, in order for users to redeem the auth codes they voted for, you will have to create some sort of command for them to enter their auth code and then check it with Motivote's database to see if it is valid. Let's do that now.
Project Insanity Bases
In order to add a command to a Project Insanity base, you must first open
Code:
./src/server/model/players/packets/Commands.java
"." refers to the location in which your source's "src" directory (or similar) is located.
Search for your playerCommands method [void playerCommands(Client c, String playerCommand)]. Below is an example of a redeem checker.
Code:
if (playerCommand.startswith("redeem")) {
String auth = playerCommand.replace("redeem ", "");
boolean success = AuthService.provider().redeemNow(auth);
if (success) {
c.getItems().addItem(995, 10000);
c.sendMessage("Auth redeemed, thanks for voting!");
}
else {
c.sendMessage("Invalid auth supplied, please try again later.");
}
}
YOU'RE DONE! THAT'S IT!
Ruse Bases
Somewhere in your source, there should be CommandPacketListener.java, inside it find the playerCommands method. At the bottom, put the following code:
Code:
if (command[0].equalsIgnoreCase("redeem")) {
String auth = command[1];
boolean success = AuthService.provider().redeemNow(auth);
Item item = new Item(995, 100000);
if (success) {
player.getInventory().add(item, true);
player.getPacketSender().sendMessage("Auth redeemed, thanks for voting!");
}
else {
player.getPacketSender().sendMessage("Invalid auth supplied, please try again later.");
}
}
YOU'RE DONE! THAT'S IT!
Spoiler for Extra information:
Non-blocking API Calls
Motivote supports Future<?>s if you want to store it and check progress.
An example for experienced developers below utilizing an ExecutorService. Note that implementations can vary widely which is why I do not supply a specific example for any server.
Code:
// somewhere in your class constructor or something
ExecutorService svc = Executors.newCachedThreadPool();
// somewhere in your command handler
// assumed variables: final String auth, final Player player
svc.execute(new Runnable() {
@Override
public void run() {
if (success) {
player.give(995, 100000);
player.message("Thanks for voting!");
}
else {
player.message("Invalid auth provided. Try again later.");
}
}
});
Info API Calls
Motivote also allows you to gather information about a specific auth code, so you can do some extra checks or store info in your database
Code:
Vote vote = AuthService.provider().infoNow("AUTH_CODE_HERE");
System.out.println("Auth: " + vote.address());
System.out.println("Callback Date: " + vote.callbackDate()); // as a UNIX timestamp
System.out.println("IP: " + vote.address()); // voter's IP address as reported by Motivote
System.out.println("Fulfilled: " + vote.fulfilled()); // reward has been given for auth
System.out.println("Ready: " + vote.ready()); // vote has been verified, auth has been generated, ready to be fulfilled if not already
System.out.println("Redeemed: " + vote.redeemed()); // vote has been redeemed in this API call, can return no if vote has already been redeemed.
Non-singleton pattern
I have included a simple singleton pattern to make it easier for newcomers to initialize and access the AuthService. I strongly advise against that (singleton) approach since it is a classic case of bad design.
Code:
AuthService authSvc = new Motivote("API_KEY_HERE");
System.out.println(authSvc.redeemNow("AUTH_CODE_HERE"));
Last edited by funkE; 12-06-2015 at 04:09 PM.
Reason: update and title stuff
Could you explain to someone like me, who has no idea what is it but looks sort of intresting... what is this supposed to do?
It's basically an easy to use vote page. Every vote generates an auth code people can use to redeem a reward on your server. It handles everything for you. This way, you don't have to manage any code and you can simply embed the motivote vote page in your own website to provide a seamless experience