This repository was archived by the owner on Apr 7, 2019. It is now read-only.
-
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.
working ws connection & player updates
- Loading branch information
1 parent
04483fa
commit 4a32b7a
Showing
9 changed files
with
275 additions
and
29 deletions.
There are no files selected for viewing
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
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
65 changes: 65 additions & 0 deletions
65
src/main/java/io/rudin/minetest/tileserver/job/UpdatePlayerJob.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,65 @@ | ||
package io.rudin.minetest.tileserver.job; | ||
|
||
import io.rudin.minetest.tileserver.blockdb.tables.pojos.Player; | ||
import io.rudin.minetest.tileserver.service.EventBus; | ||
import org.jooq.DSLContext; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import java.sql.Timestamp; | ||
import java.util.List; | ||
|
||
import static io.rudin.minetest.tileserver.blockdb.tables.Player.PLAYER; | ||
|
||
@Singleton | ||
public class UpdatePlayerJob implements Runnable { | ||
|
||
@Inject | ||
public UpdatePlayerJob(DSLContext ctx, EventBus eventBus){ | ||
this.ctx = ctx; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
private final DSLContext ctx; | ||
|
||
private final EventBus eventBus; | ||
|
||
private Timestamp timestamp = new Timestamp(0L); | ||
|
||
private boolean running = false; | ||
|
||
@Override | ||
public void run() { | ||
if (running) | ||
return; | ||
|
||
try { | ||
running = true; | ||
|
||
List<Player> players = ctx | ||
.selectFrom(PLAYER) | ||
.where(PLAYER.MODIFICATION_DATE.gt(timestamp)) | ||
.fetch() | ||
.into(Player.class); | ||
|
||
for (Player player : players) { | ||
|
||
Timestamp modificationDate = player.getModificationDate(); | ||
|
||
if (modificationDate.after(timestamp)) { | ||
//Rember newest modification date | ||
this.timestamp = modificationDate; | ||
} | ||
|
||
EventBus.PlayerMovedEvent event = new EventBus.PlayerMovedEvent(); | ||
event.player = player; | ||
eventBus.post(event); | ||
} | ||
} finally { | ||
running = false; | ||
|
||
} | ||
|
||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/io/rudin/minetest/tileserver/service/EventBus.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,20 @@ | ||
package io.rudin.minetest.tileserver.service; | ||
|
||
import io.rudin.minetest.tileserver.blockdb.tables.pojos.Player; | ||
|
||
public interface EventBus { | ||
|
||
void post(Object obj); | ||
|
||
void register(Object listener); | ||
|
||
class TileChangedEvent { | ||
public int x,y,zoom; | ||
public int mapblockX, mapblockZ; | ||
} | ||
|
||
class PlayerMovedEvent { | ||
public Player player; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/rudin/minetest/tileserver/service/impl/EventBusImpl.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 io.rudin.minetest.tileserver.service.impl; | ||
|
||
import io.rudin.minetest.tileserver.TileServer; | ||
import io.rudin.minetest.tileserver.blockdb.tables.Player; | ||
import io.rudin.minetest.tileserver.service.EventBus; | ||
|
||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class EventBusImpl implements EventBus { | ||
|
||
private final com.google.common.eventbus.EventBus eventBus = new com.google.common.eventbus.EventBus(); | ||
|
||
@Override | ||
public void post(Object obj) { | ||
|
||
if (obj instanceof PlayerMovedEvent){ | ||
PlayerMovedEvent e = (PlayerMovedEvent)obj; | ||
System.out.println("Player-move: " + e.player.getName() + " @" + e.player.getPosx() + "/" + e.player.getPosz()); | ||
} | ||
|
||
if (obj instanceof TileChangedEvent){ | ||
//TileChangedEvent e = (TileChangedEvent)obj; | ||
//System.out.println("Mapblock changed: " + e.mapblockX + "/" + e.mapblockZ + " (Coordinates: " + e.mapblockX*16 + "/" + e.mapblockZ*16 + ") @ zoom " + e.zoom); | ||
} | ||
|
||
eventBus.post(obj); | ||
} | ||
|
||
@Override | ||
public void register(Object listener) { | ||
eventBus.register(listener); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/io/rudin/minetest/tileserver/ws/WebSocketHandler.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,28 @@ | ||
package io.rudin.minetest.tileserver.ws; | ||
|
||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; | ||
import org.eclipse.jetty.websocket.api.annotations.WebSocket; | ||
|
||
import java.io.IOException; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
@WebSocket | ||
public class WebSocketHandler { | ||
|
||
public static final Queue<Session> sessions = new ConcurrentLinkedQueue<>(); | ||
|
||
@OnWebSocketConnect | ||
public void connected(Session session) { | ||
sessions.add(session); | ||
} | ||
|
||
@OnWebSocketClose | ||
public void closed(Session session, int statusCode, String reason) { | ||
sessions.remove(session); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/io/rudin/minetest/tileserver/ws/WebSocketUpdater.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,75 @@ | ||
package io.rudin.minetest.tileserver.ws; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.eventbus.Subscribe; | ||
import io.rudin.minetest.tileserver.service.EventBus; | ||
import org.eclipse.jetty.websocket.api.Session; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class WebSocketUpdater { | ||
|
||
@Inject | ||
public WebSocketUpdater(EventBus eventBus){ | ||
this.eventBus = eventBus; | ||
this.mapper = new ObjectMapper(); | ||
} | ||
|
||
private final EventBus eventBus; | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public void init(){ | ||
eventBus.register(this); | ||
} | ||
|
||
public static class EventContainer { | ||
public String type; | ||
public Object data; | ||
} | ||
|
||
@Subscribe void onPlayerMove(EventBus.PlayerMovedEvent e){ | ||
try { | ||
EventContainer container = new EventContainer(); | ||
container.data = e; | ||
container.type = "player-move"; | ||
String json = mapper.writeValueAsString(container); | ||
|
||
for (Session session : WebSocketHandler.sessions) { | ||
try { | ||
session.getRemote().sendString(json); | ||
|
||
} catch (Exception e3) { | ||
//TODO | ||
} | ||
} | ||
} catch (Exception e2){ | ||
e2.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
@Subscribe void onTileUpdate(EventBus.TileChangedEvent e){ | ||
try { | ||
EventContainer container = new EventContainer(); | ||
container.data = e; | ||
container.type = "tile-update"; | ||
String json = mapper.writeValueAsString(container); | ||
|
||
for (Session session : WebSocketHandler.sessions) { | ||
try { | ||
session.getRemote().sendString(json); | ||
|
||
} catch (Exception e3) { | ||
//TODO | ||
} | ||
} | ||
} catch (Exception e2){ | ||
e2.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.