2022-12-13 02:43:05 +00:00
|
|
|
import 'dart:developer';
|
2022-12-18 14:54:19 +00:00
|
|
|
import 'dart:io';
|
2022-12-13 02:43:05 +00:00
|
|
|
|
2022-12-18 22:06:55 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-12-14 22:17:31 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
|
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
|
|
|
import 'package:mchess/chessapp/chess_utils.dart';
|
2022-11-13 13:25:47 +00:00
|
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
|
|
|
|
|
|
|
class ServerConnection {
|
2022-12-18 00:04:08 +00:00
|
|
|
late WebSocketChannel channel;
|
2022-12-13 02:36:22 +00:00
|
|
|
late bool wasConnected = false;
|
2022-11-13 13:25:47 +00:00
|
|
|
late int counter = 0;
|
2022-12-14 22:17:31 +00:00
|
|
|
late Stream broadcast;
|
2022-11-13 13:25:47 +00:00
|
|
|
|
|
|
|
static final ServerConnection _instance = ServerConnection._internal();
|
|
|
|
|
|
|
|
ServerConnection._internal() {
|
2022-12-18 02:21:39 +00:00
|
|
|
log("ServerConnection._internal constructor is called");
|
2022-12-13 02:36:22 +00:00
|
|
|
connect();
|
2022-11-13 13:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
factory ServerConnection() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
factory ServerConnection.getInstance() {
|
|
|
|
return ServerConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(String message) {
|
2022-12-18 00:04:08 +00:00
|
|
|
channel.sink.add(message);
|
2022-11-13 13:25:47 +00:00
|
|
|
counter++;
|
|
|
|
}
|
2022-11-19 12:24:38 +00:00
|
|
|
|
2022-12-13 02:36:22 +00:00
|
|
|
void connect() {
|
2022-12-18 00:04:08 +00:00
|
|
|
if (wasConnected) channel.sink.close();
|
2022-12-18 15:11:00 +00:00
|
|
|
|
2022-12-18 22:06:55 +00:00
|
|
|
if (kDebugMode) {
|
|
|
|
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
|
|
|
|
} else {
|
|
|
|
channel =
|
|
|
|
WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080'));
|
|
|
|
}
|
2022-12-18 15:11:00 +00:00
|
|
|
|
2022-12-18 02:21:39 +00:00
|
|
|
log(channel.closeCode.toString());
|
|
|
|
|
2022-12-13 02:36:22 +00:00
|
|
|
wasConnected = true;
|
|
|
|
|
2022-12-18 00:04:08 +00:00
|
|
|
broadcast = channel.stream.asBroadcastStream();
|
2022-12-14 22:17:31 +00:00
|
|
|
|
|
|
|
broadcast.listen((data) {
|
|
|
|
log("Data received:");
|
|
|
|
log(data);
|
|
|
|
|
|
|
|
var receivedString = data.toString();
|
2022-12-18 00:04:08 +00:00
|
|
|
var splitString = receivedString.split(' ');
|
2022-12-14 22:17:31 +00:00
|
|
|
|
2022-12-18 00:04:08 +00:00
|
|
|
if (splitString[0] == "cl") {
|
|
|
|
ChessColor onBottom =
|
|
|
|
splitString[1] == "white" ? ChessColor.white : ChessColor.black;
|
|
|
|
ChessBloc.getInstance().add(ColorDetermined(myColor: onBottom));
|
2022-12-14 22:17:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-18 00:04:08 +00:00
|
|
|
if (splitString[0] == "bd") {
|
|
|
|
if (splitString[1] == "init") ChessBloc.getInstance().add(InitBoard());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (splitString[1] == ('mv')) {
|
|
|
|
var startSquare = ChessCoordinate.fromString(splitString[2]);
|
|
|
|
var endSquare = ChessCoordinate.fromString(splitString[3]);
|
2022-12-14 22:17:31 +00:00
|
|
|
|
2022-12-18 00:04:08 +00:00
|
|
|
log('Move received : ${splitString[2]}:${splitString[3]}');
|
2022-12-14 22:17:31 +00:00
|
|
|
|
|
|
|
ChessBloc.getInstance()
|
|
|
|
.add(PieceMoved(startSquare: startSquare, endSquare: endSquare));
|
|
|
|
}
|
|
|
|
});
|
2022-11-19 12:24:38 +00:00
|
|
|
}
|
2022-11-13 13:25:47 +00:00
|
|
|
}
|