Commit 465e4177 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Actual question database stored in Sugar ORM

Question database is persistent until a new question file is loaded.
parent d3611bd7
......@@ -29,4 +29,5 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.github.rustamg:file-dialogs:1.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.github.satyan:sugar:1.4'
}
......@@ -19,7 +19,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:name=".KvizServerApplication">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
......@@ -39,6 +40,12 @@
<activity android:name=".QuizUpActivity"
android:screenOrientation="portrait">
</activity>
<meta-data android:name="DATABASE" android:value="questions.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="onlab.kvizserver" />
</application>
</manifest>
\ No newline at end of file
......@@ -48,7 +48,6 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Bundle extras = getIntent().getExtras();
String questionFileName = extras.getString("QUESTION_FILE_NAME", null);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
......@@ -59,7 +58,7 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
numberOfPlayers = ClientHolder.size();
gameControlFragment = GameControlFragment.newInstance(numberOfPlayers);
questionListFragment = QuestionListFragment.newInstance(questionFileName);
questionListFragment = QuestionListFragment.newInstance();
ViewPager vpProfile = (ViewPager) findViewById(R.id.GameActivityViewPager);
vpProfile.setAdapter(new GameActivityPagerAdapter(getSupportFragmentManager(), gameControlFragment, questionListFragment));
......@@ -144,23 +143,6 @@ public class GameActivity extends AppCompatActivity implements GameControlFragme
}
}
public static List<Question> readQuestions(InputStream inputStream) {
List<Question> loadedQuestions = new ArrayList<>();
BufferedReader buffreader;
try {
buffreader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
Question question = new Question(parts);
loadedQuestions.add(question);
}
} catch (IOException e) {
e.printStackTrace();
}
return loadedQuestions;
}
private void calculateGuessQuestionWinner() {
PlayerAnswer bestAnswer = playerAnswers[0];
int indexOfBestAnswer = 0;
......
package onlab.kvizserver;
import com.orm.SugarApp;
public class KvizServerApplication extends SugarApp {
}
......@@ -54,7 +54,6 @@ public class LobbyActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ClientListAdapter adapter;
private String questionFileName;
private String gameMode;
private ArrayList<Thread> threads;
......@@ -88,7 +87,6 @@ public class LobbyActivity extends AppCompatActivity {
HOSTNAME = "";
} 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);
}
......@@ -119,7 +117,6 @@ public class LobbyActivity extends AppCompatActivity {
} else {
intent = new Intent(getApplicationContext(), QuizUpActivity.class);
}
intent.putExtra("QUESTION_FILE_NAME", questionFileName);
startActivity(intent);
}
}
......
package onlab.kvizserver;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
......@@ -16,8 +15,14 @@ import android.widget.TextView;
import com.rustamg.filedialogs.FileDialog;
import com.rustamg.filedialogs.OpenFileDialog;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import onlab.kvizserver.model.Question;
public class MainActivity extends AppCompatActivity implements FileDialog.OnFileSelectedListener {
......@@ -34,6 +39,17 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
final EditText txtName = (EditText) findViewById(R.id.txtName);
Button ChooseQuestionFileBtn = (Button) findViewById(R.id.ChooseQuestionFile);
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
boolean firstRun = sharedPref.getBoolean(getString(R.string.first_run), true);
if (firstRun) {
Log.w("firstrun", "firstrun");
initializeDatabase();
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.first_run), false);
editor.commit();
}
ChooseQuestionFileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
......@@ -44,10 +60,17 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
StartLobbyBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (fileCanonicalPath != null) {
try {
InputStream inputStream = new FileInputStream(new File(fileCanonicalPath));
readQuestions(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(getApplicationContext(), LobbyActivity.class);
String hname = txtName.getText().toString();
intent.putExtra("STRING_HOSTNAME", hname);
intent.putExtra("QUESTION_FILE_NAME", fileCanonicalPath);
intent.putExtra("GAME_MODE", gameMode);
startActivity(intent);
......@@ -55,6 +78,30 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile
});
}
private void initializeDatabase() {
try {
InputStream inputStream = getAssets().open("multiple.txt");
readQuestions(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readQuestions(InputStream inputStream) {
Question.deleteAll(Question.class);
try {
BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
Question question = new Question(parts);
question.save();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFileSelected(FileDialog dialog, File file) {
try {
......
......@@ -11,20 +11,11 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import onlab.kvizserver.model.Question;
import static onlab.kvizserver.GameActivity.readQuestions;
public class QuestionListFragment extends Fragment {
private static final String QUESTION_FILE_NAME = "QUESTION_FILE_NAME";
private String questionFileName;
private OnFragmentInteractionListener mListener;
......@@ -37,10 +28,9 @@ public class QuestionListFragment extends Fragment {
// Required empty public constructor
}
public static QuestionListFragment newInstance(String questionFileName) {
public static QuestionListFragment newInstance() {
QuestionListFragment fragment = new QuestionListFragment();
Bundle args = new Bundle();
args.putString(QUESTION_FILE_NAME, questionFileName);
fragment.setArguments(args);
return fragment;
}
......@@ -48,9 +38,6 @@ public class QuestionListFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
questionFileName = getArguments().getString(QUESTION_FILE_NAME);
}
}
@Override
......@@ -63,19 +50,7 @@ public class QuestionListFragment extends Fragment {
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
InputStream inputStream = null;
try {
if (questionFileName == null) {
inputStream = getContext().getAssets().open("multiple.txt");
} else {
inputStream = new FileInputStream(new File(questionFileName));
}
} catch (IOException e) {
e.printStackTrace();
}
questions = readQuestions(inputStream);
questions = Question.listAll(Question.class);
initRecyclerView();
}
......
......@@ -35,11 +35,9 @@ public class QuizUpActivity extends AppCompatActivity {
private int round = 0;
private int numberOfPlayers;
private final List<String> answers = new ArrayList<>();
//private int[] playerAnswerValues;
private int correctAnswer;
private String questionString;
private int[] playerScores;
//private String[] playerNames;
private PlayerAnswer[] playerAnswers;
private List<BufferedReader> inputs = new ArrayList<>();
......@@ -61,23 +59,12 @@ public class QuizUpActivity extends AppCompatActivity {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Bundle extras = getIntent().getExtras();
String questionFileName = extras.getString("QUESTION_FILE_NAME", null);
gson = new Gson();
updateConversationHandler = new Handler();
InputStream inputStream = null;
try {
if (questionFileName == null) {
inputStream = getAssets().open("multiple.txt");
} else {
inputStream = new FileInputStream(new File(questionFileName));
}
} catch (IOException e) {
e.printStackTrace();
}
questions = GameActivity.readQuestions(inputStream);
questions = Question.listAll(Question.class);
//nem feleletválasztós kérdések eltávolítása
Iterator<Question> it = questions.iterator();
......
package onlab.kvizserver.model;
import com.orm.SugarRecord;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Question {
public class Question extends SugarRecord {
private String topic;
private String questionText;
private String correctAnswer;
private List<String> otherAnswers = new ArrayList<>();
private String wrongAnswer1 = null;
private String wrongAnswer2 = null;
private String wrongAnswer3 = null;
private boolean enabled;
public static final int MULTIPLE_CHOICE = 0;
public static final int GUESS = 1;
public Question() {
}
public Question(String[] data) {
topic = data[0];
questionText = data[1];
correctAnswer = data[2];
for (int i=3;i<data.length-1;i++) {
otherAnswers.add(data[i]);
if (data.length >= 5) {
wrongAnswer1 = data[3];
}
if (data.length >= 6) {
wrongAnswer2 = data[4];
}
if (data.length >= 7) {
wrongAnswer3 = data[5];
}
enabled = data[data.length-1].equals("enabled");
}
......@@ -35,12 +48,24 @@ public class Question {
public List<String> getAnswers() {
List<String> list = new ArrayList<>();
list.add(correctAnswer);
list.addAll(otherAnswers);
if (wrongAnswer1 != null)
list.add(wrongAnswer1);
if (wrongAnswer2 != null)
list.add(wrongAnswer2);
if (wrongAnswer3 != null)
list.add(wrongAnswer3);
Collections.shuffle(list);
return list;
}
public List<String> getOtherAnswers() {
List<String> otherAnswers = new ArrayList<>();
if (wrongAnswer1 != null)
otherAnswers.add(wrongAnswer1);
if (wrongAnswer2 != null)
otherAnswers.add(wrongAnswer2);
if (wrongAnswer3 != null)
otherAnswers.add(wrongAnswer3);
return otherAnswers;
}
......@@ -57,12 +82,10 @@ public class Question {
}
public int getType() {
if (otherAnswers.size() == 3) {
if (wrongAnswer1 != null) {
return MULTIPLE_CHOICE;
} else if (otherAnswers.size() == 0) {
return GUESS;
} else {
return -1;
return GUESS;
}
}
......
......@@ -58,7 +58,7 @@
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Question"
android:text="Send next question"
android:id="@+id/NextQuestion"
android:enabled="false"
android:textSize="20sp" />
......@@ -66,7 +66,7 @@
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show correct answer to players"
android:text="Send correct answer"
android:id="@+id/CorrectAnswer"
android:enabled="false"
android:textSize="20sp" />
......
......@@ -4,4 +4,5 @@
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="first_run">first run</string>
</resources>
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