|
46 | 46 |
|
47 | 47 | await connection.on("GroupFull", (groupName) => { |
48 | 48 | alert(groupName + " heeft al het maximum van twee spelers.") |
49 | | - }) |
| 49 | + }); |
50 | 50 |
|
51 | 51 | await connection.on("GroupMade", (groupName, userIds) => { |
52 | 52 | var groupNameElement = document.querySelector("#GroupName"); |
|
95 | 95 | console.error(err); |
96 | 96 | } |
97 | 97 | }); |
| 98 | + |
98 | 99 | function generateGroupName() { |
99 | 100 |
|
100 | 101 | let randomString = (Math.random() + 1).toString(36).substring(7); |
|
134 | 135 | const board = document.querySelector("#GameBoard"); |
135 | 136 | board.setAttribute("style", "display:grid;width: 150px;grid-template-columns: auto auto auto;gap: 5px 5px;"); |
136 | 137 | for (let i = 0; i < 9; i++) { |
137 | | - let span = document.createElement("div"); |
138 | | - span.innerHTML = " "; |
139 | | - span.style = "border:solid;#333333;1px;width:50px;height:50px;"; |
140 | | - span.id = "cell" + i; |
141 | | - span.onclick = async () => { |
142 | | - let cell = document.getElementById("cell" + i); |
143 | | - if (cell.innerHTML == ' ') { |
144 | | - await connection.invoke("SendMove", groupName, playerSymbol, i); |
145 | | - } |
146 | | - }; |
147 | | - |
148 | | - |
149 | | - board.appendChild(span); |
150 | | - } |
151 | | - } |
| 138 | + const boardCell = document.createElement("board-cell"); |
| 139 | + boardCell.setAttribute("index", i); |
| 140 | + boardCell.setAttribute("group-name", groupName); |
| 141 | + boardCell.setAttribute("player-symbol", playerSymbol); |
152 | 142 |
|
153 | | - connection.on("UpdateBoard", (playerSymbol, position) => { |
154 | | - |
155 | | - let cell = document.querySelector("#cell" + position); |
156 | | - cell.innerHTML = playerSymbol; |
157 | | - }); |
158 | | - |
159 | | - connection.on("GameWon", (playerSymbol) => { |
160 | | - for (let i = 0; i < 9; i++) { |
161 | | - let span = document.getElementById("cell" + i); |
162 | | - span.onclick = null; |
| 143 | + boardCell.connection = connection; |
| 144 | + board.appendChild(boardCell); |
163 | 145 | } |
164 | | - |
165 | | - const message = "<h1>" + playerSymbol + " heeft gewonnen!" + "</h1>"; |
166 | | - |
167 | | - showGameEndPopUp(message); |
168 | | - }); |
169 | | - |
170 | | - function showGameEndPopUp(message) { |
171 | | - const popup = document.createElement("div"); |
172 | | - popup.id = "GameEndPopUp"; |
173 | | - popup.innerHTML = message; |
174 | | - popup.style = "z-index: 10;width: 50%;position: absolute;top: 20%;left: 25%;background: #333333;opacity: 0.7;color: white;text-align: center;padding: 1rem;" |
175 | | - |
176 | | - const button = document.createElement("button"); |
177 | | - button.onclick = () => { |
178 | | - connection.stop(); |
179 | | - hideGameEndPopUp(); |
180 | | - location.href = location.href; |
181 | | - }; |
182 | | - button.innerHTML = "Terug naar beginscherm."; |
183 | | - popup.appendChild(button); |
184 | | - document.querySelector("body").appendChild(popup); |
185 | | - } |
186 | | - |
187 | | - function hideGameEndPopUp() { |
188 | | - document.querySelector("#GameEndPopUp").remove(); |
189 | 146 | } |
190 | 147 |
|
191 | 148 | async function setupConnection() { |
|
212 | 169 | return connection; |
213 | 170 | } |
214 | 171 | }); |
215 | | - |
216 | | - |
0 commit comments