Skip to content

Commit

Permalink
Fix player id issues
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed May 10, 2023
1 parent 5d71563 commit 48733ad
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 131 deletions.
7 changes: 4 additions & 3 deletions app/lib/logic/connection/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,10 @@ class ClientGameConnection with GameConnection, ConnectedGameConnection {
send(ServerConnectionMessage.removeDeck(deckIndex, seatIndex));
}

void changeVisibility(int index, int? seatIndex, DeckVisibility visibility) {
send(
ServerConnectionMessage.changeVisibility(index, seatIndex, visibility));
void changeVisibility(int index, int? seatIndex, DeckVisibility visibility,
[DeckVisibility? ownVisibility]) {
send(ServerConnectionMessage.changeVisibility(
index, seatIndex, visibility, ownVisibility));
}

void shuffle(int index, int? seatIndex) {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/logic/connection/logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ part 'logic.g.dart';
class GamePlayer with _$GamePlayer {
const factory GamePlayer({
required String name,
required String id,
required int id,
}) = _GamePlayer;

factory GamePlayer.fromJson(Map<String, dynamic> json) =>
Expand Down
16 changes: 8 additions & 8 deletions app/lib/logic/connection/logic.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GamePlayer _$GamePlayerFromJson(Map<String, dynamic> json) {
/// @nodoc
mixin _$GamePlayer {
String get name => throw _privateConstructorUsedError;
String get id => throw _privateConstructorUsedError;
int get id => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand All @@ -35,7 +35,7 @@ abstract class $GamePlayerCopyWith<$Res> {
GamePlayer value, $Res Function(GamePlayer) then) =
_$GamePlayerCopyWithImpl<$Res, GamePlayer>;
@useResult
$Res call({String name, String id});
$Res call({String name, int id});
}

/// @nodoc
Expand All @@ -62,7 +62,7 @@ class _$GamePlayerCopyWithImpl<$Res, $Val extends GamePlayer>
id: null == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as String,
as int,
) as $Val);
}
}
Expand All @@ -75,7 +75,7 @@ abstract class _$$_GamePlayerCopyWith<$Res>
__$$_GamePlayerCopyWithImpl<$Res>;
@override
@useResult
$Res call({String name, String id});
$Res call({String name, int id});
}

