-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Lost-Fly/dev-map
Working client-server app logic | Map | Server | Heroes | Multiplayer
- Loading branch information
Showing
139 changed files
with
4,235 additions
and
674 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
android/src/com/aqwsxlostfly/packandgo/AndroidLauncher.java
This file was deleted.
Oops, something went wrong.
145 changes: 145 additions & 0 deletions
145
android/src/com/aqwsxlostfly/packandgo/client/AndroidLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
android/src/com/aqwsxlostfly/packandgo/client/dto/InputStateImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
android/src/com/aqwsxlostfly/packandgo/client/ws/EventListenerCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
34 changes: 34 additions & 0 deletions
34
android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
android/src/com/aqwsxlostfly/packandgo/client/ws/WsEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.