Skip to content

Commit

Permalink
Add client side support.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpm committed Nov 7, 2024
1 parent f89ce13 commit 746c9d2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 27 deletions.
16 changes: 16 additions & 0 deletions dlgr/griduniverse/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,7 @@ def game_loop(self):
class Griduniverse(Experiment):
"""Define the structure of the experiment."""

channel = "griduniverse_ctrl"
state_count = 0
replay_path = "/grid"

Expand Down Expand Up @@ -1839,6 +1840,10 @@ def parse_message(self, raw_message):
message = json.loads(body)
return channel, message

def publish(self, msg):
"""Publish a message to all griduniverse clients"""
self.redis_conn.publish(self.channel, json.dumps(msg))

def handle_connect(self, msg):
player_id = msg["player_id"]
if self.config.get("replay", False):
Expand All @@ -1860,6 +1865,17 @@ def handle_connect(self, msg):
game.node_by_player_id[player_id] = node.id
self.session.add(node)
self.session.commit()
# Send a broadcast message
self.publish(
"griduniverse",
{
"type": "player_added",
"player_id": player_id,
"game": network.id,
"broadcast_channel": game.broadcast_channel,
"control_channel": game.control_channel,
},
)
logger.info("Spawning player on the grid...")
# We use the current node id modulo the number of colours
# to pick the user's colour. This ensures that players are
Expand Down
12 changes: 11 additions & 1 deletion dlgr/griduniverse/static/scripts/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,15 @@
);
}

function onPlayerAdded(msg) {
var newPlayerId = msg.player_id,
ego = players.ego();

if (ego && newPlayerId === ego.id) {
socket.addGameChannels(msg.broadcast_channel, msg.control_channel);
}
}

function onMoveRejected(msg) {
var offendingPlayerId = msg.player_id,
ego = players.ego();
Expand Down Expand Up @@ -1288,6 +1297,7 @@
stop: gameOverHandler(player_id),
wall_built: addWall,
move_rejection: onMoveRejected,
player_added: onPlayerAdded,
},
};
const socket = new socketlib.GUSocket(socketSettings);
Expand All @@ -1297,7 +1307,7 @@
type: "connect",
player_id: isSpectator ? "spectator" : player_id,
};
socket.send(data);
socket.sendGlobal(data);
});

players.ego_id = player_id;
Expand Down
69 changes: 57 additions & 12 deletions dlgr/griduniverse/static/scripts/dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dlgr/griduniverse/static/scripts/dist/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dlgr/griduniverse/static/scripts/dist/difi.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dlgr/griduniverse/static/scripts/dist/questionnaire.js.map

Large diffs are not rendered by default.

57 changes: 46 additions & 11 deletions dlgr/griduniverse/static/scripts/gusocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@ export class GUSocket {
constructor(settings) {
const tolerance =
settings.lagTolerance === undefined ? 0.1 : settings.lagTolerance;

this.broadcastChannel = settings.broadcast;
this.controlChannel = settings.control;
this.globalBroadcastChannel = settings.broadcast;
this.globalControlChannel = settings.control;
this.callbackMap = settings.callbackMap;
this.socket = this._makeSocket(
settings.endpoint,
this.broadcastChannel,
this.globalBroadcastChannel,
tolerance,
);

this.socket.onmessage = (event) => {
this._globalDispatch(event);
};
}

addGameChannels(broadcastChannel, controlChannel) {
this.broadcastChannel = broadcastChannel;
this.controlChannel = controlChannel;
this.gameSocket = this._makeSocket(
settings.endpoint,
this.broadcastChannel,
tolerance,
);

this.gameSocket.onmessage = (event) => {
this._dispatch(event);
};
}
Expand All @@ -33,6 +46,13 @@ export class GUSocket {
return isOpen;
}

sendGlobal(data) {
const msg = JSON.stringify(data);
const channel = this.globalControlChannel;
console.log(`Sending message to the ${channel} channel: ${msg}`);
this.socket.send(`${channel}:${msg}`);
}

send(data) {
const msg = JSON.stringify(data);
const channel = this.controlChannel;
Expand All @@ -58,6 +78,27 @@ export class GUSocket {
return socket;
}

_baseDispatch(event) {
const msg = JSON.parse(event.data.substring(marker.length));
const callback = this.callbackMap[msg.type];
if (callback !== undefined) {
callback(msg);
} else {
console.log(`Unrecognized message type ${msg.type} from backend.`);
}
}

_globalDispatch(event) {
const marker = `${this.globalBroadcastChannel}:`;
if (!event.data.startsWith(marker)) {
console.log(
`Message was not on channel ${this.globalBroadcastChannel}. Ignoring.`,
);
return;
}
_baseDispatch(event);
}

_dispatch(event) {
const marker = `${this.broadcastChannel}:`;
if (!event.data.startsWith(marker)) {
Expand All @@ -66,12 +107,6 @@ export class GUSocket {
);
return;
}
const msg = JSON.parse(event.data.substring(marker.length));
const callback = this.callbackMap[msg.type];
if (callback !== undefined) {
callback(msg);
} else {
console.log(`Unrecognized message type ${msg.type} from backend.`);
}
_baseDispatch(event);
}
}

0 comments on commit 746c9d2

Please sign in to comment.