Advertisement
Guest User

Untitled

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