Commit 201e2c79 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Control commands automatized in QuizUp mode.

In QuizUp mode questions are no more sent by pressing a button by server user. They are sent automatically with timer.
parent 61221149
......@@ -15,6 +15,7 @@
android:layout_centerVertical="true"
android:textSize="40sp"
android:id="@+id/Message"
android:gravity="center"
android:text="Üzenet" />
</RelativeLayout>
......
......@@ -21,6 +21,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/QuestionTextTextView"
android:text="Kérdés"
android:textSize="20sp"/>
......
......@@ -3,6 +3,6 @@ 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 biztosan 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
......@@ -21,36 +21,31 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import onlab.kvizserver.model.MultipleChoiceQuestion;
public class QuizUpActivity extends AppCompatActivity {
private List<MultipleChoiceQuestion> questions = new ArrayList<>();
private int round = 0;
private int numberOfPlayers;
private final List<String> answers = new ArrayList<>();
private int[] playerAnswers;
private int correctAnswer;
private String questionString;
private int[] playerScores;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
private int numberOfPlayers;
final List<TextView> playerAnswerTextViews = new ArrayList<>();
final List<TextView> playerScoreTextViews = new ArrayList<>();
final List<String> answers = new ArrayList<>();
private final List<TextView> playerAnswerTextViews = new ArrayList<>();
private final List<TextView> playerScoreTextViews = new ArrayList<>();
private TextView questionText;
private List<TextView> answerTextViews;
int[] playerAnswers;
int correctAnswer;
Handler updateConversationHandler;
String questionString;
int[] playerScores;
private Handler updateConversationHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -81,10 +76,8 @@ public class QuizUpActivity extends AppCompatActivity {
e.printStackTrace();
}
Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
Button correctAnswerButton = (Button) findViewById(R.id.CorrectAnswer);
final TextView questionText = (TextView) findViewById(R.id.QuestionText);
final List<TextView> answerTextViews = new ArrayList<>();
questionText = (TextView) findViewById(R.id.QuestionText);
answerTextViews = new ArrayList<>();
answerTextViews.add((TextView) findViewById(R.id.Answer1TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer2TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer3TextView));
......@@ -99,7 +92,7 @@ public class QuizUpActivity extends AppCompatActivity {
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerAnswerTextView = new TextView(this);
playerAnswerTextView.setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
//playerAnswerTextView.setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswersLinearLayout.addView(playerAnswerTextView);
playerAnswerTextViews.add(playerAnswerTextView);
......@@ -107,7 +100,7 @@ public class QuizUpActivity extends AppCompatActivity {
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.setText("The score of the " + Integer.toString(i + 1) + ". player: 0");
playerScoreTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswersLinearLayout.addView(playerScoreTextView);
playerScoreTextViews.add(playerScoreTextView);
......@@ -127,86 +120,122 @@ public class QuizUpActivity extends AppCompatActivity {
e.printStackTrace();
}
nextQuestionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (round < questions.size()) {
MultipleChoiceQuestion question = questions.get(round);
questionString = question.getQuestionText();
questionText.setText(questionString);
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = -1;
}
for (int i=0;i<numberOfPlayers;i++) {
CommunicationThread commThread = new CommunicationThread(i);
Thread newthread = new Thread(commThread);
newthread.start();
}
answers.clear();
answers.addAll(question.getAnswers());
for (int i=0;i<4;i++) {
answerTextViews.get(i).setText(answers.get(i));
}
sendNextQuestionDelayed(5000);
sendTextMessageDelayed("Round 1", 3500);
sendNextQuestionDelayed(5000);
}
String correct = question.getCorrectAnswer();
for (int i=0;i<4;i++) {
if (answers.get(i).equals(correct)) {
correctAnswer = i;
}
}
private void sendNextQuestion() {
if (round < 7) {
MultipleChoiceQuestion question = questions.get(round);
questionString = question.getQuestionText();
questionText.setText(questionString);
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
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";
outputs.get(i).println(message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
round++;
} else {
questionText.setText("Játék vége.");
for (int i=0;i<4;i++) {
answerTextViews.get(i).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]) + "##";
}
if (playerScores[0] > playerScores[1]) {
outputs.get(0).println(messageForPlayers[0] + "Nyertél!");
outputs.get(1).println(messageForPlayers[1] + "Vesztettél!");
} else if (playerScores[1] > playerScores[0]) {
outputs.get(1).println(messageForPlayers[1] + "Nyertél!");
outputs.get(0).println(messageForPlayers[0] + "Vesztettél!");
} else {
outputs.get(0).println(messageForPlayers[0] + "Döntetlen!");
outputs.get(1).println(messageForPlayers[1] + "Döntetlen!");
}
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = -1;
}
answers.clear();
answers.addAll(question.getAnswers());
for (int i=0;i<4;i++) {
answerTextViews.get(i).setText(answers.get(i));
}
String correct = question.getCorrectAnswer();
for (int i=0;i<4;i++) {
if (answers.get(i).equals(correct)) {
correctAnswer = i;
}
}
});
correctAnswerButton.setOnClickListener(new View.OnClickListener() {
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
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";
outputs.get(i).println(message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
round++;
} else {
questionText.setText("Játék vége.");
for (int i=0;i<4;i++) {
answerTextViews.get(i).setText("");
}
for (int i=0;i<2;i++) {
playerAnswerTextViews.get(i).setText("");
playerScoreTextViews.get(i).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]) + "##";
}
if (playerScores[0] > playerScores[1]) {
outputs.get(0).println(messageForPlayers[0] + "Nyertél!");
outputs.get(1).println(messageForPlayers[1] + "Vesztettél!");
} else if (playerScores[1] > playerScores[0]) {
outputs.get(1).println(messageForPlayers[1] + "Nyertél!");
outputs.get(0).println(messageForPlayers[0] + "Vesztettél!");
} else {
outputs.get(0).println(messageForPlayers[0] + "Döntetlen!");
outputs.get(1).println(messageForPlayers[1] + "Döntetlen!");
}
}
}
private void sendNextQuestionDelayed(int delay) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void onClick(View view) {
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);
outputs.get(i).println(message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
sendNextQuestion();
}
});
}
});
}, delay);
}
for (int i=0;i<numberOfPlayers;i++) {
CommunicationThread commThread = new CommunicationThread(i);
Thread newthread = new Thread(commThread);
newthread.start();
private void sendResults() {
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);
outputs.get(i).println(message);
Log.d("KvizServer", Integer.toString(i) + "-es kliensnek küldött üzenet: " + message);
}
}
private void sendTextMessageDelayed(final String message, int delay) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i=0;i<2;i++) {
outputs.get(i).println("message##" + Integer.toString(playerScores[i])
+ "##" + Integer.toString(playerScores[1-i]) + "##" + message);
}
}
});
}
}, delay);
}
class CommunicationThread implements Runnable {
private int index;
......@@ -231,10 +260,24 @@ public class QuizUpActivity extends AppCompatActivity {
int answerInt = Integer.parseInt(strings[0]);
playerAnswers[index] = answerInt;
if (playerAnswers[index] == correctAnswer) {
playerScores[index] += (int) (21 - Double.parseDouble(strings[1]));
int gainedScore = (int) (21 - Double.parseDouble(strings[1]));
if (round < 6 ) {
playerScores[index] += gainedScore;
} else {
playerScores[index] += 2 * gainedScore;
}
}
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) {
sendResults();
if (round < 7) {
sendTextMessageDelayed("Round " + Integer.toString(round + 1), 2500);
sendNextQuestionDelayed(4000);
} else {
sendNextQuestionDelayed(2500);
}
}
}
} catch (IOException e) {
e.printStackTrace();
......
......@@ -14,35 +14,30 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The place of the question."
android:id="@+id/QuestionText"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"/>
......@@ -54,20 +49,6 @@
</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"
android:textSize="20sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
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