Skip to content

Commit

Permalink
Merge pull request #6 from Lost-Fly/dev-map
Browse files Browse the repository at this point in the history
Working client-server app logic | Map | Server | Heroes | Multiplayer
  • Loading branch information
Lost-Fly authored May 7, 2024
2 parents 09a5dc4 + 37c5a34 commit 416efb5
Show file tree
Hide file tree
Showing 139 changed files with 4,235 additions and 674 deletions.
Binary file modified .gitignore
Binary file not shown.
6 changes: 5 additions & 1 deletion android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:fullBackupContent="true"
Expand All @@ -13,8 +15,10 @@
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:ignore="UnusedAttribute">


<activity
android:name="com.aqwsxlostfly.packandgo.AndroidLauncher"
android:name="com.aqwsxlostfly.packandgo.client.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout"
Expand Down
16 changes: 0 additions & 16 deletions android/src/com/aqwsxlostfly/packandgo/AndroidLauncher.java

This file was deleted.

145 changes: 145 additions & 0 deletions android/src/com/aqwsxlostfly/packandgo/client/AndroidLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.aqwsxlostfly.packandgo.client;

import android.os.Bundle;

import com.aqwsxlostfly.packandgo.Main;
import com.aqwsxlostfly.packandgo.client.dto.InputStateImpl;
import com.aqwsxlostfly.packandgo.client.ws.EventListenerCallback;
import com.aqwsxlostfly.packandgo.client.ws.WebSocketClient;
import com.aqwsxlostfly.packandgo.client.ws.WebSocketListener;
import com.aqwsxlostfly.packandgo.client.ws.WsEvent;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.java_websocket.handshake.ServerHandshake;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

