jp.ms.common.engine 1.0.0

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.
Files changed (257) hide show
  1. package/dist/base/game-control-player-turn.d.ts +12 -0
  2. package/dist/base/game-control-player-turn.d.ts.map +1 -0
  3. package/dist/base/game-control-player-turn.js +27 -0
  4. package/dist/base/game-control-player-turn.js.map +1 -0
  5. package/dist/base/game-control-restart.d.ts +10 -0
  6. package/dist/base/game-control-restart.d.ts.map +1 -0
  7. package/dist/base/game-control-restart.js +21 -0
  8. package/dist/base/game-control-restart.js.map +1 -0
  9. package/dist/base/game-master.d.ts +34 -0
  10. package/dist/base/game-master.d.ts.map +1 -0
  11. package/dist/base/game-master.js +83 -0
  12. package/dist/base/game-master.js.map +1 -0
  13. package/dist/base/game-player-move.d.ts +13 -0
  14. package/dist/base/game-player-move.d.ts.map +1 -0
  15. package/dist/base/game-player-move.js +28 -0
  16. package/dist/base/game-player-move.js.map +1 -0
  17. package/dist/base/game-player.d.ts +61 -0
  18. package/dist/base/game-player.d.ts.map +1 -0
  19. package/dist/base/game-player.js +140 -0
  20. package/dist/base/game-player.js.map +1 -0
  21. package/dist/base/game-room.d.ts +47 -0
  22. package/dist/base/game-room.d.ts.map +1 -0
  23. package/dist/base/game-room.js +80 -0
  24. package/dist/base/game-room.js.map +1 -0
  25. package/dist/base/game-round.d.ts +28 -0
  26. package/dist/base/game-round.d.ts.map +1 -0
  27. package/dist/base/game-round.js +37 -0
  28. package/dist/base/game-round.js.map +1 -0
  29. package/dist/base/game-state-machine.d.ts +13 -0
  30. package/dist/base/game-state-machine.d.ts.map +1 -0
  31. package/dist/base/game-state-machine.js +21 -0
  32. package/dist/base/game-state-machine.js.map +1 -0
  33. package/dist/base/game-table-seat.d.ts +15 -0
  34. package/dist/base/game-table-seat.d.ts.map +1 -0
  35. package/dist/base/game-table-seat.js +32 -0
  36. package/dist/base/game-table-seat.js.map +1 -0
  37. package/dist/base/game-table-seats.d.ts +21 -0
  38. package/dist/base/game-table-seats.d.ts.map +1 -0
  39. package/dist/base/game-table-seats.js +67 -0
  40. package/dist/base/game-table-seats.js.map +1 -0
  41. package/dist/base/game-table.d.ts +87 -0
  42. package/dist/base/game-table.d.ts.map +1 -0
  43. package/dist/base/game-table.js +130 -0
  44. package/dist/base/game-table.js.map +1 -0
  45. package/dist/base/index.d.ts +13 -0
  46. package/dist/base/index.d.ts.map +1 -0
  47. package/dist/base/index.js +26 -0
  48. package/dist/base/index.js.map +1 -0
  49. package/dist/base/standard-game-state.d.ts +20 -0
  50. package/dist/base/standard-game-state.d.ts.map +1 -0
  51. package/dist/base/standard-game-state.js +57 -0
  52. package/dist/base/standard-game-state.js.map +1 -0
  53. package/dist/base-rooms/fast-tournament-room.d.ts +14 -0
  54. package/dist/base-rooms/fast-tournament-room.d.ts.map +1 -0
  55. package/dist/base-rooms/fast-tournament-room.js +53 -0
  56. package/dist/base-rooms/fast-tournament-room.js.map +1 -0
  57. package/dist/base-rooms/index.d.ts +10 -0
  58. package/dist/base-rooms/index.d.ts.map +1 -0
  59. package/dist/base-rooms/index.js +14 -0
  60. package/dist/base-rooms/index.js.map +1 -0
  61. package/dist/base-rooms/scheduled-tournament-room.d.ts +28 -0
  62. package/dist/base-rooms/scheduled-tournament-room.d.ts.map +1 -0
  63. package/dist/base-rooms/scheduled-tournament-room.js +166 -0
  64. package/dist/base-rooms/scheduled-tournament-room.js.map +1 -0
  65. package/dist/base-rooms/simple-room.d.ts +9 -0
  66. package/dist/base-rooms/simple-room.d.ts.map +1 -0
  67. package/dist/base-rooms/simple-room.js +13 -0
  68. package/dist/base-rooms/simple-room.js.map +1 -0
  69. package/dist/base-rooms/tournament-participant.d.ts +6 -0
  70. package/dist/base-rooms/tournament-participant.d.ts.map +1 -0
  71. package/dist/base-rooms/tournament-participant.js +3 -0
  72. package/dist/base-rooms/tournament-participant.js.map +1 -0
  73. package/dist/base-rooms/tournament-prize.d.ts +7 -0
  74. package/dist/base-rooms/tournament-prize.d.ts.map +1 -0
  75. package/dist/base-rooms/tournament-prize.js +3 -0
  76. package/dist/base-rooms/tournament-prize.js.map +1 -0
  77. package/dist/base-rooms/tournament-room.d.ts +47 -0
  78. package/dist/base-rooms/tournament-room.d.ts.map +1 -0
  79. package/dist/base-rooms/tournament-room.js +111 -0
  80. package/dist/base-rooms/tournament-room.js.map +1 -0
  81. package/dist/base-rooms/tournament-state.d.ts +23 -0
  82. package/dist/base-rooms/tournament-state.d.ts.map +1 -0
  83. package/dist/base-rooms/tournament-state.js +63 -0
  84. package/dist/base-rooms/tournament-state.js.map +1 -0
  85. package/dist/base-rooms/tournament-winner.d.ts +8 -0
  86. package/dist/base-rooms/tournament-winner.d.ts.map +1 -0
  87. package/dist/base-rooms/tournament-winner.js +3 -0
  88. package/dist/base-rooms/tournament-winner.js.map +1 -0
  89. package/dist/board-game/base/board-cell.d.ts +16 -0
  90. package/dist/board-game/base/board-cell.d.ts.map +1 -0
  91. package/dist/board-game/base/board-cell.js +42 -0
  92. package/dist/board-game/base/board-cell.js.map +1 -0
  93. package/dist/board-game/base/board-figure.d.ts +16 -0
  94. package/dist/board-game/base/board-figure.d.ts.map +1 -0
  95. package/dist/board-game/base/board-figure.js +38 -0
  96. package/dist/board-game/base/board-figure.js.map +1 -0
  97. package/dist/board-game/base/board-game-master.d.ts +19 -0
  98. package/dist/board-game/base/board-game-master.d.ts.map +1 -0
  99. package/dist/board-game/base/board-game-master.js +36 -0
  100. package/dist/board-game/base/board-game-master.js.map +1 -0
  101. package/dist/board-game/base/board-player-move.d.ts +11 -0
  102. package/dist/board-game/base/board-player-move.d.ts.map +1 -0
  103. package/dist/board-game/base/board-player-move.js +30 -0
  104. package/dist/board-game/base/board-player-move.js.map +1 -0
  105. package/dist/board-game/base/board-player.d.ts +13 -0
  106. package/dist/board-game/base/board-player.d.ts.map +1 -0
  107. package/dist/board-game/base/board-player.js +31 -0
  108. package/dist/board-game/base/board-player.js.map +1 -0
  109. package/dist/board-game/base/board-table.d.ts +17 -0
  110. package/dist/board-game/base/board-table.d.ts.map +1 -0
  111. package/dist/board-game/base/board-table.js +35 -0
  112. package/dist/board-game/base/board-table.js.map +1 -0
  113. package/dist/board-game/d1-board-game/d1-board-table.d.ts +19 -0
  114. package/dist/board-game/d1-board-game/d1-board-table.d.ts.map +1 -0
  115. package/dist/board-game/d1-board-game/d1-board-table.js +41 -0
  116. package/dist/board-game/d1-board-game/d1-board-table.js.map +1 -0
  117. package/dist/board-game/d2-board-game/d2-board-cell.d.ts +12 -0
  118. package/dist/board-game/d2-board-game/d2-board-cell.d.ts.map +1 -0
  119. package/dist/board-game/d2-board-game/d2-board-cell.js +26 -0
  120. package/dist/board-game/d2-board-game/d2-board-cell.js.map +1 -0
  121. package/dist/board-game/d2-board-game/d2-board-pattern-move-figure.d.ts +29 -0
  122. package/dist/board-game/d2-board-game/d2-board-pattern-move-figure.d.ts.map +1 -0
  123. package/dist/board-game/d2-board-game/d2-board-pattern-move-figure.js +51 -0
  124. package/dist/board-game/d2-board-game/d2-board-pattern-move-figure.js.map +1 -0
  125. package/dist/board-game/d2-board-game/d2-board-table.d.ts +49 -0
  126. package/dist/board-game/d2-board-game/d2-board-table.d.ts.map +1 -0
  127. package/dist/board-game/d2-board-game/d2-board-table.js +184 -0
  128. package/dist/board-game/d2-board-game/d2-board-table.js.map +1 -0
  129. package/dist/board-game/d2-board-game/types.d.ts +5 -0
  130. package/dist/board-game/d2-board-game/types.d.ts.map +1 -0
  131. package/dist/board-game/d2-board-game/types.js +3 -0
  132. package/dist/board-game/d2-board-game/types.js.map +1 -0
  133. package/dist/board-game/dice.d.ts +5 -0
  134. package/dist/board-game/dice.d.ts.map +1 -0
  135. package/dist/board-game/dice.js +14 -0
  136. package/dist/board-game/dice.js.map +1 -0
  137. package/dist/board-game/index.d.ts +13 -0
  138. package/dist/board-game/index.d.ts.map +1 -0
  139. package/dist/board-game/index.js +26 -0
  140. package/dist/board-game/index.js.map +1 -0
  141. package/dist/cards-game/cards/card.d.ts +25 -0
  142. package/dist/cards-game/cards/card.d.ts.map +1 -0
  143. package/dist/cards-game/cards/card.js +45 -0
  144. package/dist/cards-game/cards/card.js.map +1 -0
  145. package/dist/cards-game/cards/cards.d.ts +55 -0
  146. package/dist/cards-game/cards/cards.d.ts.map +1 -0
  147. package/dist/cards-game/cards/cards.js +63 -0
  148. package/dist/cards-game/cards/cards.js.map +1 -0
  149. package/dist/cards-game/cards/deck36.d.ts +2 -0
  150. package/dist/cards-game/cards/deck36.d.ts.map +1 -0
  151. package/dist/cards-game/cards/deck36.js +43 -0
  152. package/dist/cards-game/cards/deck36.js.map +1 -0
  153. package/dist/cards-game/cards/deck52.d.ts +2 -0
  154. package/dist/cards-game/cards/deck52.d.ts.map +1 -0
  155. package/dist/cards-game/cards/deck52.js +59 -0
  156. package/dist/cards-game/cards/deck52.js.map +1 -0
  157. package/dist/cards-game/cards/ranks.d.ts +18 -0
  158. package/dist/cards-game/cards/ranks.d.ts.map +1 -0
  159. package/dist/cards-game/cards/ranks.js +23 -0
  160. package/dist/cards-game/cards/ranks.js.map +1 -0
  161. package/dist/cards-game/cards/suits.d.ts +8 -0
  162. package/dist/cards-game/cards/suits.d.ts.map +1 -0
  163. package/dist/cards-game/cards/suits.js +13 -0
  164. package/dist/cards-game/cards/suits.js.map +1 -0
  165. package/dist/cards-game/cards-game-master.d.ts +24 -0
  166. package/dist/cards-game/cards-game-master.d.ts.map +1 -0
  167. package/dist/cards-game/cards-game-master.js +65 -0
  168. package/dist/cards-game/cards-game-master.js.map +1 -0
  169. package/dist/cards-game/cards-player.d.ts +12 -0
  170. package/dist/cards-game/cards-player.d.ts.map +1 -0
  171. package/dist/cards-game/cards-player.js +29 -0
  172. package/dist/cards-game/cards-player.js.map +1 -0
  173. package/dist/cards-game/cards-table.d.ts +11 -0
  174. package/dist/cards-game/cards-table.d.ts.map +1 -0
  175. package/dist/cards-game/cards-table.js +20 -0
  176. package/dist/cards-game/cards-table.js.map +1 -0
  177. package/dist/cards-game/index.d.ts +11 -0
  178. package/dist/cards-game/index.d.ts.map +1 -0
  179. package/dist/cards-game/index.js +55 -0
  180. package/dist/cards-game/index.js.map +1 -0
  181. package/dist/const.d.ts +3 -0
  182. package/dist/const.d.ts.map +1 -0
  183. package/dist/const.js +6 -0
  184. package/dist/const.js.map +1 -0
  185. package/dist/index.d.ts +6 -0
  186. package/dist/index.d.ts.map +1 -0
  187. package/dist/index.js +22 -0
  188. package/dist/index.js.map +1 -0
  189. package/dist/utils/circle-list.d.ts +29 -0
  190. package/dist/utils/circle-list.d.ts.map +1 -0
  191. package/dist/utils/circle-list.js +103 -0
  192. package/dist/utils/circle-list.js.map +1 -0
  193. package/dist/utils/index.d.ts +4 -0
  194. package/dist/utils/index.d.ts.map +1 -0
  195. package/dist/utils/index.js +9 -0
  196. package/dist/utils/index.js.map +1 -0
  197. package/dist/utils/shuffle.d.ts +3 -0
  198. package/dist/utils/shuffle.d.ts.map +1 -0
  199. package/dist/utils/shuffle.js +16 -0
  200. package/dist/utils/shuffle.js.map +1 -0
  201. package/dist/utils/tests/circle-list.test.d.ts +2 -0
  202. package/dist/utils/tests/circle-list.test.d.ts.map +1 -0
  203. package/dist/utils/tests/circle-list.test.js +60 -0
  204. package/dist/utils/tests/circle-list.test.js.map +1 -0
  205. package/package.json +54 -0
  206. package/readme.md +3 -0
  207. package/src/base/game-control-player-turn.ts +39 -0
  208. package/src/base/game-control-restart.ts +30 -0
  209. package/src/base/game-master.ts +109 -0
  210. package/src/base/game-player-move.ts +33 -0
  211. package/src/base/game-player.ts +184 -0
  212. package/src/base/game-room.ts +127 -0
  213. package/src/base/game-round.ts +62 -0
  214. package/src/base/game-state-machine.ts +25 -0
  215. package/src/base/game-table-seat.ts +37 -0
  216. package/src/base/game-table-seats.ts +78 -0
  217. package/src/base/game-table.ts +237 -0
  218. package/src/base/index.ts +25 -0
  219. package/src/base/standard-game-state.ts +69 -0
  220. package/src/base-rooms/fast-tournament-room.ts +59 -0
  221. package/src/base-rooms/index.ts +19 -0
  222. package/src/base-rooms/scheduled-tournament-room.ts +181 -0
  223. package/src/base-rooms/simple-room.ts +15 -0
  224. package/src/base-rooms/tournament-participant.ts +5 -0
  225. package/src/base-rooms/tournament-prize.ts +7 -0
  226. package/src/base-rooms/tournament-room.ts +169 -0
  227. package/src/base-rooms/tournament-state.ts +78 -0
  228. package/src/base-rooms/tournament-winner.ts +7 -0
  229. package/src/board-game/base/board-cell.ts +50 -0
  230. package/src/board-game/base/board-figure.ts +49 -0
  231. package/src/board-game/base/board-game-master.ts +56 -0
  232. package/src/board-game/base/board-player-move.ts +29 -0
  233. package/src/board-game/base/board-player.ts +43 -0
  234. package/src/board-game/base/board-table.ts +52 -0
  235. package/src/board-game/d1-board-game/d1-board-table.ts +56 -0
  236. package/src/board-game/d2-board-game/d2-board-cell.ts +30 -0
  237. package/src/board-game/d2-board-game/d2-board-pattern-move-figure.ts +79 -0
  238. package/src/board-game/d2-board-game/d2-board-table.ts +262 -0
  239. package/src/board-game/d2-board-game/types.ts +4 -0
  240. package/src/board-game/dice.ts +9 -0
  241. package/src/board-game/index.ts +28 -0
  242. package/src/cards-game/cards/card.ts +60 -0
  243. package/src/cards-game/cards/cards.ts +86 -0
  244. package/src/cards-game/cards/deck36.ts +80 -0
  245. package/src/cards-game/cards/deck52.ts +112 -0
  246. package/src/cards-game/cards/ranks.ts +19 -0
  247. package/src/cards-game/cards/suits.ts +10 -0
  248. package/src/cards-game/cards-game-master.ts +83 -0
  249. package/src/cards-game/cards-player.ts +40 -0
  250. package/src/cards-game/cards-table.ts +28 -0
  251. package/src/cards-game/index.ts +21 -0
  252. package/src/const.ts +3 -0
  253. package/src/index.ts +5 -0
  254. package/src/utils/circle-list.ts +130 -0
  255. package/src/utils/index.ts +8 -0
  256. package/src/utils/shuffle.ts +12 -0
  257. package/src/utils/tests/circle-list.test.ts +72 -0
