Search in sources :

Example 1 with Message

use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.

the class LoadingScreen method show.

@Override
public void show() {
    this.skin = new Skin(Gdx.files.internal(Assets.SKIN));
    this.camera = new OrthographicCamera(1920 / 4, 1080 / 4);
    this.stage = ComputerCombatGame.makeNewStage(camera);
    Pixmap cursor = new Pixmap(Gdx.files.internal(Assets.CURSOR));
    Gdx.graphics.setCursor(Gdx.graphics.newCursor(cursor, 0, 0));
    this.progressBar = new ProgressBar(0, 1, 0.01f, false, skin.get("default-horizontal", ProgressBarStyle.class));
    this.statusLabel = new Label("Loading Assets...", skin);
    Table table = new Table(skin);
    table.setFillParent(true);
    table.add(statusLabel).row();
    table.add(progressBar);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            int index = 0;
            int tries = 0;
            JsonReader json = new JsonReader();
            JsonValue parsed = json.parse(Gdx.files.internal("connections.json"));
            List<String> connections = new ArrayList<>();
            for (JsonValue jsonValue : parsed.child) {
                connections.add(jsonValue.getString("ip") + " " + jsonValue.getString("port"));
            }
            System.out.println(connections);
            boolean connected = false;
            while (connected == false) {
                try {
                    String[] connection = connections.get(index).split(" ");
                    game.setServerAPI(new ServerAPI(Gdx.net.newClientSocket(Net.Protocol.TCP, connection[0], Integer.parseInt(connection[1]), new SocketHints())));
                    connected = true;
                    Thread.sleep(5);
                } catch (Exception e) {
                    tries++;
                    statusLabel.setText("Failed to Connect to server. Trying again. (" + tries + ")");
                    if (tries > 4) {
                        statusLabel.setText("Trying a different server...");
                        tries = 0;
                        index++;
                        index = index % connections.size();
                    }
                }
            }
            game.getServerAPI().sendMessage(new Message(Type.CONNECTION_REQUEST, "CONNECTION_REQUEST"));
        }
    });
    thread.start();
    stage.addActor(table);
}
Also used : Table(com.badlogic.gdx.scenes.scene2d.ui.Table) Message(com.janfic.games.computercombat.network.Message) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) JsonValue(com.badlogic.gdx.utils.JsonValue) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) JsonReader(com.badlogic.gdx.utils.JsonReader) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) ArrayList(java.util.ArrayList) List(java.util.List) ServerAPI(com.janfic.games.computercombat.network.client.ServerAPI) SocketHints(com.badlogic.gdx.net.SocketHints) ProgressBar(com.badlogic.gdx.scenes.scene2d.ui.ProgressBar) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 2 with Message

use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.

the class LoginScreen method show.

