Skip to content

Commit

Permalink
Made basic wordComplaining
Browse files Browse the repository at this point in the history
  • Loading branch information
uliana57 committed Nov 24, 2019
1 parent e736a8b commit 9457b06
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 71 deletions.
54 changes: 46 additions & 8 deletions lib/app_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:http/http.dart' as http;
import 'package:mobx/mobx.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

import 'dictionary.dart';
import 'game_state.dart';
Expand All @@ -17,6 +18,12 @@ abstract class _AppState with Store {
@observable
bool loaded = false;

@observable
var uuid = Uuid();

@observable
String deviceId;

@action
void saveGameLog(gameLog) {
if (!File('$documentsPath/gameLogs.json').existsSync()) {
Expand All @@ -25,8 +32,8 @@ abstract class _AppState with Store {
File('$documentsPath/gameLogs.json')
.writeAsStringSync(jsonEncode(gameLogs));
} else {
List gameLogs = jsonDecode(File('$documentsPath/gameLogs.json')
.readAsStringSync());
List gameLogs =
jsonDecode(File('$documentsPath/gameLogs.json').readAsStringSync());
gameLogs.add(gameLog);
File('$documentsPath/gameLogs.json')
.writeAsStringSync(jsonEncode(gameLogs));
Expand All @@ -42,8 +49,8 @@ abstract class _AppState with Store {
@action
Future<void> sendSavedGameLogs() async {
if (File('$documentsPath/gameLogs.json').existsSync()) {
List gameLogs = jsonDecode(
File('$documentsPath/gameLogs.json').readAsStringSync());
List gameLogs =
jsonDecode(File('$documentsPath/gameLogs.json').readAsStringSync());
Set sentLogs = Set();
for (var gameLog in gameLogs) {
var response = await sendGameLog(gameLog);
Expand All @@ -54,15 +61,39 @@ abstract class _AppState with Store {
for (var gameLog in sentLogs) {
gameLogs.remove(gameLog);
}
if (gameLogs.isEmpty)
if (gameLogs.isEmpty) {
File('$documentsPath/gameLogs.json').deleteSync();
else {
File('$documentsPath/gameLogs.json').writeAsStringSync(
jsonEncode(gameLogs));
} else {
File('$documentsPath/gameLogs.json')
.writeAsStringSync(jsonEncode(gameLogs));
}
}
}

@action
Future<void> sendSavedWordsComplains() async {
if (File('$documentsPath/wordsComplains.json').existsSync()) {
var response = await http.post(
'http://the-hat-dev.appspot.com/$deviceId/complain',
body: File('$documentsPath/wordsComplains.json').readAsStringSync());
if (response.statusCode == 200)
File('$documentsPath/wordsComplains.json').deleteSync();
}
}

@action
void saveWordsComplains(wordsComplains) {
List savedWordsComplains;
if (File('$documentsPath/wordsComplains.json').existsSync()) {
savedWordsComplains = jsonDecode(
File('$documentsPath/wordsComplains.json').readAsStringSync());
} else {
savedWordsComplains = List();
}
File('$documentsPath/wordsComplains.json')
.writeAsStringSync(jsonEncode(savedWordsComplains + wordsComplains));
}

@observable
GameState gameState;

Expand All @@ -87,6 +118,13 @@ abstract class _AppState with Store {
await sendSavedGameLogs();
dictionary = Dictionary(prefs, documentsPath);
await dictionary.load();
if (prefs.getString('deviceId') == null) {
deviceId = uuid.v4();
prefs.setString('deviceId', deviceId);
} else {
deviceId = prefs.getString('deviceId');
}
await sendSavedWordsComplains();
loaded = true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/dictionary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ class Dictionary {
List usedWords = getUsedWords();
int usedWordsIter = getUsedWordsIter();
for (var i = 0; i < size; i++) {
int bucketIdx = (Normal.generate(1)[0] / 3 * difficultyDispersion +
difficulty).round();
int bucketIdx =
(Normal.generate(1)[0] / 3 * difficultyDispersion + difficulty)
.round();
while (bucketIdx < 0 || bucketIdx > 100) {
bucketIdx =
(Normal.generate(1)[0] / 3 * difficultyDispersion + difficulty)
Expand Down
31 changes: 19 additions & 12 deletions lib/game_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ abstract class _GameState with Store {
lastStateLength = prefs.getInt('lastStateLength');
mainStateLength = prefs.getInt('mainStateLength');
fixTeams = prefs.getBool('fixTeams');
audioPlayer.loadAll(['round_start_timer_tick.wav',
'round_start_timer_timeout.wav', 'round_timer_timeout.wav',
'word_outcome_fail.wav', 'word_outcome_ok.wav',
'word_outcome_timeout.wav']);
audioPlayer.loadAll([
'round_start_timer_tick.wav',
'round_start_timer_timeout.wav',
'round_timer_timeout.wav',
'word_outcome_fail.wav',
'word_outcome_ok.wav',
'word_outcome_timeout.wav'
]);
}

@observable
Expand All @@ -49,6 +53,9 @@ abstract class _GameState with Store {
'attempts': []
};

@observable
List wordComplains = [];

@observable
int mainStateLength;

Expand Down Expand Up @@ -232,13 +239,13 @@ abstract class _GameState with Store {
newTurnTimer = Timer.periodic(Duration(seconds: 1), (Timer _timeout) {
newTurnTimerSecondPass();
if (newTurnTimerCnt == 0) {
audioPlayer.play(
'round_start_timer_timeout.wav', mode: PlayerMode.LOW_LATENCY);
audioPlayer.play('round_start_timer_timeout.wav',
mode: PlayerMode.LOW_LATENCY);
_timeout.cancel();
newTurn();
} else {
audioPlayer.play(
'round_start_timer_tick.wav', mode: PlayerMode.LOW_LATENCY);
audioPlayer.play('round_start_timer_tick.wav',
mode: PlayerMode.LOW_LATENCY);
}
});
}
Expand All @@ -255,8 +262,8 @@ abstract class _GameState with Store {
} else if (timer == 0) {
timeSpent = stopwatch.elapsedMilliseconds;
if (lastStateLength != 0) {
audioPlayer.play(
'round_timer_timeout.wav', mode: PlayerMode.LOW_LATENCY);
audioPlayer.play('round_timer_timeout.wav',
mode: PlayerMode.LOW_LATENCY);
}
stopwatch.reset();
changeState('last');
Expand All @@ -269,8 +276,8 @@ abstract class _GameState with Store {
'time': timeSpent,
'extra_time': lastStateLength,
});
audioPlayer.play(
'round_start_timer_timeout.wav', mode: PlayerMode.LOW_LATENCY);
audioPlayer.play('round_start_timer_timeout.wav',
mode: PlayerMode.LOW_LATENCY);
changeState('verdict');
stopwatch.stop();
}
Expand Down
17 changes: 17 additions & 0 deletions lib/game_state.g.dart

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

6 changes: 4 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class MyApp extends StatelessWidget {
);
},
child: Text('Настройки',
style: TextStyle(color: Colors.blue,
style: TextStyle(
color: Colors.blue,
fontSize: 25)))))
]),
TableRow(children: [
Expand Down Expand Up @@ -104,7 +105,8 @@ class MyApp extends StatelessWidget {
builder: (context) => Info()));
},
child: Text('Правила и доп. информация',
style: TextStyle(color: Colors.blue,
style: TextStyle(
color: Colors.blue,
fontSize: 25)))))
])
]));
Expand Down
7 changes: 4 additions & 3 deletions lib/match.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ class Match extends StatelessWidget {
if (currentGameState.validateAll()) {
currentGameState.createHat(currentAppState.dictionary);
currentGameState.timer = currentGameState.mainStateLength;
currentGameState.gameLog['start_timestamp'] = DateTime
.now()
.millisecondsSinceEpoch;
currentGameState.gameLog['start_timestamp'] =
DateTime
.now()
.millisecondsSinceEpoch;
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
Expand Down
Loading

0 comments on commit 9457b06

Please sign in to comment.