Skip to content

Commit

Permalink
feat: add week games
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesblasco committed Mar 15, 2022
1 parent b434168 commit b589731
Show file tree
Hide file tree
Showing 20 changed files with 717 additions and 82 deletions.
10 changes: 6 additions & 4 deletions lib/puzzle/bloc/puzzle_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class PuzzleBloc extends Bloc<PuzzleEvent, PuzzleState> {

final PuzzlesRepository _puzzlesRepository;

final puzzleVersion = DateTime.now().difference(DateTime(2022, 3, 14)).inDays;

Future<void> _onPuzzleFetched(
PuzzleFetched event,
Emitter<PuzzleState> emit,
) async {
emit(state.copyWith(status: GameStatus.loading));
final game = await _puzzlesRepository.getPuzzle();
final game = await _puzzlesRepository.getPuzzle(puzzleVersion);
emit(state.copyWith(status: GameStatus.setup, history: [game]));
}

Expand Down Expand Up @@ -88,13 +90,13 @@ class PuzzleBloc extends Bloc<PuzzleEvent, PuzzleState> {
void _onPuzzleShared(PuzzleShared event, Emitter emit) {
if (!state.puzzle.isSolved) return;

sharePuzzle(state);
sharePuzzle(state, puzzleVersion);
}
}

void sharePuzzle(PuzzleState state) {
void sharePuzzle(PuzzleState state, int version) {
final introMessage = '''
https://flutter-rush.web.app/ (#${DateTime.now().difference(DateTime(2022, 2, 27)).inDays})
https://flutter-rush.web.app/ (#$version)
''';
final buffer = StringBuffer();
for (final state in state.history) {
Expand Down
13 changes: 12 additions & 1 deletion packages/puzzle_models/lib/src/models/position.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'position.g.dart';

/// {@template position}
/// 2-dimensional position model.
///
/// (0, 0) is the top left corner of the board.
/// {@endtemplate}
@JsonSerializable()
class Position extends Equatable {
/// {@macro position}
const Position(this.x, this.y)
Expand All @@ -18,14 +22,18 @@ class Position extends Equatable {
);

/// A position with the same [value] offset for both axis
const Position.all(int value)
const Position.all(int value)
: assert(
value >= 0,
'value must be greater than or equal to 0.',
),
x = value,
y = value;

/// Converts a JSON [Map] into a [Position] instance
factory Position.fromJson(Map<String, dynamic> json) =>
_$PositionFromJson(json);

/// A position with a zero offset in both axis
static const Position zero = Position(0, 0);

Expand Down Expand Up @@ -72,4 +80,7 @@ class Position extends Equatable {

@override
List<Object> get props => [x, y];

/// Converts this [Position] instance into a JSON [Map]
Map<String, dynamic> toJson() => _$PositionToJson(this);
}
17 changes: 17 additions & 0 deletions packages/puzzle_models/lib/src/models/position.g.dart

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

11 changes: 11 additions & 0 deletions packages/puzzle_models/lib/src/models/rush_puzzle.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:puzzle_models/puzzle_models.dart';

part 'rush_puzzle.g.dart';

/// {@template rush_puzzle}
/// Represents a Rush Hour puzzle.
///
/// For more information see:
/// - https://en.wikipedia.org/wiki/Rush_Hour_(puzzle)
/// {@endtemplate}
@JsonSerializable()
class RushPuzzle extends Equatable {
/// {@macro rush_puzzle}
RushPuzzle({
Expand Down Expand Up @@ -36,6 +40,13 @@ class RushPuzzle extends Equatable {
lastVehicleMoved = null,
jammedVehicleId = '';

/// Converts a JSON [Map] into a [Position] instance
factory RushPuzzle.fromJson(Map<String, dynamic> json) =>
_$RushPuzzleFromJson(json);

/// Converts this [Position] instance into a JSON [Map]
Map<String, dynamic> toJson() => _$RushPuzzleToJson(this);

/// The [Position] that the [jammedVehicleId] has to reach in order to solve
/// the puzzle.
static const Position exit = Position(6, 2);
Expand Down
33 changes: 33 additions & 0 deletions packages/puzzle_models/lib/src/models/rush_puzzle.g.dart

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

11 changes: 11 additions & 0 deletions packages/puzzle_models/lib/src/models/vehicle.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:puzzle_models/puzzle_models.dart';

part 'vehicle.g.dart';

/// {@template vehicle}
/// Representation of a [RushPuzzle] vehicle.
///
/// [Vehicle] can have variable length, but the width is assumed to be one.
/// The orientation of the vehicle is fixed and determined by [Steering].
/// {@endtemplate}
@JsonSerializable()
class Vehicle extends Equatable {
/// {@macro vehicle}
Vehicle({
Expand All @@ -24,6 +28,13 @@ class Vehicle extends Equatable {
? Position(type.length - 1, 0)
: Position(0, type.length - 1));

/// Converts a JSON [Map] into a [Position] instance
factory Vehicle.fromJson(Map<String, dynamic> json) =>
_$VehicleFromJson(json);

/// Converts this [Position] instance into a JSON [Map]
Map<String, dynamic> toJson() => _$VehicleToJson(this);

/// Unique identifier for this vehicle.
final String id;

Expand Down
38 changes: 38 additions & 0 deletions packages/puzzle_models/lib/src/models/vehicle.g.dart

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

5 changes: 5 additions & 0 deletions packages/puzzle_models/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ environment:

dependencies:
equatable: ^2.0.3
json_annotation: ^4.4.0



dev_dependencies:
build_runner: ^2.1.8
json_serializable: ^6.1.5
test: ^1.19.2
very_good_analysis: ^2.4.0
79 changes: 79 additions & 0 deletions packages/puzzles_repository/assets/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"jammedVehicleId": "X",
"difficulty": "beginner",
"minMoves": "8",
"vehicles": {
"A": {
"id": "A",
"type": "taxi",
"steering": "horizontal",
"firstPosition": {
"x": 0,
"y": 0
}
},
"P": {
"id": "P",
"type": "secondaryBus",
"steering": "vertical",
"firstPosition": {
"x": 0,
"y": 1
}
},
"B": {
"id": "B",
"type": "car",
"steering": "vertical",
"firstPosition": {
"x": 0,
"y": 4
}
},
"X": {
"id": "X",
"type": "ambulance",
"steering": "horizontal",
"firstPosition": {
"x": 1,
"y": 2
}
},
"Q": {
"id": "Q",
"type": "secondaryTruck",
"steering": "vertical",
"firstPosition": {
"x": 3,
"y": 1
}
},
"R": {
"id": "R",
"type": "bus",
"steering": "horizontal",
"firstPosition": {
"x": 2,
"y": 5
}
},
"C": {
"id": "C",
"type": "police",
"steering": "horizontal",
"firstPosition": {
"x": 4,
"y": 4
}
},
"O": {
"id": "O",
"type": "truck",
"steering": "vertical",
"firstPosition": {
"x": 5,
"y": 0
}
}
}
}
Loading

0 comments on commit b589731

Please sign in to comment.