use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.
the class SignUpScreen method render.
@Override
public void render(float f) {
stage.act(f);
stage.draw();
if (game.getServerAPI().hasMessage()) {
Message message = game.getServerAPI().readMessage();
Window window = new Window("", skin);
Label messageLabel = new Label("", skin);
messageLabel.setWrap(true);
TextButton okayButton = new TextButton("Okay", skin);
TextButton xButton = new TextButton("X", skin);
xButton.setColor(Color.RED);
xButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
window.remove();
}
});
window.getTitleTable().add(xButton);
if (message.type == Type.ERROR) {
window.getTitleLabel().setText("ERROR");
messageLabel.setText(message.getMessage());
okayButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
window.remove();
}
});
} else if (message.type == Type.PROFILE_INFO) {
window.getTitleLabel().setText("Success!");
messageLabel.setText("Welcome " + userNameField.getText().trim() + "! Your profile has been created! Login to start playing!");
messageLabel.setWrap(true);
okayButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
game.popScreen();
}
});
}
window.add(messageLabel).expand().grow().row();
window.add(okayButton).row();
window.setSize(stage.getWidth() / 2, stage.getHeight() / 2);
window.setPosition(stage.getWidth() / 4, stage.getHeight() / 4);
stage.addActor(window);
}
}
use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.
the class AWSServices method userLogin.
public Message userLogin(String username, String password) {
System.out.println("[SERVER][AWS]: Attempting Login...");
Map<String, String> params = new HashMap<>();
params.put("USERNAME", username);
params.put("PASSWORD", password);
InitiateAuthRequest request = InitiateAuthRequest.builder().clientId("7ivqqa7m71v3ob6qnof7djh90t").authParameters(params).authFlow(AuthFlowType.USER_PASSWORD_AUTH).build();
try {
InitiateAuthResponse response = cognito.initiateAuth(request);
GetUserRequest userRequest = GetUserRequest.builder().accessToken(response.authenticationResult().accessToken()).build();
List<AttributeType> userAttributes = cognito.getUser(userRequest).userAttributes();
String data = "";
for (AttributeType userAttribute : userAttributes) {
if (userAttribute.name().equals("sub")) {
String sub = userAttribute.value();
data = sub;
}
}
System.out.println("[SERVER][AWS]: Login Successful");
Message m = new Message(Type.PROFILE_INFO, data);
return m;
} catch (NotAuthorizedException e) {
System.out.println("[SERVER][AWS]: Login Failed");
return new Message(Type.NO_AUTH, "NO AUTH");
} catch (UserNotConfirmedException e) {
System.out.println("[SERVER][AWS]: Needs Verification");
return new Message(Type.VERIFY_WITH_CODE, "Please use verification code");
} catch (AwsServiceException | SdkClientException e) {
System.out.println("[SERVER][AWS]: Login Failed due to error.");
e.printStackTrace();
}
return null;
}
use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.
the class Server method readMessage.
public static Message readMessage(InputStream is) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String content = "";
String line;
while (is.available() > 0 && (line = reader.readLine()) != null) {
if (line.equals("END")) {
break;
}
content += line + "\n";
}
Json json = new Json();
Message m = json.fromJson(Message.class, content);
return m;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of com.janfic.games.computercombat.network.Message in project computercombat by janfic.
the class HumanPlayer method getMove.
@Override
public Move getMove() {
// Expect Message in Return
float timeStart = System.nanoTime();
float delta = 0;
while (client.hasMessage() == false && client.getSocket().isConnected()) {
delta = System.nanoTime() - timeStart;
if (delta / 1000000000f >= 10) {
try {
client.sendMessage(new Message(Type.PING, "PING"));
System.out.println("PINGED CLIENT");
timeStart = System.nanoTime();
} catch (Exception e) {
return null;
}
}
}
// Return Retrieved and Deserialized Move
Message m = client.readMessage();
if (m.getType() == Type.MOVE_REQUEST) {
Move move = json.fromJson(Move.class, m.getMessage());
return move;
} else {
return null;
}
// if method returns null, player forfiets?
}
Aggregations