Repository: setokinto/slack-shogi

Issue:
待った に対応する
あえて 待った をありにしたい。

Discussion:

Patch:
diff --git a/app/kifu.py b/app/kifu.py
new file mode 100644
index 0000000..a0a0c5d
--- /dev/null
+++ b/app/kifu.py
@@ -0,0 +1,13 @@
+
+
+class Kifu:
+    
+    def __init__(self):
+        self.kifu = []
+    
+    def add(self, from_x, from_y, to_x, to_y, promote):
+        self.kifu.append((from_x, from_y, to_x, to_y, promote))
+
+    def pop(self):
+        return self.kifu.pop()
+
diff --git a/app/modules/shogi_input.py b/app/modules/shogi_input.py
index 58bea57..7996f02 100644
--- a/app/modules/shogi_input.py
+++ b/app/modules/shogi_input.py
@@ -6,6 +6,7 @@ from app.slack_utils.user import User as UserFinder
 from app.modules.shogi import Shogi as ShogiModule
 from app.modules.parse_input import ParseInput
 from app.validator import BasicUserValidator, AllPassUserValidator
+from app.kifu import Kifu
 
 
 class UserDifferentException(Exception):
@@ -136,6 +137,13 @@ class ShogiInput:
             "_shogi": shogi,
         }
 
+    @staticmethod
+    def matta(channel_id, user_id):
+        shogi = ShogiInput.manager.get_shogi(channel_id)
+        if not shogi.validate(shogi, user_id):
+            raise UserDifferentException()
+        shogi.matta()
+
 
 class Shogi:
 
@@ -150,9 +158,11 @@ class Shogi:
         self.second_user_name = users[1]["name"]
         self.id = uuid.uuid4().hex
         self._validator = validator
+        self.kifu = Kifu()
 
     def move(self, from_x, from_y, to_x, to_y, promote):
         self.shogi.move(from_x, from_y, to_x, to_y, promote)
+        self.kifu.add(from_x, from_y, to_x, to_y, promote)
 
     def drop(self, koma, to_x, to_y):
         self.shogi.drop(koma, to_x, to_y)
@@ -172,6 +182,15 @@ class Shogi:
     def set_validator(self, validator):
         self._validator = validator
 
+    def matta(self):
+        if len(self.kifu.kifu) == 0:
+            raise KomaCannotMoveException
+        self.kifu.pop()
+        self.shogi = ShogiModule()
+        for kifu in self.kifu.kifu:
+            from_x, from_y, to_x, to_y, promote = kifu
+            self.shogi.move(from_x, from_y, to_x, to_y, promote)
+
     @property
     def first(self):
         return self.shogi.first
diff --git a/app/shogi.py b/app/shogi.py
index c28afea..3173412 100644
--- a/app/shogi.py
+++ b/app/shogi.py
@@ -113,3 +113,19 @@ def resign(channel, message):
     message.send(board_str)
     ShogiInput.clear(channel.channel_id)
 
+@respond_to("待った")
+@channel_info
+@should_exist_shogi
+def matta(channel, message):
+    try:
+        ShogiInput.matta(channel.channel_id, channel.own_id)
+        message.send("mattaed")
+    except UserDifferentException:
+        message.reply("You cannot matta because *it's not your turn*")
+    except KomaCannotMoveException:
+        message.reply("You cannot matta because koma not moved")
+    finally:
+        board = ShogiInput.get_shogi_board(channel.channel_id)
+        board_str = ShogiOutput.make_board_emoji(board)
+        message.send(board_str)
+


Test patch:
diff --git a/test/modules/shogi_input_test.py b/test/modules/shogi_input_test.py
index 58dca13..272b547 100644
--- a/test/modules/shogi_input_test.py
+++ b/test/modules/shogi_input_test.py
@@ -4,7 +4,6 @@ from app.modules.shogi_input import ShogiInput, UserDifferentException, KomaCann
 from app.modules.shogi import Koma
 
 
-
 class ShogiTest(unittest.TestCase):
 
     def setUp(self):
@@ -107,3 +106,48 @@ class ShogiTest(unittest.TestCase):
         ShogiInput.setAllMode(channel_id)
         ShogiInput.move("34歩", channel_id, shogi.first_user_id)
 
+    def test_matta(self):
+        channel_id = "test_matta"
+        shogi = ShogiInput.init(channel_id, [{
+            "id": "user1",
+            "name": "user1name",
+        }, {
+            "id": "user2",
+            "name": "user2name",
+        }])
+        ShogiInput.move("76歩", channel_id, shogi.first_user_id)
+        self.assertEqual(shogi.board[5][2], Koma.fu)
+        ShogiInput.matta(channel_id, shogi.second_user_id)
+        self.assertEqual(shogi.board[5][2], Koma.empty)
+        ShogiInput.move("76歩", channel_id, shogi.first_user_id)
+        self.assertEqual(shogi.board[5][2], Koma.fu)
+ 
+    def test_matta_for_UserDifferentException(self):
+        channel_id = "test_matta_for_UserDifferentException"
+        shogi = ShogiInput.init(channel_id, [{
+            "id": "user1",
+            "name": "user1name",
+        }, {
+            "id": "user2",
+            "name": "user2name",
+        }])
+        ShogiInput.move("76歩", channel_id, shogi.first_user_id)
+        self.assertEqual(shogi.board[5][2], Koma.fu)
+        with self.assertRaises(UserDifferentException):
+            ShogiInput.matta(channel_id, shogi.first_user_id)
+        ShogiInput.move("34歩", channel_id, shogi.second_user_id)
+        with self.assertRaises(UserDifferentException):
+            ShogiInput.matta(channel_id, shogi.second_user_id)
+
+    def test_matta_for_KomaCannotMoveException(self):
+        channel_id = "test_matta_for_KomaCannotMoveException"
+        shogi = ShogiInput.init(channel_id, [{
+            "id": "user1",
+            "name": "user1name",
+        }, {
+            "id": "user2",
+            "name": "user2name",
+        }])
+        with self.assertRaises(KomaCannotMoveException):
+            ShogiInput.matta(channel_id, shogi.first_user_id)
+