fix: matchmaking required min 1 player #5

This commit is contained in:
starnakin 2024-02-20 14:04:38 +01:00
parent 6b42a99404
commit 4d2d4dfb98
3 changed files with 48 additions and 9 deletions

View File

@ -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] = "";
});
}

View File

@ -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 `
<h1>Select mode</h1>
<input type="number" value="2" id="nb_players-input">
<input type="number" value="2" min="1" id="nb_players-input">
<input type="button" value="Find a game" id="button">
<span id="detail"></span>
`;

View File

@ -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)
waiting_room.remove(waiter, close_code)