Skip to content

Commit

Permalink
Add warn mode
Browse files Browse the repository at this point in the history
  • Loading branch information
evgfilim1 committed Apr 12, 2024
1 parent bbcd25d commit 375d8bc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Состояние "Удалён" при дисквалификации на экране информации об игроке.
- Подсветка членов победившей команды при окончании игры.
- Отображение списка игроков перед повторными речами, между которыми голоса поделились поровну.
- Режим раздачи фолов.

### Изменено

Expand Down
31 changes: 28 additions & 3 deletions lib/screens/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MainScreen extends StatefulWidget {
class _MainScreenState extends State<MainScreen> {
var _showRoles = false;
final _notes = TextEditingController();
var _warnMode = false;

@override
void initState() {
Expand Down Expand Up @@ -72,6 +73,8 @@ class _MainScreenState extends State<MainScreen> {
);
}

void _resetWarnMode() => setState(() => _warnMode = false);

@override
Widget build(BuildContext context) {
final controller = context.watch<GameController>();
Expand Down Expand Up @@ -104,6 +107,13 @@ class _MainScreenState extends State<MainScreen> {
),
),
actions: [
IconButton(
onPressed:
controller.isGameActive ? () => setState(() => _warnMode = !_warnMode) : null,
icon: const Icon(Icons.report),
tooltip: _warnMode ? "Вернуться в нормальный режим" : "Включить режим выдачи фолов",
color: _warnMode ? Colors.red : null,
),
const RestartGameIconButton(),
MenuAnchor(
builder: (context, controller, child) => IconButton(
Expand Down Expand Up @@ -135,29 +145,44 @@ class _MainScreenState extends State<MainScreen> {
],
),
drawer: const AppDrawer(),
body: _RotatableMainScreenBody(showRoles: _showRoles),
body: _RotatableMainScreenBody(
showRoles: _showRoles,
warnOnTap: _warnMode,
onPrevStateTap: _resetWarnMode,
onNextStateTap: _resetWarnMode,
),
),
);
}
}

class _RotatableMainScreenBody extends StatelessWidget {
final bool showRoles;
final bool warnOnTap;
final VoidCallback? onPrevStateTap;
final VoidCallback? onNextStateTap;

const _RotatableMainScreenBody({
required this.showRoles,
required this.warnOnTap,
this.onPrevStateTap,
this.onNextStateTap,
});

@override
Widget build(BuildContext context) {
final controller = context.watch<GameController>();
final children = <Widget>[
if (controller.isGameInitialized) PlayerButtons(showRoles: showRoles),
if (controller.isGameInitialized) PlayerButtons(showRoles: showRoles, warnOnTap: warnOnTap),
Expanded(
child: Column(
children: [
const Expanded(child: Center(child: GameStateInfo())),
if (controller.isGameInitialized) const GameBottomControlBar(),
if (controller.isGameInitialized)
GameBottomControlBar(
onTapBack: onPrevStateTap,
onTapNext: onNextStateTap,
),
],
),
),
Expand Down
17 changes: 15 additions & 2 deletions lib/widgets/bottom_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ import "../utils/ui.dart";
import "confirmation_dialog.dart";

class GameBottomControlBar extends StatelessWidget {
const GameBottomControlBar({super.key});
final VoidCallback? onTapBack;
final VoidCallback? onTapNext;

const GameBottomControlBar({
super.key,
this.onTapBack,
this.onTapNext,
});

void _onTapBack(GameController controller) {
controller.setPreviousState();
onTapBack?.call();
}

Future<void> _saveStats(
BuildContext context,
Expand Down Expand Up @@ -98,6 +110,7 @@ class GameBottomControlBar extends StatelessWidget {
}
await _saveStats(context, controller, playersContainer, nextStateAssumption);
}
onTapNext?.call();
}

@override
Expand All @@ -107,7 +120,7 @@ class GameBottomControlBar extends StatelessWidget {
final nextStateAssumption = controller.nextStateAssumption;
return BottomControlBar(
backLabel: previousState?.prettyName ?? "(недоступно)",
onTapBack: previousState != null ? controller.setPreviousState : null,
onTapBack: previousState != null ? () => _onTapBack(controller) : null,
onTapNext: nextStateAssumption != null ? () => _onTapNext(context, controller) : null,
nextLabel: nextStateAssumption?.prettyName ?? "(недоступно)",
);
Expand Down
22 changes: 18 additions & 4 deletions lib/widgets/player_buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ class BasicPlayerButtons extends StatelessWidget {

class PlayerButtons extends StatefulWidget {
final bool showRoles;
final bool warnOnTap;

const PlayerButtons({
super.key,
this.showRoles = false,
required this.showRoles,
required this.warnOnTap,
});

@override
Expand All @@ -117,11 +119,23 @@ class PlayerButtons extends StatefulWidget {
class _PlayerButtonsState extends State<PlayerButtons> {
bool _expanded = false;

bool get showRoles => widget.showRoles;

void _onPlayerButtonTap(BuildContext context, int playerNumber) {
final controller = context.read<GameController>();
final player = controller.players.getByNumber(playerNumber);
if (widget.warnOnTap && controller.isGameActive && player.state.isAlive) {
controller.warnPlayer(playerNumber);
showSnackBar(
context,
SnackBar(
content: Text("Игрок ${player.number} получил фол"),
action: SnackBarAction(
label: "Отменить",
onPressed: () => controller.warnMinusPlayer(playerNumber),
),
),
);
return;
}
if (controller.state case GameStateNightCheck(activePlayerNumber: final pn)) {
final p = controller.players.getByNumber(pn);
if (!p.state.isAlive) {
Expand Down Expand Up @@ -185,7 +199,7 @@ class _PlayerButtonsState extends State<PlayerButtons> {
onTap: player.state.isAlive || controller.state.stage == GameStage.nightCheck
? () => _onPlayerButtonTap(context, playerNumber)
: null,
showRole: showRoles,
showRole: widget.showRoles,
expanded: expanded,
);
}
Expand Down
13 changes: 8 additions & 5 deletions lib/widgets/restart_game_icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ class RestartGameIconButton extends StatelessWidget {
}

@override
Widget build(BuildContext context) => IconButton(
onPressed: () => _askRestartGame(context),
tooltip: "Перезапустить игру",
icon: const Icon(Icons.restart_alt),
);
Widget build(BuildContext context) {
final controller = context.watch<GameController>();
return IconButton(
onPressed: controller.isGameInitialized ? () => _askRestartGame(context) : null,
tooltip: "Перезапустить игру",
icon: const Icon(Icons.restart_alt),
);
}
}

0 comments on commit 375d8bc

Please sign in to comment.