Skip to content

Commit

Permalink
Add basic classes
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed May 7, 2023
1 parent 6fd8adf commit c4ad5e9
Show file tree
Hide file tree
Showing 20 changed files with 1,871 additions and 84 deletions.
5 changes: 4 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"name": "Name",
"description": "Description",
"cancel": "Cancel",
"address": "Address"
"address": "Address",
"game": "Game",
"players": "Players",
"noPlayers": "No players"
}
14 changes: 10 additions & 4 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:material_leap/l10n/leap_localizations.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:town/pages/game/logic.dart';
import 'package:town/pages/game/page.dart';
import 'package:town/services/connection.dart';
import 'package:window_manager/window_manager.dart';

import 'cubits/settings.dart';
import 'pages/game/logic/logic.dart';
import 'pages/home/page.dart';
import 'pages/settings/page.dart';
import 'setup.dart'
Expand Down Expand Up @@ -107,9 +107,15 @@ class FlowApp extends StatelessWidget {
GoRoute(
path: 'game',
pageBuilder: _fadeTransitionBuilder(
(context, state) => GamePage(
connection: state.extra as GameConnection,
),
(context, state) {
if (state.extra is! GameConnection) {
context.go('/');
return const SizedBox.shrink();
}
return GamePage(
connection: state.extra as GameConnection,
);
},
),
),
GoRoute(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/models/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

part 'server.freezed.dart';

const kDefaultPort = 71035;
const kDefaultPort = 10357;

@freezed
class GameServer with _$GameServer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import 'dart:async';
import 'dart:convert';

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:town/pages/game/server.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import 'logic.dart';
import 'server.dart';

part 'client.freezed.dart';
part 'client.g.dart';
Expand All @@ -16,6 +16,11 @@ class ClientConnectionMessage with _$ClientConnectionMessage {
List<GamePlayer> players,
) = FetchedPlayersClientConnectionMessage;

const factory ClientConnectionMessage.chatMessage(
String message,
String from,
) = ChatMessageClientConnectionMessage;

factory ClientConnectionMessage.fromJson(Map<String, dynamic> json) =>
_$ClientConnectionMessageFromJson(json);
}
Expand All @@ -25,8 +30,8 @@ class ClientGameConnection extends GameConnection {

ClientGameConnection(this.channel);

factory ClientGameConnection.connect(String address) {
final channel = WebSocketChannel.connect(Uri.parse(address));
factory ClientGameConnection.connect(Uri address) {
final channel = WebSocketChannel.connect(address);
return ClientGameConnection(channel);
}

Expand All @@ -35,11 +40,11 @@ class ClientGameConnection extends GameConnection {
}

void _send(ServerConnectionMessage message) {
channel.sink.add(message.toJson());
channel.sink.add(jsonEncode(message.toJson()));
}

Future<T> _waitForResponse<T extends ClientConnectionMessage>() async {
return channel.stream
return await channel.stream
.map((event) => jsonDecode(event))
.map((event) => ClientConnectionMessage.fromJson(event))
.where((event) => event is T)
Expand Down
Loading

0 comments on commit c4ad5e9

Please sign in to comment.