-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
84 lines (72 loc) · 2.32 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const ws = new WebSocket(`ws://localhost:8080`);
const Ludo = () => {
const [ludoBoard, setludoBoard] = React.useState([]);
const [diceValue, setdiceValue] = React.useState('');
const [myColor, setColor] = React.useState('');
const [turn, setTurn] = React.useState('');
ws.onmessage = (event) => {
const serverMessage = JSON.parse(event.data);
if (serverMessage.type === `newboard`) {
setludoBoard(serverMessage.board);
}
if (serverMessage.type === `dice`) {
setdiceValue(serverMessage.value);
}
if (serverMessage.type === `assignColor`) {
setColor(serverMessage.color)
}
if (serverMessage.type === `turn`) {
setTurn(serverMessage.player)
}
if (serverMessage.type === `wrongturn`) {
setTurn(serverMessage.msg)
}
if (serverMessage.type === `winner`) {
setTurn(serverMessage.message)
setludoBoard([])
setdiceValue('')
}
if (serverMessage.type === `connecting`) {
setTurn(serverMessage.message)
}
};
const onClickHandler = (location, spriteColor) => {
if (spriteColor != myColor)
{
return
}
const object_tosend = {type: `spriteclick`,
color: spriteColor,
coordinates: location,
}
ws.send(JSON.stringify(object_tosend))
};
const DisplayBoard = () =>{
return (<div>
{ludoBoard.map((row, rowindex)=> {
return (<div key= {rowindex}>
{row.map((col, colindex) =>{
return(<div className={"cell".concat(rowindex.toString(), colindex.toString())} key = {"cell".concat(rowindex.toString(), colindex.toString())}>
{col.map((sprites, sprites_index)=>
{
return(<div onClick ={()=>onClickHandler([rowindex, colindex,sprites_index],sprites)} className={sprites} key={sprites_index}> </div>)
})}
</div>)
})}
</div>)
})}
<div className="dice" >
{diceValue}
</div>
<div className={"color ".concat(myColor)} >
</div>
<div className="text_box">
{turn}
</div>
</div>)} ;
return (<div>
<DisplayBoard />
</div>
);
};
ReactDOM.render(<Ludo />, document.querySelector(`#root`))