Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Game_Class #24

Merged
merged 20 commits into from
Apr 24, 2024
60 changes: 44 additions & 16 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,23 @@ <h1>The Word Search Game</h1>


var gameId;
var player = new Player;
var player;

class Player
{
nick;
score;
status;
totalPoints;
gameWins;
nick="";
score=0;
//status;
totalPoints=0;
gameWins=0;
}

class UserEvent
{
gameId;
player;
cell;
action;
gameId=-1;
player="";
cell=-1;
action=-1;
}

function requestGameList() {
Expand Down Expand Up @@ -153,7 +153,8 @@ <h1>The Word Search Game</h1>
gameIndex: gameIndex,
modeIndex: mode
};
//socket.send(JSON.stringify(data));

socket.send(JSON.stringify(data));
}

// replace leaveGame function with one that sends a WebSocket message
Expand Down Expand Up @@ -196,8 +197,8 @@ <h1>The Word Search Game</h1>
//create cells and add it to the row
const cell = document.createElement("td");
cell.setAttribute("id",k.toString());
cell.onclick=buttonClick(k,1);
cell.onmouseover=buttonClick(k,2);


const cellText=document.createTextNode(`${k}`);
cell.appendChild(cellText);
row.appendChild(cell);
Expand All @@ -209,16 +210,43 @@ <h1>The Word Search Game</h1>
tbl.appendChild(tblBody);
document.body.appendChild(tbl);
tbl.setAttribute("border","1.5");
eventListener();

}

function buttonClick(cell,action)
function eventListener()
{
var cells = document.querySelectorAll("td");
for(var i=0;i<cells.length;i++)
{
cells[i].addEventListener("click",function()
{
buttonEvent(this.getAttribute("id"),0);
});
cells[i].addEventListener("mouseover",function()
{
buttonEvent(this.getAttribute("id"),1);
});
cells[i].addEventListener("mouseover",function()
{
buttonEvent(this.getAttribute("id"),2);
});

}
}
function buttonEvent(cell,action)
{
let button= document.getElementById(cell);
button.style.backgroundColor="blue";
U = new UserEvent();
U.cell=cell;
U.cell=parseInt(cell);
U.action=action;
U.player=player;
U.gameId=gameId;

var data = {
type: "UpdateGame",
events: U
};
socket.send(JSON.stringify(U));
console.log(JSON.stringify(U));
}
Expand Down