diff --git a/KvizClient/app/build.gradle b/KvizClient/app/build.gradle index eab076a5fab314780a424320882592a5431421fd..8c468969ea529bae9276091ff8f24a3a57b69837 100644 --- a/KvizClient/app/build.gradle +++ b/KvizClient/app/build.gradle @@ -23,6 +23,7 @@ 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' diff --git a/KvizClient/app/src/main/java/onlab/kvizclient/GameActivity.java b/KvizClient/app/src/main/java/onlab/kvizclient/GameActivity.java index 831898c223fc296c1ef335591cd8b3b8eac812ed..2dcd4313a626c2ee58900fc960b4aac0afebd073 100644 --- a/KvizClient/app/src/main/java/onlab/kvizclient/GameActivity.java +++ b/KvizClient/app/src/main/java/onlab/kvizclient/GameActivity.java @@ -1,5 +1,7 @@ package onlab.kvizclient; +import android.app.FragmentManager; +import android.app.FragmentTransaction; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -19,9 +21,9 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.UnknownHostException; -public class GameActivity extends AppCompatActivity { +public class GameActivity extends AppCompatActivity implements MultipleChoiceFragment.OnFragmentInteractionListener { - private TextView questionText; + private TextView displayTextView; private BufferedReader input; @@ -29,14 +31,15 @@ public class GameActivity extends AppCompatActivity { Handler updateConversationHandler; + Thread commThread; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); - questionText = (TextView) findViewById(R.id.QuestionTextView); - final EditText answerEditText = (EditText) findViewById(R.id.AnswerEditText); - Button sendAnswerButton = (Button) findViewById(R.id.SendAnswerButton); + displayTextView = (TextView) findViewById(R.id.DisplayTextView); + updateConversationHandler = new Handler(); try { @@ -48,16 +51,15 @@ public class GameActivity extends AppCompatActivity { e.printStackTrace(); } - sendAnswerButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - output.println(answerEditText.getText()); - } - }); + CommunicationThread ct = new CommunicationThread(); + commThread = new Thread(ct); + commThread.start(); + } - CommunicationThread commThread = new CommunicationThread(); - Thread newthread = new Thread(commThread); - newthread.start(); + @Override + protected void onDestroy() { + commThread.interrupt(); + super.onDestroy(); } class CommunicationThread implements Runnable { @@ -66,10 +68,9 @@ public class GameActivity extends AppCompatActivity { while (!Thread.currentThread().isInterrupted()) { try { String read = input.readLine(); + Log.d("GameActivity", "Szervertől kapott üzenet: " + read); if (read != null && !read.equals("$$$$")) { - Log.d("GameActivity", "Szervertől kapott üzenet: " + read); updateConversationHandler.post(new GameActivity.updateUIThread(read)); - //String params[] = read.split("##"); } } catch (IOException e) { Thread.currentThread().interrupt(); @@ -87,8 +88,27 @@ public class GameActivity extends AppCompatActivity { @Override public void run() { - questionText.setText(msg); + String[] params = msg.split("##"); + if (params.length == 5) { + replaceFragment(params); + } } } + private void replaceFragment(String[] strings) { + FragmentManager fm = getFragmentManager(); + FragmentTransaction fragmentTransaction = fm.beginTransaction(); + MultipleChoiceFragment frag = MultipleChoiceFragment.newInstance(strings[0], strings[1], strings[2], strings[3], strings[4]); + fragmentTransaction.replace(R.id.FragmentContainer, frag); + fragmentTransaction.commit(); + } + + @Override + public void onAnswered(String answer) { + Log.d("A válasz: ", answer); + displayTextView.setText(answer); + output.println(answer); + } + + } diff --git a/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java b/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java index 8e07e8655f9fd2e0c63225d836fae9eab5489e44..eeb174033f666b0e508a36851355f849c32585a5 100644 --- a/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java +++ b/KvizClient/app/src/main/java/onlab/kvizclient/LobbyActivity.java @@ -98,6 +98,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection @Override protected void onDestroy() { + Log.d("LobbyActivity", "Destroyed"); if (mNsdManager != null && discovering) { mNsdManager.stopServiceDiscovery(mDiscoveryListener); discovering = false; @@ -294,7 +295,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection } catch (Exception e) { e.printStackTrace(); } - while (!Thread.currentThread().isInterrupted()) { + while (!Thread.currentThread().isInterrupted() && exit) { try { if (serverModel.getSocket().isConnected()) { Log.d("LobbyActivity", "Connected"); diff --git a/KvizClient/app/src/main/java/onlab/kvizclient/MultipleChoiceFragment.java b/KvizClient/app/src/main/java/onlab/kvizclient/MultipleChoiceFragment.java new file mode 100644 index 0000000000000000000000000000000000000000..a67aaab6ec7cabe265952a59ad98f6090ca506c6 --- /dev/null +++ b/KvizClient/app/src/main/java/onlab/kvizclient/MultipleChoiceFragment.java @@ -0,0 +1,123 @@ +package onlab.kvizclient; + +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +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.widget.Button; +import android.widget.TextView; + +public class MultipleChoiceFragment extends Fragment { + + private static final String QUESTION = "QUESTION"; + private static final String ANSWER1 = "ANSWER1"; + private static final String ANSWER2 = "ANSWER2"; + private static final String ANSWER3 = "ANSWER3"; + private static final String ANSWER4 = "ANSWER4"; + + private String question; + private String[] answers = new String[4]; + + private TextView questionTextTextView; + private Button[] answerButtons = new Button[4]; + + private OnFragmentInteractionListener mListener; + + public MultipleChoiceFragment() { + // Required empty public constructor + } + + public static MultipleChoiceFragment newInstance(String question, String answer1, String answer2, String answer3, String answer4) { + MultipleChoiceFragment fragment = new MultipleChoiceFragment(); + Bundle args = new Bundle(); + args.putString(QUESTION, question); + args.putString(ANSWER1, answer1); + args.putString(ANSWER2, answer2); + args.putString(ANSWER3, answer3); + args.putString(ANSWER4, answer4); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + question = getArguments().getString(QUESTION); + answers[0] = getArguments().getString(ANSWER1); + answers[1] = getArguments().getString(ANSWER2); + answers[2] = getArguments().getString(ANSWER3); + answers[3] = getArguments().getString(ANSWER4); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + + return inflater.inflate(R.layout.fragment_multiple_choice, container, false); + } + + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + questionTextTextView = (TextView) getView().findViewById(R.id.QuestionTextTextView); + + questionTextTextView.setText(question); + + answerButtons[0] = (Button) getView().findViewById(R.id.Answer1Button); + answerButtons[1] = (Button) getView().findViewById(R.id.Answer2Button); + answerButtons[2] = (Button) getView().findViewById(R.id.Answer3Button); + answerButtons[3] = (Button) getView().findViewById(R.id.Answer4Button); + + for (int i=0;i<4;i++) { + answerButtons[i].setOnClickListener(new MyOnClickListener(i)); + answerButtons[i].setText(answers[i]); + } + } + + @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(String answer); + } + + class MyOnClickListener implements View.OnClickListener { + + int index; + public MyOnClickListener(int index) { + this.index = index; + } + + @Override + public void onClick(View v) { + if (mListener != null) { + mListener.onAnswered(answerButtons[index].getText().toString()); + Log.d("FRAGMENT", "Válasz történt"); + } + } + + }; +} diff --git a/KvizClient/app/src/main/res/layout/activity_game.xml b/KvizClient/app/src/main/res/layout/activity_game.xml index 798ccbb80fd81f024ffb62746fccbecbb2b06147..2a42a64c5d7167977ca5014b7b4bd945e5f5fa4b 100644 --- a/KvizClient/app/src/main/res/layout/activity_game.xml +++ b/KvizClient/app/src/main/res/layout/activity_game.xml @@ -14,20 +14,16 @@ <TextView android:layout_width="match_parent" android:layout_height="wrap_content" - android:id="@+id/QuestionTextView" - android:text="Ide jön a kérdés"/> + android:id="@+id/DisplayTextView" + android:text="Ez a TextView nem tartozik a fragmenthez"/> - <EditText + <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:id="@+id/AnswerEditText" - android:text="Ide írd a választ"/> + android:orientation="vertical" + android:id="@+id/FragmentContainer"> - <Button - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:id="@+id/SendAnswerButton" - android:text="Send"/> + </LinearLayout> </LinearLayout> diff --git a/KvizClient/app/src/main/res/layout/fragment_multiple_choice.xml b/KvizClient/app/src/main/res/layout/fragment_multiple_choice.xml new file mode 100644 index 0000000000000000000000000000000000000000..89c38a0d75e528becbcacc9742b3c19c6d4b9bb3 --- /dev/null +++ b/KvizClient/app/src/main/res/layout/fragment_multiple_choice.xml @@ -0,0 +1,49 @@ +<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.MultipleChoiceFragment"> + +<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/QuestionTextTextView" + android:text="Kérdés" + android:textSize="20sp"/> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/Answer1Button" + android:text="Válasz1" + android:textSize="20sp"/> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/Answer2Button" + android:text="Válasz2" + android:textSize="20sp"/> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/Answer3Button" + android:text="Válasz3" + android:textSize="20sp"/> + + <Button + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/Answer4Button" + android:text="Válasz4" + android:textSize="20sp"/> + +</LinearLayout> + +</FrameLayout> diff --git a/KvizServer/app/src/main/assets/multiple.txt b/KvizServer/app/src/main/assets/multiple.txt new file mode 100644 index 0000000000000000000000000000000000000000..ec39d326f8e22dc45bc9505f43df6fdd78bedcff --- /dev/null +++ b/KvizServer/app/src/main/assets/multiple.txt @@ -0,0 +1,8 @@ +Melyik hónap 31 napos? július február június november +Mi Olaszország fővárosa? Róma London Berlin Moszkva +Melyik nem emlős? szula oroszlán farkas rozmár +Melyik kontinensen található Peru? Dél-Amerika Észak-Amerika Ázsia Afrika +Melyik város van legtávolabb Budapesttől? Sydney Makó Jeruzsálem London +Melyik nem trapéz? deltoid paralelogramma négyzet téglalap +Melyik NEM gázbolygó? Föld Jupiter Uránusz Neptunusz +Melyik folyó partján fekszik Budapest? Duna Jangce Nílus Szajna diff --git a/KvizServer/app/src/main/java/onlab/kvizserver/GameActivity.java b/KvizServer/app/src/main/java/onlab/kvizserver/GameActivity.java index 10753a6553be57a53d1371d1eef3e95a4a0131cf..a7c092c9ac9d60a89fdbb577ac9582ba888e70fe 100644 --- a/KvizServer/app/src/main/java/onlab/kvizserver/GameActivity.java +++ b/KvizServer/app/src/main/java/onlab/kvizserver/GameActivity.java @@ -26,10 +26,11 @@ import java.util.List; import onlab.kvizserver.model.ClientModel; import onlab.kvizserver.model.GuessQuestion; +import onlab.kvizserver.model.MultipleChoiceQuestion; public class GameActivity extends AppCompatActivity { - private List<GuessQuestion> questions = new ArrayList<>(); + private List<MultipleChoiceQuestion> questions = new ArrayList<>(); private int questionIndex = 0; @@ -41,6 +42,8 @@ public class GameActivity extends AppCompatActivity { final List<TextView> playerAnswerTextViews = new ArrayList<>(); + final List<String> answers = new ArrayList<>(); + Handler updateConversationHandler; @Override @@ -55,15 +58,16 @@ public class GameActivity extends AppCompatActivity { BufferedReader buffreader; try { if (questionFileName == null) { - buffreader = new BufferedReader(new InputStreamReader(getAssets().open("defaultQuestions.txt"))); + buffreader = new BufferedReader(new InputStreamReader(getAssets().open("multiple.txt"))); } else { buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(questionFileName)))); } String line; while ((line = buffreader.readLine()) != null) { String[] parts = line.split("\t"); - GuessQuestion guessQuestion = new GuessQuestion(parts[0], Integer.parseInt(parts[1])); - questions.add(guessQuestion); + MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion( + parts[0], parts[1], parts[2], parts[3], parts[4]); + questions.add(multipleChoiceQuestion); } } catch (IOException e) { e.printStackTrace(); @@ -71,7 +75,11 @@ public class GameActivity extends AppCompatActivity { Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion); final TextView questionText = (TextView) findViewById(R.id.QuestionText); - final TextView correctAnswerText = (TextView) findViewById(R.id.CorrectAnswerText); + 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)); final LinearLayout playerAnswersLinearLayout = (LinearLayout) findViewById(R.id.PlayerAnswers); numberOfPlayers = ClientHolder.size(); @@ -101,18 +109,25 @@ public class GameActivity extends AppCompatActivity { @Override public void onClick(View view) { if (questionIndex < questions.size()) { - GuessQuestion question = questions.get(questionIndex); + MultipleChoiceQuestion question = questions.get(questionIndex); String questionString = question.getQuestionText(); questionText.setText(questionString); - correctAnswerText.setText("Correct answer: " + question.getCorrectAnswer()); + answers.clear(); + answers.addAll(question.getAnswers()); + for (int i=0;i<4;i++) { + answerTextViews.get(i).setText(answers.get(i)); + } for (int i=0;i<outputs.size();i++) { playerAnswerTextViews.get(i).setText("The answer of the " + Integer.toString(i + 1) + ". player: "); - outputs.get(i).println(questionString); + outputs.get(i).println(questionString + "##" + answers.get(0) + "##" + answers.get(1) + + "##" + answers.get(2) + "##" + answers.get(3)); } questionIndex++; } else { questionText.setText("No more questions."); - correctAnswerText.setText(""); + for (int i=0;i<4;i++) { + answerTextViews.get(i).setText(""); + } } } }); diff --git a/KvizServer/app/src/main/java/onlab/kvizserver/model/MultipleChoiceQuestion.java b/KvizServer/app/src/main/java/onlab/kvizserver/model/MultipleChoiceQuestion.java new file mode 100644 index 0000000000000000000000000000000000000000..1aac592530895bf20d463151902e1dd5451a9e70 --- /dev/null +++ b/KvizServer/app/src/main/java/onlab/kvizserver/model/MultipleChoiceQuestion.java @@ -0,0 +1,35 @@ +package onlab.kvizserver.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class MultipleChoiceQuestion { + private String questionText; + private String correctAnswer; + private List<String> otherAnswers = new ArrayList<>(); + + public MultipleChoiceQuestion(String questionText, String correctAnswer, String ans2, String ans3, String ans4) { + this.questionText = questionText; + this.correctAnswer = correctAnswer; + otherAnswers.add(ans2); + otherAnswers.add(ans3); + otherAnswers.add(ans4); + } + + public String getQuestionText() { + return questionText; + } + + public String getCorrectAnswer() { + return correctAnswer; + } + + public List<String> getAnswers() { + List<String> list = new ArrayList<>(); + list.add(correctAnswer); + list.addAll(otherAnswers); + Collections.shuffle(list); + return list; + } +} diff --git a/KvizServer/app/src/main/res/layout/activity_game.xml b/KvizServer/app/src/main/res/layout/activity_game.xml index b1988fe42c7dcaef6636d2d02682bad7863f9b5e..b213e60606401f149847ec35dc15a0b91ee0f8ea 100644 --- a/KvizServer/app/src/main/res/layout/activity_game.xml +++ b/KvizServer/app/src/main/res/layout/activity_game.xml @@ -21,8 +21,29 @@ <TextView android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="Correct answer:" - android:id="@+id/CorrectAnswerText" + android:text="Answer1" + android:id="@+id/Answer1TextView" + android:textSize="30sp"/> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Answer2" + android:id="@+id/Answer2TextView" + android:textSize="30sp"/> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Answer3" + android:id="@+id/Answer3TextView" + android:textSize="30sp"/> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Answer4" + android:id="@+id/Answer4TextView" android:textSize="30sp"/> <LinearLayout