flutter: Support importing game

This commit is contained in:
Calcitem 2021-06-08 00:53:53 +08:00
parent 75e99a81de
commit cd9030e8ff
7 changed files with 129 additions and 1 deletions

View File

@ -208,6 +208,14 @@
"@startNewGame": {
"description": "Start new game"
},
"importGame": "Spiel importieren",
"@importGame": {
"description": "Import game"
},
"gameImported": "Spiel aus der Zwischenablage importiert.",
"@gameImported": {
"description": "Game imported from clipboard."
},
"startRecording": "Starte die Aufnahme",
"@startRecording": {
"description": "Start recording"

View File

@ -208,6 +208,14 @@
"@startNewGame": {
"description": "Start new game"
},
"importGame": "Import game",
"@importGame": {
"description": "Import game"
},
"gameImported": "Game imported from clipboard.",
"@gameImported": {
"description": "Game imported from clipboard."
},
"startRecording": "Start recording",
"@startRecording": {
"description": "Start recording"

View File

@ -208,6 +208,14 @@
"@startNewGame": {
"description": "Start new game"
},
"importGame": "وارد کردن بازی",
"@importGame": {
"description": "Import game"
},
"gameImported": "بازی از کلیپ بورد وارد شده است.",
"@gameImported": {
"description": "Game imported from clipboard."
},
"startRecording": "شروع به ضبط کنید",
"@startRecording": {
"description": "Start recording"

View File

@ -52,6 +52,8 @@
"thinking": "对方思考中...",
"newGame": "新局",
"startNewGame": "开始新局",
"importGame": "导入棋谱",
"gameImported": "棋谱已从剪贴板导入",
"startRecording": "开始录制",
"recording": "录制中...",
"stopRecording": "停止录制",

View File

@ -26,6 +26,7 @@ class GameRecorder {
int cur = -1;
String? lastPositionWithRemove = "";
var _history = <Move>[];
final tag = "[GameRecorder]";
GameRecorder({this.cur = -1, this.lastPositionWithRemove});
@ -37,6 +38,50 @@ class GameRecorder {
_history = newHistory;
}
String parseNotation(String ntt) {
String move = "";
if (ntt.length == 3 && ntt[0] == "x") {
if (notationToMove[ntt.substring(1, 3)] != null) {
move = '-' + notationToMove[ntt.substring(1, 3)]!;
}
} else if (ntt.length == 2) {
if (notationToMove[ntt] != null) {
move = notationToMove[ntt]!;
}
} else if (ntt.length == 5 && ntt[2] == '-') {
if (notationToMove[(ntt.substring(0, 2))] != null &&
notationToMove[(ntt.substring(3, 5))] != null) {
move = notationToMove[(ntt.substring(0, 2))]! +
'->' +
notationToMove[(ntt.substring(3, 5))]!;
}
} else {
print("$tag Parse notation $ntt failed.");
}
return move;
}
void import(String moveList) {
List<Move> newHistory = [];
List<String> list = moveList.split(' ');
for (var i in list) {
i = i.trim();
if (i.length > 0 && !i.endsWith(".")) {
String m = parseNotation(i);
if (m != "") {
newHistory.add(Move(m));
}
}
}
if (newHistory.length > 0) {
setHistory(newHistory);
}
}
void jumpToHead() {
cur = 0;
}

View File

@ -323,6 +323,8 @@ Map<int, int> squareToIndex = {
31: 0
};
Map<int, int> indexToSquare = squareToIndex.map((k, v) => MapEntry(v, k));
/*
a b c d e f g
7 X --- X --- X 7
@ -365,6 +367,31 @@ Map<int, String> squareToNotation = {
31: "a7"
};
Map<int, int> indexToSquare = squareToIndex.map((k, v) => MapEntry(v, k));
Map<String, String> notationToMove = {
"d5": "(1,1)",
"e5": "(1,2)",
"e4": "(1,3)",
"e3": "(1,4)",
"d3": "(1,5)",
"c3": "(1,6)",
"c4": "(1,7)",
"c5": "(1,8)",
"d6": "(2,1)",
"f6": "(2,2)",
"f4": "(2,3)",
"f2": "(2,4)",
"d2": "(2,5)",
"b2": "(2,6)",
"b4": "(2,7)",
"b6": "(2,8)",
"d7": "(3,1)",
"g7": "(3,2)",
"g4": "(3,3)",
"g1": "(3,4)",
"d1": "(3,5)",
"a1": "(3,6)",
"a4": "(3,7)",
"a7": "(3,8)",
};
enum GameResult { pending, win, lose, draw, none }

View File

@ -549,6 +549,27 @@ class _GamePageState extends State<GamePage>
}
}
onImportGameButtonPressed() async {
Navigator.of(context).pop();
ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
if (data == null || data.text == null) {
return;
}
String text = data.text!;
print("Clipboard text:");
print('$text');
await onTakeBackAllButtonPressed(pop: false);
await Game.instance.position.recorder.import(text);
await onStepForwardAllButtonPressed(pop: false);
showTip(S.of(context).gameImported);
}
/*
onStartRecordingButtonPressed() async {
Navigator.of(context).pop();
@ -655,6 +676,15 @@ class _GamePageState extends State<GamePage>
),
onPressed: onStartNewGameButtonPressed,
),
SizedBox(height: AppTheme.sizedBoxHeight),
SimpleDialogOption(
child: Text(
S.of(context).importGame,
style: AppTheme.simpleDialogOptionTextStyle,
textAlign: TextAlign.center,
),
onPressed: onImportGameButtonPressed,
),
/*
SizedBox(height: AppTheme.sizedBoxHeight),
Config.experimentsEnabled