|
| 1 | +import type { BoardCell, BoardCellHistory, Cell, Player } from "~/types/IGameBoard" |
| 2 | +import type { BoardCellHistoryServer, BoardCellServer, ConnectionServer, PlayerServer, ScoresServer, Side } from "~/types/IGameBoardServer" |
| 3 | + |
| 4 | + |
| 5 | +let ws: WebSocket | null = null |
| 6 | +interface Options { |
| 7 | + onDraw?: ()=>void, |
| 8 | + onWin?: ()=>void, |
| 9 | +} |
| 10 | + |
| 11 | +export const useGameConnection = (options?: Options) => { |
| 12 | + |
| 13 | + const playerTurn = ref<Side|null>(null) |
| 14 | + const connectionId = ref<string | undefined>() |
| 15 | + const connectionsCount = ref<number>(0) |
| 16 | + const boards = ref<BoardCellServer[]>([]); |
| 17 | + const scores = ref<ScoresServer>({ blue: 0, red: 0 }); |
| 18 | + const connections = ref<ConnectionServer[]>([]); |
| 19 | + const histories = ref<BoardCellHistoryServer[]>([]); |
| 20 | + const players = ref<PlayerServer[]>([]) |
| 21 | + const isScpectactors = computed(()=> players.value.findIndex(({id}) => id === connectionId.value) === -1) |
| 22 | + const isPlayer = computed(()=> players.value.findIndex(({id}) => id === connectionId.value) > -1) |
| 23 | + const playerSide = computed(()=> players.value.find(({id}) => id === connectionId.value)?.side) |
| 24 | + const winnerPlayer = ref<PlayerServer|undefined>() |
| 25 | + const isWinner = computed(()=> winnerPlayer.value?.id === connectionId.value) |
| 26 | + const waitingPlayer = computed(()=> players.value.length < 2) |
| 27 | + |
| 28 | + const connect = () => { |
| 29 | + |
| 30 | + const config = useRuntimeConfig() |
| 31 | + |
| 32 | + ws = new WebSocket(config.public.wsBase as string); |
| 33 | + |
| 34 | + // event emmited when connected |
| 35 | + ws.onopen = function () { |
| 36 | + ws?.send(stringifyData("wants-connection-id", {})); |
| 37 | + } |
| 38 | + |
| 39 | + // event emmited when receiving message |
| 40 | + ws.onmessage = function (ev) { |
| 41 | + const data = ev.data |
| 42 | + try { |
| 43 | + |
| 44 | + const objData = bindDataResponse(data) |
| 45 | + |
| 46 | + if (typeof objData !== 'undefined') { |
| 47 | + switch (objData.key) { |
| 48 | + case 'on-connect': |
| 49 | + if (typeof objData.data.id === 'string') { |
| 50 | + connectionId.value = objData.data?.id as string | undefined |
| 51 | + ws?.send(stringifyData("wants-connections-count", {})) |
| 52 | + } |
| 53 | + break; |
| 54 | + case 'on-connections-update': |
| 55 | + if (typeof objData.data?.count === 'number') { |
| 56 | + connectionsCount.value = objData.data?.count |
| 57 | + connections.value = objData.data?.connections as ConnectionServer[] |
| 58 | + } |
| 59 | + break; |
| 60 | + case 'on-board-update': |
| 61 | + if (typeof objData.data?.boards === 'object' && Array.isArray(objData.data?.boards)) { |
| 62 | + boards.value = objData.data?.boards as BoardCellServer[] |
| 63 | + } |
| 64 | + break; |
| 65 | + case 'on-score-update': |
| 66 | + if (typeof objData.data?.score === 'object') { |
| 67 | + scores.value = objData.data?.score as ScoresServer |
| 68 | + } |
| 69 | + break; |
| 70 | + case 'on-turn-update': |
| 71 | + if (typeof objData.data?.turn === 'string') { |
| 72 | + if(playerTurn.value != objData.data?.turn){ |
| 73 | + options?.onDraw && options?.onDraw() |
| 74 | + } |
| 75 | + playerTurn.value = objData.data?.turn as Side |
| 76 | + } |
| 77 | + break; |
| 78 | + case 'on-winner': |
| 79 | + if (typeof objData.data?.turn === 'string') { |
| 80 | + try { |
| 81 | + const playersCloned = useCloned(players) |
| 82 | + options?.onWin && options?.onWin() |
| 83 | + winnerPlayer.value = playersCloned.cloned.value.find(player => player.side === objData.data?.turn) |
| 84 | + } catch (error) { |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + break; |
| 89 | + case 'on-players-update': |
| 90 | + if (typeof objData.data?.players === 'object' && Array.isArray(objData.data?.players)) { |
| 91 | + try { |
| 92 | + players.value = objData.data?.players as PlayerServer[] |
| 93 | + } catch (error) { |
| 94 | + } |
| 95 | + } |
| 96 | + break; |
| 97 | + case 'on-board-reset': |
| 98 | + winnerPlayer.value = undefined |
| 99 | + break; |
| 100 | + case 'player-disconnect': |
| 101 | + |
| 102 | + break; |
| 103 | + default: |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + } catch (error) { |
| 110 | + |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + ws.onerror = () => { |
| 115 | + connect() |
| 116 | + } |
| 117 | + |
| 118 | + ws.onclose = () => { |
| 119 | + connect() |
| 120 | + } |
| 121 | + ws.addEventListener("player-on-side", (event) => { |
| 122 | + |
| 123 | + }) |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + const playerDraw = (position: { x: Cell; y: Cell }): void => { |
| 128 | + if(ws?.readyState === ws?.OPEN){ |
| 129 | + ws?.send(stringifyData("draw-cell", position)) |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + const loadPlayersAndBoards = (): void => { |
| 134 | + if(ws?.readyState === ws?.OPEN){ |
| 135 | + ws?.send(stringifyData("wants-board-update", {})) |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + const disconnect = () => { |
| 140 | + ws?.close() |
| 141 | + } |
| 142 | + const tryConnect = () => { |
| 143 | + if (ws?.readyState === ws?.CLOSED || !ws) { |
| 144 | + connect() |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return { |
| 149 | + connect, |
| 150 | + disconnect, |
| 151 | + connectionId, |
| 152 | + connectionsCount, |
| 153 | + playerTurn, |
| 154 | + tryConnect, |
| 155 | + waitingPlayer, |
| 156 | + boards, |
| 157 | + histories, |
| 158 | + players, |
| 159 | + playerDraw, |
| 160 | + loadPlayersAndBoards, |
| 161 | + isPlayer, |
| 162 | + isScpectactors, |
| 163 | + winnerPlayer, |
| 164 | + isWinner, |
| 165 | + playerSide, |
| 166 | + connections, |
| 167 | + scores, |
| 168 | + } |
| 169 | +} |
0 commit comments