game: add: max score win

This commit is contained in:
starnakin 2024-02-27 17:05:30 +01:00 committed by AdrienLSH
parent d5ab413806
commit 6edca15931
5 changed files with 47 additions and 9 deletions

View File

@ -12,8 +12,9 @@ class Game
* @param {Client} client * @param {Client} client
* @param {CallableFunction} goal_handler * @param {CallableFunction} goal_handler
* @param {CallableFunction} finish_handler * @param {CallableFunction} finish_handler
* @param {CallableFunction} disconnect_handler
*/ */
constructor(client, id, goal_handler, finish_handler) constructor(client, id, disconnect_handler, goal_handler, finish_handler)
{ {
/** /**
* @type {Client} * @type {Client}
@ -34,6 +35,11 @@ class Game
* @type {CallableFunction} * @type {CallableFunction}
*/ */
this.finish_handler = finish_handler; this.finish_handler = finish_handler;
/**
* @type {CallableFunction}
*/
this.disconnect_handler = disconnect_handler;
} }
/** /**
@ -274,6 +280,11 @@ class Game
await this._receive(data); await this._receive(data);
}; };
this._socket.onclose = async () => {
this._socket = undefined;
await this.disconnect_handler();
};
return this.wait_init(); return this.wait_init();
} }

View File

@ -1,7 +1,7 @@
export function generateRandomColor() export function generateRandomColor()
{ {
return `#${Math.floor(Math.random()*16777215).toString(16)}` return `#${Math.floor(Math.random()*16777215).toString(16)}`;
} }
/** /**
@ -14,7 +14,7 @@ export function transformData(data)
for (let index = 0; index < data.length; index++) { for (let index = 0; index < data.length; index++) {
newData.push({x: Math.round(data[index] / 1000), newData.push({x: Math.round(data[index] / 1000),
y: index}); y: index + 1});
} }
return newData; return newData;
@ -27,8 +27,8 @@ export function range(start, stop, step = 1)
{ {
if (stop === undefined) if (stop === undefined)
{ {
stop = start stop = start;
start = 0 start = 0;
} }
let newArr = []; let newArr = [];
for (let i = start; i <= stop; i += step) for (let i = start; i <= stop; i += step)
@ -53,9 +53,9 @@ export function get_labels(dataset)
let labels = Array.from(labelsSet); let labels = Array.from(labelsSet);
labels.sort(function(a, b){return b - a}); labels.sort(function(a, b){return b - a;});
labels.reverse() labels.reverse();
return labels; return labels;
} }

View File

@ -6,6 +6,7 @@ import { Player } from "../api/game/Player.js";
import { lang } from "../index.js"; import { lang } from "../index.js";
import "https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"; import "https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js";
import { get_labels, transformData } from "../utils/graph.js"; import { get_labels, transformData } from "../utils/graph.js";
import { sleep } from "../utils/sleep.js";
export default class extends AbstractView export default class extends AbstractView
{ {
@ -149,6 +150,11 @@ export default class extends AbstractView
createGraph() createGraph()
{ {
let players = this.game.players;
if (players === undefined)
return;
let graph = document.createElement("canvas"); let graph = document.createElement("canvas");
graph.height = 450; graph.height = 450;
@ -162,7 +168,7 @@ export default class extends AbstractView
let datasets = []; let datasets = [];
this.game.players.forEach(player => { players.forEach(player => {
let data = transformData(player.score); let data = transformData(player.score);
@ -228,9 +234,15 @@ export default class extends AbstractView
}); });
} }
async on_disconnect()
{
sleep(500);
await reloadView();
}
async postInit() async postInit()
{ {
this.game = new Game(client, this.game_id, this.on_goal, this.on_finish); this.game = new Game(client, this.game_id, this.on_disconnect, this.on_goal, this.on_finish);
this.keys_pressed = []; this.keys_pressed = [];
this.my_player = undefined; this.my_player = undefined;

View File

@ -103,6 +103,14 @@ class Game(AbstractRoom):
self.broadcast("goal", {"player_id": goal_taker.user_id, self.broadcast("goal", {"player_id": goal_taker.user_id,
"timestamp": timestamp}) "timestamp": timestamp})
if (len(goal_taker.score) >= config.GAME_MAX_SCORE):
connected_players: list[Player] = self.get_players_connected()
if (len(connected_players) == 2):
self.finish(connected_players[not connected_players.index(goal_taker)])
else:
goal_taker.eliminate()
def get_player_by_user_id(self, user_id: int) -> Player: def get_player_by_user_id(self, user_id: int) -> Player:
for player in self.players: for player in self.players:
@ -151,6 +159,7 @@ class Game(AbstractRoom):
if (self.model.started): if (self.model.started):
if (len(connected_players) == 1): if (len(connected_players) == 1):
print([player.username for player in connected_players])
last_player: Player = connected_players[0] last_player: Player = connected_players[0]
self.finish(last_player) self.finish(last_player)
return return

View File

@ -31,6 +31,12 @@ class Player(Spectator):
self.username: str = User.objects.get(pk = self.user_id).username self.username: str = User.objects.get(pk = self.user_id).username
def eliminate(self):
self.disconnect(1000)
self.game.update_player(self)
def receive(self, data: dict): def receive(self, data: dict):
detail: str = data.get("detail") detail: str = data.get("detail")