package com.rubenvandeven.emotionhero; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * The current player of the game (for this device) * Created by ruben on 22/08/16. */ public class Player { final static String PLAYERINFO_FILENAME = "playerinfo.json"; private static Player ourInstance; private Context c; PlayerInfo info; /** * Api calls are on player basis, so call them from here */ public ApiRestClient api; public static final String PREFS_NAME = "PlayerPrefs"; public static Player getInstance(Context c) { if(ourInstance == null) { ourInstance = new Player(c); } return ourInstance; } private Player(Context c) { this.c = c; this.info = loadPlayerInfo(); api = new ApiRestClient(this); } public Context getContext() { return c; } /** * Set the Id as it is used on the API * @return */ public String getRemoteId() { SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); return settings.getString("remoteId", null); } public void setRemoteId(String remoteId) { SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("remoteId", remoteId); editor.commit(); } public boolean hasSetSnapshotConfirm() { return true; // this is kioskmode, always snapshot! // SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); // return settings.contains("snapshot_confirm"); } public boolean allowsSnapshots() { return true; // this is kioskmode: always snapshot! // SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); // return settings.getBoolean("snapshot_confirm", false); } public void setAllowsSnapshots(boolean allows) { SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("snapshot_confirm", allows); editor.commit(); } /** * Set the JWT as it is used on the API * @return */ public String getJWT() { SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); return settings.getString("jwt", null); } public void setJWT(String jwt) { SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("jwt", jwt); editor.commit(); } public PlayerInfo getPlayerInfo() { return this.info; } public PlayerInfo loadPlayerInfo() { try{ FileInputStream fis = c.openFileInput(PLAYERINFO_FILENAME ); StringBuilder builder = new StringBuilder(); int ch; while((ch = fis.read()) != -1){ builder.append((char)ch); } Log.d("PLAYER", builder.toString()); return PlayerInfo.fromJson(builder.toString()); } catch (IOException e) { return new PlayerInfo(); } } public boolean isNew() { if(this.info.reachedLevelId < 0) { return true; } return false; } public void savePlayerScore(Score score) { info.addScore(score); savePlayerInfo(info); } public void savePlayerInfo(PlayerInfo playerInfo) { try { FileOutputStream fos = c.openFileOutput(PLAYERINFO_FILENAME, Context.MODE_PRIVATE); fos.write(playerInfo.toJson().getBytes()); fos.close(); } catch(IOException e) { // for now skip error Log.e("PlayerInfo", "Could not save player information!"); } } private GameOpenHelper gameOpenHelper; public GameOpenHelper getGameOpenHelper() { if(gameOpenHelper == null) { gameOpenHelper = new GameOpenHelper(getContext()); } return gameOpenHelper; } }