diff --git a/frontend/static/js/utils/formUtils.js b/frontend/static/js/utils/formUtils.js index 54c0717..0222a4e 100644 --- a/frontend/static/js/utils/formUtils.js +++ b/frontend/static/js/utils/formUtils.js @@ -1,8 +1,8 @@ export function clear(property_name, elements_id) { elements_id.forEach(element_id => { - let element = document.getElementById(element_id) - element[property_name] = "" + let element = document.getElementById(element_id); + element[property_name] = ""; }); } diff --git a/frontend/static/js/views/MatchMakingView.js b/frontend/static/js/views/MatchMakingView.js index 8acadd6..8c1b96a 100644 --- a/frontend/static/js/views/MatchMakingView.js +++ b/frontend/static/js/views/MatchMakingView.js @@ -3,6 +3,7 @@ import { clear, fill_errors } from "../utils/formUtils.js"; import AbstractAuthenticatedView from "./abstracts/AbstractAuthenticatedView.js"; export default class extends AbstractAuthenticatedView { + constructor(params) { super(params, "Matchmaking"); @@ -10,6 +11,7 @@ export default class extends AbstractAuthenticatedView { async press_button() { + clear("innerText", ["detail"]); if (client.matchmaking.searching) { client.matchmaking.stop(); @@ -27,8 +29,6 @@ export default class extends AbstractAuthenticatedView { ondisconnect(event) { - if (event.code === 1000) - clear("innerText", ["detail"]); document.getElementById("button").value = "Find a game"; } @@ -50,13 +50,42 @@ export default class extends AbstractAuthenticatedView { async postInit() { - document.getElementById("button").onclick = this.press_button.bind(this); + let button = document.getElementById("button"); + + button.onclick = this.press_button.bind(this); + + let input = document.getElementById("nb_players-input"); + + input.addEventListener('keydown', async ev => { + if (ev.key !== 'Enter') + return; + + if (client.matchmaking.searching) + client.matchmaking.stop(); + + let nb_players = document.getElementById("nb_players-input").value; + + await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), nb_players); + + document.getElementById("button").value = "Stop matchmaking"; + }); + + let update = () => { + if (input.value < 2) + button.disabled = true; + else + button.disabled = false; + }; + + ["change", "oninput"].forEach((event_name) => { + input.addEventListener(event_name, update); + }); } async getHtml() { return `

Select mode

- + `; diff --git a/matchmaking/consumers.py b/matchmaking/consumers.py index cb2041d..8480d26 100644 --- a/matchmaking/consumers.py +++ b/matchmaking/consumers.py @@ -23,11 +23,20 @@ class MatchMaking(WebsocketConsumer): self.channel_layer.group_add(self.group_name, self.channel_name) - self.mode = int(self.scope['url_route']['kwargs']['mode']) + self.mode: int = int(self.scope['url_route']['kwargs']['mode']) self.group_name = self.mode - + waiting_room: WaitingRoom = normal.get(self.mode) waiting_room.append(Waiter(user.pk, self)) + + if (self.mode < 2): + data: dict = { + "detail": "The mode must be > 1.", + } + self.send(json.dumps(data)) + self.disconnect(1000) + return + waiting_room.broadcast(f"{len(waiting_room)} / {waiting_room.mode}") if (len(waiting_room) == waiting_room.mode): game_id: int = GameModel().create(waiting_room.get_users_id()) @@ -35,7 +44,8 @@ class MatchMaking(WebsocketConsumer): waiting_room.clear() def disconnect(self, close_code): + super().close(close_code) waiting_room: WaitingRoom = normal.get(self.mode) waiter: Waiter = waiting_room.get_member_by_socket(self) if (waiter is not None): - waiting_room.remove(waiter, 1016) \ No newline at end of file + waiting_room.remove(waiter, close_code) \ No newline at end of file