startGame function
Implementation
Future<void> startGame(BuildContext context, String type, Arguments arguments) {
GameSocket s = GameSocket();
Completer completer = Completer<void>();
// BoardData b = BoardData();
s.socket.onConnect((_) {
// print("CONEXIÓN ESTABLECIDA");
});
Map jsonData;
s.type = type;
if (type == "CREATECUSTOM" || type == "JOINCUSTOM") {
s.type = "CUSTOM";
}
switch (type) {
case "AI":
{
jsonData = {
"gameType": "AI",
"time": arguments.time,
"increment": arguments.increment,
"hostColor": arguments.hostColor,
"difficulty": arguments.difficulty
};
}
break;
case "COMP":
{
jsonData = {"gameType": "COMPETITIVE", "time": arguments.time};
}
break;
case "CREATECUSTOM":
{
jsonData = {
"gameType": "CUSTOM",
"time": arguments.time,
"increment": arguments.increment,
"hostColor": arguments.hostColor
};
s.socket.once(
'room_created',
(data) => {
s.room = data[0]["roomID"],
},
);
// completer.complete();
// s.socket.emit('find_room', jsonData);
}
// return completer.future;
break;
case "JOINCUSTOM":
{
jsonData = {"gameType": "CUSTOM", "roomID": arguments.roomID};
}
break;
case "SPECTATOR":
{
jsonData = {"roomID": arguments.roomID};
s.socket.emit('join_room', jsonData);
s.room = arguments.roomID;
s.spectatorMode = true;
}
break;
case "TOURNAMENT":
{
jsonData = {"gameType": "TOURNAMENT", "matchID": arguments.roomID};
}
break;
default:
{
jsonData = {};
// print("Animal has metido mal el tipo");
}
}
var movements = [];
s.socket.once(
'cancelled', (data) => {Navigator.pop(context), Completer().complete()});
s.socket.once(
'room',
(data) => {
// print(data),
s.player1 = data[0]["light"],
s.player2 = data[0]["dark"],
if (type != "SPECTATOR")
{
s.room = data[0]["roomID"],
s.iAmWhite = data[0]["color"] == "LIGHT",
if (data[0]["moves"].length > 0)
{
s.pendingMovements = data[0]["moves"],
}
}
else
{
movements = data[0]["moves"],
s.pendingMovements = movements,
s.iAmWhite = true
},
s.timer = data[0]["initialTimer"],
// print(data),
// print(s.room),
if (type == "COMP" ||
type == "CREATECUSTOM" ||
type == "TOURNAMENT")
Navigator.pop(context),
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const GamePage()),
),
completer.complete()
});
// print("CONEXIÓN ESTABLECIDA2");
s.socket.on(
'error',
(data) => {
// print(data)
});
if (type != "SPECTATOR" && type != "") {
s.socket.emit('find_room', jsonData);
}
return completer.future;
}