@@ -0,0 +1,181 @@
1
+ import schedule from 'node-schedule';
2
+ import dayjs from 'dayjs';
3
+ import { chunk, sumBy } from 'lodash';
4
+
5
+ import { GamePlayer } from '../base';
6
+ import { IGameTable } from '../base/game-table';
7
+
8
+ import { TournamentRoom, TournamentRoomConfig } from './tournament-room';
9
+
10
+ export abstract class ScheduledTournamentRoom<
11
+ GT extends IGameTable<GP>,
12
+ GP extends GamePlayer,
13
+ > extends TournamentRoom<GT, GP> {
14
+ private readonly startDate: Date | null;
15
+ private readonly minUserForMergeTables: number;
16
+ private readonly startJob: Promise<schedule.Job> | null;
17
+
18
+ constructor (config: TournamentRoomConfig, minUserForMergeTables, startDate: Date | null) {
19
+ super(config);
20
+ this.minUserForMergeTables = minUserForMergeTables;
21
+ this.startDate = startDate;
22
+ if (startDate) {
23
+ this.startJob = this.scheduleTournament();
24
+ }
25
+ }
26
+
27
+ handleJoinTournament (player: GP) {
28
+ if (this.getState().isWaiting()) {
29
+ this.addUserRoomToQueue(player);
30
+ } else if (this.getState().isInProgress()) {
31
+ if (this.isParticipant(player.getId())) {
32
+ // redirect to table
33
+ } else {
34
+ // redirect
35
+ }
36
+ } else {
37
+ // redirect to status
38
+ }
39
+ }
40
+
41
+ handleLeaveTournament (player: GP) {
42
+ if (this.getState().isWaiting()) {
43
+ this.removeUserFromQueue(player);
44
+ } else if (this.getState().isInProgress()) {
45
+
46
+ }
47
+ }
48
+
49
+ handleFinishRound () {
50
+ }
51
+
52
+ handleRestartRound = (table: GT) => {
53
+ if (this.isTournamentFinish()) {
54
+ this.finishTournament();
55
+ } else if (this.isTableFinish(table)) {
56
+ const lastPlayer = table.getLastPlayer();
57
+ if (lastPlayer) {
58
+ table.removePlayerFromSeat(lastPlayer);
59
+ this.removeTable(table);
60
+ this.processAloneUser(lastPlayer, table);
61
+ }
62
+ } else if (this.isMergeTables(table)) {
63
+ this.mergeTables(table);
64
+ } else {
65
+
66
+ }
67
+ };
68
+
69
+ handleStartTournament = () => {
70
+ this.startTournament();
71
+ };
72
+
73
+ startTournament () {
74
+ const onlineQueuers = this.getOnlineQueuers();
75
+ if (onlineQueuers.length > 1) {
76
+ this.initTables(onlineQueuers);
77
+ this.getTables().forEach((table) => table.startNewRound());
78
+ this.getState().toStart();
79
+ } else {
80
+ this.getState().toCancel();
81
+ }
82
+ }
83
+
84
+ finishTournament () {
85
+ this.getState().toFinish();
86
+ this.calculateWinners();
87
+ }
88
+
89
+ async scheduleTournament (): Promise<schedule.Job> {
90
+ if (this.getState().isWaiting() && dayjs().isBefore(this.startDate)) {
91
+ return schedule.scheduleJob(this.startDate ?? 0, this.handleStartTournament);
92
+ }
93
+ }
94
+
95
+ isTournamentFinish (): boolean {
96
+ return this.remainOneTable() && this.getTables()[0].remainOnePlayer();
97
+ }
98
+
99
+ isTableFinish (table: GT): boolean {
100
+ return !this.remainOneTable() && table.remainOnePlayer();
101
+ }
102
+
103
+ isMergeTables (table: GT) {
104
+ const otherTables = this.getTables().filter((item) => item.getId() !== table.getId());
105
+ const totalSeats = this.getMaxPlayers() * otherTables.length;
106
+ const playersOnSeats = sumBy(otherTables, (item) => item.getPlayersWithQueue().length);
107
+ const freeSeats = totalSeats - playersOnSeats;
108
+
109
+ return freeSeats >= table.getPlayers().length && table.getPlayers().length <= this.minUserForMergeTables;
110
+ }
111
+
112
+ initTables (players: Array<GP>): Array<GT> {
113
+ const newTables: Array<GT> = [];
114
+ const tablesDividedOnTables = chunk<GP>(players, this.getMaxPlayers());
115
+ tablesDividedOnTables.forEach((tablePlayers) => {
116
+ if (tablePlayers.length > 1) {
117
+ const newTable = this.createTable();
118
+ tablePlayers.forEach((player) => this.movePlayerToAnotherTable(player, null, newTable));
119
+ } else {
120
+ this.processAloneUser(tablePlayers[0], null);
121
+ }
122
+ });
123
+
124
+ return newTables;
125
+ }
126
+
127
+ mergeTables (table: GT) {
128
+ this.removeTable(table);
129
+
130
+ const movingUsers = table.getPlayers();
131
+ movingUsers.forEach((movingPlayer) => {
132
+ const freeTable = this.getTableWithFreeSeats();
133
+ if (freeTable) {
134
+ table.removePlayerFromSeat(movingPlayer);
135
+ this.movePlayerToAnotherTable(movingPlayer, table, freeTable);
136
+ }
137
+ });
138
+ }
139
+
140
+ movePlayerToAnotherTable (player: GP, fromTable: GT | null, toTable: GT) {
141
+ if (toTable.isFull()) {
142
+ toTable.addPlayerToQueue(player);
143
+ } else {
144
+ toTable.addPlayerToSeat(player);
145
+ }
146
+ }
147
+
148
+ processAloneUser (aloneUser: GP, fromTable: GT | null) {
149
+ const freeTable = this.getTableWithFreeSeats();
150
+ const redundantPlayers = this.getRedundantPlayers();
151
+ const firstTable = this.getFirstTable();
152
+ aloneUser.flush();
153
+
154
+ if (freeTable) {
155
+ this.movePlayerToAnotherTable(aloneUser, fromTable, freeTable);
156
+ } else if (redundantPlayers.length > 0) {
157
+ redundantPlayers.forEach((user) => user.flush());
158
+ this.initTables([...redundantPlayers, aloneUser]);
159
+ } else if (firstTable) {
160
+ this.movePlayerToAnotherTable(aloneUser, fromTable, firstTable);
161
+ }
162
+ }
163
+
164
+ getRedundantPlayers (): Array<GP> {
165
+ const table = this.getFirstTable();
166
+ if (table) {
167
+ const redundantQueuers = table.getRedundantQueuers();
168
+ redundantQueuers.forEach((user) => {
169
+ table.removePlayerFromQueue(user);
170
+ });
171
+
172
+ return redundantQueuers;
173
+ }
174
+
175
+ return [];
176
+ }
177
+
178
+ async cancelJob () {
179
+ return (await this.startJob)?.cancel();
180
+ }
181
+ }
@@ -0,0 +1,15 @@
1
+ import { GamePlayer, GameRoom } from '../base';
2
+ import { IGameTable } from '../base/game-table';
3
+
4
+ export abstract class SimpleRoom<GT extends IGameTable<GP>, GP extends GamePlayer> extends GameRoom<GT, GP> {
5
+ abstract handleEnterTheTable (player: GP, tableId: string);
6
+
7
+ abstract handleExitTheTable (player: GP, tableId: string);
8
+
9
+ handleFinishRound () {
10
+ // save users balances
11
+ }
12
+
13
+ handleRestartRound () {
14
+ }
15
+ }
@@ -0,0 +1,5 @@
1
+ export interface ITournamentParticipant {
2
+ userId: string;
3
+ userName: string;
4
+ userAvatarUrl: string;
5
+ }
@@ -0,0 +1,7 @@
1
+ export type TournamentPrize = {
2
+ minPlayers: number;
3
+ maxPlayers: number;
4
+ prizes: Record<string, number | undefined>;
5
+ }
6
+
7
+ export type TournamentPrizes = Array<TournamentPrize>;
@@ -0,0 +1,169 @@
1
+ import { first, remove, reverse } from 'lodash';
2
+
3
+ import { GamePlayer, GameRoom } from '../base';
4
+ import { IGameTable } from '../base/game-table';
5
+ import { GameRoomConfig } from '../base/game-room';
6
+
7
+ import { TournamentState } from './tournament-state';
8
+ import { ITournamentWinner } from './tournament-winner';
9
+ import { ITournamentParticipant } from './tournament-participant';
10
+ import { TournamentPrizes } from './tournament-prize';
11
+
12
+ export type TournamentRoomConfig = {
13
+ ticketPrice: number;
14
+ prizeTable: TournamentPrizes;
15
+ } & GameRoomConfig
16
+
17
+ export abstract class TournamentRoom<
18
+ GT extends IGameTable<GP>,
19
+ GP extends GamePlayer,
20
+ > extends GameRoom<GT, GP> {
21
+ private readonly state: TournamentState;
22
+ private readonly prizeTable: TournamentPrizes;
23
+ private queue: Array<GP>;
24
+ private readonly userRating: Array<ITournamentWinner>;
25
+ private winners: Array<ITournamentWinner>;
26
+ private readonly participants: Array<ITournamentParticipant>;
27
+ private readonly ticketPrice: number;
28
+
29
+ constructor (config: TournamentRoomConfig) {
30
+ super(config);
31
+ this.state = new TournamentState();
32
+ this.ticketPrice = config.ticketPrice;
33
+ this.prizeTable = config.prizeTable;
34
+ this.queue = [];
35
+ this.userRating = [];
36
+ this.winners = [];
37
+ this.participants = [];
38
+ }
39
+
40
+ abstract handleJoinTournament (player: GP);
41
+
42
+ abstract handleLeaveTournament (player: GP);
43
+
44
+ abstract startTournament ();
45
+
46
+ abstract finishTournament ();
47
+
48
+ abstract isTournamentFinish (): boolean;
49
+
50
+ getParticipants () {
51
+ return this.participants;
52
+ }
53
+
54
+ addParticipant (participant: GP) {
55
+ return this.participants.push({
56
+ userId: participant.getId(),
57
+ userName: participant.getFullName(),
58
+ userAvatarUrl: participant.getAvatarUrl(),
59
+ });
60
+ }
61
+
62
+ getParticipant (userId: string) {
63
+ return this.participants.find((item) => item.userId === userId);
64
+ }
65
+
66
+ isParticipant (userId: string): boolean {
67
+ return !!this.participants.find((item) => item.userId === userId);
68
+ }
69
+
70
+ getQueue () {
71
+ return this.queue;
72
+ }
73
+
74
+ getOnlineQueuers () {
75
+ return this.queue.filter((item) => item.hasOpenConnection());
76
+ }
77
+
78
+ addUserRoomToQueue (player: GP) {
79
+ const hasThisUser = this.queue.find((queuer) => queuer.getId() === player.getId());
80
+ if (!hasThisUser) {
81
+ player.getInTournamentQueue(this.getId());
82
+ this.queue.push(player);
83
+ }
84
+ }
85
+
86
+ removeUserFromQueue (player: GP) {
87
+ player.quit();
88
+ remove(this.queue, (queuer) => queuer.getId() === player.getId());
89
+ }
90
+
91
+ clearQueue () {
92
+ this.queue.forEach((player) => player.quit());
93
+ this.queue = [];
94
+ }
95
+
96
+ getRatingList () {
97
+ return this.userRating;
98
+ }
99
+
100
+ addPlayerToRating (player: GP) {
101
+ remove(this.userRating, (user) => player.getId() === user.userId);
102
+
103
+ this.userRating.push({
104
+ userId: player.getId(),
105
+ userName: player.getFullName(),
106
+ userAvatarUrl: player.getAvatarUrl() ?? '',
107
+ prize: 0,
108
+ rank: 0,
109
+ });
110
+ }
111
+
112
+ getWinner (): ITournamentWinner | undefined {
113
+ return first(this.winners);
114
+ }
115
+
116
+ getWinners () {
117
+ return this.winners;
118
+ }
119
+
120
+ setWinners (winners: Array<ITournamentWinner>) {
121
+ this.winners = winners;
122
+ }
123
+
124
+ getState (): TournamentState {
125
+ return this.state;
126
+ }
127
+
128
+ isTournament (): boolean {
129
+ return true;
130
+ }
131
+
132
+ calculateWinners () {
133
+ const firstTen = this.getSortedRatingList();
134
+ const winners: Array<ITournamentWinner> = [];
135
+
136
+ for (let i = 0; i < firstTen.length; i += 1) {
137
+ const ratingUser = firstTen[i];
138
+ const rank = i + 1;
139
+ const prize = this.getPrize(rank);
140
+
141
+ winners.push({
142
+ ...ratingUser,
143
+ prize,
144
+ rank,
145
+ });
146
+ }
147
+
148
+ this.setWinners(winners);
149
+ }
150
+
151
+ getSortedRatingList (): Array<ITournamentWinner> {
152
+ return reverse(this.getRatingList());
153
+ }
154
+
155
+ getTournamentFund (): number {
156
+ return this.participants.length * this.ticketPrice;
157
+ }
158
+
159
+ getPrize (rank: number): number {
160
+ const allPlayersCount = this.getRatingList().length;
161
+
162
+ const prize = this.prizeTable.find((item) =>
163
+ allPlayersCount >= item.minPlayers && allPlayersCount <= item.maxPlayers);
164
+
165
+ const rankPrize = prize?.prizes[rank] ?? 0;
166
+
167
+ return rankPrize * this.getTournamentFund() / 100;
168
+ }
169
+ }
@@ -0,0 +1,78 @@
1
+ import { GameStateMachine, StateMachineConfig } from '../base/game-state-machine';
2
+
3
+ export enum TournamentStateMachineStates {
4
+ STATE_WAITING_FOR_START = 'STATE_WAITING_FOR_START',
5
+ STATE_IN_PROGRESS = 'STATE_IN_PROGRESS',
6
+ STATE_FINISHED = 'STATE_FINISHED',
7
+ STATE_CANCELLED = 'STATE_CANCELLED',
8
+ }
9
+
10
+ export enum TournamentStateMachineActions {
11
+ ACTION_START = 'ACTION_START',
12
+ ACTION_FINISH = 'ACTION_FINISH',
13
+ ACTION_CANCEL = 'ACTION_CANCEL',
14
+ }
15
+
16
+ const {
17
+ STATE_WAITING_FOR_START,
18
+ STATE_CANCELLED,
19
+ STATE_FINISHED,
20
+ STATE_IN_PROGRESS,
21
+ } = TournamentStateMachineStates;
22
+
23
+ const {
24
+ ACTION_CANCEL,
25
+ ACTION_FINISH,
26
+ ACTION_START
27
+ } = TournamentStateMachineActions;
28
+
29
+ const config: StateMachineConfig = {
30
+ [STATE_WAITING_FOR_START]: {
31
+ on: {
32
+ [ACTION_START]: STATE_IN_PROGRESS,
33
+ [ACTION_CANCEL]: STATE_CANCELLED,
34
+ },
35
+ },
36
+ [STATE_IN_PROGRESS]: {
37
+ on: {
38
+ [ACTION_FINISH]: STATE_FINISHED,
39
+ [ACTION_CANCEL]: STATE_CANCELLED,
40
+ },
41
+ },
42
+ [STATE_FINISHED]: { on: {} },
43
+ [STATE_CANCELLED]: { on: {} },
44
+ };
45
+
46
+ export class TournamentState extends GameStateMachine<TournamentStateMachineStates, TournamentStateMachineActions> {
47
+ constructor () {
48
+ super(TournamentStateMachineStates.STATE_WAITING_FOR_START, config);
49
+ }
50
+
51
+ isWaiting () {
52
+ return this.getCurrentState() === TournamentStateMachineStates.STATE_WAITING_FOR_START;
53
+ }
54
+
55
+ isCancelled () {
56
+ return this.getCurrentState() === TournamentStateMachineStates.STATE_CANCELLED;
57
+ }
58
+
59
+ isInProgress () {
60
+ return this.getCurrentState() === TournamentStateMachineStates.STATE_IN_PROGRESS;
61
+ }
62
+
63
+ isFinished () {
64
+ return this.getCurrentState() === TournamentStateMachineStates.STATE_FINISHED;
65
+ }
66
+
67
+ toStart () {
68
+ this.send(TournamentStateMachineActions.ACTION_START);
69
+ }
70
+
71
+ toFinish () {
72
+ this.send(TournamentStateMachineActions.ACTION_FINISH);
73
+ }
74
+
75
+ toCancel () {
76
+ this.send(TournamentStateMachineActions.ACTION_CANCEL);
77
+ }
78
+ }
@@ -0,0 +1,7 @@
1
+ export interface ITournamentWinner {
2
+ userId: string;
3
+ userName: string;
4
+ userAvatarUrl: string;
5
+ prize: number;
6
+ rank: number;
7
+ }
@@ -0,0 +1,50 @@
1
+ import { last } from 'lodash';
2
+
3
+ import { BoardFigure } from './board-figure';
4
+
5
+ export class BoardCell<BF extends BoardFigure> {
6
+ private readonly id: number;
7
+ private readonly figures: Array<BF>;
8
+
9
+ constructor (id: number) {
10
+ this.id = id;
11
+ this.figures = [];
12
+ }
13
+
14
+ getId (): number {
15
+ return this.id;
16
+ }
17
+
18
+ getFigures (): Array<BF> {
19
+ return this.figures;
20
+ }
21
+
22
+ getFigure (): BF | null {
23
+ return last(this.figures) ?? null;
24
+ }
25
+
26
+ addFigure (figure: BF): void {
27
+ this.figures.push(figure);
28
+ figure.addCell(this.getId());
29
+ }
30
+
31
+ hasFigure (): boolean {
32
+ return this.figures.length > 0;
33
+ }
34
+
35
+ popFigure (): BF | undefined {
36
+ return this.figures.pop();
37
+ }
38
+
39
+ isFree () {
40
+ return this.figures.length === 0;
41
+ }
42
+
43
+ hasYoursFigure (color: number) {
44
+ return this.getFigure()?.getColor() === color;
45
+ }
46
+
47
+ hasEnemiesFigure (color: number) {
48
+ return this.hasFigure() && !this.hasYoursFigure(color);
49
+ }
50
+ }
@@ -0,0 +1,49 @@
1
+ import { last } from 'lodash';
2
+
3
+ import { GamePlayerColor } from '../../base/game-player';
4
+
5
+ export abstract class BoardFigure {
6
+ private readonly id: number;
7
+ private readonly color: number;
8
+ private readonly cells: Array<number>;
9
+
10
+ constructor (id: number, color: number) {
11
+ this.id = id;
12
+ this.color = color;
13
+ this.cells = [];
14
+ }
15
+
16
+ abstract getType (): number;
17
+
18
+ getId () {
19
+ return this.id;
20
+ }
21
+
22
+ getColor () {
23
+ return this.color;
24
+ }
25
+
26
+ getCells () {
27
+ return this.cells;
28
+ }
29
+
30
+ getCell () {
31
+ return last(this.cells);
32
+ }
33
+
34
+ getState () {
35
+ return 0;
36
+ }
37
+
38
+ addCell (cellId: number) {
39
+ this.cells.push(cellId);
40
+ }
41
+
42
+ isWhite () {
43
+ return this.color === GamePlayerColor.WHITE;
44
+ }
45
+
46
+ isBlack () {
47
+ return this.color === GamePlayerColor.BLACK;
48
+ }
49
+ }
@@ -0,0 +1,56 @@
1
+ import { GameMaster } from '../../base';
2
+ import { IGameRound } from '../../base/game-round';
3
+ import { BoardPlayer } from './board-player';
4
+ import { IGameTable } from '../../base/game-table';
5
+
6
+ import { KillingMove, KillingMoveResult } from '../d2-board-game/d2-board-pattern-move-figure';
7
+ import { BoardCell } from './board-cell';
8
+ import { BoardFigure } from './board-figure';
9
+
10
+ export abstract class BoardGameMaster<
11
+ GT extends IGameTable<GP>,
12
+ GR extends IGameRound<GP>,
13
+ GP extends BoardPlayer,
14
+ GSR,
15
+ > extends GameMaster<GT, GR, GP, GSR> {
16
+ private figuresCount: number = 0;
17
+
18
+ abstract initStartFigures ();
19
+
20
+ abstract figuresStartPositions: Array<Array<number>>;
21
+
22
+ setFiguresStartPositions (figures: Array<Array<number>>) {
23
+ this.figuresStartPositions = figures;
24
+ }
25
+
26
+ findPath (
27
+ possibleMoves: Array<KillingMoveResult>,
28
+ movingFigureId: number | undefined,
29
+ movingFigureNewPosition: number | undefined,
30
+ ): KillingMove | undefined {
31
+ const foundMove = possibleMoves.find((item) => item.figureId === movingFigureId);
32
+
33
+ if (foundMove) {
34
+ return foundMove.paths.find((path) => path.targetCells.find((targetCell) => targetCell === movingFigureNewPosition));
35
+ }
36
+ }
37
+
38
+ killFigure (cell: BoardCell<BoardFigure> | null) {
39
+ const killedFigure = cell?.popFigure();
40
+ const rival = this.table.getPlayerByColor(killedFigure?.getColor() ?? 0);
41
+ rival?.popFigure(killedFigure?.getId() ?? 0);
42
+ }
43
+
44
+ isFinish () {
45
+ return this.table.getPlayers().filter((item) => item.hasNoFigures()).length > 0;
46
+ }
47
+
48
+ getFiguresCount () {
49
+ return this.figuresCount;
50
+ }
51
+
52
+ increaseFiguresCount () {
53
+ this.figuresCount += 1;
54
+ }
55
+ }
56
+
@@ -0,0 +1,29 @@
1
+ import { GamePlayerMove } from '../../base/game-player-move';
2
+
3
+ export class BoardPlayerMove {
4
+ private readonly move?: GamePlayerMove;
5
+
6
+ constructor (move: GamePlayerMove | undefined) {
7
+ this.move = move;
8
+ }
9
+
10
+ getRoomId () {
11
+ return this.move?.getRoomId();
12
+ }
13
+
14
+ getTableId () {
15
+ return this.move?.getTableId();
16
+ }
17
+
18
+ getAction () {
19
+ return this.move?.getAction();
20
+ }
21
+
22
+ getFigureId (): number | undefined {
23
+ return parseInt(this.move?.getAttr('figureId') ?? '', 10);
24
+ }
25
+
26
+ getFigureNewPosition (): number | undefined {
27
+ return parseInt(this.move?.getAttr('figureNewPosition') ?? '', 10);
28
+ }
29
+ }
@@ -0,0 +1,43 @@
1
+ import { remove } from 'lodash';
2
+
3
+ import { GamePlayer } from '../../base';
4
+ import { BoardFigure } from './board-figure';
5
+
6
+ export abstract class BoardPlayer extends GamePlayer {
7
+ private readonly figures: Array<BoardFigure>;
8
+
9
+ constructor (
10
+ id: string,
11
+ fullName: string,
12
+ avatarUrl: string,
13
+ balance: number,
14
+ color: number,
15
+ ) {
16
+ super(id, fullName, avatarUrl, balance, color);
17
+ this.figures = [];
18
+ }
19
+
20
+ getFigures (): Array<BoardFigure> {
21
+ return this.figures;
22
+ }
23
+
24
+ hasNoFigures (): boolean {
25
+ return this.figures.length === 0;
26
+ }
27
+
28
+ addFigure (figure: BoardFigure): void {
29
+ this.figures.push(figure);
30
+ }
31
+
32
+ popFigure (figureId: number) {
33
+ remove(this.figures, (item) => item.getId() === figureId);
34
+ }
35
+
36
+ findFigure (figureId: number) {
37
+ return this.figures.find((item) => item.getId() === figureId);
38
+ }
39
+
40
+ hasFigure (figureId: number) {
41
+ return !!this.findFigure(figureId);
42
+ }
43
+ }