Advertisement
Guest User

Untitled

a guest
May 13th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.63 KB | None | 0 0
  1. package org.cyberdeck.client.state;
  2.  
  3. import java.util.prefs.Preferences;
  4.  
  5. import org.cyberdeck.client.GameClient;
  6. import org.cyberdeck.client.SessionInfo;
  7. import org.cyberdeck.client.net.XmlRpcClient;
  8. import org.cyberdeck.client.ui.GameGUI;
  9. import org.cyberdeck.client.ui.layout.CancelAccept;
  10. import org.cyberdeck.client.ui.layout.VideoSettings;
  11. import org.cyberdeck.client.ui.widget.LoginPane;
  12. import org.cyberdeck.common.interfaces.Authenticator;
  13. import org.cyberdeck.common.util.Password;
  14. import org.cyberdeck.common.util.XmlRpcStatus;
  15. import org.lwjgl.opengl.Display;
  16. import org.lwjgl.opengl.DisplayMode;
  17. import org.lwjgl.input.Keyboard;
  18. import org.newdawn.slick.GameContainer;
  19. import org.newdawn.slick.Graphics;
  20. import org.newdawn.slick.SlickException;
  21. import org.newdawn.slick.state.BasicGameState;
  22. import org.newdawn.slick.state.StateBasedGame;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25.  
  26. import redstone.xmlrpc.XmlRpcException;
  27. import redstone.xmlrpc.XmlRpcFault;
  28.  
  29. import de.matthiasmann.twl.Button;
  30. import de.matthiasmann.twl.CallbackWithReason;
  31. import de.matthiasmann.twl.DialogLayout;
  32. import de.matthiasmann.twl.EditField;
  33. import de.matthiasmann.twl.Label;
  34. import de.matthiasmann.twl.PopupWindow;
  35. import de.matthiasmann.twl.DialogLayout.Group;
  36. import de.matthiasmann.twl.EditField.Callback;
  37.  
  38. public class LoginState extends BasicGameState {
  39.    
  40.     private static Logger log = LoggerFactory.getLogger(LoginState.class);
  41.    
  42.     int stateID = -1;
  43.     private GameGUI gui;
  44.     private LoginPane root = new LoginPane();
  45.     private Preferences prefs;
  46.     protected final DisplayMode desktopMode;
  47.     protected VideoSettings.CallbackReason vidDlgCloseReason;
  48.     protected boolean closeRequested;
  49.  
  50.     private boolean loginRequested = false;
  51.     private boolean focusRequested = false;
  52.     private boolean forceLogin = false;
  53.  
  54.     private EditField cUsername;
  55.     private EditField cPassword;
  56.     private Button bLogin;
  57.     private PopupWindow forceLoginDlg;
  58.  
  59.     public LoginState(int stateID, GameGUI gui) {
  60.         this.stateID = stateID;
  61.         this.gui = gui;
  62.         desktopMode = Display.getDesktopDisplayMode();
  63.         prefs = Preferences.userNodeForPackage(GameClient.class);
  64.     }
  65.  
  66.     @Override
  67.     public int getID() {
  68.         return this.stateID;
  69.     }
  70.  
  71.     @Override
  72.     public void init(GameContainer gc, StateBasedGame game)
  73.     throws SlickException {
  74.         final PopupWindow settingsDlg = new PopupWindow(root);
  75.         final VideoSettings settings = new VideoSettings(
  76.                 Preferences.userNodeForPackage(GameClient.class),
  77.                 desktopMode);
  78.         settingsDlg.setTheme("settingdialog");
  79.         settingsDlg.add(settings);
  80.         settingsDlg.setCloseOnClickedOutside(false);
  81.         settings.setTheme("settings");
  82.         settings.addCallback(new CallbackWithReason<VideoSettings.CallbackReason>() {
  83.             public void callback(VideoSettings.CallbackReason reason) {
  84.                 vidDlgCloseReason = reason;
  85.                 settingsDlg.closePopup();
  86.                 if (reason == VideoSettings.CallbackReason.ACCEPT) {
  87.                     settings.storeSettings();
  88.                 }
  89.             }
  90.         });
  91.         root.addButton("Settings", "Opens a dialog which might be used to change video settings", new Runnable() {
  92.             public void run() {
  93.                 settings.readSettings();
  94.                 settingsDlg.openPopupCentered();
  95.             }
  96.         });
  97.        
  98.         forceLoginDlg = new PopupWindow(root);
  99.         final CancelAccept cxlAccept = new CancelAccept("This account is already logged in.\n\nDo you wish to disconnect the existing session and login?");
  100.         forceLoginDlg.setTheme("settingdialog");
  101.         forceLoginDlg.add(cxlAccept);
  102.         forceLoginDlg.setCloseOnClickedOutside(false);
  103.         cxlAccept.setTheme("settings");
  104.         cxlAccept.addCallback(new CallbackWithReason<CancelAccept.CallbackReason>() {
  105.             public void callback(CancelAccept.CallbackReason reason) {
  106.                 log.info("in cxlAccept callback.  CallbackReason = "+reason.toString());
  107.                 forceLoginDlg.closePopup();
  108.                 if (reason == CancelAccept.CallbackReason.ACCEPT) {
  109.                     forceLogin = true;
  110.                     loginRequested = true;
  111.                 } else {
  112.                     forceLogin = false;
  113.                     loginRequested = false;
  114.                     cPassword.setText("");
  115.                     cPassword.requestKeyboardFocus();
  116.                     cUsername.setEnabled(true);
  117.                     cPassword.setEnabled(true);
  118.                     bLogin.setEnabled(true);
  119.                 }
  120.             }
  121.         });
  122.        
  123.         root.addButton("Exit", new Runnable() {
  124.             public void run() {
  125.                 closeRequested = true;
  126.             }
  127.         });
  128.  
  129.         Label lUsername = new Label("Username");
  130.         lUsername.setTheme("/loginpane.fpscounter");
  131.         Label lPassword = new Label("Password");
  132.         lPassword.setTheme("/loginpane.fpscounter");
  133.         cUsername = new EditField();
  134.         cUsername.setText(prefs.get("prevUsername", ""));
  135.         cPassword = new EditField();
  136.         cPassword.setPasswordMasking(true);
  137.         bLogin = new Button("Login");
  138.         bLogin.setTheme("/button");
  139.         bLogin.addCallback(new Runnable() {
  140.             public void run() {
  141.                 loginRequested = true;
  142.             }
  143.         });
  144.         cUsername.addCallback(new Callback() {
  145.             public void callback(int key) {
  146.                 if (key == Keyboard.KEY_RETURN) {
  147.                     if (cPassword.getTextLength()>0) {
  148.                         loginRequested = true;
  149.                     } else {
  150.                         cPassword.requestKeyboardFocus();
  151.                     }
  152.                 }
  153.             }
  154.         });
  155.         cPassword.addCallback(new Callback() {
  156.             public void callback(int key) {
  157.                 if (key == Keyboard.KEY_RETURN) {
  158.                     if (cPassword.getTextLength()>0) {
  159.                         loginRequested = true;
  160.                     }
  161.                 }
  162.             }
  163.         });
  164.         bLogin.setTooltipContent("Login to the game");
  165.         root.setStatus("Please enter your username and password to login");
  166.         DialogLayout layout = root.getLayout();
  167.  
  168.         Group ghLabels = layout.createParallelGroup().addWidget(lUsername).addWidget(lPassword);
  169.         Group ghControls = layout.createParallelGroup().addWidget(cUsername).addWidget(cPassword).addGap(100);
  170.         Group vRowA = layout.createParallelGroup().addWidget(lUsername).addWidget(cUsername);
  171.         Group vRowB = layout.createParallelGroup().addWidget(lPassword).addWidget(cPassword);
  172.         layout.setHorizontalGroup(layout.createParallelGroup().
  173.                 addGroup(layout.createSequentialGroup().addGroup(ghLabels).addGroup(ghControls)).
  174.                 addGroup(layout.createSequentialGroup().addGap().addWidget(bLogin).addGap()));
  175.         layout.setVerticalGroup(layout.createSequentialGroup().
  176.                 addGroup(layout.createSequentialGroup().addGroup(vRowA).addGroup(vRowB)).
  177.                 addGap(DialogLayout.MEDIUM_GAP, DialogLayout.MEDIUM_GAP, Short.MAX_VALUE).
  178.                 addGroup(layout.createParallelGroup().addWidget(bLogin)));
  179.     }
  180.  
  181.     @Override
  182.     public void render(GameContainer container, StateBasedGame game, Graphics g)
  183.     throws SlickException {
  184.         gui.update();
  185.     }
  186.  
  187.     @Override
  188.     public void update(GameContainer container, StateBasedGame game, int delta)
  189.     throws SlickException {
  190.         // if we've accepted new display settings, apply them
  191.         if (vidDlgCloseReason == VideoSettings.CallbackReason.ACCEPT) {
  192.             gui.reload(container);
  193.         }
  194.         vidDlgCloseReason = null;
  195.  
  196.         // if the exit button has been clicked, exit...
  197.         if (closeRequested) {
  198.             container.exit();
  199.         }
  200.  
  201.         if (loginRequested) {
  202.             // ignore login requests if password field is empty
  203.             if (cPassword.getTextLength()<1) {
  204.                 loginRequested = false;
  205.             } else {
  206.                 Boolean openPopup = false;
  207.                 cUsername.setEnabled(false);
  208.                 cPassword.setEnabled(false);
  209.                 bLogin.setEnabled(false);
  210.                 prefs.put("prevUsername", cUsername.getText());
  211.                 root.setStatus("Logging in to server...");
  212.                 Authenticator auth = (Authenticator)XmlRpcClient.get().createProxy(Authenticator.class);
  213.                 String result = null;
  214.                 try {
  215.                     result = auth.login(cUsername.getText(), Password.hash(cPassword.getText()), forceLogin);
  216.                 } catch (XmlRpcException e) {
  217.                     root.setStatus(e.getMessage());
  218.                     cPassword.setText("");
  219.                     cPassword.requestKeyboardFocus();
  220.                 } catch (XmlRpcFault e) {
  221.                     try {
  222.                         if (e.getErrorCode() == XmlRpcStatus.SESSIONEXISTS) {
  223.                             openPopup = true;
  224.                         }
  225.                         root.setStatus(XmlRpcStatus.getMessage(e.getErrorCode()));
  226.                     } catch (Exception e1) {
  227.                         root.setStatus(e1.getMessage());
  228.                     }
  229.                     if (!openPopup) {
  230.                         cPassword.setText("");
  231.                         cPassword.requestKeyboardFocus();
  232.                     }
  233.                 }
  234.                 if (result != null) {
  235.                     root.setStatus("Authentication accepted, entering game...");
  236.                     SessionInfo.get().setSessionId(result);
  237.                     game.enterState(GameClient.PLAYSTATE);
  238.                 }
  239.                 if (!openPopup) {
  240.                     cUsername.setEnabled(true);
  241.                     cPassword.setEnabled(true);
  242.                     bLogin.setEnabled(true);
  243.                 } else {
  244.                     forceLoginDlg.openPopupCentered();
  245.                 }
  246.                 loginRequested = false;
  247.             }
  248.         }
  249.  
  250.         if (!focusRequested) {
  251.             if (cUsername.getTextLength()>0) {
  252.                 cPassword.requestKeyboardFocus();
  253.             } else {
  254.                 cUsername.requestKeyboardFocus();
  255.             }
  256.             focusRequested = true;
  257.         }
  258.     }
  259.    
  260.     @Override
  261.     public void enter(GameContainer container, StateBasedGame game) throws SlickException {
  262.         gui.setRoot(root);
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement