Commit dffe1427 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

QuizUpActivity added

parent 664a6fa5
......@@ -21,7 +21,7 @@
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -31,9 +31,12 @@
<activity
android:name=".LobbyActivity"
android:label="@string/title_activity_lobby"
android:screenOrientation="landscape"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".GameActivity"></activity>
<activity
android:name=".GameActivity"
android:screenOrientation="portrait"></activity>
<activity android:name=".QuizUpActivity"></activity>
</application>
</manifest>
\ No newline at end of file
......@@ -20,6 +20,8 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
public class GameActivity extends AppCompatActivity implements MultipleChoiceFragment.OnFragmentInteractionListener {
......@@ -33,6 +35,8 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
Thread commThread;
private Date questionReceivedDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -98,6 +102,9 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
private void replaceFragment(String[] strings) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (Integer.parseInt(strings[5]) == -1) {
questionReceivedDate = new Date();
}
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]));
......@@ -107,9 +114,11 @@ public class GameActivity extends AppCompatActivity implements MultipleChoiceFra
@Override
public void onAnswered(String answer) {
Log.d("A válasz: ", answer);
displayTextView.setText(answer);
output.println(answer);
Date questionAnsweredDate = new Date();
double answerTime = (double)(questionAnsweredDate.getTime() - questionReceivedDate.getTime()) / 1000;
Log.d("A válasz: ", answer + " (" + Double.toString(answerTime) + "s)");
displayTextView.setText(answer + " (" + Double.toString(answerTime) + "s)");
output.println(answer + "##" + Double.toString(answerTime));
}
......
......@@ -332,7 +332,12 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
break;
case "GameStarted":
exit = false;
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
Intent intent;
if (params[1].equals("normal")) {
intent = new Intent(getApplicationContext(), GameActivity.class);
} else {
intent = new Intent(getApplicationContext(), QuizUpActivity.class);
}
startActivity(intent);
break;
case "Disconnect":
......
package onlab.kvizclient;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MessageFragment extends Fragment {
private static final String MESSAGE = "message";
private String message;
public MessageFragment() {
// Required empty public constructor
}
public static MessageFragment newInstance(String message) {
MessageFragment fragment = new MessageFragment();
Bundle args = new Bundle();
args.putString(MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
message = getArguments().getString(MESSAGE);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_message, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView messageTextView = (TextView) getView().findViewById(R.id.Message);
messageTextView.setText(message);
}
}
\ No newline at end of file
......@@ -37,6 +37,8 @@ public class MultipleChoiceFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private boolean answered = false;
public MultipleChoiceFragment() {
// Required empty public constructor
}
......@@ -123,6 +125,7 @@ public class MultipleChoiceFragment extends Fragment {
}
if (own_answer != -1) {
answered = true;
leftImageViews[own_answer].setImageResource(R.drawable.ic_keyboard_arrow_right_black_48dp);
}
......@@ -162,9 +165,12 @@ public class MultipleChoiceFragment extends Fragment {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onAnswered(answerButtons[index].getText().toString());
Log.d("FRAGMENT", "Válasz történt");
if (!answered) {
answered = true;
if (mListener != null) {
mListener.onAnswered(answerButtons[index].getText().toString());
Log.d("FRAGMENT", "Válasz történt");
}
}
}
......
package onlab.kvizclient;
import android.app.Fragment;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
public class QuizUpActivity extends AppCompatActivity implements MultipleChoiceFragment.OnFragmentInteractionListener {
private BufferedReader input;
private PrintWriter output;
Handler updateConversationHandler;
Thread commThread;
private Date questionReceivedDate;
TextView ownScoreTextView;
TextView opponentScoreTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_up);
updateConversationHandler = new Handler();
ownScoreTextView = (TextView) findViewById(R.id.OwnScore);
opponentScoreTextView = (TextView) findViewById(R.id.OpponentScore);
try {
input = new BufferedReader(new InputStreamReader(ServerHolder.getSocket().getInputStream()));
output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(ServerHolder.getSocket().getOutputStream())),true);
output.println("$$$$");
} catch (IOException e) {
e.printStackTrace();
}
CommunicationThread ct = new CommunicationThread();
commThread = new Thread(ct);
commThread.start();
}
@Override
protected void onDestroy() {
commThread.interrupt();
super.onDestroy();
}
class CommunicationThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
Log.d("QuizUpActivity", "Szervertől kapott üzenet: " + read);
if (read != null && !read.equals("$$$$")) {
updateConversationHandler.post(new QuizUpActivity.updateUIThread(read));
}
} catch (IOException e) {
Thread.currentThread().interrupt();
}
}
}
}
class updateUIThread implements Runnable {
String msg;
public updateUIThread(String msg) {
this.msg = msg;
}
@Override
public void run() {
String[] params = msg.split("##");
if (params[0].equals("question") || params[0].equals("message")) {
ownScoreTextView.setText(params[1]);
opponentScoreTextView.setText(params[2]);
replaceFragment(params);
}
}
}
private void replaceFragment(String[] strings) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (Integer.parseInt(strings[8]) == -1) {
questionReceivedDate = new Date();
}
Fragment frag;
if (strings[0].equals("question")) {
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]));
} else {
frag = MessageFragment.newInstance(strings[3]);
}
fragmentTransaction.replace(R.id.FragmentContainer, frag);
fragmentTransaction.commit();
}
@Override
public void onAnswered(String 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));
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="onlab.kvizclient.QuizUpActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:id="@+id/OwnScore"
android:textSize="30sp"
android:text="0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:id="@+id/OpponentScore"
android:textSize="30sp"
android:text="0"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/FragmentContainer">
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<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.MessageFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="40sp"
android:id="@+id/Message"
android:text="Üzenet" />
</RelativeLayout>
</FrameLayout>
......@@ -34,7 +34,8 @@
android:label="@string/title_activity_lobby"
android:screenOrientation="landscape"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".GameActivity"></activity>
<activity android:name=".GameActivity" />
<activity android:name=".QuizUpActivity"></activity>
</application>
</manifest>
\ No newline at end of file
......@@ -22,6 +22,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import onlab.kvizserver.model.ClientModel;
......@@ -108,7 +109,7 @@ public class GameActivity extends AppCompatActivity {
new OutputStreamWriter(ClientHolder.get(i).getClientsocket().getOutputStream())), true);
output.println("$$$$");
outputs.add(output);
output.println("GameStarted");
output.println("GameStarted##normal");
}
} catch (IOException e) {
e.printStackTrace();
......@@ -192,12 +193,14 @@ public class GameActivity extends AppCompatActivity {
String read = input.readLine();
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 (read.equals(answers.get(i))) {
if (strings[0].equals(answers.get(i))) {
playerAnswers[index] = i;
}
}
updateConversationHandler.post(new GameActivity.updateUIThread(index, read));
updateConversationHandler.post(new GameActivity.updateUIThread(index,
strings[0] + " (" + strings[1] + "s)"));
}
} catch (IOException e) {
e.printStackTrace();
......
......@@ -54,6 +54,7 @@ public class LobbyActivity extends AppCompatActivity {
private ClientListAdapter adapter;
private String questionFileName;
private String gameMode;
private ArrayList<Thread> threads;
......@@ -87,6 +88,8 @@ public class LobbyActivity extends AppCompatActivity {
} else {
HOSTNAME = extras.getString("STRING_HOSTNAME");
questionFileName = extras.getString("QUESTION_FILE_NAME", null);
gameMode = extras.getString("GAME_MODE");
Log.d("LobbyActivity", "The game mode is " + gameMode);
}
} else {
HOSTNAME = (String) savedInstanceState.getSerializable("STRING_HOSTNAME");
......@@ -106,7 +109,12 @@ public class LobbyActivity extends AppCompatActivity {
serverThread.interrupt();
commThread.interrupt();
ClientHolder.addAll(clients);
Intent intent = new Intent(getApplicationContext(), GameActivity.class);
Intent intent;
if (gameMode.equals("normal")) {
intent = new Intent(getApplicationContext(), GameActivity.class);
} else {
intent = new Intent(getApplicationContext(), QuizUpActivity.class);
}
intent.putExtra("QUESTION_FILE_NAME", questionFileName);
startActivity(intent);
}
......
......@@ -10,6 +10,7 @@ import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import com.rustamg.filedialogs.FileDialog;
......@@ -22,6 +23,8 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
private String fileCanonicalPath;
private String gameMode = "normal";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -45,6 +48,7 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
String hname = txtName.getText().toString();
intent.putExtra("STRING_HOSTNAME", hname);
intent.putExtra("QUESTION_FILE_NAME", fileCanonicalPath);
intent.putExtra("GAME_MODE", gameMode);
startActivity(intent);
}
......@@ -63,4 +67,20 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
}
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio_normal:
if (checked)
gameMode = "normal";
Log.d("MainActivity", "Game mode changed to Normal");
break;
case R.id.radio_quizup:
if (checked)
gameMode = "quizup";
Log.d("MainActivity", "Game mode changed to QuizUp");
break;
}
}
}
package onlab.kvizserver;
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 java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import onlab.kvizserver.model.MultipleChoiceQuestion;
public class QuizUpActivity extends AppCompatActivity {
private List<MultipleChoiceQuestion> questions = new ArrayList<>();
private int round = 0;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
private int numberOfPlayers;
final List<TextView> playerAnswerTextViews = new ArrayList<>();
final List<String> answers = new ArrayList<>();
int[] playerAnswers;
int correctAnswer;
Handler updateConversationHandler;
String questionString;
int[] playerScores;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Bundle extras = getIntent().getExtras();
String questionFileName = extras.getString("QUESTION_FILE_NAME", null);
updateConversationHandler = new Handler();
BufferedReader buffreader;
try {
if (questionFileName == null) {
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");
MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion(
parts[0], parts[1], parts[2], parts[3], parts[4]);
questions.add(multipleChoiceQuestion);
}
} catch (IOException e) {
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<>();
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);