tarot-ens-info/app/src/main/java/fr/ynerant/tarot/ui/newgame/NewGameFragment.java

570 lines
21 KiB
Java

package fr.ynerant.tarot.ui.newgame;
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.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 NewGameFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
ViewModelProviders.of(this).get(NewGameViewModel.class);
final View root = inflater.inflate(R.layout.fragment_new_game, 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;
}
while (Player.getAllPlayers().isEmpty()) {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
TextView textView = root.findViewById(R.id.text_new_game);
textView.setText(R.string.menu_new_game);
final SeekBar nb_players = root.findViewById(R.id.nb_players);
final Spinner player1 = root.findViewById(R.id.player1);
final Spinner player2 = root.findViewById(R.id.player2);
final Spinner player3 = root.findViewById(R.id.player3);
final Spinner player4 = root.findViewById(R.id.player4);
final Spinner player5 = root.findViewById(R.id.player5);
final Spinner player6 = root.findViewById(R.id.player6);
final LinearLayout player4_layout = root.findViewById(R.id.player4_layout);
final LinearLayout player5_layout = root.findViewById(R.id.player5_layout);
final LinearLayout player6_layout = root.findViewById(R.id.player6_layout);
final List<Player> players = new ArrayList<>();
//noinspection ConstantConditions
ArrayAdapter<Player> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, new ArrayList<>(Player.getAllPlayers()));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
player1.setAdapter(adapter);
int id = 0;
while (adapter.getItem(id) != Player.THIS_PLAYER)
++id;
player1.setSelection(id);
player2.setAdapter(adapter);
player2.setSelection(1);
player3.setAdapter(adapter);
player3.setSelection(2);
player4.setAdapter(adapter);
player4.setSelection(3);
player5.setAdapter(adapter);
player5.setSelection(4);
player6.setAdapter(adapter);
player6.setSelection(5);
final Spinner[] spinners = new Spinner[]{player1, player2, player3, player4, player5, player6};
final Spinner attacker = root.findViewById(R.id.attacker);
final Spinner follower = root.findViewById(R.id.follower);
final LinearLayout follower_layout = root.findViewById(R.id.follower_layout);
final SeekBar attack_points = root.findViewById(R.id.attack_score);
final SeekBar bet = root.findViewById(R.id.bet);
final SeekBar nb_ends = root.findViewById(R.id.nb_ends);
final Switch chelem_announced = root.findViewById(R.id.chelem_announced);
final Switch chelem_realized = root.findViewById(R.id.chelem_realized);
final SeekBar little_end = root.findViewById(R.id.little_end);
final Button handle1 = root.findViewById(R.id.player1_handle);
final Button handle2 = root.findViewById(R.id.player2_handle);
final Button handle3 = root.findViewById(R.id.player3_handle);
final Button handle4 = root.findViewById(R.id.player4_handle);
final Button handle5 = root.findViewById(R.id.player5_handle);
final Button handle6 = root.findViewById(R.id.player6_handle);
final Button[] handles = new Button[]{handle1, handle2, handle3, handle4, handle5, handle6};
final TextView handle1Text = root.findViewById(R.id.player1_handle_text);
final TextView handle2Text = root.findViewById(R.id.player2_handle_text);
final TextView handle3Text = root.findViewById(R.id.player3_handle_text);
final TextView handle4Text = root.findViewById(R.id.player4_handle_text);
final TextView handle5Text = root.findViewById(R.id.player5_handle_text);
final TextView handle6Text = root.findViewById(R.id.player6_handle_text);
final TextView[] handleTexts = new TextView[]{handle1Text, handle2Text, handle3Text, handle4Text, handle5Text, handle6Text};
final ToggleButton misery1 = root.findViewById(R.id.player1_misery);
final ToggleButton misery2 = root.findViewById(R.id.player2_misery);
final ToggleButton misery3 = root.findViewById(R.id.player3_misery);
final ToggleButton misery4 = root.findViewById(R.id.player4_misery);
final ToggleButton misery5 = root.findViewById(R.id.player5_misery);
final ToggleButton misery6 = root.findViewById(R.id.player6_misery);
final ToggleButton[] miseries = new ToggleButton[]{misery1, misery2, misery3, misery4, misery5, misery6};
final TextView misery1Text = root.findViewById(R.id.player1_misery_text);
final TextView misery2Text = root.findViewById(R.id.player2_misery_text);
final TextView misery3Text = root.findViewById(R.id.player3_misery_text);
final TextView misery4Text = root.findViewById(R.id.player4_misery_text);
final TextView misery5Text = root.findViewById(R.id.player5_misery_text);
final TextView misery6Text = root.findViewById(R.id.player6_misery_text);
final TextView[] miseryTexts = new TextView[]{misery1Text, misery2Text, misery3Text, misery4Text, misery5Text, misery6Text};
final TextView points1 = root.findViewById(R.id.player1_points);
final TextView points2 = root.findViewById(R.id.player2_points);
final TextView points3 = root.findViewById(R.id.player3_points);
final TextView points4 = root.findViewById(R.id.player4_points);
final TextView points5 = root.findViewById(R.id.player5_points);
final TextView points6 = root.findViewById(R.id.player6_points);
final TextView[] points = new TextView[]{points1, points2, points3, points4, points5, points6};
final TextView totalPoints1 = root.findViewById(R.id.player1_total_points);
final TextView totalPoints2 = root.findViewById(R.id.player2_total_points);
final TextView totalPoints3 = root.findViewById(R.id.player3_total_points);
final TextView totalPoints4 = root.findViewById(R.id.player4_total_points);
final TextView totalPoints5 = root.findViewById(R.id.player5_total_points);
final TextView totalPoints6 = root.findViewById(R.id.player6_total_points);
final TextView[] totalPoints = new TextView[]{totalPoints1, totalPoints2, totalPoints3, totalPoints4, totalPoints5, totalPoints6};
final TextView points1Text = root.findViewById(R.id.player1_points_text);
final TextView points2Text = root.findViewById(R.id.player2_points_text);
final TextView points3Text = root.findViewById(R.id.player3_points_text);
final TextView points4Text = root.findViewById(R.id.player4_points_text);
final TextView points5Text = root.findViewById(R.id.player5_points_text);
final TextView points6Text = root.findViewById(R.id.player6_points_text);
final TextView[] pointsText = new TextView[]{points1Text, points2Text, points3Text, points4Text, points5Text, points6Text};
final Runnable updateScore = new Runnable() {
@Override
public void run() {
Game game = getGame();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i) {
points[i].setText(String.valueOf(game.getPoints(i)));
}
}
Game getGame() {
Game game = new Game();
game.setGameType(Game.GameType.getGameType(nb_players.getProgress() + 3));
game.setAttacker((Player) attacker.getSelectedItem());
if (game.getGameType().getNbPlayers() >= 5)
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());
List<Integer> handle = new ArrayList<>();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i)
handle.add((int) (1024.0F * (1.0F - handles[i].getAlpha())));
game.setHandles(handle);
game.setChelemAnnounced(chelem_announced.isChecked());
game.setChelemRealized(chelem_realized.isChecked());
game.setLittleEnd(little_end.getProgress());
List<Boolean> miseriesList = new ArrayList<>();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i)
miseriesList.add(miseries[i].isChecked());
game.setMiseries(miseriesList);
game.calculateScores();
return game;
}
};
final Spinner.OnItemSelectedListener updatePlayers = new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
players.clear();
players.add((Player) player1.getSelectedItem());
players.add((Player) player2.getSelectedItem());
players.add((Player) player3.getSelectedItem());
if (nb_players.getProgress() >= 1)
players.add((Player) player4.getSelectedItem());
if (nb_players.getProgress() >= 2)
players.add((Player) player5.getSelectedItem());
if (nb_players.getProgress() == 3)
players.add((Player) player6.getSelectedItem());
//noinspection ConstantConditions
ArrayAdapter<Player> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, new ArrayList<>(players));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
attacker.setAdapter(adapter);
follower.setAdapter(adapter);
for (int i = 0; i < 6; ++i) {
miseryTexts[i].setText(((Player) spinners[i].getSelectedItem()).getName());
pointsText[i].setText(((Player) spinners[i].getSelectedItem()).getName());
handleTexts[i].setText(((Player) spinners[i].getSelectedItem()).getName());
totalPoints[i].setText(String.valueOf(((Player) spinners[i].getSelectedItem()).getScore(Game.GameType.values()[nb_players.getProgress()])));
}
updateScore.run();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
};
Spinner.OnItemSelectedListener justUpdate = new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
updateScore.run();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
ToggleButton.OnCheckedChangeListener justUpdate2 = new ToggleButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateScore.run();
}
};
updatePlayers.onItemSelected(null, null, 0, 0);
for (int i = 0; i < 6; ++i) {
spinners[i].setOnItemSelectedListener(updatePlayers);
miseries[i].setOnCheckedChangeListener(justUpdate2);
}
attacker.setOnItemSelectedListener(justUpdate);
follower.setOnItemSelectedListener(justUpdate);
nb_players.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
TextView nb_players_info = root.findViewById(R.id.nb_players_info);
int resource = R.string.players4;
switch (progress) {
case 0:
resource = R.string.players3;
break;
case 1:
resource = R.string.players4;
break;
case 2:
resource = R.string.players5;
break;
case 3:
resource = R.string.players6;
break;
}
nb_players_info.setText(resource);
player4_layout.setVisibility(View.GONE);
handle4Text.setVisibility(View.GONE);
handle4.setVisibility(View.GONE);
misery4Text.setVisibility(View.GONE);
misery4.setVisibility(View.GONE);
points4Text.setVisibility(View.GONE);
points4.setVisibility(View.GONE);
totalPoints4.setVisibility(View.GONE);
player5_layout.setVisibility(View.GONE);
handle5Text.setVisibility(View.GONE);
handle5.setVisibility(View.GONE);
misery5Text.setVisibility(View.GONE);
misery5.setVisibility(View.GONE);
points5Text.setVisibility(View.GONE);
points5.setVisibility(View.GONE);
totalPoints5.setVisibility(View.GONE);
player6_layout.setVisibility(View.GONE);
handle6Text.setVisibility(View.GONE);
handle6.setVisibility(View.GONE);
misery6Text.setVisibility(View.GONE);
misery6.setVisibility(View.GONE);
points6Text.setVisibility(View.GONE);
points6.setVisibility(View.GONE);
totalPoints6.setVisibility(View.GONE);
follower_layout.setVisibility(View.GONE);
switch (progress + 3) {
case 6:
player6_layout.setVisibility(View.VISIBLE);
handle6Text.setVisibility(View.VISIBLE);
handle6.setVisibility(View.VISIBLE);
misery6Text.setVisibility(View.VISIBLE);
misery6.setVisibility(View.VISIBLE);
points6Text.setVisibility(View.VISIBLE);
points6.setVisibility(View.VISIBLE);
totalPoints6.setVisibility(View.VISIBLE);
case 5:
player5_layout.setVisibility(View.VISIBLE);
handle5Text.setVisibility(View.VISIBLE);
handle5.setVisibility(View.VISIBLE);
misery5Text.setVisibility(View.VISIBLE);
misery5.setVisibility(View.VISIBLE);
points5Text.setVisibility(View.VISIBLE);
points5.setVisibility(View.VISIBLE);
totalPoints5.setVisibility(View.VISIBLE);
follower_layout.setVisibility(View.VISIBLE);
case 4:
player4_layout.setVisibility(View.VISIBLE);
handle4Text.setVisibility(View.VISIBLE);
handle4.setVisibility(View.VISIBLE);
misery4Text.setVisibility(View.VISIBLE);
misery4.setVisibility(View.VISIBLE);
points4Text.setVisibility(View.VISIBLE);
points4.setVisibility(View.VISIBLE);
totalPoints4.setVisibility(View.VISIBLE);
}
little_end.setMax(progress + 3);
for (int i = 0; i < 6; ++i)
totalPoints[i].setText(String.valueOf(((Player) spinners[i].getSelectedItem()).getScore(Game.GameType.values()[nb_players.getProgress()])));
updatePlayers.onItemSelected(null, null, 0, 0);
updateScore.run();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
attack_points.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
TextView attack = root.findViewById(R.id.nb_points_attack);
TextView defense = root.findViewById(R.id.nb_points_defense);
attack.setText(String.valueOf(progress));
defense.setText(String.valueOf(91 - progress));
updateScore.run();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
bet.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
TextView textView = root.findViewById(R.id.bet_info);
switch (progress) {
case 1:
textView.setText(R.string.guard);
break;
case 2:
textView.setText(R.string.guard_without);
break;
case 3:
textView.setText(R.string.guard_against);
break;
default:
textView.setText(R.string.little);
break;
}
updateScore.run();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
nb_ends.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
TextView textView = root.findViewById(R.id.nb_ends_info);
switch (progress) {
case 1:
textView.setText(R.string.one_end);
attack_points.setProgress(51);
break;
case 2:
textView.setText(R.string.two_ends);
attack_points.setProgress(41);
break;
case 3:
textView.setText(R.string.three_ends);
attack_points.setProgress(36);
break;
default:
textView.setText(R.string.null_end);
attack_points.setProgress(56);
break;
}
updateScore.run();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
chelem_announced.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateScore.run();
}
});
chelem_realized.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateScore.run();
}
});
little_end.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
TextView textView = root.findViewById(R.id.little_end_info);
if (progress == 0)
textView.setText(R.string.nobody);
else
textView.setText(((Player) spinners[progress - 1].getSelectedItem()).getName());
if (!fromUser)
return;
updateScore.run();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
attack_points.setProgress(51);
View.OnClickListener onHandleClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
int x = ((int) (1024.0F * (1.0F - b.getAlpha())) + 1) % 4;
b.setAlpha(1.0F - x / 1024.0F);
switch (x) {
case 0:
b.setText(R.string.handle0);
break;
case 1:
b.setText(R.string.handle1);
break;
case 2:
b.setText(R.string.handle2);
break;
case 3:
b.setText(R.string.handle3);
break;
}
updateScore.run();
}
};
for (int i = 0; i < 6; ++i)
handles[i].setOnClickListener(onHandleClick);
final Button btn = root.findViewById(R.id.add_game_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "La partie est enregistrée !", Toast.LENGTH_SHORT).show();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
Game g = getGame();
try {
URL url = new URL("https://ynerant.fr/tarot/game.php?add_game");
HttpURLConnection co = (HttpURLConnection) url.openConnection();
co.setRequestMethod("POST");
co.setRequestProperty("token", PreferenceManager.getDefaultSharedPreferences(MainActivity.INSTANCE).getString("token", null));
co.setRequestProperty("json", g.toJson());
co.connect();
co.getResponseCode();
co.disconnect();
Game.updateGames();
MainActivity.INSTANCE.runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 6; ++i) {
totalPoints[i].setText(String.valueOf(((Player) spinners[i].getSelectedItem()).getScore(Game.GameType.values()[nb_players.getProgress()])));
miseries[i].setChecked(false);
handles[i].setAlpha(1.0F);
handles[i].setText(R.string.handle0);
}
little_end.setProgress(0);
chelem_announced.setChecked(false);
chelem_realized.setChecked(false);
}
});
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
Game getGame() {
Game game = new Game();
game.setGameType(Game.GameType.getGameType(nb_players.getProgress() + 3));
game.setAttacker((Player) attacker.getSelectedItem());
if (game.getGameType().getNbPlayers() >= 5)
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());
List<Integer> handle = new ArrayList<>();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i)
handle.add((int) (1024.0F * (1.0F - handles[i].getAlpha())));
game.setHandles(handle);
game.setChelemAnnounced(chelem_announced.isChecked());
game.setChelemRealized(chelem_realized.isChecked());
game.setLittleEnd(little_end.getProgress());
List<Boolean> miseriesList = new ArrayList<>();
for (int i = 0; i < game.getGameType().getNbPlayers(); ++i)
miseriesList.add(miseries[i].isChecked());
game.setMiseries(miseriesList);
game.calculateScores();
return game;
}
});
return root;
}
}