Skip to content

Commit

Permalink
Cryptically branch operations (#360)
Browse files Browse the repository at this point in the history
* Add validationOperation for all operation classes.
Some minor changes in other classes

* protected to public

* Add mockito

* Set scope to test

* Remove extra newline

* Moved result checking responsiblity to caller class

* Add BPMOperationResult and OperationError enum

* Forgot to set operationError

* EvolveOperation Test

* Remove pointless mocking

* Use getter for name

* Remove comment

* Operation config private to protected

* Operation config private to protected

* Extract method, doOperation

* Prep for operation handling

* Setter for pokemon, for transfer after evolving

* evolve operation implemntation

* Fix test due to call method which causes null exception.
Change mock calls to prevent method from actually running.

* Print out success messages after operation

* Extract initializtion of class varaibles. Called at setPokemon to update data

* Transfer operation

* Change all mock to not call method to prevent any side effects or error that might occur

* More meaningful message

* Transfer operation test

* Clear spacing

* tab to space in pom.xml

* tab to space again..

* method name were starting with caps

* Reordering of imports

* Renaming of BPM to Bpm

* Renaming of BPM to Bpm

* OperationID to OpeartionId

* Renaming ID to Id

* Method name were caps

* Update OperationTest method

* Refactoring

* Whole bunch of finals in AccountManager

* Refactoring

* Javadocs and minor refactoring

* Javadoc fix

* Adding of finals to BpmOperationResult

* Javadocs again

* Javadoc for BpmOperationResult

* Fix - Javadoc for BpmOperationResult

* Renaming - Javadoc for BpmOperationResult

* Javadoc for BpmResult

* Missing period - Javadoc for BpmOperationResult

* Missing periods - Javadoc for BpmResult

* Add finals. Sorting of imports

* Javadoc for PlayerAccount

* Final and import sorting for OperationConfirmationController

* Add mocking constructor for all Operations

* Javadoc for OperationTest

* Periods.. periods...

* Javadoc OperationTest

* More.... Javadoc OperationTest

* More....... Javadoc OperationTest

* Operation: OperationID to OperationId

* Remove old Operation file

* PokemonModel: method and varaible names from IV to iv or Iv

* period - Javadoc for PlayerAccount

* PTC to Ptc

* PTC to Ptc

* Javadoc for Operations

* Missing period again.. - Javadoc for Operations

* Add super() call in OperationConfirmationController

* Javadoc for LoginDataTest

* Extract fail message to constant. Fix magic number

* Refactoring of PokemonModel

* Refactoring of PokemonModel

* Fix EvoveOpeartionTest

* Missing break statement in deleteLoginData(LoginType). Adding of finals, refactoring

* Adding of finals, sorting of imports, refactoring of PokemonTab

* Adding of finals, sorting of imports, refactoring of PokemonTable, PokemonTableModel

* Missing file

* Some more fixes

* Try fixes

* Try fixes

* Fix case-sensitive file names
  • Loading branch information
Wolfsblvt committed Sep 7, 2016
1 parent fcf402d commit b4d53df
Show file tree
Hide file tree
Showing 32 changed files with 1,669 additions and 947 deletions.
6 changes: 1 addition & 5 deletions BlossomsPokemonGoManager.iml
Expand Up @@ -13,11 +13,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.pokegoapi:PokeGOAPI-library:0.4.1" level="project" />
<orderEntry type="library" name="Maven: svarzee.gps:gpsoauth:0.3" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.4.1" level="project" />
<orderEntry type="library" name="Maven: io.reactivex:rxjava:1.1.8" level="project" />
<orderEntry type="library" name="Maven: net.jpountz.lz4:lz4:1.3.0" level="project" />
<orderEntry type="library" name="Maven: com.pokegoapi:PokeGOAPI-library:0.4.1" level="project" />
<orderEntry type="library" name="Maven: svarzee.gps:gpsoauth:0.3" level="project" />
<orderEntry type="library" name="Maven: net.iharder:base64:2.3.9" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.9.0" level="project" />
<orderEntry type="library" name="Maven: com.squareup.moshi:moshi:1.2.0" level="project" />
Expand All @@ -30,5 +25,6 @@
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-all:1.10.19" level="project" />
</component>
</module>
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -115,5 +115,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
18 changes: 18 additions & 0 deletions src/me/corriekay/pokegoutil/DATA/enums/OperationError.java
@@ -0,0 +1,18 @@
package me.corriekay.pokegoutil.DATA.enums;

/**
* Describes the different errors returned in the operations classes.
*
*/
public enum OperationError {
IN_GYM,
NOT_EVOLVABLE,
INSUFFICENT_CANDIES,
INSUFFICENT_STARDUSTS,
IS_FAVORITE,

EVOLVE_FAIL,
TRANSFER_FAIL,

NOT_IMPLEMENTED
}
117 changes: 69 additions & 48 deletions src/me/corriekay/pokegoutil/DATA/managers/AccountController.java
@@ -1,5 +1,19 @@
package me.corriekay.pokegoutil.DATA.managers;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.player.PlayerProfile;
import com.pokegoapi.auth.CredentialProvider;
Expand All @@ -16,20 +30,13 @@
import me.corriekay.pokegoutil.windows.PokemonGoMainWindow;
import okhttp3.OkHttpClient;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/*this controller does the login/log off, and different account information (aka player data)
*
*/
@Deprecated
public final class AccountController {

private static final AccountController S_INSTANCE = new AccountController();
private static final AccountController instance = new AccountController();
private static boolean sIsInit = false;
private static ConfigNew config = ConfigNew.getConfig();
protected PokemonGoMainWindow mainWindow = null;
Expand All @@ -44,14 +51,15 @@ private AccountController() {
}

public static AccountController getInstance() {
return S_INSTANCE;
return instance;
}

public static void initialize(Console console) {
if (sIsInit)
public static void initialize(final Console console) {
if (sIsInit) {
return;
}

S_INSTANCE.console = console;
instance.console = console;

sIsInit = true;
}
Expand All @@ -63,16 +71,16 @@ public static void logOn() {
OkHttpClient http;
CredentialProvider cp;
PokemonGo go = null;
while (!S_INSTANCE.logged) {
while (!instance.logged) {
//BEGIN LOGIN WINDOW
go = null;
cp = null;
http = new OkHttpClient();

JTextField username = new JTextField(config.getString(ConfigKey.LOGIN_PTC_USERNAME));
JTextField password = new JPasswordField(config.getString(ConfigKey.LOGIN_PTC_PASSWORD));
final JTextField username = new JTextField(config.getString(ConfigKey.LOGIN_PTC_USERNAME));
final JTextField password = new JPasswordField(config.getString(ConfigKey.LOGIN_PTC_PASSWORD));

boolean directLoginWithSavedCredentials = checkForSavedCredentials();
final boolean directLoginWithSavedCredentials = checkForSavedCredentials();

int response;

Expand All @@ -95,13 +103,13 @@ public static void logOn() {
UIManager.put("OptionPane.cancelButtonText", "Exit");
UIManager.put("OptionPane.okButtonText", "Ok");

JPanel panel1 = new JPanel(new BorderLayout());
final JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(new JLabel("PTC Username: "), BorderLayout.LINE_START);
panel1.add(username, BorderLayout.CENTER);
JPanel panel2 = new JPanel(new BorderLayout());
final JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(new JLabel("PTC Password: "), BorderLayout.LINE_START);
panel2.add(password, BorderLayout.CENTER);
Object[] panel = {panel1, panel2,};
final Object[] panel = {panel1, panel2,};

response = JOptionPane.showConfirmDialog(null, panel, "Login", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
Expand All @@ -120,7 +128,7 @@ public static void logOn() {
} else {
deleteLoginData(LoginType.PTC);
}
} catch (Exception e) {
} catch (final Exception e) {
alertFailedLogin(e.getMessage());
deleteLoginData(LoginType.PTC);
continue;
Expand All @@ -137,13 +145,13 @@ public static void logOn() {

//We're gonna try to load the page using the users browser
JOptionPane.showMessageDialog(null, "A webpage should open up, please allow the permissions, and then copy the code into your clipboard. Press OK to continue", "Google Auth", JOptionPane.PLAIN_MESSAGE);
boolean success = Browser.openUrl(GoogleUserCredentialProvider.LOGIN_URL);
final boolean success = Browser.openUrl(GoogleUserCredentialProvider.LOGIN_URL);

if (!success) {
// Okay, couldn't open it. We use the manual copy window
UIManager.put("OptionPane.cancelButtonText", "Copy To Clipboard");
if (JOptionPane.showConfirmDialog(null, "Please copy this link and paste it into a browser.\nThen, allow the permissions, and copy the code into your clipboard.", "Google Auth", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.CANCEL_OPTION) {
StringSelection ss = new StringSelection(GoogleUserCredentialProvider.LOGIN_URL);
final StringSelection ss = new StringSelection(GoogleUserCredentialProvider.LOGIN_URL);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
}
UIManager.put("OptionPane.cancelButtonText", "Cancel");
Expand All @@ -158,19 +166,21 @@ public static void logOn() {
if (refresh) {
// Based on usage in https://github.com/Grover-c13/PokeGOAPI-Java
provider = new GoogleUserCredentialProvider(http, authCode);
provider.refreshToken(authCode);
} else {
provider = new GoogleUserCredentialProvider(http);
provider.login(authCode);
}
cp = provider;
if (config.getBool(ConfigKey.LOGIN_SAVE_AUTH) || checkSaveAuth()) {
if (!refresh)
if (!refresh) {
config.setString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN, provider.getRefreshToken());
}
config.setBool(ConfigKey.LOGIN_SAVE_AUTH, true);
} else {
deleteLoginData(LoginType.GOOGLE);
}
} catch (Exception e) {
} catch (final Exception e) {
alertFailedLogin(e.getMessage());
deleteLoginData(LoginType.GOOGLE);
continue;
Expand All @@ -182,7 +192,7 @@ public static void logOn() {
UIManager.put("OptionPane.okButtonText", "Ok");
UIManager.put("OptionPane.cancelButtonText", "Cancel");

if (cp != null)
if (cp != null) {
try {
go = new PokemonGo(http);
go.login(cp);
Expand All @@ -191,22 +201,23 @@ public static void logOn() {
deleteLoginData(LoginType.BOTH);
continue;
}
else
} else {
throw new IllegalStateException();
S_INSTANCE.logged = true;
}
instance.logged = true;
}
S_INSTANCE.go = go;
instance.go = go;
initOtherControllers(go);
S_INSTANCE.mainWindow = new PokemonGoMainWindow(go, S_INSTANCE.console);
S_INSTANCE.mainWindow.start();
instance.mainWindow = new PokemonGoMainWindow(go, instance.console);
instance.mainWindow.start();
}

private static void initOtherControllers(PokemonGo go) {
private static void initOtherControllers(final PokemonGo go) {
InventoryManager.initialize(go);
PokemonBagManager.initialize(go);
}

private static void alertFailedLogin(String message) {
private static void alertFailedLogin(final String message) {
JOptionPane.showMessageDialog(null, "Unfortunately, your login has failed. Reason: " + message + "\nPress OK to try again.", "Login Failed", JOptionPane.PLAIN_MESSAGE);
}

Expand All @@ -222,51 +233,60 @@ private static LoginType checkSavedConfig() {
if (!config.getBool(ConfigKey.LOGIN_SAVE_AUTH)) {
return LoginType.NONE;
} else {
if (getLoginData(LoginType.GOOGLE) != null) return LoginType.GOOGLE;
if (getLoginData(LoginType.PTC) != null) return LoginType.PTC;
if (getLoginData(LoginType.GOOGLE) != null) {
return LoginType.GOOGLE;
}
if (getLoginData(LoginType.PTC) != null) {
return LoginType.PTC;
}
return LoginType.NONE;
}
}

private static List<String> getLoginData(LoginType type) {
private static List<String> getLoginData(final LoginType type) {
switch (type) {
case GOOGLE:
String token = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
final String token = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
return (token != null) ? Collections.singletonList(token) : null;
case PTC:
String username = config.getString(ConfigKey.LOGIN_PTC_USERNAME);
String password = config.getString(ConfigKey.LOGIN_PTC_PASSWORD);
final String username = config.getString(ConfigKey.LOGIN_PTC_USERNAME);
final String password = config.getString(ConfigKey.LOGIN_PTC_PASSWORD);
return (username != null && password != null) ? Arrays.asList(username, password) : null;
default:
return null;
}
}


private static void deleteLoginData(LoginType type) {
private static void deleteLoginData(final LoginType type) {
deleteLoginData(type, false);
}

private static void deleteLoginData(LoginType type, boolean justCleanup) {
if (!justCleanup) config.delete(ConfigKey.LOGIN_SAVE_AUTH);
private static void deleteLoginData(final LoginType type, final boolean justCleanup) {
if (!justCleanup) {
config.delete(ConfigKey.LOGIN_SAVE_AUTH);
}
switch (type) {
case BOTH:
config.delete(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
config.delete(ConfigKey.LOGIN_PTC_USERNAME);
config.delete(ConfigKey.LOGIN_PTC_PASSWORD);
break;
case GOOGLE:
config.delete(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
break;
case PTC:
config.delete(ConfigKey.LOGIN_PTC_USERNAME);
config.delete(ConfigKey.LOGIN_PTC_PASSWORD);
break;
default:


}
}

private static boolean checkForSavedCredentials() {
LoginType savedLogin = checkSavedConfig();
final LoginType savedLogin = checkSavedConfig();
if (savedLogin == LoginType.NONE) {
return false;
} else {
Expand All @@ -282,17 +302,18 @@ public static void logOff() throws Exception {
if (!sIsInit) {
throw new ExceptionInInitializerError("AccountController needs to be initialized before logging on");
}
if (!S_INSTANCE.logged)
if (!instance.logged) {
return;
}

S_INSTANCE.logged = false;
S_INSTANCE.mainWindow.setVisible(false);
S_INSTANCE.mainWindow.dispose();
S_INSTANCE.mainWindow = null;
instance.logged = false;
instance.mainWindow.setVisible(false);
instance.mainWindow.dispose();
instance.mainWindow = null;
logOn();
}

public static PlayerProfile getPlayerProfile() {
return S_INSTANCE.go != null ? S_INSTANCE.go.getPlayerProfile() : null;
return instance.go != null ? instance.go.getPlayerProfile() : null;
}
}

0 comments on commit b4d53df

Please sign in to comment.