Skip to content

Commit

Permalink
Merge pull request #125 from StenAL/features
Browse files Browse the repository at this point in the history
Add language selector to login screen, use enums in localization code
  • Loading branch information
PhilippvK authored Nov 8, 2024
2 parents c3ecc99 + 47c0620 commit 93d3977
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 325 deletions.
7 changes: 7 additions & 0 deletions client/src/main/java/agolf/GameApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.awt.Font;
import java.awt.Image;
import org.moparforia.client.Launcher;
import org.moparforia.shared.Locale;

public class GameApplet extends AApplet {

Expand Down Expand Up @@ -242,6 +243,12 @@ protected void trackTestLogin(String username, String password) {
this.gameContainer.connection.writeData("ttlogin\t" + username + "\t" + password);
}

protected void trackTestLogin(String username, String password, Locale locale) {
this.textManager.setLocale(locale, this);
this.gameContainer.connection.writeData("language\t" + locale);
this.trackTestLogin(username, password);
}

public boolean isEmailVerified() {
return this.syncUnknownBool.get();
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/agolf/GolfConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void handlePacket(String cmd) {

this.socketConnection.closeConnection();
} else if (args[0].equals("versok")) {
this.writeData("language\t" + this.gameContainer.params.getChatLang());
this.writeData("language\t" + this.gameContainer.params.getChatLocale());
String var4 = this.gameContainer.params.getSessionLocale();
if (var4 != null) {
this.writeData("sessionlocale\t" + var4);
Expand Down
65 changes: 45 additions & 20 deletions client/src/main/java/agolf/TrackTestLoginPanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package agolf;

import com.aapeli.colorgui.Choicer;
import com.aapeli.multiuser.UsernameValidator;
import java.awt.Button;
import java.awt.Color;
Expand All @@ -9,27 +10,30 @@
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.moparforia.shared.Locale;

class TrackTestLoginPanel extends Panel implements ActionListener, KeyListener {
class TrackTestLoginPanel extends Panel implements ActionListener, KeyListener, ItemListener {

private GameApplet gameApplet;
private int width;
private int height;
private Locale locale;
private TextField textFieldName;
private TextField textFieldPassword;
private Button buttonOk;
private Label labelError;
private Label labelName;
private Label labelName2;
private Label labelPassword;
private Label labelPassword2;
private Choicer languageChoicer;

protected TrackTestLoginPanel(GameApplet gameApplet, int width, int height) {
this.gameApplet = gameApplet;
this.width = width;
this.height = height;
this.locale = gameApplet.param.getLocale();
this.setSize(width, height);
this.create();
}
Expand All @@ -39,8 +43,8 @@ public void addNotify() {
this.repaint();
}

public void paint(Graphics var1) {
this.update(var1);
public void paint(Graphics g) {
this.update(g);
}

public void update(Graphics g) {
Expand All @@ -52,7 +56,7 @@ public void actionPerformed(ActionEvent evt) {
String username = this.textFieldName.getText().trim();
String password = this.textFieldPassword.getText().trim();
// String password = '';
this.gameApplet.trackTestLogin(username, password);
this.gameApplet.trackTestLogin(username, password, locale);
}

public void keyPressed(KeyEvent evt) {}
Expand All @@ -72,7 +76,23 @@ private void create() {
this.textFieldPassword.setBackground(Color.white);
this.textFieldPassword.setForeground(Color.black);
textFieldPassword.setEchoChar('*');
// this.add(this.textFieldPassword); //Don't show this field

this.languageChoicer = new Choicer();
this.languageChoicer.setBounds(this.width / 2 - 75, this.height / 2 - 10, 150, 25);
this.languageChoicer.addItem("English");
this.languageChoicer.addItem("Finnish");
this.languageChoicer.addItem("Swedish");
this.languageChoicer.setBackground(Color.white);
this.languageChoicer.setForeground(Color.black);
this.languageChoicer.addItemListener(this);
int selectedLanguageIndex =
switch (locale) {
case EN_US -> 0;
case FI_FI -> 1;
case SV_SE -> 2;
};
this.languageChoicer.setSelectedIndex(selectedLanguageIndex);
this.add(this.languageChoicer);

this.buttonOk = new Button("OK");
this.buttonOk.setBounds(this.width / 2 - 75, this.height / 2 + 50, 75, 25);
Expand All @@ -87,18 +107,6 @@ private void create() {
labelName = new Label("Nickname:");
labelName.setBounds(width / 2 - 200, height / 2 - 60, 75, 25);
add(labelName);
// No more labels needed
// labelName2 = new Label("");
// labelName2.setBounds(width / 2 + 80, height / 2 - 60, 75, 25);
// labelName2.setForeground(Color.red);
// add(labelName2);
// labelPassword = new Label("");
// labelPassword.setBounds(width / 2 - 150, height / 2 - 10, 75, 25);
// add(labelPassword);
// labelPassword2 = new Label("");
// labelPassword2.setBounds(width / 2 + 80, height / 2 - 10, 75, 25);
// labelPassword2.setForeground(Color.red);
// add(labelPassword2);
}

public void keyTyped(KeyEvent e) {}
Expand All @@ -117,4 +125,21 @@ public void keyReleased(KeyEvent e) {
buttonOk.setEnabled(false);
}
}

@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == this.languageChoicer) {
switch (this.languageChoicer.getSelectedIndex()) {
case 0:
this.locale = Locale.EN_US;
break;
case 1:
this.locale = Locale.FI_FI;
break;
case 2:
this.locale = Locale.SV_SE;
break;
}
}
}
}
2 changes: 1 addition & 1 deletion client/src/main/java/agolf/lobby/LobbyChatPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected LobbyChatPanel(GameContainer gameContainer, int width, int height, int
this.disableChatInput(2);
}

this.setOutputToGlobal(Languages.getLanguageIdByString(gameContainer.params.getChatLang()));
this.setOutputToGlobal(Languages.getLanguageId(gameContainer.params.getChatLocale()));
this.addChatListener(this);
}

Expand Down
Binary file modified client/src/main/java/com/aapeli/client/BadWordFilter.java
Binary file not shown.
12 changes: 6 additions & 6 deletions client/src/main/java/com/aapeli/client/LocalizationNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import com.aapeli.tools.Tools;
import com.aapeli.tools.XmlUnit;
import org.moparforia.shared.Language;
import org.moparforia.shared.Locale;

class LocalizationNode {

private String language;
private Language language;
private String singular;
private String plural;
private String zero;
private final TextManager textManager;

protected LocalizationNode(TextManager textManager, String language, XmlUnit unit, boolean reversed) {
this.textManager = textManager;
this.language = language.substring(0, 2).toLowerCase();
protected LocalizationNode(Locale locale, XmlUnit unit, boolean reversed) {
this.language = locale.getLanguage();
this.singular = unit.getChildValue("singular");
this.plural = unit.getChildValue("plural");
this.zero = unit.getChildValue("zero");
Expand All @@ -30,7 +30,7 @@ protected String getLocalization(int quantity) {
return this.zero;
}

if (this.plural != null && !this.language.equals("fr")) {
if (this.plural != null && !this.language.equals(Language.FRENCH)) {
return this.plural;
}
} else if ((quantity < 0 || quantity > 1) && this.plural != null) {
Expand Down
91 changes: 44 additions & 47 deletions client/src/main/java/com/aapeli/client/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
import org.moparforia.shared.Language;
import org.moparforia.shared.Locale;

public final class Parameters {

private static final String LOCALHOST = "127.0.0.1";
// private static final String aString1416 = "192.168.1.23";
private static final String ENGLISH_LANGUAGE = "en";
private static final String PLAYFORIA_SITE_NAME = "playforia";
private static final String PLAYFORIA_QUIT_PAGE = "http://www.playforia.com/";
private static final String QUIT_TARGET = "_top";
Expand All @@ -21,9 +22,9 @@ public final class Parameters {
private String codeBaseHost;
private String documentBaseHost;
private String serverIp;
private String locale;
private String translationLanguage;
private String chatLang;
private Language language;
private Locale translationLocale;
private Locale chatLocale;
private String siteName;
private String username;
private String sessionLocale;
Expand Down Expand Up @@ -112,24 +113,24 @@ public int getServerPort() {
return this.serverPort;
}

public String getLocale() {
return this.locale;
public Language language() {
return this.language;
}

public String getTranslationLang() {
return this.translationLanguage;
public Locale getTranslationLocale() {
return this.translationLocale;
}

public String getUsername() {
return this.username;
}

public String getChatLang() {
return this.chatLang != null ? this.chatLang : this.translationLanguage;
public Locale getChatLocale() {
return this.chatLocale != null ? this.chatLocale : this.translationLocale;
}

public String getLang() {
return this.getChatLang();
public Locale getLocale() {
return this.getChatLocale();
}

public String getSiteName() {
Expand Down Expand Up @@ -392,9 +393,9 @@ public AApplet getAApplet() {

public void destroy() {
this.serverIp = null;
this.locale = null;
this.translationLanguage = null;
this.chatLang = null;
this.language = null;
this.translationLocale = null;
this.chatLocale = null;
this.siteName = null;
this.sessionLocale = null;
this.session = null;
Expand Down Expand Up @@ -449,9 +450,9 @@ protected String getSubgame() {
private void init() {
this.serverIp = this.getParamServer();
this.serverPort = this.getParamPort();
this.locale = this.getParamLocale();
this.translationLanguage = this.getParamLanguage();
this.chatLang = this.getParamChatLanguage();
this.language = this.getParamLanguage();
this.translationLocale = this.getParamLocale();
this.chatLocale = this.getParamChatLocale();
this.siteName = this.getParamSiteName();
this.sessionLocale = this.getParameter("sessionlocale");
this.session = this.getParameter("session");
Expand Down Expand Up @@ -514,72 +515,68 @@ private int getParamPort() {
}
}

private String getParamLocale() {
String locale;
private Language getParamLanguage() {
String language;
try {
locale = this.getParameter("locale");
if (locale != null) {
return locale;
language = this.getParameter("language");
if (language != null) {
return Language.fromString(language);
}
} catch (Exception e) {
}

if (this.codeBaseHost.endsWith("aapeli.com")) {
return "fi";
return Language.FINNISH;
} else if (this.codeBaseHost.endsWith("playray.com")) {
return ENGLISH_LANGUAGE;
return Language.ENGLISH;
} else {
if (this.codeBaseHost.endsWith(".playforia.com")) {
try {
locale = this.codeBaseHost.substring(0, this.codeBaseHost.indexOf(46));
if (locale.length() > 0 && !locale.equals("www")) {
return locale;
language = this.codeBaseHost.substring(0, this.codeBaseHost.indexOf(46));
if (language.length() > 0 && !language.equals("www")) {
return Language.fromString(language);
}
} catch (Exception e) {
}
}

if (this.codeBaseHost.contains("playray")) {
try {
locale = this.codeBaseHost.substring(this.codeBaseHost.lastIndexOf(46) + 1);
if (locale.length() > 0) {
return locale;
language = this.codeBaseHost.substring(this.codeBaseHost.lastIndexOf(46) + 1);
if (language.length() > 0) {
return Language.fromString(language);
}
} catch (Exception e) {
}
}

return ENGLISH_LANGUAGE;
return Language.ENGLISH;
}
}

private String getParamLanguage() {
private Locale getParamLocale() {
try {
String language = this.getParameter("lang");
if (language != null) {
return language;
String locale = this.getParameter("locale");
if (locale != null) {
return Locale.fromString(locale);
}

language = this.getParameter("language");
if (language != null) {
return language;
}
} catch (Exception e) {
}

return null;
}

private String getParamChatLanguage() {
private Locale getParamChatLocale() {
try {
String chatLanguage = this.getParameter("chatlang");
if (chatLanguage != null) {
return chatLanguage;
String chatLocale = this.getParameter("chatlocale");
if (chatLocale != null) {
return Locale.fromString(chatLocale);
}

chatLanguage = this.getParameter("serverlang");
if (chatLanguage != null) {
return chatLanguage;
chatLocale = this.getParameter("serverlocale");
if (chatLocale != null) {
return Locale.fromString(chatLocale);
}
} catch (Exception e) {
}
Expand Down
Loading

0 comments on commit 93d3977

Please sign in to comment.