diff --git a/secret_hitler/lib/GameSetting.dart b/secret_hitler/lib/GameSetting.dart index ae61b4d..efbf69d 100644 --- a/secret_hitler/lib/GameSetting.dart +++ b/secret_hitler/lib/GameSetting.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:secret_hitler/rollassigning.dart'; +import 'package:secret_hitler/state_management.dart'; +import 'package:provider/provider.dart'; class GameSetting extends StatefulWidget { const GameSetting({super.key, required this.title}); @@ -22,7 +24,11 @@ class _GameSetting extends State { int _numPlayers = 5; List playerNameController = []; List playerWidgets = []; + List expantionSetting = []; bool first_time=true; + bool isCheckedLibPlus=false; + bool isCheckedFascistPlus=false; + void _incrementPlayers() { setState(() { @@ -62,36 +68,50 @@ class _GameSetting extends State { ); } + String gameType(){ + if(isCheckedLibPlus){ + return 'LibPlus'; + } + else if(isCheckedFascistPlus){ + return 'FascistPlus'; + } + else{ + return 'Normal'; + } + } + void _setPlayers() { List playersName = []; - setState(() { - for (int i = 0; i < _numPlayers; i++) { - if (playerNameController[i].text.length != 0) { - playersName.add( - playerNameController[i].text - ); - } else { - showMessageDialog(context, 'You must fill player ${i+1} name'); - return; - // break; - } - + for (int i = 0; i < _numPlayers; i++) { + if (playerNameController[i].text.length != 0) { + playersName.add( + playerNameController[i].text + ); + } else { + showMessageDialog(context, 'You must fill player ${i+1} name'); + return; + // break; } - Navigator.push( - context, - MaterialPageRoute(builder: (context) => RollAssigning(data: playersName)), - ); + } + GameState gameState = Provider.of(context,listen: false); + + gameState.setUp(playersName,gameType:gameType()); + setState(() { }); + Navigator.push( + context, + MaterialPageRoute(builder: (context) => RollAssigning()), + ); } - void _setUp() {} @override Widget build(BuildContext context) { - // playerNameController = []; + playerWidgets = []; + if (first_time){ for (int i = 0; i < _numPlayers; i++) { playerNameController.add(new TextEditingController()); @@ -147,6 +167,45 @@ class _GameSetting extends State { ); playerWidgets.add(SizedBox(height: 16.0)); } + + expantionSetting = []; + if (_numPlayers==7||_numPlayers==9){//Liberal plus + Widget libPlusWidget=Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Radio( + value:true, + groupValue: isCheckedLibPlus, + onChanged: (bool? value) { + setState(() { + isCheckedLibPlus = value ?? false; + }); + }, + ), + Text('Liberal plus'), + ], + ); + expantionSetting.add(libPlusWidget); + } + if (_numPlayers==8||_numPlayers==10){//Fascist Plus + Widget fascistPlusWidget=Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Radio( + value:true, + groupValue: isCheckedFascistPlus, + onChanged: (bool? value) { + setState(() { + isCheckedFascistPlus = value ?? false; + + }); + }, + ), + Text('Fascist plus'), + ], + ); + expantionSetting.add(fascistPlusWidget); + } // This method is rerun every time setState is called, for instance as done // by the _incrementCounter method above. // @@ -227,7 +286,8 @@ class _GameSetting extends State { ], ), - ]+playerWidgets, + + ]+expantionSetting+playerWidgets, ), ),), floatingActionButton: FloatingActionButton( diff --git a/secret_hitler/lib/gameFunctions.dart b/secret_hitler/lib/gameFunctions.dart index 52b0574..c248cfc 100644 --- a/secret_hitler/lib/gameFunctions.dart +++ b/secret_hitler/lib/gameFunctions.dart @@ -6,13 +6,8 @@ void showRoleDialog(BuildContext context,GameState game_state) { context: context, barrierDismissible:false, builder: (BuildContext context) { - String rool=''; - if (game_state.roles[game_state.names.indexOf(game_state.willSearch)]=='Liberal'){ - rool='Liberal'; - } - else{ - rool='Fascist'; - } + String role=game_state.findPlayerWithName(game_state.willSearch).searchResult(); + return AlertDialog( title: const Text('Message'), // content: Text("You investigated ${name}, and their role is ${role}."), @@ -23,7 +18,7 @@ void showRoleDialog(BuildContext context,GameState game_state) { const TextSpan(text: 'You investigated '), TextSpan(text: game_state.willSearch, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)), const TextSpan(text: ' and their role is '), - TextSpan(text: '$rool.', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)), + TextSpan(text: '$role.', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)), ], ), @@ -44,12 +39,12 @@ void showRoleDialog(BuildContext context,GameState game_state) { void onPressedSuccessElection(BuildContext context,String name,GameState game_state) { // first_selected=true; - if(game_state.veto && game_state.roles[game_state.names.indexOf(name)]=='Liberal' && game_state.roles[game_state.turn]=='Liberal'){ + if(game_state.veto && game_state.findPlayerWithName(name).role=='Liberal' && game_state.players[game_state.turn].role=='Liberal'){ game_state.activateVeto(); } game_state.changeState('elected'); game_state.changePresidentSelected(-1); - if(game_state.roles[game_state.names.indexOf(name)]=='Hitler'&& game_state.hitlerState){ + if(game_state.findPlayerWithName(name).role=='Hitler'&& game_state.hitlerState){ game_state.hitlerChancellor(); } diff --git a/secret_hitler/lib/gameUI.dart b/secret_hitler/lib/gameUI.dart index fd5d003..9e14931 100644 --- a/secret_hitler/lib/gameUI.dart +++ b/secret_hitler/lib/gameUI.dart @@ -10,10 +10,8 @@ import 'package:shared_preferences/shared_preferences.dart'; class Game extends StatefulWidget { - final List name; - final List roles; - const Game({Key? key, required this.name, required this.roles}) : super(key: key); + const Game({Key? key}) : super(key: key); @override _Game createState() => _Game(); @@ -32,9 +30,9 @@ class _Game extends State { print("initState"); gameState = Provider.of(context,listen: false); // game_state = Game_state(names: widget.name, roles: widget.roles); - if(!gameState.continuegame) { - gameState.setUp(widget.name, widget.roles); - } + // if(!gameState.continuegame) { + // gameState.setUp(widget.name); + // } print("initState after setUp"); super.initState(); _prefs.then((SharedPreferences prefs) { diff --git a/secret_hitler/lib/gameUIFunctions.dart b/secret_hitler/lib/gameUIFunctions.dart index 01ef453..e9708fe 100644 --- a/secret_hitler/lib/gameUIFunctions.dart +++ b/secret_hitler/lib/gameUIFunctions.dart @@ -158,10 +158,11 @@ void discardChancellor(GameState game_state){ } void discardPresident(GameState game_state){ - game_state.changeLastPresident(game_state.names[game_state.turn]); + // game_state.changeLastPresident(game_state.names[game_state.turn]); + game_state.changeLastPresident(game_state.players[game_state.turn].name); game_state.changeState('president_discarded'); game_state.changeChancellorSelect(-1); - game_state.addLog("${game_state.names[game_state.turn]} discard:${game_state.dec[game_state.presidentSelected]}\n"); + game_state.addLog("${game_state.players[game_state.turn].name} discard:${game_state.dec[game_state.presidentSelected]}\n"); game_state.disDec.add(game_state.dec.removeAt(game_state.presidentSelected)); game_state.updatePage(); } @@ -194,7 +195,7 @@ List getUiState(context,width,height,GameState game_state){ TextSpan(text: 'Round '), TextSpan(text: '${game_state.round}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), TextSpan(text: ' has begun.\n'), - TextSpan(text: '${game_state.names[game_state.turn]},',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)), + TextSpan(text: '${game_state.players[game_state.turn].name},',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)), TextSpan(text: ' select your Chancellor for the upcoming election.'), ], ), @@ -255,7 +256,7 @@ List getUiState(context,width,height,GameState game_state){ text: TextSpan( style:TextStyle(color:Colors.black,fontSize: 15), children: [ - TextSpan(text: '${game_state.names[game_state.turn]},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), + TextSpan(text: '${game_state.players[game_state.turn].name},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), TextSpan(text: ' in your esteemed role as the'), TextSpan(text: ' President,',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)), TextSpan(text: ' please select a policy card to be discarded.'), @@ -469,7 +470,7 @@ List getUiState(context,width,height,GameState game_state){ text: TextSpan( style:TextStyle(color:Colors.black,fontSize: 20), children: [ - TextSpan(text: '${game_state.names[game_state.turn]},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)), + TextSpan(text: '${game_state.players[game_state.turn].name},', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28)), TextSpan(text: ' as the '), TextSpan(text: 'President,',style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), TextSpan(text: ' you may now privately reveal the top three policy cards.'), @@ -530,7 +531,7 @@ List getUiState(context,width,height,GameState game_state){ text: TextSpan( style:TextStyle(color:Colors.black), children: [ - TextSpan(text: '${game_state.names[game_state.turn]}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)), + TextSpan(text: '${game_state.players[game_state.turn].name}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18)), TextSpan(text: " when you're ready, you have the option to eliminate a person. Removing Hitler would lead to a victory for the Liberals.\n"), if(game_state.state=='kill_veto') TextSpan(text: "When both the President and Chancellor, both loyal to the Liberals, concur, the Veto option becomes viable."), ], @@ -598,7 +599,7 @@ List getUiState(context,width,height,GameState game_state){ text: TextSpan( style:TextStyle(color:Colors.black,fontSize: 20), children: [ - TextSpan(text: '${game_state.names[game_state.turn]}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), + TextSpan(text: '${game_state.players[game_state.turn].name}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), TextSpan(text: ' you have the power to investigate the role of a person, whether they be a Fascist or a Liberal.'), ], @@ -664,7 +665,7 @@ List getUiState(context,width,height,GameState game_state){ text: TextSpan( style:TextStyle(color:Colors.black, fontSize: 20), children: [ - TextSpan(text: '${game_state.names[game_state.turn]}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), + TextSpan(text: '${game_state.players[game_state.turn].name}, as the President,', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25)), TextSpan(text: 'you have the honor of selecting the next president!'), ], ), diff --git a/secret_hitler/lib/main.dart b/secret_hitler/lib/main.dart index 102e130..9cd3962 100644 --- a/secret_hitler/lib/main.dart +++ b/secret_hitler/lib/main.dart @@ -123,7 +123,7 @@ class _HomeState extends State { context, MaterialPageRoute( builder: (context) => - Game(name: gameState.names,roles:gameState.roles)), + Game()), ); }, child: const Text('Continue previous game'), diff --git a/secret_hitler/lib/rollassigning.dart b/secret_hitler/lib/rollassigning.dart index 880d5ea..6e1a688 100644 --- a/secret_hitler/lib/rollassigning.dart +++ b/secret_hitler/lib/rollassigning.dart @@ -1,39 +1,31 @@ import 'package:flutter/material.dart'; import 'package:secret_hitler/gameUI.dart'; -import 'dart:convert'; +import 'package:secret_hitler/state_management.dart'; +import 'package:provider/provider.dart'; + class RollAssigning extends StatefulWidget { - final List data; - const RollAssigning({Key? key, required this.data}) : super(key: key); + + const RollAssigning({Key? key}) : super(key: key); @override _RollAssigning createState() => _RollAssigning(); } class _RollAssigning extends State { - List roles=[]; List seen = []; + late GameState gameState; @override initState() { - int number_players=widget.data.length; + gameState = Provider.of(context,listen: false); + gameState.roleAssigning(gameState.playerNames, gameState.gameType); + int number_players=gameState.players.length; seen = []; for (int i = 0; i < number_players; i++) { seen.add(false); } - int libc=(number_players/2).floor()+1; - int fasc=number_players-libc-1; - roles=[]; - for (int i = 0; i < libc; i++) { - roles.add('Liberal'); - } - for (int i = 0; i < fasc; i++) { - roles.add('Fascist'); - } - roles.add('Hitler'); - roles=roles..shuffle(); - // print(roles); setState(() { }); } @@ -110,7 +102,7 @@ class _RollAssigning extends State { Navigator.push( context, - MaterialPageRoute(builder: (context) => Game(name: widget.data,roles:roles)), + MaterialPageRoute(builder: (context) => Game()), ); } @@ -119,7 +111,6 @@ class _RollAssigning extends State { @override Widget build(BuildContext context) { print(seen); - print(roles); bool every_one_know_his_role=false; every_one_know_his_role=seen.every((value)=>value==true); @@ -141,36 +132,14 @@ class _RollAssigning extends State { // child: Text(widget.data.toString()), children:[ SizedBox(height: 16.0), - ]+widget.data.asMap().entries.map((entry)=> + ]+gameState.players.asMap().entries.map((entry)=> GestureDetector( onTap: seen[entry.key] ?null:(){ - List extend=[]; - if(roles[entry.key]=='Fascist'){ - for (int i = 0; i < widget.data.length; i++) { - if((roles[i]=='Fascist') &&(i!=entry.key) ){ - extend.add(TextSpan(text:'Fascist: ')); - extend.add(TextSpan(text:'${widget.data[i]}\n', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))); - } - else if(roles[i]=='Hitler'){ - // extend=extend+'Hitler:'+widget.data[i]+'\n'; - extend.add(TextSpan(text:'Hitler: ')); - extend.add(TextSpan(text:'${widget.data[i]}\n', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16,color:Colors.red))); - } - } - } - else if(roles[entry.key]=='Hitler' && roles.length<=6){ - for (int i = 0; i < widget.data.length; i++) { - if((roles[i]=='Fascist') &&(i!=entry.key) ){ - // extend=extend+'Fascist:'+widget.data[i]+'\n'; - extend.add(TextSpan(text:'Fascist: ')); - extend.add(TextSpan(text:'${widget.data[i]}\n', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))); - } - } - - } - - - showMessageDialog(context,entry.key, entry.value,roles[entry.key],extend); + List extend=gameState.nightInfo(entry.value); + + + + showMessageDialog(context,entry.key, entry.value.name,entry.value.role,extend); }, child:Card( color:seen[entry.key]?Colors.grey[300]:Colors.white, @@ -181,7 +150,7 @@ class _RollAssigning extends State { child:Center( child:Row( mainAxisAlignment: MainAxisAlignment.center, - children:[Text(entry.value)] + children:[Text(entry.value.name)] ) ), ), diff --git a/secret_hitler/lib/state_management.dart b/secret_hitler/lib/state_management.dart index fbd7357..b3ce5c3 100644 --- a/secret_hitler/lib/state_management.dart +++ b/secret_hitler/lib/state_management.dart @@ -4,10 +4,48 @@ import 'dart:math'; import 'package:shared_preferences/shared_preferences.dart'; + +bool toBoolean(String str, [bool strict = false]) { + if (strict == true) { + return str == '1' || str == 'true'; + } + return str != '0' && str != 'false' && str != ''; +} + +class Player{ + String name=''; + String role=''; + bool killed=false; + List additionalInfo=[]; + + Player({required this.name,required this.role,this.killed = false}); + + String searchResult(){ + if (role=='Liberal' || additionalInfo.contains('FascistPlus')){ + return 'Liberal'; + } + else{ + return 'Fascist'; + } + } + void addAdditionalInfo(String info){ + additionalInfo.add(info); + } + + @override + String toString() { + return 'Player(${name},${role},${killed?'killed':'alive'},${additionalInfo})'; + } +} + + class GameState extends ChangeNotifier { bool _continuegame=false; bool _lockTester=false; + String _gameType='Normal'; + List _playerNames=[]; + int _numberPlayers=0; // int _numberPlayersInitial=0; int _round=0; @@ -17,8 +55,10 @@ class GameState extends ChangeNotifier { bool _topThreeSeen=false; bool _chaos = false; bool _isHitlerAlive=true; - List _names=[]; - List _roles=[]; + + List _players=[]; + + List _dec=[]; List _disDec=[]; String _state=''; @@ -26,7 +66,6 @@ class GameState extends ChangeNotifier { String _lastChancellor=''; String _lastPresident=''; int _turn=0; - List _killedturns=[]; List> _governments=[]; List _chancellorList=[]; String _selectedChancellor=''; @@ -71,12 +110,13 @@ class GameState extends ChangeNotifier { int get numberRejected=>_numberRejected; String get selectedChancellor => _selectedChancellor; int get round=>_round; - Listget names=>_names; + // Listget names=>_names; + Listget players=>_players; int get turn=>_turn; List get chancellorList=> _chancellorList; bool get veto =>_veto; bool get continuegame =>_continuegame; - List get roles=>_roles; + // List get roles=>_roles; // bool get chaos =>_chaos; List get dec=>_dec; int get presidentSelected =>_presidentSelected; @@ -90,6 +130,8 @@ class GameState extends ChangeNotifier { String get willresident =>_willPresident; bool get special =>_special; bool get lockTester =>_lockTester; + String get gameType =>_gameType; + List get playerNames=> _playerNames; void changeLockTester(bool value){ _lockTester=value; @@ -112,11 +154,16 @@ class GameState extends ChangeNotifier { // if(_firstStarter+_round/_numberPlayers>=1){ // _firstStarter=((_firstStarter+_round)%_numberPlayers)-_round; // } - var index=_names.indexOf(_willKilled); - if(_roles[index]=='Hitler'){ + + // var index=_names.indexOf(_willKilled); + Player eliminatedPlayer=_players.lastWhere((player){ + return player.name==_willKilled; + }); + if(eliminatedPlayer.role=='Hitler'){ _isHitlerAlive=false; } - _killedturns.add(index); + // _killedturns.add(index); + eliminatedPlayer.killed=true; // _names.removeAt(index); // _roles.removeAt(index); // _numberPlayers = _names.length; @@ -218,23 +265,228 @@ class GameState extends ChangeNotifier { void updateChancellorList(){ _chancellorList=[]; - for (int i = 0; i < _names.length; i++) { - if (i==_turn || !_chaos &&(_names[i]==_lastChancellor || (_numberPlayers>5 && _names[i]==_lastPresident))){ + for (int i = 0; i < _players.length; i++) { + if (i==_turn || _players[i].killed || !_chaos &&(_players[i].name==_lastChancellor || (_numberPlayers>5 && _players[i].name==_lastPresident))){ continue; } - _chancellorList.add(_names[i]); + _chancellorList.add(_players[i].name); } - _killedturns.forEach((element) { - _chancellorList.remove(element); - }); _selectedChancellor=_chancellorList[0]; } + void roleAssigning(List names,String type){ + int numberPlayers=names.length; + Listroles=[]; + _players=[]; + + switch (type){ + case 'Normal': + int libc=0; + int fasc=0; + if(numberPlayers==5){ + libc=3; + fasc=1; + } + else if(numberPlayers==6){ + libc=4; + fasc=1; + } + else if(numberPlayers==7){ + libc=4; + fasc=2; + } + else if(numberPlayers==8){ + libc=5; + fasc=2; + } + else if(numberPlayers==9){ + libc=5; + fasc=3; + } + else if(numberPlayers==10){ + libc=6; + fasc=3; + } + for (int i = 0; i < libc; i++) { + roles.add('Liberal'); + } + for (int i = 0; i < fasc; i++) { + roles.add('Fascist'); + } + roles.add('Hitler'); + roles=roles..shuffle(); + break; + case 'LibPlus': + int libc=0; + int fasc=0; + if(numberPlayers==5){ + libc=3; + fasc=1; + } + else if(numberPlayers==6){ + libc=4; + fasc=1; + } + else if(numberPlayers==7){ + libc=4; + fasc=2; + } + else if(numberPlayers==8){ + libc=5; + fasc=2; + } + else if(numberPlayers==9){ + libc=5; + fasc=3; + } + else if(numberPlayers==10){ + libc=6; + fasc=3; + } + for (int i = 0; i < libc-1; i++) { + roles.add('Liberal'); + } + for (int i = 0; i < fasc; i++) { + roles.add('Fascist'); + } + roles.add('Hitler'); + roles.add('LibPlus'); + roles=roles..shuffle(); + + break; + case 'FascistPlus': + int libc=0; + int fasc=0; + if(numberPlayers==5){ + libc=3; + fasc=1; + } + else if(numberPlayers==6){ + libc=4; + fasc=1; + } + else if(numberPlayers==7){ + libc=4; + fasc=2; + } + else if(numberPlayers==8){ + libc=5; + fasc=2; + } + else if(numberPlayers==9){ + libc=5; + fasc=3; + } + else if(numberPlayers==10){ + libc=6; + fasc=3; + } + for (int i = 0; i < libc; i++) { + roles.add('Liberal'); + } + for (int i = 0; i < fasc-1; i++) { + roles.add('Fascist'); + } + roles.add('Hitler'); + roles.add('FascistPlus'); + roles=roles..shuffle(); + + break; + + } + for (int i =0;i nightInfo(Player player){ + List nightInfo=[]; + switch (_gameType) { + case 'LibPlus': + case 'Normal': + if(player.role=='Fascist'){ + List fascistList=[]; + List hitlerList=[]; + for (int i = 0; i < _players.length; i++) { + if((_players[i].role=='Fascist') &&(player.name!=_players[i].name) ){ + fascistList.add(_players[i].name); + } + else if(_players[i].role=='Hitler'){ + hitlerList.add(_players[i].name); + } + else if(_gameType=='LibPlus'&&_players[i].additionalInfo.contains('LibPlus')){ + hitlerList.add(_players[i].name); + } + + + } + nightInfo.add(TextSpan(text:'Fascist: ')); + for(int i=0;i names,List roles){ + } + + void setUp(List names,{String gameType='Normal'}){ _round=1; - _names=names; - _roles=roles; + _playerNames=names; + _gameType=gameType; + roleAssigning(names,gameType); + // _names=names; + // _roles=roles; // _numberPlayersInitial=names.length; _numberPlayers=names.length; _dec=[]; @@ -253,6 +505,8 @@ class GameState extends ChangeNotifier { updateChancellorList(); } + + void endGame(){ if(_libBoard.length==5 || !_isHitlerAlive){ _state='lib win'; @@ -263,29 +517,39 @@ class GameState extends ChangeNotifier { } List candidateList(){ - List _candidateList=[... _names]; - _candidateList.removeAt(_turn); - _killedturns.forEach((element) { - _candidateList.remove(element); - }); + // List _candidateList=[... _names]; + // _candidateList.removeAt(_turn); + // _killedturns.forEach((element) { + // _candidateList.remove(element); + // }); + List _candidateList=[]; + for(int i=0;i<_players.length;i++ ){ + if(i!=_turn && _players[i].killed==false){ + _candidateList.add(_players[i].name); + } + } return _candidateList; } void nextRound(){ + endGame(); _round=_round+1; int killedEffect=0; do { if (_special) { - _turn = _names.indexOf(_willPresident); + _turn =_players.lastIndexWhere((player){ + return player.name==_willPresident; + }); + // _turn = _names.indexOf(_willPresident); _specialEffect=-1; } else { _turn = (_firstStarter + _round + _specialEffect + killedEffect) % _numberPlayers; } killedEffect++; - }while(_killedturns.contains(_turn)); + }while(_players[_turn].killed==true); _special=false; if(_dec.length<=2){ _dec=_dec+_disDec; @@ -314,6 +578,13 @@ class GameState extends ChangeNotifier { _state='base'; } + Player findPlayerWithName(String name){ + return _players.lastWhere((player){ + return player.name==name; + }); + + } + @override String toString() { @@ -322,9 +593,7 @@ class GameState extends ChangeNotifier { round:${_round} first_starter:${_firstStarter} number_players:${_numberPlayers} - playersname:${_names} - roles:${_roles} - killed_players:${_killedturns} + players:${_players} dec:${_dec} dis_dec:${_disDec} number_rejected:${_numberRejected} @@ -339,6 +608,7 @@ class GameState extends ChangeNotifier { } void loadSharedPreferences(final SharedPreferences prefs) { + // // Retrieve values from SharedPreferences and assign them to variables _continuegame = true; _lockTester = prefs.getBool('_lockTester') ?? false; @@ -350,8 +620,13 @@ class GameState extends ChangeNotifier { _topThreeSeen = prefs.getBool('_topThreeSeen') ?? false; _chaos = prefs.getBool('_chaos') ?? false; _isHitlerAlive = prefs.getBool('_isHitlerAlive') ?? false; - _names = prefs.getStringList('_names') ?? []; - _roles = prefs.getStringList('_roles') ?? []; + List names = prefs.getStringList('_names') ?? []; + List roles = prefs.getStringList('_roles') ?? []; + List killed = (prefs.getStringList('_killedturns') ?? []).map((e) => toBoolean(e)).toList(); + _players=[]; + for(int i=0;i int.parse(e)).toList(); + _chancellorList = prefs.getStringList('_chancellorList') ?? []; _selectedChancellor = prefs.getString('_selectedChancellor') ?? ''; _presidentSelected = prefs.getInt('_presidentSelected') ?? -1; @@ -380,6 +655,7 @@ class GameState extends ChangeNotifier { void updateSharedPreferences(final SharedPreferences prefs){ + //TODO load gametype and additionalInfo prefs.setBool('hasGame',true); prefs.setBool('_lockTester', _lockTester); prefs.setInt('_numberPlayers',_numberPlayers); @@ -390,8 +666,9 @@ class GameState extends ChangeNotifier { prefs.setBool('_topThreeSeen',_topThreeSeen); prefs.setBool('_chaos',_chaos); prefs.setBool('_isHitlerAlive',_isHitlerAlive); - prefs.setStringList('_names', _names); - prefs.setStringList('_roles', _roles); + prefs.setStringList('_names', _players.map((player) => player.name).toList()); + prefs.setStringList('_roles', _players.map((player) => player.role).toList()); + prefs.setStringList('_killedturns', _players.map((player) => player.killed.toString()).toList()); prefs.setStringList('_dec', _dec); prefs.setStringList('_disDec', _disDec); prefs.setString('_state', _state); @@ -399,7 +676,7 @@ class GameState extends ChangeNotifier { prefs.setString('_lastChancellor', _lastChancellor); prefs.setString('_lastPresident', _lastPresident); prefs.setInt('_turn', _turn); - prefs.setStringList('_killedturns', _killedturns.map((e) => e.toString()).toList()); + // List> _governments=[]; prefs.setStringList('_chancellorList',_chancellorList); prefs.setString('_selectedChancellor',_selectedChancellor); diff --git a/secret_hitler/lib/tester.dart b/secret_hitler/lib/tester.dart index ba811fb..57edb96 100644 --- a/secret_hitler/lib/tester.dart +++ b/secret_hitler/lib/tester.dart @@ -39,7 +39,7 @@ class GameTester { Random random = Random(); int index = random.nextInt(2); gameState.changeChancellorSelect(index); - if(gameState.possibleVeto &&(gameState.roles[gameState.turn]=='lib') && (gameState.roles[gameState.names.indexOf(gameState.selectedChancellor)]=='lib')) { + if(gameState.possibleVeto &&(gameState.players[gameState.turn].role=='lib') && (gameState.findPlayerWithName(gameState.selectedChancellor).role=='lib')) { Future.delayed(Duration(seconds: 2),() { gameState.changeLockTester(false); vetoOnPressed(gameState);