chkgk / otree_slider_puzzle

Two-player slider puzzle for oTree
MIT License
2 stars 1 forks source link

Convert move_history into a list? #1

Closed yukitakahashi1 closed 4 years ago

yukitakahashi1 commented 4 years ago

Hi Christian, I'd like to write a function in models.py that examines participants' move history. However, the code currently stores the move history as [{"from":[2,1],"to":[2,2]},{"from":[2,2],"to":[2,1]}]. Is it possible to modify the code so that it stores the move history without "from", "to", {}, :, etc., and we can easily convert it into a list? I'm wondering if something like the following are possible: [2,1],[2,2],[2,2],[2,1] or [[2,1],[2,2]],[[2,2],[2,1]].

chkgk commented 4 years ago

Hi,

Currently, the history has the form of a list of dictionaries. Each dictionary has "to" and "from" keys. The values are lists containing the coordinates in the puzzle. I generate this object in javascript, then convert it to a json-formatted string and store it in the move_history field.

To work with this history object in python, you would first convert the string back to a usable object:

# this gives you a list of dictionaries with "to" and "from" keys which reference coordinate lists.
history = json.loads(self.move_history)

# (Note: I assume that you reference move_history from a method inside the player. if you call it from a page, it would be self.player.move_history etc.)

To get the list-only format you have in mind, you could transform it using a list-comprehension:

# [{"from":[2,1],"to":[2,2]},{"from":[2,2],"to":[2,1]}] -> [[2,1],[2,2]],[[2,2],[2,1]]
list_only_history = [[move['from'], move['to']] for move in history]

I hope this helps.

yukitakahashi1 commented 4 years ago

Thank you. It worked perfectly!