80 lines
2.4 KiB
Dart
80 lines
2.4 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:mchess/api/move.dart';
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
|
import 'package:mchess/chess_bloc/chess_events.dart';
|
|
import 'package:mchess/chess_bloc/chess_position.dart';
|
|
import 'package:mchess/api/register.dart';
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
|
|
|
class ServerConnection {
|
|
late WebSocketChannel channel;
|
|
late bool wasConnected = false;
|
|
late int counter = 0;
|
|
Stream broadcast = const Stream.empty();
|
|
|
|
static final ServerConnection _instance = ServerConnection._internal();
|
|
|
|
ServerConnection._internal() {
|
|
log("ServerConnection._internal constructor is called");
|
|
}
|
|
|
|
factory ServerConnection() {
|
|
return _instance;
|
|
}
|
|
|
|
factory ServerConnection.getInstance() {
|
|
return ServerConnection();
|
|
}
|
|
|
|
void send(String message) {
|
|
channel.sink.add(message);
|
|
counter++;
|
|
}
|
|
|
|
void connect(String playerID, lobbyID) {
|
|
if (wasConnected) channel.sink.close();
|
|
|
|
if (kDebugMode) {
|
|
channel =
|
|
WebSocketChannel.connect(Uri.parse('ws://localhost:8080/api/ws'));
|
|
} else {
|
|
channel =
|
|
WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080'));
|
|
}
|
|
send(jsonEncode(WebsocketMessageIdentifyPlayer(
|
|
playerID: (playerID), lobbyID: (lobbyID))));
|
|
|
|
log(channel.closeCode.toString());
|
|
|
|
wasConnected = true;
|
|
|
|
broadcast = channel.stream.asBroadcastStream();
|
|
|
|
broadcast.listen(handleIncomingData);
|
|
}
|
|
|
|
void handleIncomingData(dynamic data) {
|
|
log("Data received:");
|
|
log(data);
|
|
var apiMove = ApiMove.fromJson(jsonDecode(data));
|
|
|
|
//Todo: Implement status messages
|
|
// e.g. to tell the client that both players connected, who is playing which color and so on
|
|
|
|
var move = ChessMove.fromApiMove(apiMove);
|
|
if (move == ChessPosition.getInstance().lastMove) {
|
|
//This is our own move that got resent by the server. Do not process.
|
|
} else {
|
|
log('lastMove: from: ${ChessPosition.getInstance().lastMove?.from} to: ${ChessPosition.getInstance().lastMove?.to}');
|
|
log('constructed move: from: ${move.from} to: ${move.to}');
|
|
log('Move received : ${move.from.toAlphabetical()}->${move.to.toAlphabetical()}');
|
|
|
|
ChessBloc.getInstance()
|
|
.add(OpponentPieceMoved(startSquare: move.from, endSquare: move.to));
|
|
}
|
|
}
|
|
}
|