public class AndroidLauncher extends AndroidApplication {
private MessageProcessor messageProcessor;
private static final int MAX_RECONNECT_ATTEMPTS = 5;

public void connectSocket(WebSocketClient webSocketClient) {
int reconnectAttempts = 0;
while (!webSocketClient.isOpen() && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
try {
webSocketClient.connect();
return;
} catch (Exception e) {
Gdx.app.error("ERROR SOCKET CONNECT", "Attempt " + (reconnectAttempts + 1) + " failed: " + e.getMessage());
reconnectAttempts++;
}
}

Gdx.app.error("ERROR SOCKET CONNECT", "Maximum reconnection attempts reached");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

EventListenerCallback callback = event -> {
messageProcessor.processEvent(event);
};

String wsUri = readWsUriFromProperties();
WebSocketClient webSocketClient = new WebSocketClient(getUri(wsUri), getWebsocketListener(callback));


Main main = new Main(new InputStateImpl());

messageProcessor = new MessageProcessor(main);

main.setMessageSender(message -> {
webSocketClient.send(toJson(message));
});

initialize(main, config);

connectSocket(webSocketClient);

}

private URI getUri(String strUri) {
try {
return new URI(strUri);
} catch (URISyntaxException e) {
Gdx.app.error("CONNECTION ERROR", "INCORRECT URL: " + e.getMessage());
throw new RuntimeException(e);
}
}

private WebSocketListener getWebsocketListener(EventListenerCallback callback) {
WebSocketListener webSocketListener = new WebSocketListener() {
@Override
public void onMessageReceived(String message) {
// Gdx.app.log("MESSAGE RECEIVED", "MESSAGE: " + message);
WsEvent wsEvent = new WsEvent();
wsEvent.setData(message);
callback.onEvent(wsEvent);
}

@Override
public void onConnect(ServerHandshake handshake) {
Gdx.app.log("CONNECTION CREATED", "HTTP_STATUS: " + handshake.getHttpStatusMessage());
WsEvent wsEvent = new WsEvent();
wsEvent.setData("CONNECTION_OPENED");
callback.onEvent(wsEvent);
}

@Override
public void onClose(int code, String reason) {
Gdx.app.log("CONNECTION CLOSED", "CODE: " + code + " REASON: " + reason);
WsEvent wsEvent = new WsEvent();
wsEvent.setData("CONNECTION_CLOSED");
callback.onEvent(wsEvent);
}

@Override
public void onError(Exception ex) {
Gdx.app.error("CONNECTION ERROR", "ERROR_MESSAGE: " + ex.getMessage());
WsEvent wsEvent = new WsEvent();
wsEvent.setData("ERROR_OCCURRED");
callback.onEvent(wsEvent);
}
};

return webSocketListener;
}

private String toJson(Object object) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(object);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


private String readWsUriFromProperties() {
Properties properties = new Properties();
try {
InputStream inputStream = getAssets().open("env.properties");
properties.load(inputStream);
return properties.getProperty("WS_URI");
} catch (IOException e) {
Gdx.app.error("ERROR ENV", "Failed to read WS_URI from properties file: " + e.getMessage());
throw new RuntimeException(e);
}
}


public AndroidApplicationConfiguration getConfig() {
return new AndroidApplicationConfiguration();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.aqwsxlostfly.packandgo.client;

import com.aqwsxlostfly.packandgo.Main;
import com.aqwsxlostfly.packandgo.client.ws.WsEvent;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;


public class MessageProcessor {
private final Main main;

public MessageProcessor(Main main) {
this.main = main;
}

public void processEvent(WsEvent event) {
String data = event.getData();

if (data != null) {

JsonReader jsonReader = new JsonReader();
JsonValue parsed = jsonReader.parse(data);

if (parsed.isArray()) {
processArray(parsed);
} else if (parsed.isObject()) {
processObject(parsed);
}
}
}


private void processArray(JsonValue array) {
for (JsonValue value : array) {
if (value.isObject()) {
processObject(value);
}
}
}

private void processObject(JsonValue object) {
String type = object.getString("class", null);
if (type != null) {
switch (type) {
case "sessionKey":
String meId = object.getString("id");
main.setMeId(meId);
break;
case "sessionRoom":
String sessionId = object.getString("id");
String sessionPassword = object.getString("password");
String sessionMsg = object.getString("msg");
Gdx.app.log("SESSION STATE", "MSG " + sessionMsg);
main.gameSession.setConnected(true);
main.gameSession.setId(sessionId);
main.gameSession.setPassword(sessionPassword);
main.gameSession.setSessionMsg(sessionMsg);
break;
case "evict":
String idToEvict = object.getString("id");
main.evict(idToEvict);
break;
case "player":

String id = object.getString("id");
float x = object.getFloat("x");
float y = object.getFloat("y");

main.renderer.getCurrentScreen().updatePlayerArray(id, x, y);
break;
default:
throw new RuntimeException("Unknown message type " + type);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.aqwsxlostfly.packandgo.client.dto;


import com.aqwsxlostfly.packandgo.session.InputState;

public class InputStateImpl implements InputState {

private String type;

private String id;
private float x;
private float y;

public InputStateImpl() {

}


@Override
public void setType(String type) {
this.type = type;
}

@Override
public String getType() {
return this.type;
}

@Override
public void setId(String id) {
this.id = id;
}

@Override
public String getId() {
return this.id;
}


@Override
public void setX(float x) {
this.x = x;
}

@Override
public float getX() {
return this.x;
}

@Override
public void setY(float y) {
this.y = y;
}

@Override
public float getY() {
return this.y;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.aqwsxlostfly.packandgo.client.ws;

public interface EventListenerCallback {
void onEvent(WsEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.aqwsxlostfly.packandgo.client.ws;

import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

public class WebSocketClient extends org.java_websocket.client.WebSocketClient {
private final WebSocketListener webSocketListener;

public WebSocketClient(URI serverUri, WebSocketListener webSocketListener) {
super(serverUri);
this.webSocketListener = webSocketListener;
}

@Override
public void onOpen(ServerHandshake handshake) {
webSocketListener.onConnect(handshake);
}

@Override
public void onClose(int code, String reason, boolean remote) {
webSocketListener.onClose(code, reason);
}

@Override
public void onMessage(String message) {
webSocketListener.onMessageReceived(message);
}

@Override
public void onError(Exception ex) {
webSocketListener.onError(ex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.aqwsxlostfly.packandgo.client.ws;

import org.java_websocket.handshake.ServerHandshake;

public interface WebSocketListener {
void onMessageReceived(String message);

void onConnect(ServerHandshake handshake);

void onClose(int code, String reason);

void onError(Exception ex);

}

17 changes: 17 additions & 0 deletions android/src/com/aqwsxlostfly/packandgo/client/ws/WsEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.aqwsxlostfly.packandgo.client.ws;

public class WsEvent {

private String data;

public WsEvent() {
}

public final String getData() {
return this.data;
}

public void setData(String data) {
this.data = data;
}
}
Binary file added assets/AlotOfPerson.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 416efb5

Please sign in to comment.