mchess-client/lib/connection/ws_connection.dart

105 lines
3.1 KiB
Dart
Raw Normal View History

2023-06-02 21:28:40 +00:00
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/foundation.dart';
2023-06-08 15:10:48 +00:00
import 'package:mchess/api/websocket_message.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
2023-06-02 21:28:40 +00:00
import 'package:mchess/api/register.dart';
import 'package:mchess/chess_bloc/chess_position.dart';
2023-06-29 23:49:18 +00:00
import 'package:mchess/connection_cubit/connection_cubit.dart';
2022-12-25 15:16:23 +00:00
import 'package:mchess/utils/chess_utils.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
class ServerConnection {
2022-12-18 00:04:08 +00:00
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) {
2022-12-18 00:04:08 +00:00
channel.sink.add(message);
counter++;
}
2022-11-19 12:24:38 +00:00
2023-06-29 23:49:18 +00:00
void connect(String playerID, lobbyID, String? passphrase) {
if (kDebugMode) {
channel =
WebSocketChannel.connect(Uri.parse('ws://localhost:8080/api/ws'));
} else {
2023-06-08 15:10:48 +00:00
channel = WebSocketChannel.connect(
Uri.parse('wss://chess.sw-gross.de:9999/api/ws'));
}
2023-06-29 23:49:18 +00:00
send(
jsonEncode(
WebsocketMessageIdentifyPlayer(
playerID: (playerID),
lobbyID: (lobbyID),
passphrase: (passphrase),
),
),
);
log(channel.closeCode.toString());
2022-12-18 00:04:08 +00:00
broadcast = channel.stream.asBroadcastStream();
2023-06-02 21:28:40 +00:00
broadcast.listen(handleIncomingData);
}
void handleIncomingData(dynamic data) {
log("Data received:");
log(data);
2023-06-08 15:10:48 +00:00
var apiMessage = ApiWebsocketMessage.fromJson(jsonDecode(data));
switch (apiMessage.type) {
case MessageType.colorDetermined:
handleIncomingColorDeterminedMessage(apiMessage);
2023-06-08 15:10:48 +00:00
break;
2023-06-02 21:28:40 +00:00
case MessageType.move:
2023-06-08 15:10:48 +00:00
handleIncomingMoveMessage(apiMessage);
break;
case MessageType.invalidMove:
handleInvalidMoveMessage(apiMessage);
2023-06-08 15:10:48 +00:00
}
}
2023-06-02 21:28:40 +00:00
void handleIncomingColorDeterminedMessage(ApiWebsocketMessage apiMessage) {
2023-07-03 17:41:12 +00:00
ConnectionCubit.getInstance().opponentConnected();
ChessBloc.getInstance().add(InitBoard());
ChessBloc.getInstance().add(
ColorDetermined(myColor: ChessColor.fromApiColor(apiMessage.color!)));
}
2023-06-08 15:10:48 +00:00
void handleIncomingMoveMessage(ApiWebsocketMessage apiMessage) {
var move = ChessMove.fromApiMove(apiMessage.move!);
2023-07-03 17:41:12 +00:00
if (apiMessage.position != null) {
ChessBloc.getInstance().add(ReceivedPosition(
position:
ChessPosition.getInstance().fromPGNString(apiMessage.position!)));
2023-07-03 17:41:12 +00:00
}
ChessBloc.getInstance()
.add(ReceivedMove(startSquare: move.from, endSquare: move.to));
}
void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) {
log("invalid move message received, with move: ${apiMessage.move.toString()}");
ChessBloc.getInstance()
.add(InvalidMovePlayed(move: ChessMove.fromApiMove(apiMessage.move!)));
2022-11-19 12:24:38 +00:00
}
}