Skip to content

Commit

Permalink
Adjust session formation to include self-selected games (#147)
Browse files Browse the repository at this point in the history
<!--- Please write your PR name in the present imperative tense.
Examples of that tense are: "Fix issue in the dispatcher where…",
"Improve our handling of…", etc." -->
<!-- For more information on Pull Requests, you can reference here:
https://success.vanillaforums.com/kb/articles/228-using-pull-requests-to-contribute
-->
## Describe Your Changes

I adjusted session creation to allow for self-selected games which
included refactoring some code.

## Non-Obvious Technical Information


## Checklist Before Requesting a Review
- [X] The code runs successfully.

```commandline
HERE IS SOME COMMAND LINE OUTPUT
```
<img width="905" alt="Screenshot 2024-01-30 at 2 18 26 PM"
src="https://github.com/prijatelilab/PrijateliTree/assets/25271399/7eaa4691-cd15-4e82-a283-b47fff8e63f2">


where game_type = 3 is our self_selected game

---------

Co-authored-by: Federico Domínguez Molina <fd.molina@outlook.com>
  • Loading branch information
anisfeld and FedericoDM authored Jan 30, 2024
1 parent 2d1491e commit 4d71039
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 87 deletions.
145 changes: 59 additions & 86 deletions prijateli_tree/app/routers/administration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
NETWORK_TYPE_SEGREGATED,
NETWORK_TYPE_SELF_SELECTED,
NUMBER_OF_GAMES,
NUMBER_OF_PRACTICE_GAMES,
NUMBER_OF_ROUNDS,
NUMBER_OF_SELF_SELECTED_GAMES,
ROLE_ADMIN,
ROLE_STUDENT,
ROLE_SUPER_ADMIN,
Expand Down Expand Up @@ -209,6 +211,7 @@ def create_session(
if num_games is None:
num_games = NUMBER_OF_GAMES
logging.info(f"Setting number of games to {num_games}")

player_ids = [
player_one,
player_two,
Expand Down Expand Up @@ -266,15 +269,15 @@ def create_session(
user_id=p_id,
)
)

db.commit()
db.refresh(session)

game = None
previous_game = None
network_type = [NETWORK_TYPE_INTEGRATED, NETWORK_TYPE_SEGREGATED]
random_score = random.choices(WINNING_SCORES, weights=WINNING_WEIGHTS)[0]

for i in range(2):
for i in range(NUMBER_OF_PRACTICE_GAMES):
if game:
previous_game = game

Expand All @@ -298,25 +301,7 @@ def create_session(
previous_game.next_game_id = game.id
db.commit()

bag = game.game_type.bag
rand_bag = random.sample(bag, len(bag))
position = 1
for p_list in lang_dict.values():
for p in p_list:
session_player = [
sp for sp in session.players if sp.user_id == p.id
][0]
db.add(
GamePlayer(
created_by=user.id,
game_id=game.id,
user_id=p.id,
session_player_id=session_player.id,
position=position,
initial_ball=rand_bag[position - 1],
)
)
position += 1
add_players_to_first_game(lang_dict, game, session, db)

db.commit()
db.refresh(game)
Expand All @@ -339,17 +324,31 @@ def create_session_games(
game,
db: Session = Depends(get_db),
) -> None:
regular_game_count = session.num_games - NUMBER_OF_SELF_SELECTED_GAMES

for i in range(session.num_games):
previous_game = game
game_types = (
db.query(GameType)
.filter(
GameType.network.in_(
[NETWORK_TYPE_INTEGRATED, NETWORK_TYPE_SEGREGATED]

if i < regular_game_count:
game_types = (
db.query(GameType)
.filter(
GameType.network.in_(
[NETWORK_TYPE_INTEGRATED, NETWORK_TYPE_SEGREGATED]
)
)
.all()
)
.all()
)
else:
game_types = (
db.query(GameType)
.filter(
GameType.network.in_([NETWORK_TYPE_SELF_SELECTED]),
GameType.names_hidden.is_(False),
)
.all()
)

game_type = random.choice(game_types)
n_rounds = random.choice(ROUNDS_ARRAY)

Expand All @@ -373,75 +372,49 @@ def create_session_games(
previous_game.next_game_id = game.id
db.commit()

# randomize location in network conditional on language
# one group is always in 1-2-3 and the other 4-5-6
group_1 = [p for p in previous_game.players if p.position in (1, 2, 3)]
random.shuffle(group_1)
add_players_to_game(previous_game, game, db)

group_2 = [p for p in previous_game.players if p.position in (4, 5, 6)]
random.shuffle(group_2)

pos_players = group_1 + group_2
add_players_to_game(pos_players, game, db)


def create_self_selected_games(
session,
game,
db: Session = Depends(get_db),
) -> None:
"""
Creates a self-selected game
"""
for index in range(session.num_games):
previous_game = game
game_types = (
db.query(GameType)
.filter(
GameType.network.in_([NETWORK_TYPE_SELF_SELECTED]),
GameType.names_hidden.is_(False),
def add_players_to_first_game(lang_dict, game, session, db):
user_ids = []
session_players = []
user_lists = lang_dict.values()
for user_list in user_lists:
for user in user_list:
user_ids.append(user.id)
session_players.append(
[sp for sp in session.players if sp.user_id == user.id][0]
)
.all()
)
game_type = random.choice(game_types)
n_rounds = random.choice(ROUNDS_ARRAY)

# Add score
random_score = random.choices(WINNING_SCORES, weights=WINNING_WEIGHTS)[
0
]
rand_bag = random.sample(game.game_type.bag, len(game.game_type.bag))

game = Game(
created_by=session.created_by,
game_session_id=session.id,
game_type_id=game_type.id,
rounds=n_rounds,
winning_score=random_score,
for i, session_player in enumerate(session_players):
db.add(
GamePlayer(
created_by=session.created_by,
game_id=game.id,
user_id=user_ids[i],
session_player_id=session_player.id,
position=i + 1,
initial_ball=rand_bag[i],
)
)

db.add(game)
db.commit()
db.refresh(game)

previous_game.next_game_id = game.id
db.commit()

# Sommething missing here about the network

# randomize location in network conditional on language
# one group is always in 1-2-3 and the other 4-5-6
group_1 = [p for p in previous_game.players if p.position in (1, 2, 3)]
random.shuffle(group_1)
db.commit()
db.refresh(game)

group_2 = [p for p in previous_game.players if p.position in (4, 5, 6)]
random.shuffle(group_2)

pos_players = group_1 + group_2
add_players_to_game(pos_players, game, db)
def add_players_to_game(previous_game, game, db) -> None:
""" """
# randomize location in network conditional on language
# one group is always in 1-2-3 and the other 4-5-6
group_1 = [p for p in previous_game.players if p.position in (1, 2, 3)]
random.shuffle(group_1)

group_2 = [p for p in previous_game.players if p.position in (4, 5, 6)]
random.shuffle(group_2)

def add_players_to_game(pos_players: list[GamePlayer], game, db) -> None:
""" """
pos_players = group_1 + group_2
rand_bag = random.sample(game.game_type.bag, len(game.game_type.bag))

for i, player in enumerate(pos_players):
Expand Down
4 changes: 3 additions & 1 deletion prijateli_tree/app/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
NETWORK_TYPE_SEGREGATED = "segregated"
NETWORK_TYPE_SELF_SELECTED = "self-selected"
NUMBER_OF_ROUNDS = 3
NUMBER_OF_GAMES = 15
NUMBER_OF_PRACTICE_GAMES = 2
NUMBER_OF_SELF_SELECTED_GAMES = 2
NUMBER_OF_GAMES = 12
ROLE_ADMIN = "admin"
ROLE_STUDENT = "student"
ROLE_SUPER_ADMIN = "super-admin"
Expand Down

0 comments on commit 4d71039

Please sign in to comment.