Commit 4e0694cc authored by fpeterfalvi's avatar fpeterfalvi
Browse files

KvizServer.GameActivity shows questions in a fragment.

GameControlFragment added.
Gradle version is modified. (Because of updating Android Studio.)
parent 42dc5bde
/KvizClient/.idea/vcs.xml
/KvizServer/.idea/vcs.xml
/KvizClient/.idea/misc.xml
/KvizClient/.idea/caches/build_file_checksums.ser
/KvizServer/.idea/codeStyles/Project.xml
/KvizServer/.idea/caches/build_file_checksums.ser
/KvizClient/.idea/codeStyles/Project.xml
......@@ -27,5 +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'
implementation 'com.google.code.gson:gson:2.8.2'
}
......@@ -95,7 +95,7 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
@Override
public void run() {
String[] params = msg.split("##");
if (params.length > 2) {
if (params.length > 1) {
replaceFragment(params);
}
}
......@@ -117,6 +117,8 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
} else if (strings[0].equals("answerG")) {
frag = AnsweredQuestionFragment.newInstance(strings[1], Integer.parseInt(strings[2]),
strings[3], Integer.parseInt(strings[4]));
} else if (strings[0].equals("message")) {
frag = MessageFragment.newInstance(strings[1]);
}
fragmentTransaction.replace(R.id.FragmentContainer, frag);
fragmentTransaction.commit();
......
......@@ -14,8 +14,7 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/DisplayTextView"
android:text="Ez a TextView nem tartozik a fragmenthez"/>
android:id="@+id/DisplayTextView"/>
<LinearLayout
android:layout_width="match_parent"
......
......@@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
......
#Thu Feb 15 17:43:04 CET 2018
#Mon Apr 23 18:29:22 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
......@@ -23,9 +23,10 @@ dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
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.github.rustamg:file-dialogs:1.0'
compile 'com.google.code.gson:gson:2.8.2'
implementation 'com.github.rustamg:file-dialogs:1.0'
implementation 'com.google.code.gson:gson:2.8.2'
}
package onlab.kvizserver;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
......@@ -22,39 +19,26 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import onlab.kvizserver.model.PlayerAnswer;
import onlab.kvizserver.model.Question;
public class GameActivity extends AppCompatActivity {
public class GameActivity extends AppCompatActivity implements GameControlFragment.OnFragmentInteractionListener {
private List<Question> questions;
private int questionIndex = 0;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
private int numberOfPlayers;
private final List<TextView> playerAnswerTextViews = new ArrayList<>();
private final List<String> answers = new ArrayList<>();
private PlayerAnswer[] playerAnswers;
private int correctAnswer;
private Handler updateConversationHandler;
private String questionString;
private TextView correctAnswerTextView;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
private Handler updateConversationHandler;
private Gson gson;
private GameControlFragment gameControlFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -67,6 +51,14 @@ public class GameActivity extends AppCompatActivity {
updateConversationHandler = new Handler();
numberOfPlayers = ClientHolder.size();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
gameControlFragment = GameControlFragment.newInstance(numberOfPlayers);
fragmentTransaction.replace(R.id.RootLayout, gameControlFragment);
fragmentTransaction.commit();
InputStream inputStream = null;
try {
if (questionFileName == null) {
......@@ -79,32 +71,6 @@ public class GameActivity extends AppCompatActivity {
}
questions = readQuestions(inputStream);
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));
answerTextViews.add((TextView) findViewById(R.id.Answer2TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer3TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer4TextView));
correctAnswerTextView = (TextView) findViewById(R.id.CorrectAnswerTextView);
final LinearLayout playerAnswersLinearLayout = (LinearLayout) findViewById(R.id.PlayerAnswers);
numberOfPlayers = ClientHolder.size();
playerAnswers = new PlayerAnswer[numberOfPlayers];
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = new PlayerAnswer(ClientHolder.get(i).getName());
}
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerAnswerTextView = new TextView(this);
playerAnswerTextView.setText("The answer of " + playerAnswers[i].getPlayerName() + ": ");
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswersLinearLayout.addView(playerAnswerTextView);
playerAnswerTextViews.add(playerAnswerTextView);
}
try {
for (int i=0;i<ClientHolder.size();i++) {
inputs.add(new BufferedReader(new InputStreamReader(ClientHolder.get(i).getClientsocket().getInputStream())));
......@@ -118,86 +84,10 @@ public class GameActivity extends AppCompatActivity {
e.printStackTrace();
}
nextQuestionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (questionIndex < questions.size()) {
Question question = questions.get(questionIndex);
questionString = question.getQuestionText();
questionText.setText(questionString);
correctAnswerTextView.setText("Correct answer: " + question.getCorrectAnswer());
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i].setValue(-1);
playerAnswers[i].setWinner(false);
}
answers.clear();
answers.addAll(question.getAnswers());
for (int i=0;i<4;i++) {
if (question.getType() == Question.MULTIPLE_CHOICE) {
answerTextViews.get(i).setText(answers.get(i));
} else {
answerTextViews.get(i).setText("");
}
}
String correct = question.getCorrectAnswer();
if (question.getType() == Question.MULTIPLE_CHOICE) {
for (int i=0;i<4;i++) {
if (answers.get(i).equals(correct)) {
correctAnswer = i;
}
}
} else {
correctAnswer = Integer.parseInt(correct);
}
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of " + playerAnswers[i].getPlayerName() + ": ");
if (question.getType() == Question.MULTIPLE_CHOICE) {
outputs.get(i).println("questionMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1");
} else {
outputs.get(i).println("questionG##" + questionString + "##-1");
}
}
questionIndex++;
} else {
questionText.setText("No more questions.");
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);
}
}
});
correctAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i<outputs.size();i++) {
if (answers.size() == 4) {
String playerAnswersString = gson.toJson(playerAnswers);
outputs.get(i).println("answerMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer));
} else {
calculateGuessQuestionWinner();
String playerAnswersString = gson.toJson(playerAnswers);
outputs.get(i).println("answerG##" + questionString + "##" + Integer.toString(correctAnswer)
+ "##" + playerAnswersString + "##" + Integer.toString(i));
}
}
}
});
playerAnswers = new PlayerAnswer[numberOfPlayers];
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = new PlayerAnswer(ClientHolder.get(i).getName());
}
for (int i=0;i<numberOfPlayers;i++) {
CommunicationThread commThread = new CommunicationThread(i);
......@@ -253,7 +143,8 @@ public class GameActivity extends AppCompatActivity {
@Override
public void run() {
playerAnswerTextViews.get(index).setText("The answer of " + playerAnswers[index].getPlayerName() + ": " + msg);
gameControlFragment.displayAnswerOfPlayer(index,
"The answer of " + playerAnswers[index].getPlayerName() + ": " + msg);
}
}
......@@ -289,4 +180,72 @@ public class GameActivity extends AppCompatActivity {
playerAnswers[indexOfBestAnswer].setWinner(true);
}
@Override
public void nextQuestionButtonClicked() {
if (questionIndex < questions.size()) {
Question question = questions.get(questionIndex);
questionString = question.getQuestionText();
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i].setValue(-1);
playerAnswers[i].setWinner(false);
}
answers.clear();
answers.addAll(question.getAnswers());
if (question.getType() == Question.MULTIPLE_CHOICE) {
gameControlFragment.displayMultipleChoiceQuestion(
questionString, answers, question.getCorrectAnswer());
} else {
gameControlFragment.displayGuessQuestion(questionString, question.getCorrectAnswer());
}
String correct = question.getCorrectAnswer();
if (question.getType() == Question.MULTIPLE_CHOICE) {
for (int i=0;i<4;i++) {
if (answers.get(i).equals(correct)) {
correctAnswer = i;
}
}
} else {
correctAnswer = Integer.parseInt(correct);
}
String playerAnswersString = gson.toJson(playerAnswers);
for (int i=0;i<outputs.size();i++) {
gameControlFragment.displayAnswerOfPlayer(i,"The answer of " + playerAnswers[i].getPlayerName() + ": ");
if (question.getType() == Question.MULTIPLE_CHOICE) {
outputs.get(i).println("questionMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##-1");
} else {
outputs.get(i).println("questionG##" + questionString + "##-1");
}
}
questionIndex++;
} else {
gameControlFragment.displayEndOfGame();
for (int i=0;i<outputs.size();i++) {
outputs.get(i).println("message##Játék vége");
}
}
}
@Override
public void correctAnswerButtonClicked() {
for (int i=0;i<outputs.size();i++) {
if (answers.size() == 4) {
String playerAnswersString = gson.toJson(playerAnswers);
outputs.get(i).println("answerMC##" + questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + playerAnswersString
+ "##" + Integer.toString(i) + "##" + Integer.toString(correctAnswer));
} else {
calculateGuessQuestionWinner();
String playerAnswersString = gson.toJson(playerAnswers);
outputs.get(i).println("answerG##" + questionString + "##" + Integer.toString(correctAnswer)
+ "##" + playerAnswersString + "##" + Integer.toString(i));
}
}
}
}
package onlab.kvizserver;
import android.app.Activity;
import android.app.Fragment;
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.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class GameControlFragment extends Fragment {
private static final String NUMBER_OF_PLAYERS = "NUMBER_OF_PLAYERS";
private int numberOfPlayers;
private OnFragmentInteractionListener mListener;
private TextView correctAnswerTextView;
private final List<TextView> playerAnswerTextViews = new ArrayList<>();
private TextView questionText;
private final List<TextView> answerTextViews = new ArrayList<>();
private Button nextQuestionBtn;
private Button correctAnswerButton;
public GameControlFragment() {
// Required empty public constructor
}
public static GameControlFragment newInstance(int numberOfPlayers) {
GameControlFragment fragment = new GameControlFragment();
Bundle args = new Bundle();
args.putInt(NUMBER_OF_PLAYERS, numberOfPlayers);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
numberOfPlayers = getArguments().getInt(NUMBER_OF_PLAYERS);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_game_control, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
nextQuestionBtn = (Button) getView().findViewById(R.id.NextQuestion);
correctAnswerButton = (Button) getView().findViewById(R.id.CorrectAnswer);
questionText = (TextView) getView().findViewById(R.id.QuestionText);
answerTextViews.add((TextView) getView().findViewById(R.id.Answer1TextView));
answerTextViews.add((TextView) getView().findViewById(R.id.Answer2TextView));
answerTextViews.add((TextView) getView().findViewById(R.id.Answer3TextView));
answerTextViews.add((TextView) getView().findViewById(R.id.Answer4TextView));
correctAnswerTextView = (TextView) getView().findViewById(R.id.CorrectAnswerTextView);
final LinearLayout playerAnswersLinearLayout = (LinearLayout) getView().findViewById(R.id.PlayerAnswers);
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerAnswerTextView = new TextView(getActivity());
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswersLinearLayout.addView(playerAnswerTextView);
playerAnswerTextViews.add(playerAnswerTextView);
}
correctAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.correctAnswerButtonClicked();
}
});
nextQuestionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.nextQuestionButtonClicked();
}
});
}
public void displayMultipleChoiceQuestion(String questionTextString, List<String> answers, String correctAnswerString) {
questionText.setText(questionTextString);
correctAnswerTextView.setText(correctAnswerString);
for (int i=0;i<4;i++) {
answerTextViews.get(i).setVisibility(View.VISIBLE);
answerTextViews.get(i).setText(answers.get(i));
}
}
public void displayGuessQuestion(String questionTextString, String correctAnswerString) {
questionText.setText(questionTextString);
correctAnswerTextView.setText("Correct answer: " + correctAnswerString);
for (int i=0;i<4;i++) {
answerTextViews.get(i).setVisibility(View.GONE);
}
}
public void displayEndOfGame() {
questionText.setText("No more questions.");
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);
}
public void displayAnswerOfPlayer(int index, String answer) {
playerAnswerTextViews.get(index).setText(answer);
}
@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 {
public void nextQuestionButtonClicked();
public void correctAnswerButtonClicked();
}
}
......@@ -9,65 +9,8 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/QuestionText"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer1TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer2TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer3TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer4TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CorrectAnswerTextView"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/PlayerAnswers">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Question"
android:id="@+id/NextQuestion"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show correct answer to players"
android:id="@+id/CorrectAnswer"