|
| 1 | +import { io } from "../../server"; |
| 2 | +import { Match, MatchItem, MatchRequest } from "../types/matchTypes"; |
| 3 | +import { v4 as uuidv4 } from "uuid"; |
| 4 | +import { |
| 5 | + MATCH_ACCEPTANCE_TIMEOUT, |
| 6 | + MATCH_FOUND, |
| 7 | + MATCH_SUCCESSFUL, |
| 8 | + MATCH_TIMEOUT, |
| 9 | + MATCH_UNSUCCESSFUL, |
| 10 | +} from "../utils/constants"; |
| 11 | +import { appendToMatchQueue } from "./queueHandler"; |
| 12 | +import { Socket } from "socket.io"; |
| 13 | + |
| 14 | +const matches: Match = {}; |
| 15 | + |
| 16 | +export const createMatchItem = (socket: Socket, matchRequest: MatchRequest) => { |
| 17 | + const { user, complexities, categories, languages, timeout } = matchRequest; |
| 18 | + |
| 19 | + const matchTimeout = setTimeout(() => { |
| 20 | + socket.emit(MATCH_TIMEOUT); |
| 21 | + }, timeout * 1000); |
| 22 | + |
| 23 | + const matchQueueItem: MatchItem = { |
| 24 | + socket: socket, |
| 25 | + user: user, |
| 26 | + complexities: complexities, |
| 27 | + categories: categories, |
| 28 | + languages: languages, |
| 29 | + timeout: matchTimeout, |
| 30 | + acceptedMatch: false, |
| 31 | + }; |
| 32 | + |
| 33 | + appendToMatchQueue(matchQueueItem); |
| 34 | +}; |
| 35 | + |
| 36 | +export const createMatch = (matchItems: MatchItem[]) => { |
| 37 | + const matchItem1 = matchItems[0]; |
| 38 | + const matchItem2 = matchItems[1]; |
| 39 | + |
| 40 | + clearTimeout(matchItem1.timeout); |
| 41 | + clearTimeout(matchItem2.timeout); |
| 42 | + |
| 43 | + const matchId = uuidv4(); |
| 44 | + matches[matchId] = { |
| 45 | + item1: matchItem1, |
| 46 | + item2: matchItem2, |
| 47 | + timeout: null, |
| 48 | + accepted: false, |
| 49 | + }; |
| 50 | + |
| 51 | + matchItem1.socket.join(matchId); |
| 52 | + matchItem2.socket.join(matchId); |
| 53 | + io.to(matchId).emit(MATCH_FOUND, { |
| 54 | + matchId: matchId, |
| 55 | + user1: matchItem1.user, |
| 56 | + user2: matchItem2.user, |
| 57 | + }); |
| 58 | +}; |
| 59 | + |
| 60 | +export const setMatchTimeout = (matchId: string) => { |
| 61 | + const match = matches[matchId]; |
| 62 | + if (!match) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const timeout = setTimeout(() => { |
| 67 | + io.to(matchId).emit(MATCH_UNSUCCESSFUL); |
| 68 | + delete matches[matchId]; |
| 69 | + }, MATCH_ACCEPTANCE_TIMEOUT); |
| 70 | + |
| 71 | + match.timeout = timeout; |
| 72 | +}; |
| 73 | + |
| 74 | +export const handleMatchAcceptance = (matchId: string) => { |
| 75 | + const match = matches[matchId]; |
| 76 | + if (!match) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (match.accepted) { |
| 81 | + clearTimeout(match.timeout!); |
| 82 | + io.to(matchId).emit(MATCH_SUCCESSFUL); |
| 83 | + delete matches[matchId]; |
| 84 | + } else { |
| 85 | + match.accepted = true; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +export const handleMatchDecline = (matchId: string) => { |
| 90 | + const match = matches[matchId]; |
| 91 | + if (!match) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + clearTimeout(match.timeout!); |
| 96 | + io.to(matchId).emit(MATCH_UNSUCCESSFUL); |
| 97 | + delete matches[matchId]; |
| 98 | +}; |
0 commit comments