Created
December 13, 2020 12:25
-
-
Save Mr00Anderson/84239f28919afe109766dc3da10fcc9a to your computer and use it in GitHub Desktop.
AccountClientUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.virtualhex.gdx.free_universe.network; | |
import app.virtualhex.gdx.engine.scene2d.TextFieldChangeListener; | |
import app.virtualhex.gdx.engine.scene2d.TextFieldChangedNotify; | |
import app.virtualhex.gdx.engine.scene2d.filters.*; | |
import app.virtualhex.gdx.free_universe.FuReferences; | |
import app.virtualhex.network.PacketSpeedLimiter; | |
import app.virtualhex.network.clients.AccountClient; | |
import app.virtualhex.network.clients.AccountPacket; | |
import app.virtualhex.utils.StringFormatter; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.scenes.scene2d.InputEvent; | |
import com.badlogic.gdx.scenes.scene2d.ui.*; | |
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; | |
public class AccountClientTable extends Table { | |
private static final int MAX_USERNAME_LENGTH = 23; | |
private final FuReferences refs; | |
private PacketSpeedLimiter packetSpeedLimiter = new PacketSpeedLimiter("account-packets", 250); | |
private AccountPacket.ClientState lastClientState; | |
public AccountClientTable(FuReferences refs, Skin skin) { | |
super(skin); | |
this.refs = refs; | |
defaults().space(10); | |
add("Virtual Hex Login").colspan(2); | |
row(); | |
add("Connecting to servers..."); | |
} | |
@Override | |
public void act(float delta) { | |
super.act(delta); | |
AccountClient accountClient = refs.getAccounting().getAccountClient(); | |
if(accountClient == null){ | |
return; | |
} | |
if (lastClientState == accountClient.clientState) { | |
return; | |
} | |
/* | |
Here we will build out the table reflecting the current client state | |
*/ | |
lastClientState = accountClient.clientState; | |
clear(); | |
switch (accountClient.clientState) { | |
case LOADING: { buildLoadingTable(); break; } | |
case NOT_VALID_TOKEN: {} // Fall through to email | |
case SET_EMAIL: { buildEmailSignInTable(accountClient, false); break;} | |
case INVALID_EMAIL: { buildEmailSignInTable(accountClient, true); break;} | |
case SET_LOGIN_CODE: { buildSetCodeTable(accountClient, false); break;} | |
case INVALID_CODE: { buildSetCodeTable(accountClient, true); break;} | |
case SET_USERNAME: { buildSetUsernameTable(accountClient, false, ""); break;} | |
case USERNAME_EXCEED_LENGTH_OR_SHORT: { buildSetUsernameTable(accountClient, true, StringFormatter.format("Cannot be less then 3 characters or more then {} characters long.", MAX_USERNAME_LENGTH)); break;} | |
case USERNAME_NOT_AVAILABLE: { buildSetUsernameTable(accountClient, true, "Username taken."); break;} | |
case INVALID_USERNAME: { buildSetUsernameTable(accountClient, true, "Invalid Characters"); break;} | |
case SESSION_TOKEN: { setSessionToken(accountClient); } // Fall through to resume | |
case RESUME_SESSION: { buildSessionTokenTable(accountClient); break; } | |
case ERROR_ISSUING_TOKEN: { buildErrorTable("Error issuing session..."); break; } | |
default: { buildErrorTable("Error ..."); break; } | |
} | |
} | |
private void buildErrorTable(String s) { | |
add(s).colspan(2); | |
row(); | |
add(""); | |
row(); | |
add("TODO error animated actor"); | |
} | |
private void setSessionToken(AccountClient accountClient) { | |
String token = (String) accountClient.msgObject; | |
refs.sessionToken.set(token); | |
} | |
private void buildSessionTokenTable(AccountClient accountClient) { | |
add("Virtual Hex Login").colspan(2); | |
row(); | |
add("Logged in as: "); | |
add(refs.email.get()); | |
row(); | |
TextButton logoutButton = new TextButton("Logout", getSkin()); | |
add(logoutButton).colspan(2); | |
logoutButton.addListener(new ClickListener() { | |
@Override | |
public void clicked(InputEvent event, float x, float y) { | |
if (!packetSpeedLimiter.check(accountClient.session)) return; | |
String email = refs.email.get(); | |
String token = refs.sessionToken.get(); | |
accountClient.invalidateSession(email, token); | |
} | |
}); | |
} | |
private void buildSetUsernameTable(AccountClient accountClient, boolean invalidUsername, String reason) { | |
add("Virtual Hex Login - Username").colspan(2); | |
row(); | |
add("Username: "); | |
TextFieldChangedNotify textFieldUsername = new TextFieldChangedNotify("", getSkin()); | |
add(textFieldUsername); | |
textFieldUsername.setTextFieldFilter(new TextUnderscoreNumberFilter()); | |
row(); | |
Cell statusCell = add().colspan(2).right(); | |
row(); | |
if(invalidUsername){ // TODO May be other reason | |
Label label = new Label(StringFormatter.format("Username cannot be less then 3 or greater then {}", MAX_USERNAME_LENGTH), getSkin()); | |
label.setColor(Color.RED); | |
statusCell.setActor(label); | |
} | |
textFieldUsername.addChangeListener(new TextFieldChangeListener() { | |
@Override | |
public void changed(TextField textField) { | |
verifyUsernameField(accountClient, textFieldUsername, statusCell); | |
} | |
}); | |
TextButton usernameButton = new TextButton("Submit", getSkin()); | |
add(usernameButton).colspan(2); | |
usernameButton.addListener(new ClickListener() { | |
@Override | |
public void clicked(InputEvent event, float x, float y) { | |
verifyUsernameField(accountClient, textFieldUsername, statusCell); | |
} | |
}); | |
} | |
private void buildLoadingTable() { | |
add("Chatting with server...").colspan(2); | |
row(); | |
add("Animation Place Holder"); | |
} | |
private void buildSetCodeTable(AccountClient accountClient, boolean invalidCode) { | |
add("Virtual Hex Login - Verify 6 Digit Code").colspan(2); | |
row(); | |
add("Code: "); | |
TextFieldChangedNotify textFieldCode = new TextFieldChangedNotify("", getSkin()); | |
textFieldCode.setTextFieldFilter(new WholeNumberOnlyFilter()); | |
add(textFieldCode); | |
row(); | |
Cell statusCell = add().colspan(2).right(); | |
row(); | |
textFieldCode.addChangeListener(new TextFieldChangeListener() { | |
@Override | |
public void changed(TextField textField) { | |
verifyCodeField(accountClient, textFieldCode, statusCell); | |
} | |
}); | |
if(invalidCode){ | |
statusCell.setActor(new Label("Invalid code.", getSkin())).right(); | |
} | |
TextButton verifyCodeButton = new TextButton("Verify", getSkin()); | |
add(verifyCodeButton).colspan(2); | |
verifyCodeButton.addListener(new ClickListener() { | |
@Override | |
public void clicked(InputEvent event, float x, float y) { | |
verifyCodeField(accountClient, textFieldCode, statusCell); | |
} | |
}); | |
} | |
private void verifyUsernameField(AccountClient accountClient, TextFieldChangedNotify usernameField, Cell statusCell) { | |
if (!packetSpeedLimiter.check(accountClient.session)) return; | |
String text = usernameField.getText(); | |
if(text.length() < 3 || text.length() > MAX_USERNAME_LENGTH){ | |
Label label = new Label(StringFormatter.format("Username cannot be less then 3 or greater then {}", MAX_USERNAME_LENGTH), getSkin()); | |
label.setColor(Color.RED); | |
statusCell.setActor(label); | |
return; | |
} | |
accountClient.setRegistration(text); | |
} | |
private void verifyCodeField(AccountClient accountClient, TextFieldChangedNotify textFieldCode, Cell statusCell) { | |
if (!packetSpeedLimiter.check(accountClient.session)) return; | |
String text = textFieldCode.getText(); | |
if (text.length() != 6) { | |
Label label = new Label("Invalid code.", getSkin()); | |
label.setColor(Color.RED); | |
statusCell.setActor(label); | |
return; | |
} | |
accountClient.setLoginCode(text); | |
} | |
/** | |
* | |
* @param accountClient | |
* @param invalidEmail true if the email was invalid and the field should be marked as uch | |
*/ | |
private void buildEmailSignInTable(AccountClient accountClient, boolean invalidEmail) { | |
add("Virtual Hex Login").colspan(2); | |
row(); | |
add("E-Mail: "); | |
TextFieldChangedNotify emailField = new TextFieldChangedNotify("", getSkin()); | |
add(emailField); | |
emailField.addChangeListener(new TextFieldChangeListener() { | |
@Override | |
public void changed(TextField textField) { | |
verifyAndSetEmail(emailField, accountClient); | |
} | |
}); | |
row(); | |
CheckBox rememberMe = refs.rememberEmail.getCheckBox("Remember Me", getSkin()); | |
add(rememberMe).colspan(2).right(); | |
row(); | |
if(invalidEmail){ | |
Label label = new Label("Error invalid email.", getSkin()); | |
label.setColor(Color.RED); | |
add(label).colspan(2).right(); | |
row(); | |
} | |
TextButton login = new TextButton("Login", getSkin()); | |
add(login).colspan(2); | |
login.addListener(new ClickListener() { | |
@Override | |
public void clicked(InputEvent event, float x, float y) { | |
verifyAndSetEmail(emailField, accountClient); | |
} | |
}); | |
} | |
private void verifyAndSetEmail(TextFieldChangedNotify emailField, AccountClient accountClient) { | |
String lowerCase = emailField.getText().toLowerCase(); | |
packetSpeedLimiter.check(accountClient.session); | |
accountClient.setEmail(lowerCase); | |
refs.email.set(lowerCase); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment