Skip to content

Commit

Permalink
Merge pull request #10 from iconbet/fix-daolette-game
Browse files Browse the repository at this point in the history
added fixes for #9
  • Loading branch information
iconbetdev1 authored Jan 17, 2022
2 parents d5373d9 + 61d700e commit e679458
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void game_on() {
if (!sender.equals(owner)) {
Context.revert("Only the owner can call the game_on method");
}
if (!this._game_on.get() &&
if (this._game_on.get() == null || !this._game_on.get() &&
this._roulette_score.get() != null) {
this._game_on.set(Boolean.TRUE);
}
Expand Down
30 changes: 20 additions & 10 deletions daolette-game/build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
version = '0.1.0'

dependencies {
compileOnly 'foundation.icon:javaee-api:0.9.0'
compileOnly 'foundation.icon:javaee-api:0.9.1'
compileOnly 'foundation.icon:javaee-scorex:0.5.2'

testImplementation 'foundation.icon:javaee-unittest:0.9.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation project(':daolette')
testImplementation project(':dividend-distribution-score')
testImplementation project(':game-authorization-score')
testImplementation project(':reward-distribution')
testImplementation project(':tap-token')
testImplementation project(':testinteg')
testImplementation 'foundation.icon:icon-sdk:2.0.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'

}

Expand All @@ -26,12 +26,22 @@ optimizedJar {
}

deployJar {
endpoints {
local {
uri = 'http://localhost:9082/api/v3'
nid = 0x3
}
}
endpoints {
sejong {
uri = 'https://sejong.net.solidwallet.io/api/v3/'
nid = 0x53
to = "cx9a046d3811ac426bb6ea6c2c988af16afa487555"
}
local {
uri = 'http://localhost:9082/api/v3'
nid = 0x3
}
mainnet {
uri = 'https://ctz.solidwallet.io/api/v3'
nid = 0x1
to = "cx38fd2687b202caf4bd1bda55223578f39dbb6561"
}
}
keystore = rootProject.hasProperty('keystoreName') ? "$keystoreName" : ''
password = rootProject.hasProperty('keystorePass') ? "$keystorePass" : ''
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;

import score.Address;
import score.Context;
Expand All @@ -16,7 +17,7 @@ public class DaoletteGame {

protected static final Address ZERO_ADDRESS = new Address(new byte[Address.LENGTH]);

public static final String TAG = "DAOLETTE";
public static final String TAG = "Mini Roulette";

private static final int[] BET_LIMIT_RATIOS = new int[] {147, 2675, 4315, 2725, 1930, 1454, 1136, 908, 738, 606,
500, 413, 341, 280, 227, 182, 142, 107, 76, 48, 23};
Expand All @@ -30,31 +31,75 @@ public class DaoletteGame {

private static final List<Integer> WHEEL_BLACK = List.of(2,3,6,7,10,11,14,15,18,19);

private static final List<Integer> SET_BLACK = List.of(2, 3, 6, 7, 10, 11, 14, 15, 18, 19);
private static final Set<Integer> SET_BLACK = Map.of(
2, 2,
3, 3,
6, 6,
7, 7,
10, 10,
11, 11,
14, 14,
15, 15,
18, 18,
19, 19)
.keySet();

private static final List<Integer> WHEEL_RED = List.of(1,4,5,8,9,12,13,16,17,20);

private static final List<Integer> SET_RED = List.of( 1, 4, 5, 8, 9, 12, 13, 16, 17, 20);
private static final Set<Integer> SET_RED = Map.of(
1, 1,
4, 4,
5, 5,
8, 8,
9, 9,
12, 12,
13, 13,
16, 16,
17, 17,
20, 20)
.keySet();

private static final List<Integer> WHEEL_ODD = List.of(1,3,5,7,9,11,13,15,17,19);

private static final List<Integer> SET_ODD = List.of( 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
private static final Set<Integer> SET_ODD = Map.of(
1, 1,
3, 3,
5, 5,
7, 7,
9, 9,
11, 11,
13, 13,
15, 15,
17, 17,
19, 19)
.keySet();

private static final List<Integer> WHEEL_EVEN = List.of(2,4,6,8,10,12,14,16,18,20);

private static final List<Integer> SET_EVEN = List.of( 2, 4, 6, 8, 10, 12, 14, 16, 18, 20);
private static final Set<Integer> SET_EVEN = Map.of(
2, 2,
4, 4,
6, 6,
8, 8,
10, 10,
12, 12,
14, 14,
16, 16,
18, 18,
20, 20)
.keySet();

private static final Map<String, Number> MULTIPLIERS = Map.of(
"bet_on_color", 2,
"bet_on_even_odd", 2,
"bet_on_number", 20,
"number_factor", 20.685f);

private String _GAME_ON = "game_on";
private String _TREASURY_SCORE="treasury_score";
private static final String _GAME_ON = "game_on";
private static final String _TREASURY_SCORE="treasury_score";

private VarDB<Boolean> _game_on = Context.newVarDB(this._GAME_ON, Boolean.class);
private VarDB<Address> _treasury_score = Context.newVarDB(this._TREASURY_SCORE, Address.class);
private final VarDB<Boolean> _game_on = Context.newVarDB(_GAME_ON, Boolean.class);
private final VarDB<Address> _treasury_score = Context.newVarDB(_TREASURY_SCORE, Address.class);

public DaoletteGame(@Optional boolean _on_update_var) {
if(_on_update_var) {
Expand All @@ -64,7 +109,7 @@ public DaoletteGame(@Optional boolean _on_update_var) {
}
Context.println("In __init__."+ TAG);
Context.println("owner is "+ Context.getOwner() + ". "+ TAG);
this._game_on.set(false);
// this._game_on.set(false);

}

Expand Down Expand Up @@ -193,11 +238,13 @@ public void bet_on_numbers(String numbers, @Optional String user_seed) {
}

String[] array = StringUtils.split(numbers, ',');
List<Integer> numList = List.of(mapToInt(array));
Integer[] numArray = mapToInt(array);
List<Integer> numList = List.of(numArray);
Set<Integer> numSet = fromArray(numArray);

if (numList.equals(SET_RED) || numList.equals(SET_BLACK)) {
if (numSet.equals(SET_RED) || numSet.equals(SET_BLACK)) {
this.__bet(numList, user_seed, BET_TYPES[2]);
}else if (numList.equals(SET_ODD) || numList.equals(SET_EVEN)) {
}else if (numSet.equals(SET_ODD) || numSet.equals(SET_EVEN)) {
this.__bet(numList, user_seed, BET_TYPES[3]);
}else {
this.__bet(numList, user_seed, BET_TYPES[1]);
Expand Down Expand Up @@ -254,18 +301,6 @@ public void bet_on_even_odd(boolean even_odd, @Optional String user_seed) {
this.__bet(numbers, user_seed, BET_TYPES[3]);
}

/*
A function to redefine the value of self.owner once it is possible.
To be included through an update if it is added to IconService.
Sets the value of self.owner to the score holding the game treasury.
*/
@External
public void untether() {
if ( !Context.getCaller().equals(Context.getOwner())){
Context.revert("Only the owner can call the untether method.");
}
}

/*
Generates a random # from tx hash, block timestamp and user provided
seed. The block timestamp provides the source of unpredictability.
Expand All @@ -280,17 +315,11 @@ public double get_random(String userSeed) {
userSeed = "";
}

Context.println("Entered get_random. "+ TAG);
if ( Context.getCaller().isContract() ) {
Context.revert("ICONbet: SCORE cant play games");
}
double spin;
String seed = encodeHexString(Context.getTransactionHash()) + String.valueOf(Context.getBlockTimestamp()) + userSeed;
//TODO: we can not do this in java, there is no way to access to the memory address from the icon-jvm-jdk.
//validate if the result is same as python
//( ByteBuffer.wrap(Context.hash("sha3-256", seed.getBytes())).order(ByteOrder.BIG_ENDIAN).getInt() % 100000) / 100000.0;
spin = fromByteArray( Context.hash("sha3-256", seed.getBytes())) % 100000 / 100000.0;
Context.println("Result of the spin was "+ spin + " "+ TAG);
byte[] seedHash = Context.hash("sha3-256", seed.getBytes());
BigInteger seedint = new BigInteger(1, seedHash).mod(BigInteger.valueOf(100000));
double spin = seedint.longValue() / 100000.0;
Context.println("Result of the spin was " + spin + " " + TAG);
return spin;
}

Expand All @@ -305,6 +334,10 @@ public double get_random(String userSeed) {
@SuppressWarnings("rawtypes")
public void __bet(List<Integer> numbers, String user_seed, String bet_type) {

if ( Context.getCaller().isContract() ) {
Context.revert("ICONbet: SCORE cant play games");
}

this.BetSource(Context.getOrigin(), BigInteger.valueOf(Context.getTransactionTimestamp()));

String numberStr = listToListString(numbers);
Expand Down Expand Up @@ -358,7 +391,7 @@ public void __bet(List<Integer> numbers, String user_seed, String bet_type) {

BigInteger payout;
if (bet_type.equals(BET_TYPES[1])){
payout = BigInteger.valueOf( (int)(MULTIPLIERS.get(BET_TYPES[5]).floatValue() * 1000) ).multiply(amount).divide(BigInteger.valueOf(1000l * numbers.size()));
payout = BigInteger.valueOf( (int)(MULTIPLIERS.get(BET_TYPES[5]).floatValue() * 1000) ).multiply(amount).divide(BigInteger.valueOf(1000L * numbers.size()));
}else {
payout = BigInteger.valueOf( MULTIPLIERS.get(bet_type).longValue()).multiply(amount);
}
Expand Down Expand Up @@ -408,9 +441,9 @@ public <K,V> String mapToJsonString(Map<K, V > map) {
StringBuilder sb = new StringBuilder("{");
for (Map.Entry<K, V> entry : map.entrySet()) {
if(entry.getValue() instanceof Number) {
sb.append("\""+entry.getKey()+"\":"+entry.getValue()+",");
sb.append("\"").append(entry.getKey()).append("\":").append(entry.getValue()).append(",");
}else {
sb.append("\""+entry.getKey()+"\":\""+entry.getValue()+"\",");
sb.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\",");
}
}
char c = sb.charAt(sb.length()-1);
Expand Down Expand Up @@ -446,15 +479,12 @@ public String listToListString(List<Integer> list) {

}

int fromByteArray(byte[] bytes) {
int order = ((bytes[0] & 0xFF) << 24) |
((bytes[1] & 0xFF) << 16) |
((bytes[2] & 0xFF) << 8 ) |
((bytes[3] & 0xFF) << 0 );
//TODO: this cand be negative, why???
if(order < 0) {
return order * -1;
}
return order;
@SuppressWarnings("unchecked")
<T> Set<T> fromArray(T[] array){
Map.Entry<T, T>[] entries = new Map.Entry[array.length];
for(int i=0; i< array.length; i++) {
entries[i] = Map.entry(array[i], array[i]);
}
return Map.ofEntries(entries).keySet();
}
}

0 comments on commit e679458

Please sign in to comment.