/// @nodoc
Expand All @@ -100,7 +100,7 @@ class __$$_GamePlayerCopyWithImpl<$Res>
id: null == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as String,
as int,
));
}
}
Expand All @@ -116,7 +116,7 @@ class _$_GamePlayer implements _GamePlayer {
@override
final String name;
@override
final String id;
final int id;

@override
String toString() {
Expand Down Expand Up @@ -152,15 +152,15 @@ class _$_GamePlayer implements _GamePlayer {

abstract class _GamePlayer implements GamePlayer {
const factory _GamePlayer(
{required final String name, required final String id}) = _$_GamePlayer;
{required final String name, required final int id}) = _$_GamePlayer;

factory _GamePlayer.fromJson(Map<String, dynamic> json) =
_$_GamePlayer.fromJson;

@override
String get name;
@override
String get id;
int get id;
@override
@JsonKey(ignore: true)
_$$_GamePlayerCopyWith<_$_GamePlayer> get copyWith =>
Expand Down
2 changes: 1 addition & 1 deletion app/lib/logic/connection/logic.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 27 additions & 28 deletions app/lib/logic/connection/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class ServerConnectionMessage with _$ServerConnectionMessage {
ShuffleServerConnectionMessage;

const factory ServerConnectionMessage.changeVisibility(
int deckIndex, int? seatIndex, DeckVisibility visibility) =
int deckIndex, int? seatIndex, DeckVisibility visibility,
[DeckVisibility? ownVisibility]) =
ChangeVisibilityServerConnectionMessage;

factory ServerConnectionMessage.fromJson(Map<String, dynamic> json) =>
Expand All @@ -70,6 +71,8 @@ class WebSocketClient {
final HttpConnectionInfo info;

WebSocketClient(this.socket, this.info);

int get id => socket.hashCode;
}

class ServerGameConnection with GameConnection {
Expand Down Expand Up @@ -107,14 +110,7 @@ class ServerGameConnection with GameConnection {
final client = WebSocketClient(socket, info);
clients.add(client);
_sendPlayersUpdated();
_send(
client,
ClientConnectionMessage.stateChanged(
state.onPlayer(
client.socket.hashCode,
),
),
);
_sendState(client);

// Listen for incoming messages from the client
socket.listen((event) => _onMessage(client, event), onDone: () {
Expand All @@ -126,8 +122,7 @@ class ServerGameConnection with GameConnection {

@override
List<GamePlayer> get players => clients
.map((e) => GamePlayer(
name: e.info.remoteAddress.address, id: e.hashCode.toString()))
.map((e) => GamePlayer(name: e.info.remoteAddress.address, id: e.id))
.toList();

@override
Expand All @@ -141,7 +136,7 @@ class ServerGameConnection with GameConnection {
for (final client in clients) {
_send(
client,
ClientConnectionMessage.playersUpdated(players, client.hashCode),
ClientConnectionMessage.playersUpdated(players, client.id),
);
}
}
Expand All @@ -154,16 +149,18 @@ class ServerGameConnection with GameConnection {

void _changeState(GameState state) {
stateSubject.add(state);
for (final client in clients) {
_send(
client,
ClientConnectionMessage.stateChanged(
state.onPlayer(
client.socket.hashCode,
),
clients.forEach(_sendState);
}

void _sendState(WebSocketClient client) {
_send(
client,
ClientConnectionMessage.stateChanged(
state.onPlayer(
client.id,
),
);
}
),
);
}

List<GameCard> _removeCards(List<CardIndex> cards) {
Expand Down Expand Up @@ -227,7 +224,7 @@ class ServerGameConnection with GameConnection {

message.when(
fetchPlayers: () => sendBack(
ClientConnectionMessage.playersUpdated(players, client.hashCode),
ClientConnectionMessage.playersUpdated(players, client.id),
),
chatMessage: (message) => _sendToAll(
ClientConnectionMessage.chatMessage(
Expand Down Expand Up @@ -275,15 +272,15 @@ class ServerGameConnection with GameConnection {
_changeState(state.copyWith(
seats: List<GameSeat>.from(state.seats)
..[index] = state.seats[index].copyWith(
players: [...state.seats[index].players, client.hashCode],
players: [...state.seats[index].players, client.id],
)));
},
leaveSeat: (index) {
_changeState(state.copyWith(
seats: List<GameSeat>.from(state.seats)
..[index] = state.seats[index].copyWith(
players: List<int>.from(state.seats[index].players)
..remove(client.hashCode),
..remove(client.id),
)));
},
addCards: (cards, deckIndex, seatIndex) {
Expand Down Expand Up @@ -359,19 +356,21 @@ class ServerGameConnection with GameConnection {
}
_changeState(newState);
},
changeVisibility: (deckIndex, seatIndex, visibility) {
changeVisibility: (deckIndex, seatIndex, visibility, ownVisibility) {
if (seatIndex != null) {
_changeState(state.copyWith(
seats: List<GameSeat>.from(state.seats)
..[seatIndex] = state.seats[seatIndex].copyWith(
decks: List<GameDeck>.from(state.seats[seatIndex].decks)
..[deckIndex] = state.seats[seatIndex].decks[deckIndex]
.copyWith(visibility: visibility))));
.copyWith(
visibility: visibility,
ownVisibility: ownVisibility))));
} else {
_changeState(state.copyWith(
decks: List<GameDeck>.from(state.decks)
..[deckIndex] =
state.decks[deckIndex].copyWith(visibility: visibility)));
..[deckIndex] = state.decks[deckIndex].copyWith(
visibility: visibility, ownVisibility: ownVisibility)));
}
},
shuffle: (deckIndex, seatIndex) {
Expand Down
Loading

1 comment on commit 48733ad

@vercel
Copy link

@vercel vercel bot commented on 48733ad May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

qeck – ./app

qeck-linwood.vercel.app
qeck.vercel.app
qeck-git-develop-linwood.vercel.app

Please sign in to comment.