package fr.ynerant.tarot; import android.preference.PreferenceManager; import android.util.SparseArray; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.Executors; public class Game { private static final SparseArray GAMES = new SparseArray<>(); public int id; public GameType type; public Set players; public Bet bet; public Player attacker; public Player follower; public int attackScore; // public boolean little, twenty_one, excuse; public int ends; public int handle; public Map miseries; public boolean littleForAttacker, littleForDefenser; public boolean chelemAnnounced, chelemRealized; public int getId() { return id; } public GameType getType() { return type; } public Set getPlayers() { return players; } public Bet getBet() { return bet; } public Player getAttacker() { return attacker; } public Player getFollower() { return follower; } public int getAttackScore() { return attackScore; } public int getEnds() { return ends; } public int getHandle() { return handle; } public Map getMiseries() { return miseries; } public boolean isLittleForAttacker() { return littleForAttacker; } public boolean isLittleForDefenser() { return littleForDefenser; } public boolean isChelemAnnounced() { return chelemAnnounced; } public boolean isChelemRealized() { return chelemRealized; } public int calculateScore() { // int ends = (little ? 1 : 0) + (twenty_one ? 1 : 0) + (excuse ? 1 : 0); int bound = ends == 0 ? 56 : ends == 1 ? 51 : ends == 2 ? 41 : 36; boolean win = attackScore >= bound; int gain = Math.abs(attackScore - bound); int score = 25; score += gain; score *= bet.getMultiplier(); if (handle != 0) score += 10 * (Math.abs(handle) + 1); if (!win) score = -score; if (littleForAttacker) score += 10; else if (littleForDefenser) score -= 10; if (chelemAnnounced) { if (chelemRealized) score += 400; else score -= 200; } else if (chelemRealized) score += 200; return score; } public enum GameType { THREE_PLAYERS, FOUR_PLAYERS, FIVE_PLAYERS, SIX_PLAYERS; public int getNbPlayers() { return ordinal() + 3; } public static GameType getGameType(int players) { switch (players) { case 3: return THREE_PLAYERS; case 4: return FOUR_PLAYERS; case 5: return FIVE_PLAYERS; case 6: return SIX_PLAYERS; default: throw new IllegalArgumentException("A game must have between 3 and 6 players"); } } } public enum Bet { SMALL(1), GUARD(2), GUARD_WITHOUT(4), GUARD_AGAINST(6); private final int multiplier; Bet(int multiplier) { this.multiplier = multiplier; } public int getMultiplier() { return multiplier; } } public static void updateGames() { Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { try { URL url = new URL("http://galaxyoyo.com/tarot/game.php"); final HttpURLConnection co = (HttpURLConnection) url.openConnection(); co.setRequestMethod("POST"); co.setRequestProperty("token", PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null)); co.connect(); final List games = new Gson().fromJson(new InputStreamReader(co.getInputStream()), new TypeToken>(){}.getType()); GAMES.clear(); for (Game g : games) { GAMES.put(g.getId(), g); } } catch (IOException ex) { ex.printStackTrace(); } } }); } }