Skip to content

Commit

Permalink
Add route to launch session on student computers with bit.ly link
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

- Add route '/games/sessions' that matches available bit.ly link (bit.ly/blueredgame)
- Adjust template to accept session id from instructor
- Use js to manage redirects including older one; previously we used the
standard form with query strings.

## Non-Obvious Technical Information


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

```commandline
2023-12-26 12:58:24 INFO:     Application startup complete.
2023-12-26 12:58:31 INFO:     172.25.0.1:40658 - "GET /games/sessions HTTP/1.1" 200 OK
2023-12-26 12:59:36 INFO:     172.25.0.1:53164 - "GET /games/session/4/ HTTP/1.1" 307 Temporary Redirect
2023-12-26 12:59:36 INFO:     172.25.0.1:53164 - "GET /games/session/4 HTTP/1.1" 200 OK
2023-12-26 12:59:38 INFO:     172.25.0.1:53164 - "GET /games/10/player/59/ready HTTP/1.1" 200 OK
```
  • Loading branch information
anisfeld authored Dec 26, 2023
1 parent 4c3cea3 commit c414163
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
16 changes: 12 additions & 4 deletions prijateli_tree/app/routers/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@
###############################


@router.get("/sessions", response_class=HTMLResponse)
def choose_session_id(request: Request) -> Response:
return templates.TemplateResponse(
"new_session.html",
context={"request": request, "session_id": -1},
)


@router.get("/session/{session_id}", response_class=HTMLResponse)
def route_session_access(
def choose_session_players(
request: Request, session_id: int, db: Session = Depends(get_db)
):
) -> Response:
games = db.query(Game).filter_by(game_session_id=session_id).all()
raise_exception_if_not(games, "session not found or games not created")

Expand All @@ -80,13 +88,13 @@ def route_session_access(
)


@router.get("/{game_id}/ready", response_class=HTMLResponse)
@router.get("/{game_id}/player/{player_id}/ready", response_class=HTMLResponse)
def start_session(
request: Request,
game_id: int,
player_id: int,
db: Session = Depends(get_db),
):
) -> Response:
_, player = get_game_and_player(game_id, player_id, db)
template_text = languages[player.language.abbr]

Expand Down
66 changes: 56 additions & 10 deletions prijateli_tree/app/templates/new_session.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,65 @@
{% extends "base.html" %}
{% block helper_script %}
<script>
function redirectToChoosePlayers() {
// Get the session ID from the input field
const sessionId = document.getElementById("session_id").value;

// Construct the URL with the session ID
const url = "/games/session/" + sessionId + "/";

// Redirect to the generated URL
window.location.href = url;
}

function redirectToSession() {
// Get the session ID from the input field
const playerId = document.getElementById("player_id").value;
const gameId = document.getElementById("game_id").value;

// Construct the URL with the session ID
const url = "/games/" + gameId + "/player/" + playerId + "/ready";

// Redirect to the generated URL
window.location.href = url;
}

</script>
{% endblock helper_script %}
{% block title %}
PrijateliTree - Access Session
{% endblock title %}
{% block content %}
<div class="container-sm flex-container">
<h1>Welcome.</h1>
<h2>Select the name of the student who will sit at this desk.</h2>
<div class="col-sm-4">
<form method="get" action='{{ url_for("start_session", game_id=game_id) }}'>
<!--<label for="num-games" class="col-sm-4 col-form-label"></label>-->
<select id="player_id" class="form-select form-select-sm" name="player_id">
{% for p in players %}<option value="{{ p.id }}">{{ p.user.name_str }}</option>{% endfor %}
</select>
<button type="submit" class="btn btn-primary float-end">Start</button>
</form>
</div>
{% if session_id == -1 %}
<p>Enter the session id.</p>
<div class="col-sm-4">
<form>
<input type="text" id="session_id" name="session_id">
<br>
<br>
<button type="button"
onclick="redirectToChoosePlayers()"
class="btn btn-primary float-start">Next</button>
</form>
</div>
{% else %}
<p>Select the name of the student who will sit at this desk.</p>
<div class="col-sm-4">
<form>
<input type="text" id="game_id" value={{ game_id }} hidden>
<select id="player_id" class="form-select form-select-sm" name="player_id">
{% for p in players %}<option value="{{ p.id }}">{{ p.user.name_str }}</option>{% endfor %}
</select>
<br>
<br>
<!--<button type="submit" class="btn btn-primary float-start">Start</button>-->
<button type="button"
onclick="redirectToSession()"
class="btn btn-primary float-start">Start</button>
</form>
</div>
{% endif %}
</div>
{% endblock content %}

0 comments on commit c414163

Please sign in to comment.