Statistiques personnelles (points)

This commit is contained in:
Yohann D'ANELLO 2019-12-03 15:24:31 +01:00
parent bace5d157c
commit c7691639e4
17 changed files with 477 additions and 78 deletions

View File

@ -15,7 +15,11 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -25,20 +29,25 @@ public class Game {
private static final Gson GSON;
private static final SparseArray<Game> GAMES = new SparseArray<>();
public int id;
public GameType gameType;
public List<Player> 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 List<Boolean> miseries;
public boolean littleForAttacker, littleForDefenser;
public boolean chelemAnnounced, chelemRealized;
public List<Integer> points;
private int id;
private Date date;
private GameType gameType;
private Player player1, player2, player3, player4, player5, player6;
private List<Player> players;
private int nb_players;
private Bet bet;
private Player attacker;
private Player follower;
private int attackScore;
// private boolean little, twenty_one, excuse;
private int ends;
private int handle;
private boolean misery1, misery2, misery3, misery4, misery5, misery6;
private List<Boolean> miseries;
private boolean littleForAttacker, littleForDefender;
private boolean chelemAnnounced, chelemRealized;
private int score1, score2, score3, score4, score5, score6;
private List<Integer> points;
static {
GSON = new GsonBuilder().registerTypeAdapter(Player.class, new TypeAdapter<Player>() {
@ -57,7 +66,12 @@ public class Game {
return null;
}
return Player.getPlayerById(in.nextInt());
int id = in.nextInt();
if (id == 0)
return null;
return Player.getPlayerById(id);
}
}).registerTypeAdapter(Game.GameType.class, new TypeAdapter<Game.GameType>() {
@Override
@ -77,6 +91,26 @@ public class Game {
return Game.GameType.values()[3 + in.nextInt()];
}
}).registerTypeAdapter(Date.class, new TypeAdapter<Date>() {
final DateFormat FORMAT = new SimpleDateFormat("yyyy-mm-dd HH:MM:ss");
@Override
public void write(JsonWriter out, Date value) throws IOException {
if (value == null)
out.nullValue();
else
out.value(FORMAT.format(value));
}
@Override
public Date read(JsonReader in) throws IOException {
try {
return FORMAT.parse(in.nextString());
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}).create();
}
@ -84,58 +118,122 @@ public class Game {
return id;
}
public Date getDate() {
return date;
}
public GameType getGameType() {
return gameType;
}
public void setGameType(GameType gameType) {
this.gameType = gameType;
}
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
public Bet getBet() {
return bet;
}
public void setBet(Bet bet) {
this.bet = bet;
}
public Player getAttacker() {
return attacker;
}
public void setAttacker(Player attacker) {
this.attacker = attacker;
}
public Player getFollower() {
return follower;
}
public void setFollower(Player follower) {
this.follower = follower;
}
public int getAttackScore() {
return attackScore;
}
public void setAttackScore(int attackScore) {
this.attackScore = attackScore;
}
public int getEnds() {
return ends;
}
public void setEnds(int ends) {
this.ends = ends;
}
public int getHandle() {
return handle;
}
public void setHandle(int handle) {
this.handle = handle;
}
public List<Boolean> getMiseries() {
return miseries;
}
public void setMiseries(List<Boolean> miseries) {
this.miseries = miseries;
}
public boolean isLittleForAttacker() {
return littleForAttacker;
}
public boolean isLittleForDefenser() {
return littleForDefenser;
public void setLittleForAttacker(boolean littleForAttacker) {
this.littleForAttacker = littleForAttacker;
}
public boolean isLittleForDefender() {
return littleForDefender;
}
public void setLittleForDefender(boolean littleForDefender) {
this.littleForDefender = littleForDefender;
}
public boolean isChelemAnnounced() {
return chelemAnnounced;
}
public void setChelemAnnounced(boolean chelemAnnounced) {
this.chelemAnnounced = chelemAnnounced;
}
public boolean isChelemRealized() {
return chelemRealized;
}
public void setChelemRealized(boolean chelemRealized) {
this.chelemRealized = chelemRealized;
}
public int getPoints(Player player) {
return points.get(players.indexOf(player));
}
public int getPoints(int i) {
return points.get(i);
}
public void calculateScores() {
// int ends = (little ? 1 : 0) + (twenty_one ? 1 : 0) + (excuse ? 1 : 0);
int bound = ends == 0 ? 56 : ends == 1 ? 51 : ends == 2 ? 41 : 36;
@ -154,7 +252,7 @@ public class Game {
if (littleForAttacker)
score += 10;
else if (littleForDefenser)
else if (littleForDefender)
score -= 10;
if (chelemAnnounced) {
@ -187,7 +285,6 @@ public class Game {
}
for (int i = 0; i < getGameType().getNbPlayers(); ++i) {
System.err.println(getMiseries());
if (getMiseries().get(i)) {
//noinspection ConstantConditions
scores.put(players.get(i), scores.get(players.get(i)) + 10 * (getGameType().getNbPlayers() - 1));
@ -278,6 +375,47 @@ public class Game {
GAMES.clear();
for (Game g : games) {
GAMES.put(g.getId(), g);
g.setGameType(GameType.values()[g.nb_players - 3]);
g.players = new ArrayList<>();
g.getPlayers().add(g.player1);
g.getPlayers().add(g.player2);
g.getPlayers().add(g.player3);
if (g.player4 != null)
g.getPlayers().add(g.player4);
if (g.player5 != null)
g.getPlayers().add(g.player5);
if (g.player6 != null)
g.getPlayers().add(g.player6);
g.miseries = new ArrayList<>();
g.getMiseries().add(g.misery1);
g.getMiseries().add(g.misery2);
g.getMiseries().add(g.misery3);
if (g.player4 != null)
g.getMiseries().add(g.misery4);
if (g.player5 != null)
g.getMiseries().add(g.misery5);
if (g.player6 != null)
g.getMiseries().add(g.misery6);
g.points = new ArrayList<>();
g.points.add(g.score1);
g.points.add(g.score2);
g.points.add(g.score3);
if (g.player4 != null)
g.points.add(g.score4);
if (g.player5 != null)
g.points.add(g.score5);
if (g.player6 != null)
g.points.add(g.score6);
System.out.println(g.toJson());
for (Player p : g.getPlayers()) {
p.addScore(g.getGameType(), g.getPoints(p));
}
}
}
catch (IOException ex) {

View File

@ -28,7 +28,6 @@ public class MainActivity extends AppCompatActivity {
INSTANCE = this;
Player.updatePlayers();
Game.updateGames();
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
@ -38,7 +37,7 @@ public class MainActivity extends AppCompatActivity {
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_new_game)
R.id.nav_new_game, R.id.nav_personal_stats)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

View File

@ -9,8 +9,6 @@ import androidx.annotation.NonNull;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
@ -41,20 +39,40 @@ public class Player {
return name;
}
public int getScore3() {
return score3;
public int getScore(Game.GameType type) {
switch (type) {
case THREE_PLAYERS:
return score3;
case FOUR_PLAYERS:
return score4;
case FIVE_PLAYERS:
return score5;
case SIX_PLAYERS:
return score6;
default:
return 0;
}
}
public int getScore4() {
return score4;
public void setScore(Game.GameType type, int score) {
switch (type) {
case THREE_PLAYERS:
score3 = score;
break;
case FOUR_PLAYERS:
score4 = score;
break;
case FIVE_PLAYERS:
score5 = score;
break;
case SIX_PLAYERS:
score6 = score;
break;
}
}
public int getScore5() {
return score5;
}
public int getScore6() {
return score6;
public void addScore(Game.GameType type, int score) {
setScore(type, getScore(type) + score);
}
@Override
@ -62,7 +80,8 @@ public class Player {
return obj instanceof Player && ((Player) obj).getId() == getId();
}
public static void updatePlayers() {
public static void updatePlayers() {
System.err.println(getAllPlayers());
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
@ -87,13 +106,15 @@ public class Player {
Toast.makeText(MainActivity.INSTANCE, "Bienvenue " + THIS_PLAYER.getName() + " !", Toast.LENGTH_SHORT).show();
}
});
Game.updateGames();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
public static Collection<Player> getAllPlayers() {
return PLAYERS.values();

View File

@ -83,7 +83,6 @@ public class HomeFragment extends Fragment {
int id = map.getOrDefault("id", 0);
PreferenceManager.getDefaultSharedPreferences(root.getContext()).edit().putString("token", token.getText().toString()).putInt("user_id", id).apply();
Player.updatePlayers();
Game.updateGames();
//noinspection ConstantConditions
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new NewGameFragment(), "Nouvelle partie").commit();
}

View File

@ -3,7 +3,4 @@ package fr.ynerant.tarot.ui.home;
import androidx.lifecycle.ViewModel;
public class HomeViewModel extends ViewModel {
public HomeViewModel() {
}
}

View File

@ -132,29 +132,29 @@ public class NewGameFragment extends Fragment {
Game game = getGame();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i) {
points[i].setText(String.valueOf(game.points.get(i)));
points[i].setText(String.valueOf(game.getPoints(i)));
}
}
public Game getGame() {
Game getGame() {
Game game = new Game();
game.gameType = Game.GameType.getGameType(nb_players.getProgress() + 3);
game.attacker = (Player) attacker.getSelectedItem();
game.setGameType(Game.GameType.getGameType(nb_players.getProgress() + 3));
game.setAttacker((Player) attacker.getSelectedItem());
if (game.getGameType().getNbPlayers() >= 5)
game.follower = (Player) follower.getSelectedItem();
game.attackScore = attack_points.getProgress();
game.players = players;
game.bet = Game.Bet.values()[bet.getProgress()];
game.ends = nb_ends.getProgress();
game.handle = handle.getProgress();
game.chelemAnnounced = chelem_announced.isChecked();
game.chelemRealized = chelem_realized.isChecked();
game.littleForAttacker = little_end.getProgress() == 1;
game.littleForDefenser = little_end.getProgress() == 2;
game.setFollower((Player) follower.getSelectedItem());
game.setAttackScore(attack_points.getProgress());
game.setPlayers(players);
game.setBet(Game.Bet.values()[bet.getProgress()]);
game.setEnds(nb_ends.getProgress());
game.setHandle(handle.getProgress());
game.setChelemAnnounced(chelem_announced.isChecked());
game.setChelemRealized(chelem_realized.isChecked());
game.setLittleForAttacker(little_end.getProgress() == 1);
game.setLittleForDefender(little_end.getProgress() == 2);
List<Boolean> miseriesList = new ArrayList<>();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i)
miseriesList.add(miseries[i].isChecked());
game.miseries = miseriesList;
game.setMiseries(miseriesList);
game.calculateScores();
return game;
@ -490,9 +490,10 @@ public class NewGameFragment extends Fragment {
co.setRequestProperty("token", PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null));
co.setRequestProperty("json", g.toJson());
co.connect();
System.out.println(g.toJson());
System.err.println(IOUtils.readLines(co.getInputStream()));
co.getResponseCode();
co.disconnect();
Game.updateGames();
} catch (IOException ex) {
ex.printStackTrace();
}
@ -501,25 +502,25 @@ public class NewGameFragment extends Fragment {
}
public Game getGame() {
Game getGame() {
Game game = new Game();
game.gameType = Game.GameType.getGameType(nb_players.getProgress() + 3);
game.attacker = (Player) attacker.getSelectedItem();
game.setGameType(Game.GameType.getGameType(nb_players.getProgress() + 3));
game.setAttacker((Player) attacker.getSelectedItem());
if (game.getGameType().getNbPlayers() >= 5)
game.follower = (Player) follower.getSelectedItem();
game.attackScore = attack_points.getProgress();
game.players = players;
game.bet = Game.Bet.values()[bet.getProgress()];
game.ends = nb_ends.getProgress();
game.handle = handle.getProgress();
game.chelemAnnounced = chelem_announced.isChecked();
game.chelemRealized = chelem_realized.isChecked();
game.littleForAttacker = little_end.getProgress() == 1;
game.littleForDefenser = little_end.getProgress() == 2;
game.setFollower((Player) follower.getSelectedItem());
game.setAttackScore(attack_points.getProgress());
game.setPlayers(players);
game.setBet(Game.Bet.values()[bet.getProgress()]);
game.setEnds(nb_ends.getProgress());
game.setHandle(handle.getProgress());
game.setChelemAnnounced(chelem_announced.isChecked());
game.setChelemRealized(chelem_realized.isChecked());
game.setLittleForAttacker(little_end.getProgress() == 1);
game.setLittleForDefender(little_end.getProgress() == 2);
List<Boolean> miseriesList = new ArrayList<>();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i)
miseriesList.add(miseries[i].isChecked());
game.miseries = miseriesList;
game.setMiseries(miseriesList);
game.calculateScores();
return game;

View File

@ -3,7 +3,4 @@ package fr.ynerant.tarot.ui.newgame;
import androidx.lifecycle.ViewModel;
public class NewGameViewModel extends ViewModel {
public NewGameViewModel() {
}
}

View File

@ -0,0 +1,70 @@
package fr.ynerant.tarot.ui.stats;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import fr.ynerant.tarot.Game;
import fr.ynerant.tarot.MainActivity;
import fr.ynerant.tarot.Player;
import fr.ynerant.tarot.R;
import fr.ynerant.tarot.ui.home.HomeFragment;
public class PersonalStatsFragment extends Fragment {
private PersonalStatsModel personalStatsModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, final Bundle savedInstanceState) {
personalStatsModel =
ViewModelProviders.of(this).get(PersonalStatsModel.class);
final View root = inflater.inflate(R.layout.fragment_personal_stats, container, false);
if (PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null) == null) {
//noinspection ConstantConditions
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new HomeFragment(), "Se connecter").commit();
return root;
}
//noinspection ConstantConditions
Player player = Player.getPlayerById(getArguments().getInt("playerId"));
TextView points3 = root.findViewById(R.id.points3);
TextView points4 = root.findViewById(R.id.points4);
TextView points5 = root.findViewById(R.id.points5);
TextView points6 = root.findViewById(R.id.points6);
points3.setText(String.valueOf(player.getScore(Game.GameType.THREE_PLAYERS)));
points4.setText(String.valueOf(player.getScore(Game.GameType.FOUR_PLAYERS)));
points5.setText(String.valueOf(player.getScore(Game.GameType.FIVE_PLAYERS)));
points6.setText(String.valueOf(player.getScore(Game.GameType.SIX_PLAYERS)));
return root;
}
}

View File

@ -0,0 +1,76 @@
package fr.ynerant.tarot.ui.stats;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import fr.ynerant.tarot.Game;
import fr.ynerant.tarot.MainActivity;
import fr.ynerant.tarot.Player;
import fr.ynerant.tarot.R;
import fr.ynerant.tarot.ui.home.HomeFragment;
public class PersonalStatsMenuFragment extends Fragment {
private PersonalStatsMenuModel personalStatsMenuModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, final Bundle savedInstanceState) {
personalStatsMenuModel =
ViewModelProviders.of(this).get(PersonalStatsMenuModel.class);
final View root = inflater.inflate(R.layout.fragment_personal_stats_menu, container, false);
if (PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null) == null) {
//noinspection ConstantConditions
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new HomeFragment(), "Se connecter").commit();
return root;
}
ListView players = root.findViewById(R.id.players_stats);
//noinspection ConstantConditions
final ListAdapter adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_expandable_list_item_1, new ArrayList<>(Player.getAllPlayers()));
players.setAdapter(adapter);
players.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Player player = (Player) adapter.getItem(position);
PersonalStatsFragment fragment = new PersonalStatsFragment();
Bundle bundle = new Bundle();
bundle.putInt("playerId", player.getId());
fragment.setArguments(bundle);
//noinspection ConstantConditions
getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, fragment, "Statistiques individuelles de " + player.getName())
.addToBackStack("Statistiques individuelles").commit();
}
});
return root;
}
}

View File

@ -0,0 +1,6 @@
package fr.ynerant.tarot.ui.stats;
import androidx.lifecycle.ViewModel;
public class PersonalStatsMenuModel extends ViewModel {
}

View File

@ -0,0 +1,6 @@
package fr.ynerant.tarot.ui.stats;
import androidx.lifecycle.ViewModel;
public class PersonalStatsModel extends ViewModel {
}

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Points à 3 joueurs :"/>
<TextView
android:id="@+id/points3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textAlignment="textEnd"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Points à 4 joueurs :" />
<TextView
android:id="@+id/points4"
android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Points à 5 joueurs :" />
<TextView
android:id="@+id/points5"
android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Points à 6 joueurs :" />
<TextView
android:id="@+id/points6"
android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/players_stats"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -4,15 +4,15 @@
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<!--<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/menu_home" />-->
<item
android:id="@+id/nav_new_game"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/menu_new_game" />
<item
android:id="@+id/nav_personal_stats"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/personal_stats" />
</group>
</menu>

View File

@ -16,4 +16,10 @@
android:name="fr.ynerant.tarot.ui.newgame.NewGameFragment"
android:label="@string/menu_new_game"
tools:layout="@layout/fragment_new_game" />
<fragment
android:id="@+id/nav_personal_stats"
android:name="fr.ynerant.tarot.ui.stats.PersonalStatsMenuFragment"
android:label="@string/personal_stats"
tools:layout="@layout/fragment_personal_stats_menu" />
</navigation>

View File

@ -35,4 +35,5 @@
<string name="players4">4 joueurs</string>
<string name="players5">5 joueurs</string>
<string name="players6">6 joueurs</string>
<string name="personal_stats">Statistiques individuelles</string>
</resources>

View File

@ -35,4 +35,5 @@
<string name="players4">4 joueurs</string>
<string name="players5">5 joueurs</string>
<string name="players6">6 joueurs</string>
<string name="personal_stats">Statistiques individuelles</string>
</resources>