Skip to content

Commit

Permalink
Refactor Player
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjules committed Nov 16, 2018
1 parent ccd6a3f commit d104ef6
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 54 deletions.
Expand Up @@ -41,10 +41,6 @@ public class FirestoreTest {

private static Map<String, Object> TEST_DATA = null;

/*@Rule
public final ActivityTestRule<> mActivityRule =
new ActivityTestRule<>(MainActivity.class);*/

@BeforeClass
public static void enableMock() {
MOCK_ENABLED = true;
Expand All @@ -69,8 +65,7 @@ public static void resetTestValues() {

@Before
public void setup() {
Player.resetPlayer();
Player.get().initializePlayerData(TEST_SCIPER, INITIAL_FIRSTNAME, INITIAL_LASTNAME);
Player.get().resetPlayer();
Player.get().setRole(Role.student);
resetTestValues();
}
Expand Down Expand Up @@ -154,7 +149,7 @@ public void addNewUserToDBTest() throws Exception {

@Test
public void addXpPlayerAndCurrencyTest() {
Player.resetPlayer();
Player.get().resetPlayer();

assertEquals(INITIAL_XP, Player.get().getExperience());
assertEquals(INITIAL_LEVEL, Player.get().getLevel());
Expand Down
Expand Up @@ -40,7 +40,7 @@ public void init() {

@After
public void cleanup() {
Player.resetPlayer();
Player.get().resetPlayer();
}

@Test
Expand Down
Expand Up @@ -20,7 +20,7 @@
public class UtilsItemsTest {
@Before
public void setup(){
Player.resetPlayer();
Player.get().resetPlayer();
}

@Test
Expand Down
Expand Up @@ -54,12 +54,6 @@ public class MainActivityTest {
public final ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);


@BeforeClass
public static void runOnceBeforeClass() {
Player.get().initializeDefaultPlayerData();
}

@Before
public void initIntent() {
Intents.init();
Expand Down Expand Up @@ -93,7 +87,7 @@ public void run() {

@Test
public void checkPlayerProgressionDisplay() {
Player.resetPlayer();
Player.get().resetPlayer();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Expand Down
Expand Up @@ -25,7 +25,7 @@ public class PlayerTest {

@Before
public void setup() {
Player.resetPlayer();
Player.get().resetPlayer();
}

@Test
Expand Down
Expand Up @@ -57,7 +57,6 @@ public class AddQuestionActivityTest {
public static void enableMock() {
MOCK_ENABLED = true;
Intents.init();
Player.get().initializeDefaultPlayerData();
}

@AfterClass
Expand Down
Expand Up @@ -36,7 +36,6 @@ public class QuestsActivityTeacherTest {
@BeforeClass
public static void enableMock() {
MOCK_ENABLED = true;
Player.get().initializeDefaultPlayerData();
Player.get().setRole(Role.student);
}
@AfterClass
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/ch/epfl/sweng/studyup/LoginActivity.java
Expand Up @@ -105,7 +105,9 @@ public void loadPlayerDataFromCache(List<String> playerCacheData) throws Excepti
}

Player currPlayer = Player.get();
currPlayer.initializePlayerData(sciperNum, firstName, lastName);
currPlayer.setSciperNum(sciperNum);
currPlayer.setFirstName(firstName);
currPlayer.setLastName(lastName);
currPlayer.setRole(role);
}

Expand Down
Expand Up @@ -52,7 +52,9 @@ public void runAuthentication() throws Exception {

PlayerDataContainer playerData = Authenticator.getPlayerData(token);

Player.get().initializePlayerData(playerData.sciperNum, playerData.firstName, playerData.lastname);
Player.get().setSciperNum(playerData.sciperNum);
Player.get().setFirstName(playerData.firstName);
Player.get().setLastName(playerData.lastname);
if (MOCK_ENABLED) {
Player.get().setRole(Role.student);
}
Expand Down
Expand Up @@ -55,10 +55,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);

if (MOCK_ENABLED) {
Player.get().initializeDefaultPlayerData();
}

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
Expand Down
42 changes: 13 additions & 29 deletions app/src/main/java/ch/epfl/sweng/studyup/player/Player.java
Expand Up @@ -32,11 +32,11 @@ public class Player {
private static Player instance = null;

// Basic biographical data
private String sciperNum = null;
private String firstName = null;
private String lastName = null;
private String sciperNum;
private String firstName;
private String lastName;

private String username = null;
private String username;
private Role role = null;

// Game-related data
Expand Down Expand Up @@ -73,24 +73,15 @@ public static Player get() {
* User for testing purposes.
* Clear data from current Player instance.
*/
public static void resetPlayer() {
instance = new Player();
}

/**
* Initialize the instance of Player for the FIRST TIME.
* This is used when a user logs is logged in from AuthenticationActivity OR
* the user is logged in automatically from LoginActivity.
*/
public void initializePlayerData(String sciperNum, String firstName, String lastName) {

this.sciperNum = sciperNum;
this.firstName = firstName;
this.lastName = lastName;
}

public void initializeDefaultPlayerData() {
initializePlayerData(INITIAL_SCIPER, INITIAL_FIRSTNAME, INITIAL_LASTNAME);
public void resetPlayer() {
experience = INITIAL_XP;
currency = INITIAL_CURRENCY;
level = INITIAL_LEVEL;
username = INITIAL_USERNAME;
answeredQuestions = new HashMap<>();
items = new ArrayList<>();
courses = new ArrayList<>();
courses.add(Course.SWENG);
}

/**
Expand Down Expand Up @@ -140,13 +131,6 @@ public List<Course> getCourses() {
return courses;
}

/**
* Changes the Player to the basic state, right after constructor.
*/
public void reset() {
instance = null;
}

// Setters
public void setSciperNum(String sciperNum) {
this.sciperNum = sciperNum;
Expand Down

0 comments on commit d104ef6

Please sign in to comment.