For those who may be interested here is the source code of my Slot Machine:
// (c) 2013 Come-from-Beyond
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
public final class Tri {
public static void main(String[] args) {
while (true) {
System.out.println("Time = " + System.currentTimeMillis());
try {
Thread.sleep(120000);
} catch (final Exception e) {
return;
}
try {
final ArrayList bets = new ArrayList<>();
final HashSet payments = new HashSet<>();
String block = "2680262203532249785"; // This is the genesis block id
while (true) {
String blockData = sendRequest("getBlock&block=" + block);
int i = blockData.indexOf("\"transactions\":[");
String transactions = blockData.substring(i + 16, blockData.indexOf("]", i));
while (transactions.length() > 0) {
final String transaction = transactions.substring(1, transactions.indexOf("\"", 1));
final String transactionData = sendRequest("getTransaction&transaction=" + transaction);
// Beginning of the game logic
i = transactionData.indexOf("\"sender\":\"");
final String sender = transactionData.substring(i + 10, transactionData.indexOf("\"", i + 10));
i = transactionData.indexOf("\"recipient\":\"");
final String recipient = transactionData.substring(i + 13, transactionData.indexOf("\"", i + 13));
if (recipient.equals("4427320429393039971")) {
i = transactionData.indexOf("\"amount\":");
final int amount = Integer.parseInt(transactionData.substring(i + 9, transactionData.indexOf(",", i + 9)));
if (amount >= 10 && amount <= 1000) {
bets.add(new Bet(block, transaction, amount, sender));
}
} else if (sender.equals("3994117300242888518")) {
i = transactionData.indexOf("\"referencedTransaction\":\"");
final String referencedTransaction = transactionData.substring(i + 25, transactionData.indexOf("\"", i + 25));
payments.add(referencedTransaction);
}
// End of the game logic
if (transaction.length() + 3 > transactions.length()) {
break;
}
transactions = transactions.substring(transaction.length() + 3);
}
i = blockData.indexOf("\"nextBlock\":\"");
if (i < 0) {
break;
}
block = blockData.substring(i + 13, blockData.indexOf("\"", i + 13));
}
// Beginning of the game logic
for (final Bet bet : bets) {
if (!payments.contains(bet.transaction)) {
final String request = "sendMoney&secretPhrase=REMOVED&recipient=" + bet.sender + "&amount=" + getAmount(bet) + "&fee=1&deadline=1&referencedTransaction=" + bet.transaction;
System.out.println(request);
System.out.println(sendRequest(request));
}
}
// End of the game logic
} catch (final Exception e) {
System.out.println(e.toString());
}
}
}
public static int getAmount(final Bet bet) {
final String digits = bet.block.substring(bet.block.length() - 3);
final int digit0 = Integer.parseInt("" + digits.charAt(0));
final int digit1 = Integer.parseInt("" + digits.charAt(1));
final int digit2 = Integer.parseInt("" + digits.charAt(2));
if (digit0 == digit1 && digit0 == digit2) {
return bet.amount * 10;
}
if (digit0 == digit1 || digit0 == digit2 || digit1 == digit2) {
return bet.amount * 3;
}
return 1;
}
public static String sendRequest(final String request) throws Exception {
final HttpURLConnection connection = (HttpURLConnection)(new URL("http://localhost:7876/?request=" + request)).openConnection();
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final String response = reader.readLine();
reader.close();
connection.disconnect();
return response;
}
public static final class Bet {
final String block;
final String transaction;
final int amount;
final String sender;
public Bet(final String block, final String transaction, final int amount, final String sender) {
this.block = block;
this.transaction = transaction;
this.amount = amount;
this.sender = sender;
}
}
}
It uses only Nxt blockchain, no need to store anything locally.
Pay attention that it uses "referencedTransaction". If a betting transaction cancelled due to blockchain reorganization, then the transaction that pays winning will be cancelled as well.