trucoshi 0.3.51 → 0.3.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,21 +2,25 @@ import { IGameLoop } from "./GameLoop";
|
|
|
2
2
|
import { IPlayer } from "./Player";
|
|
3
3
|
import { ITable } from "./Table";
|
|
4
4
|
import { ITeam } from "./Team";
|
|
5
|
+
import { IQueue } from "./Queue";
|
|
5
6
|
export interface IPrivateLobby {
|
|
6
7
|
gameLoop?: IGameLoop;
|
|
7
8
|
lastTeamIdx: 0 | 1;
|
|
8
9
|
_players: Array<IPlayer | {
|
|
9
10
|
id?: undefined;
|
|
10
11
|
session?: undefined;
|
|
12
|
+
teamIdx?: undefined;
|
|
11
13
|
}>;
|
|
12
14
|
get players(): Array<IPlayer>;
|
|
13
15
|
teams: Array<ITeam>;
|
|
14
16
|
maxPlayers: number;
|
|
15
17
|
table: ITable | null;
|
|
18
|
+
queue: IQueue;
|
|
16
19
|
full: boolean;
|
|
17
20
|
ready: boolean;
|
|
18
21
|
started: boolean;
|
|
19
|
-
|
|
22
|
+
_addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): IPlayer;
|
|
23
|
+
addPlayer(key: string, id: string, session: string, teamIdx?: 0 | 1, isOwner?: boolean): Promise<IPlayer>;
|
|
20
24
|
removePlayer(session: string): ILobby;
|
|
21
25
|
calculateReady(): boolean;
|
|
22
26
|
calculateFull(): boolean;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.Lobby = void 0;
|
|
4
13
|
const constants_1 = require("../constants");
|
|
@@ -8,6 +17,7 @@ const Match_1 = require("./Match");
|
|
|
8
17
|
const Player_1 = require("./Player");
|
|
9
18
|
const Table_1 = require("./Table");
|
|
10
19
|
const Team_1 = require("./Team");
|
|
20
|
+
const Queue_1 = require("./Queue");
|
|
11
21
|
function Lobby(teamSize) {
|
|
12
22
|
const lobby = {
|
|
13
23
|
lastTeamIdx: 1,
|
|
@@ -16,6 +26,7 @@ function Lobby(teamSize) {
|
|
|
16
26
|
return lobby._players.filter((player) => Boolean(player && player.id));
|
|
17
27
|
},
|
|
18
28
|
teams: [],
|
|
29
|
+
queue: (0, Queue_1.Queue)(),
|
|
19
30
|
table: null,
|
|
20
31
|
maxPlayers: teamSize ? teamSize * 2 : 6,
|
|
21
32
|
full: false,
|
|
@@ -34,8 +45,14 @@ function Lobby(teamSize) {
|
|
|
34
45
|
lobby.full = lobby.players.length >= lobby.maxPlayers;
|
|
35
46
|
return lobby.full;
|
|
36
47
|
},
|
|
37
|
-
addPlayer(
|
|
48
|
+
addPlayer(...params) {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
return lobby.queue.queue(() => lobby._addPlayer(...params));
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
_addPlayer(key, id, session, teamIdx, isOwner) {
|
|
38
54
|
const exists = lobby.players.find((player) => player.session === session);
|
|
55
|
+
const hasMovedSlots = Boolean(exists);
|
|
39
56
|
if (exists) {
|
|
40
57
|
if (exists.teamIdx === teamIdx) {
|
|
41
58
|
return exists;
|
|
@@ -56,7 +73,8 @@ function Lobby(teamSize) {
|
|
|
56
73
|
}
|
|
57
74
|
const player = (0, Player_1.Player)(key, id, teamIdx !== undefined ? teamIdx : Number(!lobby.lastTeamIdx), isOwner);
|
|
58
75
|
player.setSession(session);
|
|
59
|
-
lobby.lastTeamIdx =
|
|
76
|
+
lobby.lastTeamIdx = player.teamIdx;
|
|
77
|
+
// Find team available slot
|
|
60
78
|
for (let i = 0; i < lobby._players.length; i++) {
|
|
61
79
|
if (!lobby._players[i].id) {
|
|
62
80
|
if (player.teamIdx === 0 && i % 2 === 0) {
|
|
@@ -69,6 +87,21 @@ function Lobby(teamSize) {
|
|
|
69
87
|
}
|
|
70
88
|
}
|
|
71
89
|
}
|
|
90
|
+
if (hasMovedSlots) {
|
|
91
|
+
// Reorder other players to fit possible empty slot left by this player
|
|
92
|
+
for (let i = 0; i < lobby._players.length; i++) {
|
|
93
|
+
if (!lobby._players[i].id) {
|
|
94
|
+
for (let j = i + 2; j < lobby._players.length; j = j + 2) {
|
|
95
|
+
if (lobby._players[j].id) {
|
|
96
|
+
const p = Object.assign({}, lobby._players[j]);
|
|
97
|
+
lobby._players[j] = {};
|
|
98
|
+
lobby._players[i] = p;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
72
105
|
lobby.calculateFull();
|
|
73
106
|
lobby.calculateReady();
|
|
74
107
|
return player;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Queue = void 0;
|
|
4
|
+
const Queue = () => {
|
|
5
|
+
const queue = {
|
|
6
|
+
promise: Promise.resolve(true),
|
|
7
|
+
queue(operation) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
queue.promise = queue.promise.then(operation).then(resolve).catch(resolve);
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
return queue;
|
|
14
|
+
};
|
|
15
|
+
exports.Queue = Queue;
|