Skip to content

Commit

Permalink
Merge pull request #46 from LexifyOrganization/playerstats
Browse files Browse the repository at this point in the history
Playerstats
  • Loading branch information
WilliamPiron authored Nov 21, 2018
2 parents bbae72f + 51f4b1a commit f0e6f9b
Show file tree
Hide file tree
Showing 12 changed files with 520 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {
androidTestCompile 'com.android.support:support-annotations:23.1.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
implementation 'com.google.code.gson:gson:2.8.5'
}
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
Expand Down
140 changes: 130 additions & 10 deletions app/src/main/java/parisnanterre/fr/lexify/application/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import io.paperdb.Paper;
import parisnanterre.fr.lexify.R;
import parisnanterre.fr.lexify.computergame.ComputerGameActivity;
import parisnanterre.fr.lexify.connection.SignInActivity;
import parisnanterre.fr.lexify.database.DatabaseUser;
import parisnanterre.fr.lexify.database.User;
import parisnanterre.fr.lexify.settings.SettingsActivity;
import parisnanterre.fr.lexify.userpage.UserPage;
Expand All @@ -35,12 +45,20 @@

public class MainActivity extends Activity {

User currentUser = null;
public static User currentUser;
public static HashMap<Integer,User> userList = new HashMap<>();

private HashMap<Integer, User> userListToSerialize;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (currentUser != null) {
Toast toast_tmp = Toast.makeText(getApplicationContext(), String.valueOf(MainActivity.currentUser.get_id()), Toast.LENGTH_SHORT);
toast_tmp.show();
}

//initialize Paper
Paper.init(this);

Expand Down Expand Up @@ -73,6 +91,7 @@ protected void onCreate(Bundle savedInstanceState) {
editor.commit();
}


TextView txt_welcome = (TextView) findViewById(R.id.activity_main_txt_welcome);
Button btn_disconnect = (Button) findViewById(R.id.activity_main_btn_disconnect);
Button btn_play_game = (Button) findViewById(R.id.activity_main_btn_play_game);
Expand All @@ -83,15 +102,16 @@ protected void onCreate(Bundle savedInstanceState) {
final Button btn_profile = (Button) findViewById(R.id.activity_main_btn_see_profile);
final Button btn_account = (Button) findViewById(R.id.activity_main_btn_account);


//compte encore inutile, changer cette ligne plus tard
//btn_account.setVisibility(View.GONE);

/* Bundle b = this.getIntent().getExtras();
if (b != null)
currentUser = (User) b.getSerializable("Current user");*/

try {

//Old connection method, with a single user in "user.txt"
/*try {
FileInputStream fileInputStream = getApplicationContext().openFileInput("user.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
currentUser = (User) objectInputStream.readObject();
Expand All @@ -101,6 +121,61 @@ protected void onCreate(Bundle savedInstanceState) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}*/

//New connection method, with a list of user saved in a Hashmap<String,User> in "user.json"
/*try{
FileInputStream fileInputStream = getApplicationContext().openFileInput("user.json");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//checks if json is empty by checking the content and file size
//if yes, fills the userList with users from the local DB
//else, fills it with the json file content
if (objectInputStream.toString().equals("{}") || objectInputStream.available()==0){
final DatabaseUser db = new DatabaseUser(this);
List<User> tmplist = db.getAllUsers();
final int size = tmplist.size();
for (int i = 0; i < size; i++) {
userList.put(tmplist.get(i).get_id(), tmplist.get(i));
}
}
else{
//userList is a Hashmap<String,User> where the key is the _id from the User object
userList = (HashMap<Integer,User>) objectInputStream.readObject();
}
//currentUser contains the User object identified by the _id of the last connected User
//currentUser = userList.get(lastUser);
objectInputStream.close();
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (ClassCastException e ){
e.printStackTrace();
}*/

//New connexion method : saves the json file in SharedPreferences
try {
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = appSharedPrefs.getString("userList", "");
Type type = new TypeToken<HashMap<Integer, User>>(){}.getType();
//userList is a Hashmap<Integer,User> where the key is the _id from the User object
userList = gson.fromJson(json, type);
if (json.equals("") || userList.isEmpty()) {
final DatabaseUser db = new DatabaseUser(this);
List<User> tmplist = db.getAllUsers();
final int size = tmplist.size();
for (int i = 0; i < size; i++) {
userList.put(tmplist.get(i).get_id(), tmplist.get(i));
}
}
//Toast toast_tmp = Toast.makeText(getApplicationContext(), String.valueOf(MainActivity.currentUser.get_gamesPlayed()), Toast.LENGTH_SHORT);
//toast_tmp.show();
} catch (Exception e ){
e.printStackTrace();
}


Expand All @@ -118,33 +193,27 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onClick(View v) {

Intent i = new Intent(getApplicationContext(), VerbalGameActivity.class);
startActivity(i);

}
});

btn_computer.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Intent i = new Intent(getApplicationContext(), ComputerGameActivity.class);
startActivity(i);

}
});

