game: add: max score win
This commit is contained in:
parent
d5ab413806
commit
6edca15931
@ -12,8 +12,9 @@ class Game
|
||||
* @param {Client} client
|
||||
* @param {CallableFunction} goal_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}
|
||||
@ -34,6 +35,11 @@ class Game
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this.finish_handler = finish_handler;
|
||||
|
||||
/**
|
||||
* @type {CallableFunction}
|
||||
*/
|
||||
this.disconnect_handler = disconnect_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -274,6 +280,11 @@ class Game
|
||||
await this._receive(data);
|
||||
};
|
||||
|
||||
this._socket.onclose = async () => {
|
||||
this._socket = undefined;
|
||||
await this.disconnect_handler();
|
||||
};
|
||||
|
||||
return this.wait_init();
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
|
||||
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++) {
|
||||
newData.push({x: Math.round(data[index] / 1000),
|
||||
y: index});
|
||||
y: index + 1});
|
||||
}
|
||||
|
||||
return newData;
|
||||
@ -27,8 +27,8 @@ export function range(start, stop, step = 1)
|
||||
{
|
||||
if (stop === undefined)
|
||||
{
|
||||
stop = start
|
||||
start = 0
|
||||
stop = start;
|
||||
start = 0;
|
||||
}
|
||||
let newArr = [];
|
||||
for (let i = start; i <= stop; i += step)
|
||||
@ -53,9 +53,9 @@ export function get_labels(dataset)
|
||||
|
||||
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;
|
||||
}
|
@ -6,6 +6,7 @@ import { Player } from "../api/game/Player.js";
|
||||
import { lang } from "../index.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 { sleep } from "../utils/sleep.js";
|
||||
|
||||
export default class extends AbstractView
|
||||
{
|
||||
@ -149,6 +150,11 @@ export default class extends AbstractView
|
||||
|
||||
createGraph()
|
||||
{
|
||||
let players = this.game.players;
|
||||
|
||||
if (players === undefined)
|
||||
return;
|
||||
|
||||
let graph = document.createElement("canvas");
|
||||
|
||||
graph.height = 450;
|
||||
@ -162,7 +168,7 @@ export default class extends AbstractView
|
||||
|
||||
let datasets = [];
|
||||
|
||||
this.game.players.forEach(player => {
|
||||
players.forEach(player => {
|
||||
|
||||
let data = transformData(player.score);
|
||||
|
||||
@ -228,9 +234,15 @@ export default class extends AbstractView
|
||||
});
|
||||
}
|
||||
|
||||
async on_disconnect()
|
||||
{
|
||||
sleep(500);
|
||||
await reloadView();
|
||||
}
|
||||
|
||||
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.my_player = undefined;
|
||||
|
||||
|
@ -104,6 +104,14 @@ class Game(AbstractRoom):
|
||||
self.broadcast("goal", {"player_id": goal_taker.user_id,
|
||||
"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:
|
||||
for player in self.players:
|
||||
if (player.user_id == user_id):
|
||||
@ -151,6 +159,7 @@ class Game(AbstractRoom):
|
||||
|
||||
if (self.model.started):
|
||||
if (len(connected_players) == 1):
|
||||
print([player.username for player in connected_players])
|
||||
last_player: Player = connected_players[0]
|
||||
self.finish(last_player)
|
||||
return
|
||||
|
@ -31,6 +31,12 @@ class Player(Spectator):
|
||||
|
||||
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):
|
||||
|
||||
detail: str = data.get("detail")
|
||||
|
Loading…
Reference in New Issue
Block a user