Skip to content

Commit

Permalink
Add more connection logging, unify button design, fix send awaiting, …
Browse files Browse the repository at this point in the history
…fix BattleRequests
  • Loading branch information
filippo-orru committed Nov 7, 2021
1 parent 336c42e commit 61025c9
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 71 deletions.
70 changes: 42 additions & 28 deletions lib/connection/server_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ class ServerConnection with ChangeNotifier {
this._playerMsgIndex += 1;
int messageId = _playerMsgIndex;
Future<bool> connectionWasReset;
Future<bool> packetWasAcknowledged =
this._reliablePktInStreamCtrl.stream.any((rPkt) {
return rPkt is ReliablePktAckIn && rPkt.id == messageId;
}).then((_) => true);
Future<bool> 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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -280,28 +290,31 @@ class ServerConnection with ChangeNotifier {

StreamSubscription _handleReliablePktOut(StreamSink<dynamic> 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) {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions lib/inherit/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand All @@ -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,
Expand Down
14 changes: 9 additions & 5 deletions lib/menu/common/menu_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,16 @@ class _FeedbackDialogState extends State<FeedbackDialog>
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<String, String> body = {
"content": controller.text,
Expand All @@ -257,6 +260,7 @@ class _FeedbackDialogState extends State<FeedbackDialog>
if (mounted) Navigator.of(context).pop();
});
},
child: Text('Send'),
),
],
),
Expand Down
9 changes: 6 additions & 3 deletions lib/menu/play_selection/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
);
Expand Down
35 changes: 17 additions & 18 deletions lib/menu/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
return Scaffold(
appBar: FiarAppBar(
title: "Settings",
threeDots: [
FiarThreeDotItem(
'Feedback',
onTap: () {
showFeedbackDialog(context);
},
),
],
),
body: ListView(
children: [
Expand Down Expand Up @@ -75,12 +67,17 @@ class _SettingsScreenState extends State<SettingsScreen> {
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<UserInfo>().logOut();
Navigator.of(context).pop();
Expand Down Expand Up @@ -324,15 +321,17 @@ class _ChooseQuickchatEmojisState extends State<ChooseQuickchatEmojis> {
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 =
Expand Down
9 changes: 5 additions & 4 deletions lib/play/models/online/game_state_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class GameStateManager with ChangeNotifier {
}

void cancelIncomingBattleReq() {
// TODO communicate that it was declined
incomingBattleRequestTimer?.cancel();
incomingBattleRequest = null;
notifyListeners();
Expand All @@ -146,7 +147,7 @@ class GameStateManager with ChangeNotifier {
Future<void> startGame(OnlineRequest req) async {
if (currentGameState is! IdleState) {
// throw UnimplementedError("Maybe this should never occur");
_serverConnection.send(PlayerMsgLeave());
await leave();
}

notifyListeners();
Expand All @@ -161,11 +162,11 @@ class GameStateManager with ChangeNotifier {
}
}

void leave() {
this._serverConnection.send(PlayerMsgLeave());
Future<bool> leave() async {
return await this._serverConnection.send(PlayerMsgLeave());
}

void Function(PlayerMessage) get sendPlayerMessage =>
Future<bool> Function(PlayerMessage) get sendPlayerMessage =>
this._serverConnection.send;

void _didGameStateChange(GameState oldState, GameState newState) {
Expand Down
2 changes: 1 addition & 1 deletion lib/play/models/online/game_states/idle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions lib/play/models/online/game_states/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions lib/play/widgets/online/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/constants.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 61025c9

Please sign in to comment.