Commit 42dc5bde authored by fpeterfalvi's avatar fpeterfalvi
Browse files

GameActivity can handle guess questions.

GuessQuestionFragment and AnsweredQuestionFragment added.
parent 51e9a0a8
package onlab.kvizclient;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
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.List;
import onlab.kvizclient.model.PlayerAnswer;
public class AnsweredQuestionFragment extends Fragment {
private static final String QUESTION_TEXT = "QUESTION_TEXT";
private static final String PLAYER_ANSWERS = "PLAYER_ANSWERS";
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 String questionText;
private String playerAnswersString;
private List<PlayerAnswer> playerAnswers;
private int own_index;
private int correct_answer;
public AnsweredQuestionFragment() {
// Required empty public constructor
}
public static AnsweredQuestionFragment newInstance(String questionText, int correct_answer, String playerAnswers, int own_index) {
AnsweredQuestionFragment fragment = new AnsweredQuestionFragment();
Bundle args = new Bundle();
args.putString(QUESTION_TEXT, questionText);
args.putString(PLAYER_ANSWERS, playerAnswers);
args.putInt(CORRECT_ANSWER, correct_answer);
args.putInt(OWN_INDEX, own_index);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
questionText = getArguments().getString(QUESTION_TEXT);
playerAnswersString = getArguments().getString(PLAYER_ANSWERS);
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);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_answered_question, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView questionTextTextView = (TextView) getView().findViewById(R.id.AQFQuestionTextTextView);
questionTextTextView.setText(questionText);
TextView correctAnswerTextView = (TextView) getView().findViewById(R.id.AQFCorrectAnswerTextView);
correctAnswerTextView.setText("Correct answer: " + Integer.toString(correct_answer));
TextView ownAnswerTextView = (TextView) getView().findViewById(R.id.OwnAnswerTextView);
ownAnswerTextView.setText("Your answer: " + Integer.toString(playerAnswers.get(own_index).getValue())
+ " (" + Double.toString(playerAnswers.get(own_index).getTime()) + "s)");
if (playerAnswers.get(own_index).isWinner()) {
ownAnswerTextView.setTextColor(Color.GREEN);
ownAnswerTextView.setTextColor(Color.rgb(31, 114, 70));
ownAnswerTextView.setTypeface(null, Typeface.BOLD);
}
LinearLayout containerLinearLayout = getView().findViewById(R.id.ContainerLinearLayout);
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() + ": "
+ Integer.toString(answer.getValue()) + " ("
+ Double.toString(answer.getTime()) + "s)");
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
playerAnswerTextView.setPadding(20,30,20,0);
if (answer.isWinner()) {
playerAnswerTextView.setTextColor(Color.rgb(31, 114, 70));
playerAnswerTextView.setTypeface(null, Typeface.BOLD);
}
containerLinearLayout.addView(playerAnswerTextView);
}
}
}
}
package onlab.kvizclient;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Handler;
......@@ -23,7 +24,8 @@ import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
public class GameActivity extends AppCompatActivity implements MultipleChoiceFragment.OnFragmentInteractionListener {
public class GameActivity extends AppCompatActivity implements MultipleChoiceFragment.OnFragmentInteractionListener,
GuessQuestionFragment.OnFragmentInteractionListener {
private TextView displayTextView;
......@@ -93,7 +95,7 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
@Override
public void run() {
String[] params = msg.split("##");
if (params.length == 9) {
if (params.length > 2) {
replaceFragment(params);
}
}
......@@ -102,12 +104,20 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
private void replaceFragment(String[] strings) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (strings[0].equals("question")) {
if (strings[0].equals("questionMC") || strings[0].equals("questionG")) {
questionReceivedDate = new Date();
}
MultipleChoiceFragment frag = MultipleChoiceFragment.newInstance(
strings[1], strings[2], strings[3], strings[4], strings[5],
strings[6], Integer.parseInt(strings[7]), Integer.parseInt(strings[8]), -1);
Fragment frag = null;
if (strings[0].equals("questionMC") || strings[0].equals("answerMC")) {
frag = MultipleChoiceFragment.newInstance(
strings[1], strings[2], strings[3], strings[4], strings[5],
strings[6], Integer.parseInt(strings[7]), Integer.parseInt(strings[8]), -1);
} else if (strings[0].equals("questionG")) {
frag = GuessQuestionFragment.newInstance(strings[1], Integer.parseInt(strings[2]));
} else if (strings[0].equals("answerG")) {
frag = AnsweredQuestionFragment.newInstance(strings[1], Integer.parseInt(strings[2]),
strings[3], Integer.parseInt(strings[4]));
}
fragmentTransaction.replace(R.id.FragmentContainer, frag);
fragmentTransaction.commit();
}
......
package onlab.kvizclient;
import android.app.Activity;
import android.app.Fragment;
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.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Date;
public class GuessQuestionFragment extends Fragment {
private static final String QUESTION = "QUESTION";
private static final String TIMEOUT = "TIMEOUT";
private String question;
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 TextView answerTextView;
private OnFragmentInteractionListener mListener;
private String currentAnswerString = "";
private boolean answered = false;
private ProgressBar progressBar;
public GuessQuestionFragment() {
// Required empty public constructor
}
public static GuessQuestionFragment newInstance(String question, int timeout) {
GuessQuestionFragment fragment = new GuessQuestionFragment();
Bundle args = new Bundle();
args.putString(QUESTION, question);
args.putInt(TIMEOUT, timeout);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
question = getArguments().getString(QUESTION);
timeout = getArguments().getInt(TIMEOUT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_guess_question, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
questionTextTextView = (TextView) getView().findViewById(R.id.QuestionTextTextView);
questionTextTextView.setText(question);
answerTextView = (TextView) getView().findViewById(R.id.AnswerTextView);
Button[] numberButtons = new Button[10];
numberButtons[0] = (Button) getView().findViewById(R.id.Number0Button);
numberButtons[1] = (Button) getView().findViewById(R.id.Number1Button);
numberButtons[2] = (Button) getView().findViewById(R.id.Number2Button);
numberButtons[3] = (Button) getView().findViewById(R.id.Number3Button);
numberButtons[4] = (Button) getView().findViewById(R.id.Number4Button);
numberButtons[5] = (Button) getView().findViewById(R.id.Number5Button);
numberButtons[6] = (Button) getView().findViewById(R.id.Number6Button);
numberButtons[7] = (Button) getView().findViewById(R.id.Number7Button);
numberButtons[8] = (Button) getView().findViewById(R.id.Number8Button);
numberButtons[9] = (Button) getView().findViewById(R.id.Number9Button);
for (int i=0;i<10;i++) {
numberButtons[i].setOnClickListener(new NumberButtonOnClickListener(i));
}
ImageButton backSpaceButton = (ImageButton) getView().findViewById(R.id.BackSpaceButton);
backSpaceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!answered && currentAnswerString.length() > 0) {
currentAnswerString = currentAnswerString.substring(0,currentAnswerString.length()-1);
if (currentAnswerString.length() == 0) {
answerTextView.setText(" ");
} else {
answerTextView.setText(currentAnswerString);
}
}
}
});
ImageButton nextButton = (ImageButton) getView().findViewById(R.id.NextButton);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!answered) {
answered = true;
if (mListener != null) {
int answerInt;
if (currentAnswerString.length() == 0) {
answerInt = 0;
} else {
answerInt = Integer.parseInt(currentAnswerString);
}
mListener.onAnswered(answerInt);
Log.d("FRAGMENT", "Válasz történt");
}
}
}
});
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
public void onAttach(Activity context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onAnswered(int answer);
}
class NumberButtonOnClickListener implements View.OnClickListener {
int index;
public NumberButtonOnClickListener(int index) {
this.index = index;
}
@Override
public void onClick(View v) {
if (!answered && currentAnswerString.length() < 9 && !(currentAnswerString.length() == 0 && index == 0)) {
currentAnswerString += index;
answerTextView.setText(currentAnswerString);
}
}
};
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ő");
}
}
}
}
}
......@@ -2,8 +2,9 @@ package onlab.kvizclient.model;
public class PlayerAnswer {
private String playerName;
private int value;
private double time;
private int value = -1;
private double time = 0;
private boolean winner = false;
public PlayerAnswer(String playerName) {
this.playerName = playerName;
......@@ -32,4 +33,12 @@ public class PlayerAnswer {
public void setTime(double time) {
this.time = time;
}
public boolean isWinner() {
return winner;
}
public void setWinner(boolean winner) {
this.winner = winner;
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="onlab.kvizclient.AnsweredQuestionFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ContainerLinearLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/AQFQuestionTextTextView"
android:textSize="34sp"
android:layout_gravity="center"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/AQFCorrectAnswerTextView"
android:textSize="30sp"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:paddingRight="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/OwnAnswerTextView"
android:textSize="30sp"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:paddingRight="20dp"/>
</LinearLayout>
</FrameLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="onlab.kvizclient.GuessQuestionFragment">
<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="match_parent"
android:layout_height="wrap_content"
android:id="@+id/QuestionTextTextView"
android:textSize="34sp"
android:layout_gravity="center"
android:gravity="center"
android:text="Itt lesz a kérdés"/>
<TextView
android:textSize="40sp"
android:id="@+id/AnswerTextView"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="5"
android:rowCount="3">
<Button
android:text="7"
android:layout_width="60dp"
android:textSize="40sp"
android:id="@+id/Number7Button"/>
<Button
android:text="8"
android:layout_width="60dp"
android:textSize="40sp"
android:id="@+id/Number8Button"/>
<Button
android:text="9"
android:layout_width="60dp"
android:textSize="40sp"
android:id="@+id/Number9Button"/>
<Button
android:text="0"
android:layout_width="60dp"
android:textSize="40sp"
android:id="@+id/Number0Button"/>
<ImageButton
android:layout_width="60dp"
android:src="@drawable/ic_keyboard_backspace_black_48dp"
android:id="@+id/BackSpaceButton"
android:layout_gravity="fill_vertical"/>
<Button
android:layout_width="60dp"
android:text="4"
android:id="@+id/Number4Button"
android:textSize="40sp"/>
<Button
android:layout_width="60dp"
android:text="5"
android:id="@+id/Number5Button"
android:textSize="40sp"/>
<Button
android:layout_width="60dp"
android:text="6"
android:id="@+id/Number6Button"
android:textSize="40sp"/>
<ImageButton
android:layout_width="120dp"
android:src="@drawable/ic_play_arrow_black_48dp"
android:id="@+id/NextButton"
android:layout_columnSpan="2"
android:layout_rowSpan="2"
android:layout_gravity="fill_vertical"/>
<Button
android:layout_width="60dp"
android:text="1"
android:id="@+id/Number1Button"
android:textSize="40sp"/>
<Button
android:layout_width="60dp"
android:text="2"
android:id="@+id/Number2Button"
android:textSize="40sp"/>
<Button
android:layout_width="60dp"
android:text="3"
android:id="@+id/Number3Button"
android:textSize="40sp"/>
</GridLayout>
</LinearLayout>