listenGame function
Implementation
void listenGame(BuildContext context) {
GameSocket s = GameSocket();
bool espec = s.spectatorMode;
BoardData b = BoardData();
s.socket.on(
'error',
(data) => {
// print("ERROR" + data)
});
s.socket.on(
'moved',
(data) => {
// print(data),
if (b.reversedBoard)
{
(b.clocks[0] as TimerState)
.setTimer(data[0]["timerLight"] ~/ 1000),
(b.clocks[1] as TimerState)
.setTimer(data[0]["timerDark"] ~/ 1000),
}
else
{
(b.clocks[1] as TimerState)
.setTimer(data[0]["timerLight"] ~/ 1000),
(b.clocks[0] as TimerState)
.setTimer(data[0]["timerDark"] ~/ 1000),
},
if (!espec)
{
if (data[0]["turn"] == (!s.iAmWhite ? "DARK" : "LIGHT"))
{
simulateMovement(decodeMovement(data[0]["move"])),
},
}
else
{
// print("funciona"),
simulateMovement(decodeMovement(data[0]["move"])),
}
});
s.socket.on(
'game_over',
(data) => {
// print(data),
if (data[0]["endState"] == "CHECKMATE" &&
(data[0]["winner"] == (!s.iAmWhite ? "LIGHT" : "DARK")))
{
alertWinner(context, !s.iAmWhite,
"Ha ganado el jugador con las fichas "),
},
if (data[0]["endState"] == "SURRENDER")
{
alertWinner(context, !(data[0]["winner"] == "LIGHT"),
"Se ha rendido el jugador con las fichas "),
},
if (data[0]["endState"] == "TIMEOUT")
{
alertWinner(context, (data[0]["winner"] == "LIGHT"),
"Ha ganado por tiempo el jugador con las fichas "),
},
if (data[0]["endState"] == "DRAW")
{
alertDraw(context),
},
});
s.socket.on(
'voted_draw',
(data) => {
if (data[0]["color"] == (s.iAmWhite ? "DARK" : "LIGHT"))
alertPossibleDraw(context)
});
s.socket.on(
'voted_save',
(data) => {
if (data[0]["color"] == (s.iAmWhite ? "DARK" : "LIGHT"))
alertPossibleSave(context)
});
s.socket.onDisconnect((_) => {
// print('disconnect')
});
s.socket.on('game_saved', (data) => {alertSave(context)});
s.socket.onDisconnect((_) => {
// print('disconnect')
});
s.socket.on(
'fromServer',
(_) => {
//print(_)
});
}