Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial server logics #4

Merged
merged 31 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2c6949a
Initial server logics
Lost-Fly Mar 13, 2024
389769b
merge
Lost-Fly Mar 13, 2024
93b6af8
Base WebSocket connection logic
Lost-Fly Mar 19, 2024
fca7191
Server with websocket
Lost-Fly Mar 22, 2024
678151c
UPD
Lost-Fly Mar 22, 2024
bc228b6
Websocket update
Liza12345677564 Mar 27, 2024
49a1839
Initial GameState WS logic
Lost-Fly Mar 30, 2024
eebea55
UPD GameState WS logic
Lost-Fly Mar 30, 2024
947182f
FUlly rearranged WS client logic, ws interaction removed from core sr…
Lost-Fly Mar 31, 2024
8768b72
FUlly rearranged WS client logic, ws interaction removed from core sr…
Lost-Fly Mar 31, 2024
6e9b50c
Merge remote-tracking branch 'origin/server-side' into server-side
Lost-Fly Mar 31, 2024
1c7f944
upd gitignore
Lost-Fly Mar 31, 2024
f688402
restructed ws client-server logic
Lost-Fly Apr 2, 2024
9585494
restructed ws client-server logic
Lost-Fly Apr 2, 2024
1727d6d
Add Beta Lobby SC | Add initial (pooooor) gamesessions...
Lost-Fly Apr 21, 2024
d920ec7
Fully reorganized SERVER LOGIC | ADDED GAME SESSIONS
Lost-Fly Apr 22, 2024
cb3005f
Fully reorganized SERVER LOGIC | ADDED GAME SESSIONS
Lost-Fly Apr 22, 2024
c47a866
Update game logic (bad)
Lost-Fly Apr 30, 2024
7858d3d
upd srv logic
Lost-Fly May 1, 2024
54f1d31
upd srv logic
Lost-Fly May 1, 2024
e860012
upd
Lost-Fly May 4, 2024
087412f
Button on PlayScreen
LizaVeprova301 May 4, 2024
8aae7a9
Changed collision logic to BOX2D
Lost-Fly May 5, 2024
04e0354
Merge branch 'server-side' of https://github.com/Lost-Fly/PackAndGo-A…
LizaVeprova301 May 5, 2024
197d61c
UPD server logic
Lost-Fly May 5, 2024
20154aa
Upd server logic + UPD collision player logic
Lost-Fly May 5, 2024
56fad60
UPDATE CONNECTION LOGIC
Lost-Fly May 5, 2024
a94db36
Add one more error conn state
Lost-Fly May 6, 2024
b0593a5
Merge server-side | new GameHud
Lost-Fly May 6, 2024
f09730c
0705 not working but upd
Lost-Fly May 7, 2024
8e5c8a6
WORKING VERSION UPD BOX2D \ HUD | srever
Lost-Fly May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.getGameScreen().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
Loading