@Override
public void show() {
    this.camera = new OrthographicCamera(1920 / 4, 1080 / 4);
    this.stage = ComputerCombatGame.makeNewStage(camera);
    this.toolTipManager = TooltipManager.getInstance();
    this.toolTipManager.instant();
    Gdx.input.setInputProcessor(stage);
    Table table = new Table();
    table.setFillParent(true);
    table.defaults().pad(5);
    Table titleTable = new Table(skin);
    titleTable.setBackground("border");
    Label title = new Label("Login", skin);
    title.setAlignment(Align.center);
    TextButton backButton = new TextButton("Back", skin);
    backButton.addListener(new ClickListener() {

        @Override
        public void clicked(InputEvent event, float x, float y) {
            game.popScreen();
        }
    });
    titleTable.add(backButton);
    titleTable.add(title).growX().row();
    Table leftGroup = new Table();
    BorderedGrid grid = new BorderedGrid(skin);
    Table usernameTable = new Table(skin);
    usernameTable.background("border_filled");
    Label userNameFieldLabel = new Label(" Username: ", skin);
    Image usernameInfo = new Image(skin, "info_icon");
    usernameTable.add(usernameInfo);
    usernameTable.add(userNameFieldLabel);
    Table passwordTable = new Table(skin);
    passwordTable.background("border_filled");
    Label passwordFieldLabel = new Label(" Password: ", skin);
    Image passwordInfo = new Image(skin, "info_icon");
    passwordTable.add(passwordInfo);
    passwordTable.add(passwordFieldLabel);
    userNameField = new TextField("", skin);
    passwordField = new TextField("", skin);
    TextButton loginButton = new TextButton("Login", skin);
    TextTooltip usernameTip = new TextTooltip("Only alphanumeric characters (A-Z,0-9)", toolTipManager, skin);
    usernameInfo.addListener(usernameTip);
    TextTooltip passwordTip = new TextTooltip("Minimum 8 alphanumeric characters (A-Z,0-9)", toolTipManager, skin);
    passwordInfo.addListener(passwordTip);
    TextField.TextFieldFilter alphaNumeric = new TextField.TextFieldFilter() {

        @Override
        public boolean acceptChar(TextField tf, char c) {
            return tf.getText().length() < 12 && (Character.isAlphabetic(c) || Character.isDigit(c));
        }
    };
    userNameField.setTextFieldFilter(alphaNumeric);
    passwordField.setTextFieldFilter(alphaNumeric);
    userNameFieldLabel.setAlignment(Align.right);
    passwordFieldLabel.setAlignment(Align.right);
    passwordField.setPasswordMode(true);
    passwordField.setPasswordCharacter('*');
    passwordField.addListener(new InputListener() {

        @Override
        public boolean keyUp(InputEvent event, int keycode) {
            if (keycode == Input.Keys.ENTER) {
                boolean validSignUp = true;
                if (userNameField.getText().trim().length() <= 0 || userNameField.getText().trim().length() > 12) {
                    validSignUp = false;
                    userNameField.setColor(Color.RED);
                } else {
                    userNameField.setColor(Color.WHITE);
                }
                String password = passwordField.getText().trim();
                if (password.length() < 7) {
                    validSignUp = false;
                    passwordField.setColor(Color.RED);
                } else {
                    passwordField.setColor(Color.WHITE);
                }
                if (validSignUp) {
                    game.getServerAPI().sendMessage(new Message(Type.LOGIN_REQUEST, userNameField.getText().trim() + "," + passwordField.getText().trim()));
                }
            }
            return true;
        }
    });
    grid.pad(10);
    grid.defaults().space(6);
    leftGroup.defaults().space(10);
    // leftGroup.debugAll();
    leftGroup.add(usernameTable).growX().minHeight(25);
    leftGroup.add(userNameField).minHeight(25).row();
    leftGroup.add(passwordTable).growX().minHeight(25);
    leftGroup.add(passwordField).minHeight(25).row();
    leftGroup.add(loginButton).colspan(2).growX().row();
    loginButton.addListener(new ClickListener() {

        @Override
        public void clicked(InputEvent event, float x, float y) {
            boolean validSignUp = true;
            if (userNameField.getText().trim().length() <= 0 || userNameField.getText().trim().length() > 12) {
                validSignUp = false;
                userNameField.setColor(Color.RED);
            } else {
                userNameField.setColor(Color.WHITE);
            }
            String password = passwordField.getText().trim();
            if (password.length() < 7) {
                validSignUp = false;
                passwordField.setColor(Color.RED);
            } else {
                passwordField.setColor(Color.WHITE);
            }
            if (validSignUp) {
                game.getServerAPI().sendMessage(new Message(Type.LOGIN_REQUEST, userNameField.getText().trim() + "," + passwordField.getText().trim()));
            }
        }
    });
    grid.add(leftGroup).grow();
    table.add(titleTable).growX().row();
    table.add(grid).growY().row();
    stage.addActor(table);
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) TextFieldFilter(com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter) Table(com.badlogic.gdx.scenes.scene2d.ui.Table) Message(com.janfic.games.computercombat.network.Message) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) TextFieldFilter(com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) Image(com.badlogic.gdx.scenes.scene2d.ui.Image) BorderedGrid(com.janfic.games.computercombat.actors.BorderedGrid) TextTooltip(com.badlogic.gdx.scenes.scene2d.ui.TextTooltip) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) ClickListener(com.badlogic.gdx.scenes.scene2d.utils.ClickListener)

Example 3 with Message

use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.

the class LoginScreen method showWindow.