btn_profile.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Intent i = new Intent(getApplicationContext(), UserPage.class);
i.putExtra("user", currentUser);
startActivity(i);

}
});

Expand Down Expand Up @@ -181,7 +250,33 @@ public void onClick(View view) {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*try{
FileOutputStream fileOutputStream = openFileOutput("user.json", Context.MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
userList.put(currentUser.get_id(),currentUser);
objectOutputStream.writeObject(userList);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/

try {
//Updates in the Hashmap the info of the current user
userList.put(currentUser.get_id(),currentUser);
userListToSerialize = userList;
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(userListToSerialize);
prefsEditor.putString("userList", json);
prefsEditor.commit();
} catch(Exception e){
e.printStackTrace();
}
currentUser = null;
lil_user.setVisibility(View.GONE);
btn_profile.setVisibility(View.GONE);
Expand Down Expand Up @@ -212,6 +307,31 @@ public void onClick(View v) {
startActivity(i);
}
});

//currently test button
Button btn_stats = (Button) findViewById(R.id.activity_main_btn_playerstats);
btn_stats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentUser == null) {
Toast toast = Toast.makeText(getApplicationContext(), "Must be logged in to display stats", Toast.LENGTH_LONG);
toast.show();
}
else {
String stats = "Played : " + String.valueOf(currentUser.get_gamesPlayed());
Toast toast = Toast.makeText(getApplicationContext(), stats, Toast.LENGTH_LONG);
toast.show();

stats = "Failed : " + String.valueOf(currentUser.get_gamesFailed());
toast = Toast.makeText(getApplicationContext(), stats, Toast.LENGTH_LONG);
toast.show();

stats = "Guessed : " + String.valueOf(currentUser.get_wordGuessed());
toast = Toast.makeText(getApplicationContext(), stats, Toast.LENGTH_LONG);
toast.show();
}
}
});
}

private void updateLanguage(String language) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package parisnanterre.fr.lexify.computergame;

import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand All @@ -24,19 +28,29 @@
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import io.paperdb.Paper;
import parisnanterre.fr.lexify.R;
import parisnanterre.fr.lexify.application.MainActivity;
import parisnanterre.fr.lexify.database.User;
import parisnanterre.fr.lexify.verbalgame.VerbalGameFragment;

import static parisnanterre.fr.lexify.application.MainActivity.currentUser;
import static parisnanterre.fr.lexify.application.MainActivity.userList;

public class ComputerGameFragment extends Fragment {

private OnFragmentInteractionListener mListener;
Expand All @@ -56,6 +70,7 @@ public class ComputerGameFragment extends Fragment {
CountDownTimer countDownTimer;
ObjectAnimator animateProgressBar;
ComputerGameActivity computerGameActivity;
private HashMap<Integer, User> userListToSerialize;

public List<List<String>> create_liste_synonymes(){
List<List<String>> synonymes = new ArrayList<List<String>>();
Expand Down Expand Up @@ -176,7 +191,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

round.setText(getResources().getString(R.string.round)+" "+currentRound+"/4");

if (timeSettingComputer != 0) {
if (timeSettingComputer != 0 ) {
progressBar.setVisibility(View.VISIBLE);
currentTimerProgress = timeSettingComputer;
progressBar.setProgress(currentTimerProgress);
Expand Down Expand Up @@ -237,6 +252,12 @@ public void onClick(View v) {

@Override
public void onClick(View v) {
if (currentUser != null) {
if (currentRound != 4) {
currentUser.update_gamesFailed();
}
saveUserStats();
}
Intent i = new Intent(getActivity(), MainActivity.class);
startActivity(i);
}
Expand Down Expand Up @@ -266,6 +287,11 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(currentRound==4) {
next.setText(getResources().getString(R.string.statistics));
abandon.setVisibility(View.GONE);
if (currentUser != null) {
currentUser.update_gamesPlayed();
saveUserStats();
}

if (timeSettingComputer!=0) {
animateProgressBar.end();
countDownTimer.cancel();
Expand All @@ -277,6 +303,10 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
wordsFound++;
wordsFoundList.add((getResources().getString(R.string.yes)));
computerGameActivity.setWordsFound(wordsFound);
if (currentUser != null) {
currentUser.update_wordGuessed();
saveUserStats();
}
if (timeSettingComputer!=0) {
animateProgressBar.end();
countDownTimer.cancel();
Expand Down Expand Up @@ -387,4 +417,35 @@ private boolean equalsIgnoreAccent(String a, String b) {
return false;

}

@TargetApi(Build.VERSION_CODES.M)
private void saveUserStats(){
/*try{
FileOutputStream fileOutputStream = getContext().openFileOutput("user.json", Context.MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
userList.put(currentUser.get_id(),currentUser);
objectOutputStream.writeObject(userList);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/

try {
userList.put(currentUser.get_id(), currentUser);
userListToSerialize = userList;
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(userListToSerialize);
prefsEditor.putString("userList", json);
prefsEditor.commit();
} catch(Exception e){
e.printStackTrace();
}
}
}
Loading

0 comments on commit f0e6f9b

Please sign in to comment.