Commit 61221149 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Answering time limit added to MultipleChoiceFragment

If a time limit is set, a progress bar shows the remaining time and the fragment lets answering only during this limited time.
parent dae42bf0
......@@ -107,18 +107,18 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
}
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]));
Integer.parseInt(strings[5]), Integer.parseInt(strings[6]), Integer.parseInt(strings[7]), -1);
fragmentTransaction.replace(R.id.FragmentContainer, frag);
fragmentTransaction.commit();
}
@Override
public void onAnswered(String answer) {
public void onAnswered(int answer) {
Date questionAnsweredDate = new Date();
double answerTime = (double)(questionAnsweredDate.getTime() - questionReceivedDate.getTime()) / 1000;
Log.d("A válasz: ", answer + " (" + Double.toString(answerTime) + "s)");
Log.d("A válasz: ", Integer.toString(answer) + " (" + Double.toString(answerTime) + "s)");
displayTextView.setText(answer + " (" + Double.toString(answerTime) + "s)");
output.println(answer + "##" + Double.toString(answerTime));
output.println(Integer.toString(answer) + "##" + Double.toString(answerTime));
}
......
......@@ -2,18 +2,19 @@ package onlab.kvizclient;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
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.ProgressBar;
import android.widget.TextView;
import java.util.Date;
public class MultipleChoiceFragment extends Fragment {
......@@ -25,12 +26,14 @@ public class MultipleChoiceFragment extends Fragment {
private static final String OWN_ANSWER = "OWN_ANSWER";
private static final String OPPONENT_ANSWER = "OPPONENT_ANSWER";
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 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
private TextView questionTextTextView;
private Button[] answerButtons = new Button[4];
......@@ -39,13 +42,15 @@ public class MultipleChoiceFragment extends Fragment {
private boolean answered = false;
private ProgressBar progressBar;
public MultipleChoiceFragment() {
// Required empty public constructor
}
public static MultipleChoiceFragment newInstance(String question, String answer1, String answer2,
String answer3, String answer4, int own_answer,
int opponent_answer, int correct_answer) {
int opponent_answer, int correct_answer, int timeout) {
MultipleChoiceFragment fragment = new MultipleChoiceFragment();
Bundle args = new Bundle();
args.putString(QUESTION, question);
......@@ -56,6 +61,7 @@ public class MultipleChoiceFragment extends Fragment {
args.putInt(OWN_ANSWER, own_answer);
args.putInt(OPPONENT_ANSWER, opponent_answer);
args.putInt(CORRECT_ANSWER, correct_answer);
args.putInt(TIMEOUT, timeout);
fragment.setArguments(args);
return fragment;
}
......@@ -72,6 +78,7 @@ public class MultipleChoiceFragment extends Fragment {
own_answer = getArguments().getInt(OWN_ANSWER);
opponent_answer = getArguments().getInt(OPPONENT_ANSWER);
correct_answer = getArguments().getInt(CORRECT_ANSWER);
timeout = getArguments().getInt(TIMEOUT);
}
}
......@@ -112,27 +119,36 @@ public class MultipleChoiceFragment extends Fragment {
answerButtons[i].setText(answers[i]);
}
if (correct_answer != -1) {
if (correct_answer > -1) {
answerButtons[correct_answer].setBackgroundColor(Color.GREEN);
}
if (own_answer != correct_answer && own_answer != -1) {
if (own_answer != correct_answer && own_answer > -1) {
answerButtons[own_answer].setBackgroundColor(Color.RED);
}
if (opponent_answer != correct_answer && opponent_answer != -1) {
if (opponent_answer != correct_answer && opponent_answer > -1) {
answerButtons[opponent_answer].setBackgroundColor(Color.RED);
}
if (own_answer != -1) {
if (own_answer > -1) {
answered = true;
leftImageViews[own_answer].setImageResource(R.drawable.ic_keyboard_arrow_right_black_48dp);
}
if (opponent_answer != -1) {
if (opponent_answer > -1) {
rightImageViews[opponent_answer].setImageResource(R.drawable.ic_keyboard_arrow_left_black_48dp);
}
progressBar = (ProgressBar) getView().findViewById(R.id.determinateBar);
if (timeout <= 0) {
((ViewManager)progressBar.getParent()).removeView(progressBar);
} else {
ProgressBarThread pbt = new ProgressBarThread();
Thread progressBarThread = new Thread(pbt);
progressBarThread.start();
}
}
@Override
......@@ -153,7 +169,7 @@ public class MultipleChoiceFragment extends Fragment {
}
public interface OnFragmentInteractionListener {
void onAnswered(String answer);
void onAnswered(int answer);
}
class MyOnClickListener implements View.OnClickListener {
......@@ -168,11 +184,42 @@ public class MultipleChoiceFragment extends Fragment {
if (!answered) {
answered = true;
if (mListener != null) {
mListener.onAnswered(answerButtons[index].getText().toString());
mListener.onAnswered(index);
Log.d("FRAGMENT", "Válasz történt");
}
}
}
};
class ProgressBarThread implements Runnable {
private long startTime;
public ProgressBarThread() {
startTime = new Date().getTime();
}
@Override
public void run() {
long currentTime = new Date().getTime();
while (currentTime - startTime < timeout && !answered) {
progressBar.setProgress((int)(100 - 100 * (currentTime - startTime) / timeout));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentTime = new Date().getTime();
}
if (!answered) {
answered = true;
progressBar.setProgress(0);
if (mListener != null) {
mListener.onAnswered(-2);
Log.d("FRAGMENT", "Letelt az idő");
}
}
}
}
}
......@@ -45,6 +45,8 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
ownScoreTextView = (TextView) findViewById(R.id.OwnScore);
opponentScoreTextView = (TextView) findViewById(R.id.OpponentScore);
replaceFragment(new String[]{"message", "0", "0", "Rögtön kezdődik a játék!"});
try {
input = new BufferedReader(new InputStreamReader(ServerHolder.getSocket().getInputStream()));
output = new PrintWriter(new BufferedWriter(
......@@ -92,7 +94,7 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
@Override
public void run() {
String[] params = msg.split("##");
if (params[0].equals("question") || params[0].equals("message")) {
if (params[0].equals("question") || params[0].equals("message") || params[0].equals("answer")) {
ownScoreTextView.setText(params[1]);
opponentScoreTextView.setText(params[2]);
replaceFragment(params);
......@@ -105,12 +107,13 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
FragmentTransaction fragmentTransaction = fm.beginTransaction();
Fragment frag;
if (strings[0].equals("question")) {
if (Integer.parseInt(strings[8]) == -1) {
questionReceivedDate = new Date();
}
questionReceivedDate = new Date();
}
if (strings[0].equals("question") || strings[0].equals("answer")) {
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]));
Integer.parseInt(strings[8]), Integer.parseInt(strings[9]), Integer.parseInt(strings[10]), timeout);
} else {
frag = MessageFragment.newInstance(strings[3]);
}
......@@ -120,11 +123,11 @@ public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceF
}
@Override
public void onAnswered(String answer) {
public void onAnswered(int answer) {
Date questionAnsweredDate = new Date();
double answerTime = (double)(questionAnsweredDate.getTime() - questionReceivedDate.getTime()) / 1000;
Log.d("A válasz: ", answer + " (" + Double.toString(answerTime) + "s)");
output.println(answer + "##" + Double.toString(answerTime));
Log.d("A válasz: ", Integer.toString(answer) + " (" + Double.toString(answerTime) + "s)");
output.println(Integer.toString(answer) + "##" + Double.toString(answerTime));
}
......
......@@ -9,15 +9,22 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
<ProgressBar
android:id="@+id/determinateBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:progress="100"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/QuestionTextTextView"
android:text="Kérdés"
android:textSize="20sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
......
......@@ -194,13 +194,10 @@ public class GameActivity extends AppCompatActivity {
if (read != null && !read.equals("$$$$")) {
Log.d("GameActivity", "Klienstől kapott üzenet: " + read);
String[] strings = read.split("##");
for (int i=0;i<4;i++) {
if (strings[0].equals(answers.get(i))) {
playerAnswers[index] = i;
}
}
int answerInt = Integer.parseInt(strings[0]);
playerAnswers[index] = answerInt;
updateConversationHandler.post(new GameActivity.updateUIThread(index,
strings[0] + " (" + strings[1] + "s)"));
answers.get(answerInt) + " (" + strings[1] + "s)"));
}
} catch (IOException e) {
e.printStackTrace();
......
......@@ -38,6 +38,8 @@ public class QuizUpActivity extends AppCompatActivity {
final List<TextView> playerAnswerTextViews = new ArrayList<>();
final List<TextView> playerScoreTextViews = new ArrayList<>();
final List<String> answers = new ArrayList<>();
int[] playerAnswers;
......@@ -103,6 +105,14 @@ public class QuizUpActivity extends AppCompatActivity {
playerAnswerTextViews.add(playerAnswerTextView);
}
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerScoreTextView = new TextView(this);
playerScoreTextView.setText("The score of the " + Integer.toString(i + 1) + ". player: 0");
playerScoreTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswersLinearLayout.addView(playerScoreTextView);
playerScoreTextViews.add(playerScoreTextView);
}
try {
for (int i=0;i<ClientHolder.size();i++) {
inputs.add(new BufferedReader(new InputStreamReader(ClientHolder.get(i).getClientsocket().getInputStream())));
......@@ -179,7 +189,7 @@ public class QuizUpActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
for (int i=0;i<outputs.size();i++) {
String message = "question##" + Integer.toString(playerScores[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])
......@@ -218,16 +228,13 @@ public class QuizUpActivity extends AppCompatActivity {
if (read != null && !read.equals("$$$$")) {
Log.d("GameActivity", Integer.toString(index) + "-es klienstől kapott üzenet: " + read);
String[] strings = read.split("##");
for (int i=0;i<4;i++) {
if (strings[0].equals(answers.get(i))) {
playerAnswers[index] = i;
}
}
int answerInt = Integer.parseInt(strings[0]);
playerAnswers[index] = answerInt;
if (playerAnswers[index] == correctAnswer) {
playerScores[index] += (int) (21 - Double.parseDouble(strings[1]));
}
updateConversationHandler.post(new QuizUpActivity.updateUIThread(index,
strings[0] + " (" + strings[1] + "s)"));
String answerString = (answerInt == -2) ? "nem válaszolt" : (answers.get(answerInt) + " (" + strings[1] + "s)");
updateConversationHandler.post(new QuizUpActivity.updateUIThread(index, answerString));
}
} catch (IOException e) {
e.printStackTrace();
......@@ -249,6 +256,8 @@ public class QuizUpActivity extends AppCompatActivity {
@Override
public void run() {
playerAnswerTextViews.get(index).setText("The answer of the " + Integer.toString(index + 1) + ". player: " + msg);
playerScoreTextViews.get(index).setText(
"The score of the " + Integer.toString(index + 1) + ". player: " + 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