public void showWindow(Message message) {
    popup = new Window("", skin);
    popup.defaults().space(5);
    Label messageLabel = new Label("", skin);
    messageLabel.setWrap(true);
    messageLabel.setAlignment(Align.center);
    TextButton okayButton = new TextButton("Okay", skin);
    if (null != message.type) {
        switch(message.type) {
            case ERROR:
                popup.getTitleLabel().setText("ERROR");
                messageLabel.setText("Something went wrong. Try again.");
                popup.add(messageLabel).expand().grow().row();
                okayButton.addListener(new ClickListener() {

                    @Override
                    public void clicked(InputEvent event, float x, float y) {
                        popup.remove();
                    }
                });
                break;
            case PROFILE_INFO:
                Profile profile = SQLAPI.getSingleton().loadProfile(message.getMessage());
                game.setCurrentProfile(profile);
                game.pushScreen(new PlayScreen(game));
                break;
            case VERIFY_WITH_CODE:
                if (verifying == false) {
                    popup.getTitleLabel().setText("Verification");
                    popup.add(messageLabel).growX().row();
                    messageLabel.setText("Enter Verification Code:");
                    TextField codeField = new TextField("", skin);
                    codeField.setTextFieldFilter(new TextFieldFilter.DigitsOnlyFilter());
                    popup.add(codeField).row();
                    okayButton.addListener(new ClickListener() {

                        @Override
                        public void clicked(InputEvent event, float x, float y) {
                            if (codeField.getText().trim().length() > 0) {
                                game.getServerAPI().sendMessage(new Message(Type.VERIFICATION_CODE, userNameField.getText().trim() + "," + codeField.getText().trim()));
                                verifying = true;
                                popup.remove();
                            }
                        }
                    });
                } else {
                    popup.getTitleLabel().setText("Incorrect Verification Code");
                    messageLabel.setText("Incorrect Code. Try again.");
                    popup.add(messageLabel).expand().grow().row();
                    okayButton.addListener(new ClickListener() {

                        @Override
                        public void clicked(InputEvent event, float x, float y) {
                            popup.remove();
                        }
                    });
                    verifying = false;
                }
                break;
            case NO_AUTH:
                popup.getTitleLabel().setText("Incorrect Login");
                messageLabel.setText("Username or Password incorrect. Try Again");
                popup.add(messageLabel).expand().grow().row();
                okayButton.addListener(new ClickListener() {

                    @Override
                    public void clicked(InputEvent event, float x, float y) {
                        popup.remove();
                    }
                });
                break;
            case SUCCESS:
                if (verifying) {
                    popup.getTitleLabel().setText("Success!");
                    messageLabel.setText("Email Successfully Verified!");
                    popup.add(messageLabel).expand().grow().row();
                    okayButton.addListener(new ClickListener() {

                        @Override
                        public void clicked(InputEvent event, float x, float y) {
                            popup.remove();
                            verifying = false;
                        }
                    });
                }
                break;
            default:
                break;
        }
    }
    popup.add(okayButton).row();
    popup.setSize(stage.getWidth() / 2, stage.getHeight() / 2);
    popup.setPosition(stage.getWidth() / 4, stage.getHeight() / 4);
    stage.addActor(popup);
}
Also used : Window(com.badlogic.gdx.scenes.scene2d.ui.Window) TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Message(com.janfic.games.computercombat.network.Message) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) TextFieldFilter(com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter) Profile(com.janfic.games.computercombat.model.Profile) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) ClickListener(com.badlogic.gdx.scenes.scene2d.utils.ClickListener)

Example 4 with Message

use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.

the class MatchScreen method playerMatchComponentsMoveCheck.

private void playerMatchComponentsMoveCheck() {
    if (board.attemptedMove() && matchData.getCurrentState().currentPlayerMove.equals(game.getCurrentProfile().getUID())) {
        Move move = board.getMove();
        game.getServerAPI().sendMessage(new Message(Type.MOVE_REQUEST, json.toJson(move)));
        board.consumeMove();
    }
}
Also used : Message(com.janfic.games.computercombat.network.Message) Move(com.janfic.games.computercombat.model.moves.Move)

Example 5 with Message

use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.

the class ServerAPI method readStream.

private Message readStream() {
    String content = "";
    String line = null;
    try {
        while (isReading) {
        }
        Json json = new Json();
        isReading = true;
        // BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.equals("END")) {
                break;
            }
            content += line + "\n";
        }
        Message m = json.fromJson(Message.class, content);
        isReading = false;
        return m;
    } catch (Exception e) {
        System.out.println("CONTENT: " + content);
        System.out.println("LINE: " + line);
        e.printStackTrace();
        return null;
    }
}
Also used : Message(com.janfic.games.computercombat.network.Message) Json(com.badlogic.gdx.utils.Json) IOException(java.io.IOException)

Aggregations

Message (com.janfic.games.computercombat.network.Message)19 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)6 ClickListener (com.badlogic.gdx.scenes.scene2d.utils.ClickListener)6 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)5 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)4 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)4 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)4 Json (com.badlogic.gdx.utils.Json)4 TextFieldFilter (com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldFilter)3 BorderedGrid (com.janfic.games.computercombat.actors.BorderedGrid)3 TextField (com.badlogic.gdx.scenes.scene2d.ui.TextField)2 Window (com.badlogic.gdx.scenes.scene2d.ui.Window)2 Move (com.janfic.games.computercombat.model.moves.Move)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 UsernameExistsException (software.amazon.awssdk.services.cognitoidentityprovider.model.UsernameExistsException)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 SocketHints (com.badlogic.gdx.net.SocketHints)1 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)1