106 lines
2.5 KiB
Java
106 lines
2.5 KiB
Java
package fr.ynerant.tarot;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.preference.PreferenceManager;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
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.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Executors;
|
|
|
|
public class Player {
|
|
@SuppressLint("UseSparseArrays")
|
|
private static final Map<Integer, Player> PLAYERS = new HashMap<>();
|
|
public static Player THIS_PLAYER;
|
|
|
|
private int id;
|
|
|
|
private String name;
|
|
|
|
private int score3, score4, score5, score6;
|
|
|
|
private int getId() {
|
|
return id;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public int getScore3() {
|
|
return score3;
|
|
}
|
|
|
|
public int getScore4() {
|
|
return score4;
|
|
}
|
|
|
|
public int getScore5() {
|
|
return score5;
|
|
}
|
|
|
|
public int getScore6() {
|
|
return score6;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
return obj instanceof Player && ((Player) obj).getId() == getId();
|
|
}
|
|
|
|
public static void updatePlayers() {
|
|
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
URL url = new URL("http://galaxyoyo.com/tarot/player.php");
|
|
final HttpURLConnection co = (HttpURLConnection) url.openConnection();
|
|
co.setRequestMethod("POST");
|
|
co.setRequestProperty("token", PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null));
|
|
co.connect();
|
|
|
|
final List<Player> players = new Gson().fromJson(new InputStreamReader(co.getInputStream()), new TypeToken<ArrayList<Player>>(){}.getType());
|
|
PLAYERS.clear();
|
|
for (Player p : players) {
|
|
PLAYERS.put(p.getId(), p);
|
|
}
|
|
|
|
THIS_PLAYER = PLAYERS.get(PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getInt("user_id", 0));
|
|
|
|
MainActivity.INSTANCE.runOnUiThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(MainActivity.INSTANCE, "Bienvenue " + THIS_PLAYER.getName() + " !", Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public static Collection<Player> getAllPlayers() {
|
|
return PLAYERS.values();
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public String toString() {
|
|
return name;
|
|
}
|
|
}
|