Skip to content

Commit

Permalink
Add support for loading map grid from CSV. The parsing is TBD. See #268
Browse files Browse the repository at this point in the history
… and #269
  • Loading branch information
alecpm committed Oct 12, 2023
1 parent 630d01f commit 8dc7ca8
Showing 1 changed file with 30 additions and 9 deletions.
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

0 comments on commit 8dc7ca8

Please sign in to comment.