Commit 51e9a0a8 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

List of player answers are sent to clients in JSON format.

MultipleChoiceFragment can handle answers of more than two players.
parent 8c124c6e
......@@ -27,4 +27,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.google.code.gson:gson:2.8.2'
}
......@@ -93,7 +93,7 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
@Override
public void run() {
String[] params = msg.split("##");
if (params.length == 8) {
if (params.length == 9) {
replaceFragment(params);
}
}
......@@ -102,12 +102,12 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
private void replaceFragment(String[] strings) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (Integer.parseInt(strings[5]) == -1) {
if (strings[0].equals("question")) {
questionReceivedDate = new Date();
}
MultipleChoiceFragment frag = MultipleChoiceFragment.newInstance(
strings[0], strings[1], strings[2], strings[3], strings[4],
Integer.parseInt(strings[5]), Integer.parseInt(strings[6]), Integer.parseInt(strings[7]), -1);
strings[1], strings[2], strings[3], strings[4], strings[5],
strings[6], Integer.parseInt(strings[7]), Integer.parseInt(strings[8]), -1);
fragmentTransaction.replace(R.id.FragmentContainer, frag);
fragmentTransaction.commit();
}
......
......@@ -6,15 +6,27 @@ import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import onlab.kvizclient.model.PlayerAnswer;
public class MultipleChoiceFragment extends Fragment {
......@@ -23,15 +35,15 @@ public class MultipleChoiceFragment extends Fragment {
private static final String ANSWER2 = "ANSWER2";
private static final String ANSWER3 = "ANSWER3";
private static final String ANSWER4 = "ANSWER4";
private static final String OWN_ANSWER = "OWN_ANSWER";
private static final String OPPONENT_ANSWER = "OPPONENT_ANSWER";
private static final String PLAYER_ANSWERS = "PLAYER_ANSWERS"; //játékosok válaszait tartalmazó tömb JSON szerializálva
private static final String OWN_INDEX = "OWN_INDEX"; //az aktuális játékos válaszának indexe az előbbi tömbben
private static final String CORRECT_ANSWER = "CORRECT_ANSWER";
private static final String TIMEOUT = "TIMEOUT";
private String question;
private String[] answers = new String[4];
private int own_answer;
private int opponent_answer;
private List<PlayerAnswer> playerAnswers;
private int own_index;
private int correct_answer;
private int timeout; //hány ms adott a kérdés megválaszolására; ha nem pozitív, akkor nincs limit, a progressbar-t is töröljük
......@@ -49,8 +61,8 @@ public class MultipleChoiceFragment extends Fragment {
}
public static MultipleChoiceFragment newInstance(String question, String answer1, String answer2,
String answer3, String answer4, int own_answer,
int opponent_answer, int correct_answer, int timeout) {
String answer3, String answer4, String player_answers,
int own_index, int correct_answer, int timeout) {
MultipleChoiceFragment fragment = new MultipleChoiceFragment();
Bundle args = new Bundle();
args.putString(QUESTION, question);
......@@ -58,8 +70,8 @@ public class MultipleChoiceFragment extends Fragment {
args.putString(ANSWER2, answer2);
args.putString(ANSWER3, answer3);
args.putString(ANSWER4, answer4);
args.putInt(OWN_ANSWER, own_answer);
args.putInt(OPPONENT_ANSWER, opponent_answer);
args.putString(PLAYER_ANSWERS, player_answers);
args.putInt(OWN_INDEX, own_index);
args.putInt(CORRECT_ANSWER, correct_answer);
args.putInt(TIMEOUT, timeout);
fragment.setArguments(args);
......@@ -75,8 +87,11 @@ public class MultipleChoiceFragment extends Fragment {
answers[1] = getArguments().getString(ANSWER2);
answers[2] = getArguments().getString(ANSWER3);
answers[3] = getArguments().getString(ANSWER4);
own_answer = getArguments().getInt(OWN_ANSWER);
opponent_answer = getArguments().getInt(OPPONENT_ANSWER);
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<PlayerAnswer>>(){}.getType();
Collection<PlayerAnswer> playerAnswerCollection = gson.fromJson(getArguments().getString(PLAYER_ANSWERS), collectionType);
playerAnswers = new ArrayList<>(playerAnswerCollection);
own_index = getArguments().getInt(OWN_INDEX);
correct_answer = getArguments().getInt(CORRECT_ANSWER);
timeout = getArguments().getInt(TIMEOUT);
}
......@@ -123,21 +138,41 @@ public class MultipleChoiceFragment extends Fragment {
answerButtons[correct_answer].setBackgroundColor(Color.GREEN);
}
int own_answer = playerAnswers.get(own_index).getValue();
if (own_answer != correct_answer && own_answer > -1) {
answerButtons[own_answer].setBackgroundColor(Color.RED);
}
if (opponent_answer != correct_answer && opponent_answer > -1) {
answerButtons[opponent_answer].setBackgroundColor(Color.RED);
}
if (own_answer > -1) {
answered = true;
leftImageViews[own_answer].setImageResource(R.drawable.ic_keyboard_arrow_right_black_48dp);
}
if (opponent_answer > -1) {
rightImageViews[opponent_answer].setImageResource(R.drawable.ic_keyboard_arrow_left_black_48dp);
if (playerAnswers.size() == 2) {
int opponent_answer = playerAnswers.get(1-own_index).getValue();
if (opponent_answer != correct_answer && opponent_answer > -1) {
answerButtons[opponent_answer].setBackgroundColor(Color.RED);
}
if (opponent_answer > -1) {
rightImageViews[opponent_answer].setImageResource(R.drawable.ic_keyboard_arrow_left_black_48dp);
}
}
if (playerAnswers.size() > 2) {
LinearLayout multipleChoiceFragmentLinearLayout = (LinearLayout) getView().findViewById(R.id.MultipleChoiceFragmentLinearLayout);
for (int i=0;i<playerAnswers.size();i++) {
if (i != own_index) {
final TextView playerAnswerTextView = new TextView(getActivity());
PlayerAnswer answer = playerAnswers.get(i);
playerAnswerTextView.setText(answer.getPlayerName() + ": "
+ answers[answer.getValue()]);
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
playerAnswerTextView.setPadding(20,30,20,0);
multipleChoiceFragmentLinearLayout.addView(playerAnswerTextView);
}
}
}
progressBar = (ProgressBar) getView().findViewById(R.id.determinateBar);
......
......@@ -113,7 +113,7 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
int timeout = strings[0].equals("question") ? 10900 : -1;
frag = MultipleChoiceFragment.newInstance(
strings[3], strings[4], strings[5], strings[6], strings[7],
Integer.parseInt(strings[8]), Integer.parseInt(strings[9]), Integer.parseInt(strings[10]), timeout);
strings[8], Integer.parseInt(strings[9]), Integer.parseInt(strings[10]), timeout);
} else {
frag = MessageFragment.newInstance(strings[3]);
}
......
package onlab.kvizclient.model;
public class PlayerAnswer {
private String playerName;
private int value;
private double time;
public PlayerAnswer(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public double getTime() {
return time;
}
public void setTime(double time) {
this.time = time;
}
}
......@@ -20,6 +20,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kli"
android:textSize="50sp"
android:id="@+id/txtname"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
......
......@@ -7,6 +7,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MultipleChoiceFragmentLinearLayout"
android:orientation="vertical">
<ProgressBar
......
......@@ -27,4 +27,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.github.rustamg:file-dialogs:1.0'
compile 'com.google.code.gson:gson:2.8.2'
}
......@@ -10,6 +10,8 @@ import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
......@@ -38,19 +40,21 @@ public class GameActivity extends AppCompatActivity {
private int numberOfPlayers;
final List<TextView> playerAnswerTextViews = new ArrayList<>();
private final List<TextView> playerAnswerTextViews = new ArrayList<>();
private final List<String> answers = new ArrayList<>();
final List<String> answers = new ArrayList<>();
private PlayerAnswer[] playerAnswers;
PlayerAnswer[] playerAnswers;
private int correctAnswer;
int correctAnswer;
private Handler updateConversationHandler;
Handler updateConversationHandler;
private String questionString;
String questionString;
private TextView correctAnswerTextView;
TextView correctAnswerTextView;
private Gson gson;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -59,6 +63,8 @@ public class GameActivity extends AppCompatActivity {
Bundle extras = getIntent().getExtras();
String questionFileName = extras.getString("QUESTION_FILE_NAME", null);
gson = new Gson();
updateConversationHandler = new Handler();
InputStream inputStream = null;
......@@ -81,8 +87,8 @@ public class GameActivity extends AppCompatActivity {
it.remove();
}
Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
Button correctAnswerButton = (Button) findViewById(R.id.CorrectAnswer);
final Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
final Button correctAnswerButton = (Button) findViewById(R.id.CorrectAnswer);
final TextView questionText = (TextView) findViewById(R.id.QuestionText);
final List<TextView> answerTextViews = new ArrayList<>();
answerTextViews.add((TextView) findViewById(R.id.Answer1TextView));
......@@ -146,10 +152,12 @@ public class GameActivity extends AppCompatActivity {
}
}
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of " + playerAnswers[i].getPlayerName() + ": ");
outputs.get(i).println(questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##-1##-1##-1" );
outputs.get(i).println("question##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1");
}
questionIndex++;
} else {
......@@ -157,6 +165,12 @@ public class GameActivity extends AppCompatActivity {
for (int i=0;i<4;i++) {
answerTextViews.get(i).setText("");
}
for (int i=0;i<numberOfPlayers;i++) {
playerAnswerTextViews.get(i).setText("");
}
correctAnswerTextView.setText("");
nextQuestionBtn.setEnabled(false);
correctAnswerButton.setEnabled(false);
}
}
});
......@@ -164,10 +178,11 @@ public class GameActivity extends AppCompatActivity {
correctAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
outputs.get(i).println(questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + Integer.toString(playerAnswers[i].getValue())
+ "##" + "-1" + "##" + Integer.toString(correctAnswer) );
outputs.get(i).println("answer##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer));
}
}
});
......
......@@ -9,6 +9,8 @@ import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
......@@ -24,6 +26,7 @@ import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import onlab.kvizserver.model.PlayerAnswer;
import onlab.kvizserver.model.Question;
public class QuizUpActivity extends AppCompatActivity {
......@@ -32,11 +35,12 @@ public class QuizUpActivity extends AppCompatActivity {
private int round = 0;
private int numberOfPlayers;
private final List<String> answers = new ArrayList<>();
private int[] playerAnswers;
//private int[] playerAnswerValues;
private int correctAnswer;
private String questionString;
private int[] playerScores;
private String[] playerNames;
//private String[] playerNames;
private PlayerAnswer[] playerAnswers;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
......@@ -48,6 +52,7 @@ public class QuizUpActivity extends AppCompatActivity {
private TextView correctAnswerTextView;
private Handler updateConversationHandler;
private Gson gson;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -58,6 +63,8 @@ public class QuizUpActivity extends AppCompatActivity {
Bundle extras = getIntent().getExtras();
String questionFileName = extras.getString("QUESTION_FILE_NAME", null);
gson = new Gson();
updateConversationHandler = new Handler();
InputStream inputStream = null;
......@@ -91,11 +98,9 @@ public class QuizUpActivity extends AppCompatActivity {
numberOfPlayers = ClientHolder.size();
playerAnswers = new int[numberOfPlayers];
playerNames = new String[numberOfPlayers];
playerAnswers = new PlayerAnswer[numberOfPlayers];
for (int i=0;i<numberOfPlayers;i++) {
playerNames[i] = ClientHolder.get(i).getName();
playerAnswers[i] = new PlayerAnswer(ClientHolder.get(i).getName());
}
playerScores = new int[2];
......@@ -147,7 +152,7 @@ public class QuizUpActivity extends AppCompatActivity {
questionText.setText(questionString);
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = -1;
playerAnswers[i].setValue(-1);
}
answers.clear();
......@@ -165,12 +170,14 @@ public class QuizUpActivity extends AppCompatActivity {
correctAnswerTextView.setText("The correct answer: ");
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of " + playerNames[i] + ": ");
playerAnswerTextViews.get(i).setText("The answer of " + playerAnswers[i].getPlayerName() + ": ");
String message = "question##" + Integer.toString(playerScores[i]) + "##"
+ Integer.toString(playerScores[1-i]) + "##" + questionString
+ "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##-1##-1##-1";
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1";
outputs.get(i).println(message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
......@@ -184,6 +191,7 @@ public class QuizUpActivity extends AppCompatActivity {
playerAnswerTextViews.get(i).setText("");
playerScoreTextViews.get(i).setText("");
}
correctAnswerTextView.setText("");
String[] messageForPlayers = new String[2];
for (int i=0;i<2;i++) {
messageForPlayers[i] = "message##" + Integer.toString(playerScores[i]) + "##" + Integer.toString(playerScores[1-i]) + "##";
......@@ -218,12 +226,13 @@ public class QuizUpActivity extends AppCompatActivity {
private void sendResults() {
updateConversationHandler.post(new QuizUpActivity.updateUIThread(-1, "")); //show correct answer
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
String message = "answer##" + Integer.toString(playerScores[i]) + "##"
+ Integer.toString(playerScores[1-i]) + "##" + questionString
+ "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + Integer.toString(playerAnswers[i])
+ "##" + Integer.toString(playerAnswers[1-i]) + "##" + Integer.toString(correctAnswer);
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer);
outputs.get(i).println(message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
......@@ -270,8 +279,9 @@ public class QuizUpActivity extends AppCompatActivity {
Log.d("GameActivity", Integer.toString(index) + "-es klienstől kapott üzenet: " + read);
String[] strings = read.split("##");
int answerInt = Integer.parseInt(strings[0]);
playerAnswers[index] = answerInt;
if (playerAnswers[index] == correctAnswer) {
playerAnswers[index].setValue(answerInt);
playerAnswers[index].setTime(Double.parseDouble(strings[1]));
if (playerAnswers[index].getValue() == correctAnswer) {
int gainedScore = (int) (21 - Double.parseDouble(strings[1]));
if (round < 7 ) {
playerScores[index] += gainedScore;
......@@ -281,7 +291,7 @@ public class QuizUpActivity extends AppCompatActivity {
}
String answerString = (answerInt == -2) ? "nem válaszolt" : (answers.get(answerInt) + " (" + strings[1] + "s)");
updateConversationHandler.post(new QuizUpActivity.updateUIThread(index, answerString));
if (playerAnswers[1-index] != -1) {
if (playerAnswers[1-index].getValue() != -1) {
sendResults();
if (round < 7) {
sendTextMessageDelayed("Round " + Integer.toString(round + 1), 2500);
......@@ -313,9 +323,9 @@ public class QuizUpActivity extends AppCompatActivity {
if (index == -1) {
correctAnswerTextView.setText("The correct answer: " + answers.get(correctAnswer));
} else {
playerAnswerTextViews.get(index).setText("The answer of " + playerNames[index] + ": " + msg);
playerAnswerTextViews.get(index).setText("The answer of " + playerAnswers[index].getPlayerName() + ": " + msg);
playerScoreTextViews.get(index).setText(
"The score of " + playerNames[index] + ": " + Integer.toString(playerScores[index]));
"The score of " + playerAnswers[index].getPlayerName() + ": " + Integer.toString(playerScores[index]));
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment