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

WIP: Load Map from CSV Data #277

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 30 additions & 9 deletions dlgr/griduniverse/experiment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The Griduniverse."""

import collections
import csv
import datetime
import itertools
import json
Expand Down Expand Up @@ -94,6 +95,7 @@
"difi_group_label": unicode,
"difi_group_image": unicode,
"fun_survey": bool,
"map_csv": unicode,
"pre_difi_question": bool,
"pre_difi_group_label": unicode,
"pre_difi_group_image": unicode,
Expand Down Expand Up @@ -201,6 +203,7 @@ def __init__(self, **kwargs):
self.visibility_ramp_time = kwargs.get("visibility_ramp_time", 4)
self.background_animation = kwargs.get("background_animation", True)
self.player_overlap = kwargs.get("player_overlap", False)
self.map_csv = kwargs.get("map_csv", None)

# Motion
self.motion_speed_limit = kwargs.get("motion_speed_limit", 8)
Expand Down Expand Up @@ -507,6 +510,21 @@ def compute_payoffs(self):
player.payoff *= inter_proportions[player.color_idx]
player.payoff *= self.dollars_per_point

def load_map(self):
with open(self.map_csv) as csv_file:
grid_state = self.csv_to_grid_state(csv_file)
self.deserialize(grid_state)

def csv_to_grid_state(self, csv_file):
grid_state = {}
reader = csv.reader(csv_file)
for i, row in enumerate(reader):
for j, col in enumerate(row):
# location = (i, j)
# Process each col value
continue
return grid_state

def build_labyrinth(self):
if self.walls_density and not self.wall_locations:
start = time.time()
Expand Down Expand Up @@ -1329,7 +1347,7 @@ def handle_connect(self, msg):
return

logger.info("Client {} has connected.".format(player_id))
client_count = len(self.grid.players)
client_count = len(self.node_by_player_id)
logger.info("Grid num players: {}".format(self.grid.num_players))
if client_count < self.grid.num_players:
participant = self.session.query(dallinger.models.Participant).get(
Expand All @@ -1346,13 +1364,14 @@ def handle_connect(self, msg):
# We use the current node id modulo the number of colours
# to pick the user's colour. This ensures that players are
# allocated to colours uniformly.
self.grid.spawn_player(
id=player_id,
color_name=self.grid.limited_player_color_names[
node.id % self.grid.num_colors
],
recruiter_id=participant.recruiter_id,
)
if player_id not in self.grid.players:
self.grid.spawn_player(
id=player_id,
color_name=self.grid.limited_player_color_names[
node.id % self.grid.num_colors
],
recruiter_id=participant.recruiter_id,
)
else:
logger.info("No free network found for player {}".format(player_id))

Expand Down Expand Up @@ -1693,7 +1712,9 @@ def send_state_thread(self):
def game_loop(self):
"""Update the world state."""
gevent.sleep(0.1)
if not self.config.get("replay", False):
if self.config.get("map_csv", None):
self.grid.load_map()
elif not self.config.get("replay", False):
self.grid.build_labyrinth()
logger.info("Spawning items")
for item_type in self.item_config.values():
Expand Down
10 changes: 10 additions & 0 deletions test/test_griduniverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ def test_handle_connect_adds_player_to_grid(self, exp, a):
exp.handle_connect({"player_id": participant.id})
assert participant.id in exp.grid.players

def test_handle_connect_uses_existing_player_on_grid(self, exp, a):
participant = a.participant()
exp.grid.players[participant.id] = Player(
id=participant.id, color=[0.50, 0.86, 1.00], location=[10, 10]
)
exp.handle_connect({"player_id": participant.id})
assert participant.id in exp.node_by_player_id
assert len(exp.grid.players) == 1
assert len(exp.node_by_player_id) == 1

def test_handle_connect_is_noop_for_spectators(self, exp):
exp.handle_connect({"player_id": "spectator"})
assert exp.node_by_player_id == {}
Expand Down
Loading