|
| 1 | +import * as Ably from 'ably'; |
| 2 | +import type { Message } from 'ably'; |
| 3 | +import { faker } from '@faker-js/faker'; |
| 4 | +import './styles.css'; |
| 5 | + |
| 6 | +interface MatchOdds { |
| 7 | + match: { |
| 8 | + homeTeam: string; |
| 9 | + awayTeam: string; |
| 10 | + timestamp: string; |
| 11 | + score: string; |
| 12 | + matchOdds: { |
| 13 | + homeWin: string; |
| 14 | + draw: string; |
| 15 | + awayWin: string; |
| 16 | + }; |
| 17 | + nextGoal: { |
| 18 | + [key: string]: string; |
| 19 | + }; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +let matchData: MatchOdds | null = { |
| 24 | + match: { |
| 25 | + homeTeam: 'Royal Knights', |
| 26 | + awayTeam: 'North Rangers', |
| 27 | + timestamp: new Date().toISOString(), |
| 28 | + score: '0-0', |
| 29 | + matchOdds: { |
| 30 | + homeWin: '2.45', |
| 31 | + draw: '3.25', |
| 32 | + awayWin: '2.85', |
| 33 | + }, |
| 34 | + nextGoal: { |
| 35 | + 'Royal Knights': '1.95', |
| 36 | + 'North Rangers': '1.85', |
| 37 | + 'No Goal': '2.75', |
| 38 | + }, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +const preloadButton = document.getElementById('pre-load-odds') as HTMLButtonElement; |
| 43 | +const urlParams = new URLSearchParams(window.location.search); |
| 44 | +const channelName = urlParams.get('name') || 'pub-sub-history-rewind'; |
| 45 | +const landingPage = document.getElementById('landing-page'); |
| 46 | +const game = document.getElementById('game'); |
| 47 | +let channel: Ably.RealtimeChannel | null = null; |
| 48 | + |
| 49 | +async function enterGame() { |
| 50 | + landingPage.style.display = 'none'; |
| 51 | + game.style.display = 'block'; |
| 52 | + |
| 53 | + const client = new Ably.Realtime({ |
| 54 | + key: import.meta.env.VITE_PUBLIC_ABLY_KEY as string, |
| 55 | + clientId: faker.person.firstName(), |
| 56 | + }); |
| 57 | + |
| 58 | + channel = client.channels.get(channelName, { |
| 59 | + params: { rewind: '10' }, |
| 60 | + }); |
| 61 | + |
| 62 | + channel.subscribe(async (message) => { |
| 63 | + matchData = message.data; |
| 64 | + await addHistoryItem(message); |
| 65 | + await updateCurrentOdds(message); |
| 66 | + }); |
| 67 | + |
| 68 | + await updateRandomOdds(); |
| 69 | +} |
| 70 | + |
| 71 | +preloadButton.addEventListener('click', async () => { |
| 72 | + preloadButton.disabled = true; |
| 73 | + const client = new Ably.Realtime({ |
| 74 | + key: import.meta.env.VITE_PUBLIC_ABLY_KEY as string, |
| 75 | + clientId: faker.person.firstName(), |
| 76 | + }); |
| 77 | + |
| 78 | + const channel = client.channels.get(channelName); |
| 79 | + |
| 80 | + for (let i = 0; i < 10; i++) { |
| 81 | + const markets = ['homeWin', 'draw', 'awayWin', 'nextGoal']; |
| 82 | + const numMarketsToUpdate = Math.floor(Math.random() * 2) + 1; |
| 83 | + const marketsToUpdate = markets.sort(() => 0.5 - Math.random()).slice(0, numMarketsToUpdate); |
| 84 | + |
| 85 | + marketsToUpdate.forEach((market) => { |
| 86 | + if (market === 'nextGoal') { |
| 87 | + const team = Object.keys(matchData.match.nextGoal)[Math.floor(Math.random() * 3)]; |
| 88 | + matchData.match.nextGoal[team] = (parseFloat(matchData.match.nextGoal[team]) + (Math.random() * 0.2 - 0.1)).toFixed(2); |
| 89 | + } else { |
| 90 | + matchData.match.matchOdds[market] = (parseFloat(matchData.match.matchOdds[market]) + (Math.random() * 0.2 - 0.1)).toFixed(2); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + matchData.match.timestamp = new Date().toISOString(); |
| 95 | + await channel.publish('odds', matchData); |
| 96 | + |
| 97 | + // Show alert for each publish |
| 98 | + const alert = document.createElement('div'); |
| 99 | + alert.className = |
| 100 | + 'fixed bottom-4 right-4 bg-green-500 text-white px-4 py-2 rounded shadow-lg transition-opacity duration-500'; |
| 101 | + alert.textContent = `Update ${i + 1}/10: New odds published`; |
| 102 | + document.body.appendChild(alert); |
| 103 | + |
| 104 | + // Remove alert after 2 seconds |
| 105 | + setTimeout(() => { |
| 106 | + alert.style.opacity = '0'; |
| 107 | + setTimeout(() => alert.remove(), 500); |
| 108 | + }, 2000); |
| 109 | + |
| 110 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 111 | + } |
| 112 | + |
| 113 | + await enterGame(); |
| 114 | +}); |
| 115 | + |
| 116 | +async function updateCurrentOdds(message: Message) { |
| 117 | + const score = document.getElementById('score'); |
| 118 | + score.textContent = message.data.match.score; |
| 119 | + const currentHome = document.getElementById('current-home'); |
| 120 | + currentHome.textContent = message.data.match.matchOdds.homeWin; |
| 121 | + const currentAway = document.getElementById('current-away'); |
| 122 | + currentAway.textContent = message.data.match.matchOdds.awayWin; |
| 123 | + const currentDraw = document.getElementById('current-draw'); |
| 124 | + currentDraw.textContent = message.data.match.matchOdds.draw; |
| 125 | + const nextGoalHome = document.getElementById('next-goal-home'); |
| 126 | + nextGoalHome.textContent = message.data.match.nextGoal[message.data.match.homeTeam]; |
| 127 | + const nextGoalAway = document.getElementById('next-goal-away'); |
| 128 | + nextGoalAway.textContent = message.data.match.nextGoal[message.data.match.awayTeam]; |
| 129 | + const nextGoalNoGoal = document.getElementById('next-goal-none'); |
| 130 | + nextGoalNoGoal.textContent = message.data.match.nextGoal['No Goal']; |
| 131 | +} |
| 132 | + |
| 133 | +async function addHistoryItem(message: Message, position = 'prepend') { |
| 134 | + const history = document.getElementById('history'); |
| 135 | + const historyItem = document.createElement('div'); |
| 136 | + historyItem.id = `history-item-${message.id}`; |
| 137 | + historyItem.className = 'border-b pb-2'; |
| 138 | + const historyDiv = document.createElement('div'); |
| 139 | + historyDiv.className = 'flex justify-between text-sm text-gray-600'; |
| 140 | + historyItem.appendChild(historyDiv); |
| 141 | + |
| 142 | + const homeWin = document.createElement('span'); |
| 143 | + homeWin.textContent = `Home: ${message.data.match.matchOdds.homeWin}`; |
| 144 | + const draw = document.createElement('span'); |
| 145 | + draw.textContent = `Draw: ${message.data.match.matchOdds.draw}`; |
| 146 | + const awayWin = document.createElement('span'); |
| 147 | + awayWin.textContent = `Away: ${message.data.match.matchOdds.awayWin}`; |
| 148 | + const time = document.createElement('span'); |
| 149 | + time.textContent = new Date(message.data.match.timestamp).toLocaleTimeString([], { |
| 150 | + hour: '2-digit', |
| 151 | + minute: '2-digit', |
| 152 | + second: '2-digit', |
| 153 | + }); |
| 154 | + historyDiv.appendChild(homeWin); |
| 155 | + historyDiv.appendChild(draw); |
| 156 | + historyDiv.appendChild(awayWin); |
| 157 | + historyDiv.appendChild(time); |
| 158 | + |
| 159 | + if (position === 'prepend') { |
| 160 | + history.prepend(historyItem); |
| 161 | + } else { |
| 162 | + history.appendChild(historyItem); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +async function updateRandomOdds() { |
| 167 | + if (!matchData) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + for (let i = 0; i < 20; i++) { |
| 172 | + const delayTime = 5000; |
| 173 | + await new Promise((resolve) => { |
| 174 | + setTimeout(resolve, delayTime); |
| 175 | + }); |
| 176 | + |
| 177 | + const markets = ['homeWin', 'draw', 'awayWin', 'nextGoal']; |
| 178 | + const numMarketsToUpdate = Math.floor(Math.random() * 3) + 1; |
| 179 | + const marketsToUpdate = markets.sort(() => 0.5 - Math.random()).slice(0, numMarketsToUpdate); |
| 180 | + |
| 181 | + const newOdds = { ...matchData }; |
| 182 | + |
| 183 | + marketsToUpdate.forEach((market) => { |
| 184 | + if (market === 'nextGoal') { |
| 185 | + const team = Object.keys(newOdds.match.nextGoal)[Math.floor(Math.random() * 3)]; |
| 186 | + newOdds.match.nextGoal[team] = (parseFloat(newOdds.match.nextGoal[team]) + (Math.random() * 0.2 - 0.1)).toFixed(2); |
| 187 | + } else { |
| 188 | + newOdds.match.matchOdds[market] = (parseFloat(newOdds.match.matchOdds[market]) + (Math.random() * 0.2 - 0.1)).toFixed(2); |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + newOdds.match.timestamp = new Date().toISOString(); |
| 193 | + await channel.publish('odds', newOdds); |
| 194 | + } |
| 195 | +} |
0 commit comments