From 61025c979dccc47d844c1300a42541171ffde395 Mon Sep 17 00:00:00 2001 From: Filippo Orru Date: Sun, 7 Nov 2021 12:14:28 +0100 Subject: [PATCH] Add more connection logging, unify button design, fix send awaiting, fix BattleRequests --- lib/connection/server_connection.dart | 70 +++++++++++-------- lib/inherit/notifications.dart | 6 +- lib/menu/common/menu_common.dart | 14 ++-- lib/menu/play_selection/all.dart | 9 ++- lib/menu/settings.dart | 35 +++++----- .../models/online/game_state_manager.dart | 9 +-- lib/play/models/online/game_states/idle.dart | 2 +- lib/play/models/online/game_states/other.dart | 7 +- lib/play/widgets/online/other.dart | 3 +- lib/util/constants.dart | 2 +- 10 files changed, 86 insertions(+), 71 deletions(-) diff --git a/lib/connection/server_connection.dart b/lib/connection/server_connection.dart index 366f779..009c447 100644 --- a/lib/connection/server_connection.dart +++ b/lib/connection/server_connection.dart @@ -89,10 +89,14 @@ class ServerConnection with ChangeNotifier { this._playerMsgIndex += 1; int messageId = _playerMsgIndex; Future connectionWasReset; - Future packetWasAcknowledged = - this._reliablePktInStreamCtrl.stream.any((rPkt) { - return rPkt is ReliablePktAckIn && rPkt.id == messageId; - }).then((_) => true); + Future packetWasAcknowledged = this + ._reliablePktInStreamCtrl + .stream + .any((rPkt) { + return rPkt is ReliablePktAckIn && rPkt.id == messageId; + }) + .then((_) => true) + .timeout(Duration(milliseconds: 1000), onTimeout: () => false); connectionWasReset = this ._serverMsgStreamCtrl .stream @@ -125,6 +129,8 @@ class ServerConnection with ChangeNotifier { } void _connect({bool force = false, bool reset = false}) async { + if (catastrophicFailure) return; + if ((_sessionState is SessionStateOutDated || _sessionState is SessionStateConnected) && !force) { @@ -155,6 +161,7 @@ class ServerConnection with ChangeNotifier { } else { this._connection = await _connectDevice(); if (_connection == null) { + Logger.w("Connection is null after trying to connect"); _websocketDone(); return; } @@ -269,7 +276,10 @@ class ServerConnection with ChangeNotifier { return wsStream.listen( this._receivedWsMsg, onError: this._websocketErr, - onDone: this._websocketDone, + onDone: () { + Logger.w("wsStream was closed"); + _websocketDone(); + }, cancelOnError: true, ); } on Exception catch (e) { @@ -280,28 +290,31 @@ class ServerConnection with ChangeNotifier { StreamSubscription _handleReliablePktOut(StreamSink wsSink) { return this._reliablePktOutStreamCtrl.stream.listen( - (rPkt) { - if (rPkt is ReliablePktMsgOut) { - this._playerMsgQ.add(QueuedMessage(rPkt.id, rPkt.msg)); - if (!connected) { - return; - } - } - - String msgStr = rPkt.serialize(); - Logger.v("<< $msgStr"); - try { - wsSink.add(msgStr); - } on Exception catch (e) { - Logger.e("Exception!", e, StackTrace.current); - _websocketDone(); - return; - } - }, - onError: this._websocketErr, - onDone: this._websocketDone, - cancelOnError: true, - ); + (rPkt) { + if (rPkt is ReliablePktMsgOut) { + this._playerMsgQ.add(QueuedMessage(rPkt.id, rPkt.msg)); + if (!connected) { + return; + } + } + + String msgStr = rPkt.serialize(); + Logger.v("<< $msgStr"); + try { + wsSink.add(msgStr); + } on Exception catch (e) { + Logger.e("Exception!", e, StackTrace.current); + _websocketDone(); + return; + } + }, + onError: this._websocketErr, + onDone: () { + Logger.w("reliablePktOutStream was closed"); + this._websocketDone(); + }, + cancelOnError: true, + ); } void _receivedWsMsg(dynamic msg) { @@ -416,7 +429,8 @@ class ServerConnection with ChangeNotifier { } void _receivedReliablePacket(ReliablePacketIn rPkt) { - // Logger.d("Received reliable packet: $rPkt"); + _reliablePktInStreamCtrl.add(rPkt); + if (rPkt is ReliablePktMsgIn) { final int expectedId = this._serverMsgIndex + 1; if (rPkt.id == expectedId) { diff --git a/lib/inherit/notifications.dart b/lib/inherit/notifications.dart index 763683e..add12c5 100644 --- a/lib/inherit/notifications.dart +++ b/lib/inherit/notifications.dart @@ -154,9 +154,9 @@ class FiarNotifications { android: AndroidNotificationDetails( '1', 'Battle Requests', - 'Shown when someone requests a battle.', + 'Shown when someone wants to battle with you.', category: 'CATEGORY_MESSAGE', - importance: Importance.max, + importance: Importance.high, priority: Priority.max, timeoutAfter: BattleRequestDialog.TIMEOUT.inMilliseconds, ), @@ -181,7 +181,7 @@ class FiarNotifications { android: AndroidNotificationDetails( '3', 'Searching Game', - 'Shown persistently while searching for a game', + 'Shown persistently while searching for a game.', category: 'CATEGORY_SERVICE', importance: Importance.low, priority: Priority.low, diff --git a/lib/menu/common/menu_common.dart b/lib/menu/common/menu_common.dart index 4d93ca9..6f5ee61 100644 --- a/lib/menu/common/menu_common.dart +++ b/lib/menu/common/menu_common.dart @@ -234,13 +234,16 @@ class _FeedbackDialogState extends State mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ TextButton( + style: TextButton.styleFrom( + primary: Colors.black87, + ), child: Text('Cancel'), - onPressed: () { - Navigator.of(context).pop(); - }, + onPressed: () => Navigator.of(context).pop(), ), - ElevatedButton( - child: Text('Send'), + OutlinedButton( + style: OutlinedButton.styleFrom( + primary: Colors.blue, + ), onPressed: () async { Map body = { "content": controller.text, @@ -257,6 +260,7 @@ class _FeedbackDialogState extends State if (mounted) Navigator.of(context).pop(); }); }, + child: Text('Send'), ), ], ), diff --git a/lib/menu/play_selection/all.dart b/lib/menu/play_selection/all.dart index 1ed8d96..49c49d9 100644 --- a/lib/menu/play_selection/all.dart +++ b/lib/menu/play_selection/all.dart @@ -252,11 +252,14 @@ class FiarSimpleDialog extends StatelessWidget { showOkay ? Align( alignment: Alignment.centerRight, - child: ElevatedButton( - style: ElevatedButton.styleFrom(primary: Colors.lightBlue), + child: OutlinedButton( + style: OutlinedButton.styleFrom( + primary: Colors.blue, + ), onPressed: () => Navigator.of(context).pop(), child: Text('Okay'), - )) + ), + ) : SizedBox(), ], ); diff --git a/lib/menu/settings.dart b/lib/menu/settings.dart index fe627e1..e646e28 100644 --- a/lib/menu/settings.dart +++ b/lib/menu/settings.dart @@ -37,14 +37,6 @@ class _SettingsScreenState extends State { return Scaffold( appBar: FiarAppBar( title: "Settings", - threeDots: [ - FiarThreeDotItem( - 'Feedback', - onTap: () { - showFeedbackDialog(context); - }, - ), - ], ), body: ListView( children: [ @@ -75,12 +67,17 @@ class _SettingsScreenState extends State { MainAxisAlignment.spaceBetween, children: [ TextButton( - onPressed: () { - Navigator.of(context).pop(); - }, + style: TextButton.styleFrom( + primary: Colors.black87, + ), child: Text('Cancel'), + onPressed: () => + Navigator.of(context).pop(), ), - ElevatedButton( + OutlinedButton( + style: OutlinedButton.styleFrom( + primary: Colors.blue, + ), onPressed: () { context.read().logOut(); Navigator.of(context).pop(); @@ -324,15 +321,17 @@ class _ChooseQuickchatEmojisState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ TextButton( + style: TextButton.styleFrom( + primary: Colors.black87, + ), child: Text('Cancel'), - style: TextButton.styleFrom(primary: Colors.blueGrey), - onPressed: () { - Navigator.of(context).pop(); - }, + onPressed: () => Navigator.of(context).pop(), ), - TextButton( + OutlinedButton( + style: OutlinedButton.styleFrom( + primary: Colors.blue, + ), child: Text('Save'), - style: TextButton.styleFrom(), onPressed: _allowSave() ? () { FiarSharedPrefs.settingsQuickchatEmojis = diff --git a/lib/play/models/online/game_state_manager.dart b/lib/play/models/online/game_state_manager.dart index bef5e64..4e1d4c1 100644 --- a/lib/play/models/online/game_state_manager.dart +++ b/lib/play/models/online/game_state_manager.dart @@ -138,6 +138,7 @@ class GameStateManager with ChangeNotifier { } void cancelIncomingBattleReq() { + // TODO communicate that it was declined incomingBattleRequestTimer?.cancel(); incomingBattleRequest = null; notifyListeners(); @@ -146,7 +147,7 @@ class GameStateManager with ChangeNotifier { Future startGame(OnlineRequest req) async { if (currentGameState is! IdleState) { // throw UnimplementedError("Maybe this should never occur"); - _serverConnection.send(PlayerMsgLeave()); + await leave(); } notifyListeners(); @@ -161,11 +162,11 @@ class GameStateManager with ChangeNotifier { } } - void leave() { - this._serverConnection.send(PlayerMsgLeave()); + Future leave() async { + return await this._serverConnection.send(PlayerMsgLeave()); } - void Function(PlayerMessage) get sendPlayerMessage => + Future Function(PlayerMessage) get sendPlayerMessage => this._serverConnection.send; void _didGameStateChange(GameState oldState, GameState newState) { diff --git a/lib/play/models/online/game_states/idle.dart b/lib/play/models/online/game_states/idle.dart index ac73073..f6ff300 100644 --- a/lib/play/models/online/game_states/idle.dart +++ b/lib/play/models/online/game_states/idle.dart @@ -9,7 +9,7 @@ class IdleState extends GameState { @override GameState? handlePlayerMessage(PlayerMessage msg) { if (msg is PlayerMsgLobbyJoin) { - return WaitingForLobbyInfoState(super.gsm, code: msg.code); + return InLobbyState(super.gsm, msg.code); } else if (msg is PlayerMsgLobbyRequest) { return WaitingForLobbyInfoState(super.gsm); } else if (msg is PlayerMsgWorldwideRequest) { diff --git a/lib/play/models/online/game_states/other.dart b/lib/play/models/online/game_states/other.dart index 92bacb4..dd6d07f 100644 --- a/lib/play/models/online/game_states/other.dart +++ b/lib/play/models/online/game_states/other.dart @@ -4,12 +4,7 @@ import 'package:four_in_a_row/play/models/online/game_state_manager.dart'; import 'game_state.dart'; class WaitingForLobbyInfoState extends GameState { - final String? code; - - WaitingForLobbyInfoState( - GameStateManager gsm, { - this.code, - }) : super(gsm); + WaitingForLobbyInfoState(GameStateManager gsm) : super(gsm); @override GameState? handlePlayerMessage(PlayerMessage msg) { diff --git a/lib/play/widgets/online/other.dart b/lib/play/widgets/online/other.dart index 50c27fc..ab6557b 100644 --- a/lib/play/widgets/online/other.dart +++ b/lib/play/widgets/online/other.dart @@ -9,8 +9,7 @@ class WaitingForLobbyInfoViewer extends StatelessWidget { @override Widget build(BuildContext context) { - String title = - state.code == null ? "Contacting Server..." : "Joining Lobby..."; + String title = "Joining Lobby..."; String label = "This may take some time"; return LoadingScreen(title: title, label: label); } diff --git a/lib/util/constants.dart b/lib/util/constants.dart index ab5b9b0..26e3f11 100644 --- a/lib/util/constants.dart +++ b/lib/util/constants.dart @@ -1,6 +1,6 @@ import 'package:flutter/foundation.dart'; -const bool useLocalServer = true && kDebugMode; +const bool useLocalServer = false && kDebugMode; const HOST = useLocalServer ? "192.168.0.186" : "fourinarow.ml"; const PORT = useLocalServer ? 40146 : 80;