Skip to content

Commit

Permalink
use socket message for consume item effect
Browse files Browse the repository at this point in the history
  • Loading branch information
cguardia committed Oct 24, 2023
1 parent 5bf1993 commit 56e67d4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
13 changes: 8 additions & 5 deletions dlgr/griduniverse/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,6 @@ def __init__(self, **kwargs):
self.recruiter_id = kwargs.get("recruiter_id", "")
self.add_wall = None
self.current_item = None
self.just_consumed = False

# Determine the player's color. We don't have access to the specific
# gridworld we are running in, so we can't use the `limited_` variables
Expand Down Expand Up @@ -961,8 +960,6 @@ def tremble(self, direction):
def move(self, direction, tremble_rate=None, timestamp=None):
"""Move the player."""

self.just_consumed = False

if not self.grid.movement_enabled:
return

Expand Down Expand Up @@ -1060,7 +1057,6 @@ def serialize(self):
"identity_visible": self.identity_visible,
"recruiter_id": self.recruiter_id,
"current_item": self.current_item and self.current_item.serialize(),
"just_consumed": self.just_consumed,
}


Expand Down Expand Up @@ -1491,7 +1487,6 @@ def handle_build_wall(self, msg):

def handle_item_consume(self, msg):
player = self.grid.players[msg["player_id"]]
player.just_consumed = True
player_item = player.current_item
if player_item is None or not player_item.calories:
error_msg = {
Expand All @@ -1517,6 +1512,14 @@ def handle_item_consume(self, msg):
for player_to in self.grid.players.values():
player_to.score += player_item.public_good

consumed_msg = {
"type": "item_consumed",
"player_id": player.id,
"player_color": player.color,
"player_item": player_item and player_item.serialize(),
}
self.publish(consumed_msg)

def handle_item_pick_up(self, msg):
player = self.grid.players[msg["player_id"]]
player_item = player.current_item
Expand Down
22 changes: 10 additions & 12 deletions dlgr/griduniverse/static/scripts/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ var Player = function (settings, dimness) {
this.identity_visible = settings.identity_visible;
this.dimness = dimness;
this.replaceItem(settings.current_item || null);
this.just_consumed = settings.just_consumed;
return this;
};

Expand Down Expand Up @@ -626,16 +625,6 @@ pixels.frame(function() {
return newColor;
});

// Did someone just consume something?
players.each(function (i, player) {
if (player.just_consumed) {
pixels.canvas.style.border = "5px solid " + player.color;
setTimeout(() => {
pixels.canvas.style.border = "5px solid #000000"
}, 1000);
}
});

for (const [position, item] of gridItems.entries()) {
if (players.isPlayerAt(position)) {
if (!item.interactive && item.calories) {
Expand Down Expand Up @@ -997,6 +986,14 @@ function onMoveRejected(msg) {
}
}

function onItemConsumed(msg) {
console.log("Item consumed by player " + msg.player_id + ": " + msg.player_item.id);
pixels.canvas.style.border = "5px solid " + msg.player_color;
setTimeout(() => {
pixels.canvas.style.border = "5px solid #000000"
}, 1500);
}

function onDonationProcessed(msg) {
var recipient_id = msg.recipient_id,
team_idx,
Expand Down Expand Up @@ -1328,7 +1325,8 @@ $(document).ready(function() {
'new_round': displayLeaderboards,
'stop': gameOverHandler(player_id),
'wall_built': addWall,
'move_rejection': onMoveRejected
'move_rejection': onMoveRejected,
'item_consumed': onItemConsumed
}
};
var socket = new GUSocket(socketSettings);
Expand Down
22 changes: 10 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.

0 comments on commit 56e67d4

Please sign in to comment.