def start_shogi(channel, message, opponent_name): slacker = message._client.webapi user = User(slacker) opponent_id = user.username_to_id(opponent_name) if opponent_id is None: # In case of mention. In mention, slack transform username to userid # like @username to <@UOIFJ83F> opponent_id = opponent_name if not user.user_in_channel(opponent_id, channel.channel_id): message.reply("Error, sorry. Opponent is not found in this channel") return shogi = ShogiInput.init(channel_id=channel.channel_id, users=[{ "id": channel.own_id, "name": user.id_to_username(channel.own_id), }, { "id": opponent_id, "name": user.id_to_username(opponent_id), }]) if shogi is None: message.reply( "Shogi started already by a user. Sorry.\nIf you want to quit shogi which already exists, please say this command `resign`" ) else: message.reply("Shogi started: " + shogi.id) board = ShogiInput.get_shogi_board(channel.channel_id) board_str = ShogiOutput.make_board_emoji(board) message.send(board_str)
def test_shogi_input_is_initable(self): shogi = ShogiInput.init("channel_id", [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) self.assertEqual(shogi.channel_id, "channel_id") shogi = ShogiInput.init("channel_id", [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) self.assertIsNone(shogi) ShogiInput.clear("channel_id") shogi = ShogiInput.init("channel_id", [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) self.assertEqual(shogi.channel_id, "channel_id")
def test_shogi_input_is_initable(self): shogi = ShogiInput.init("channel_id", [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", } ]) self.assertEqual(shogi.channel_id, "channel_id") shogi = ShogiInput.init("channel_id", [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", } ]) self.assertIsNone(shogi) ShogiInput.clear("channel_id") shogi = ShogiInput.init("channel_id", [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", } ]) self.assertEqual(shogi.channel_id, "channel_id")
def test_shogi_output_reverse_format(self): # for syntax error and runtime error channel_id = "channel_id" shogi = ShogiInput.init(channel_id, [{"id": "user1", "name": "user1name"}, {"id": "user2", "name": "user2name"}]) board = ShogiInput.get_shogi_board(channel_id) board_str = ShogiOutput.make_board_emoji_reverse(board) self.assertIsNotNone(board_str)
def koma_move(channel, message, destination, piece, position=None, direction=None, promotion=None): movement_str = "".join([ x for x in [destination, piece, position, direction, promotion] if x is not None ]) #def koma_move(channel, message, position, dou, koma, sub_position=None, promote=None): # movement_str = "".join( # [x for x in [position, dou, koma, sub_position, promote] if x is not None]) try: ShogiInput.move(movement_str, channel.channel_id, channel.own_id) except UserDifferentException: message.reply("You cannot move this because *it's not your turn*") except KomaCannotMoveException: message.reply( "You cannot move this with your message *{}*".format(movement_str)) finally: board = ShogiInput.get_shogi_board(channel.channel_id) board_str = ShogiOutput.make_board_emoji(board) message.send(board_str)
def test_try_to_get_shogi_board(self): channel_id = "test_try_to_get_shogi_board" shogi = ShogiInput.init(channel_id, [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) ShogiInput.get_shogi_board(channel_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)
def test_move_method_should_work(self): channel_id = "test_move_method_should_work" 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)
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)
def test_shogi_output_format(self): # for syntax error and runtime error channel_id = "channel_id" shogi = ShogiInput.init(channel_id, [{ "id": "user1", "name": "user1name" }, { "id": "user2", "name": "user2name" }]) board = ShogiInput.get_shogi_board(channel_id) board_str = ShogiOutput.make_board_emoji(board) self.assertIsNotNone(board_str)
def test_move_method_should_raise_UserDifferentException(self): channel_id = "test_move_method_should_raise_UserDifferentException" shogi = ShogiInput.init(channel_id, [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) with self.assertRaises(UserDifferentException): ShogiInput.move("76歩", channel_id, shogi.second_user.id) with self.assertRaises(UserDifferentException): ShogiInput.move("76歩", channel_id, shogi.second_user.id)
def _warp(*args, **kwargs): channel = args[0] message = args[1] if not ShogiInput.exists(channel.channel_id): message.reply("start with してくれっ") return f(*args, **kwargs)
def _warp(*args, **kwargs): channel = args[0] message = args[1] if not ShogiInput.exists(channel.channel_id): message.reply("start withから初めてね") return f(*args, **kwargs)
def test_set_any_user_validator(self): channel_id = "test_set_validotr" shogi = ShogiInput.init(channel_id, [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) ShogiInput.move("76歩", channel_id, shogi.first_user.id) with self.assertRaises(UserDifferentException): ShogiInput.move("34歩", channel_id, shogi.first_user.id) ShogiInput.setAllMode(channel_id) ShogiInput.move("34歩", channel_id, shogi.first_user.id)
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_move_method_should_raise_KomaCannotMoveException(self): channel_id = "test_move_method_should_raise_KomaCannotMoveException" shogi = ShogiInput.init(channel_id, [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) with self.assertRaises(KomaCannotMoveException): ShogiInput.move("75歩", channel_id, shogi.first_user.id) with self.assertRaises(KomaCannotMoveException): ShogiInput.move("34歩", channel_id, shogi.first_user.id) with self.assertRaises(KomaCannotMoveException): ShogiInput.move("15151歩", channel_id, shogi.first_user.id) with self.assertRaises(KomaCannotMoveException): ShogiInput.move("Wow, it's great.", 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 set_mode(channel, message, arg): if arg == "all": ShogiInput.setAllMode(channel.channel_id) message.reply("Done! All member can move now!")
def test_matta_for_drop_komas(self): channel_id = "test_matta_for_da_komas" shogi = ShogiInput.init(channel_id, [{ "id": "user1", "name": "user1name", }, { "id": "user2", "name": "user2name", }]) ShogiInput.move("76歩", channel_id, shogi.first_user.id) ShogiInput.move("34歩", channel_id, shogi.second_user.id) ShogiInput.move("22角", channel_id, shogi.first_user.id) ShogiInput.move("同銀", channel_id, shogi.second_user.id) ShogiInput.move("55角打", channel_id, shogi.first_user.id) ShogiInput.move("33角打", channel_id, shogi.second_user.id) ShogiInput.matta(channel_id, shogi.first_user.id) ShogiInput.matta(channel_id, shogi.second_user.id) ShogiInput.move("55角打", channel_id, shogi.first_user.id) ShogiInput.move("33角打", channel_id, shogi.second_user.id) self.assertEqual(shogi.board[4][4], Koma.kaku) self.assertEqual(shogi.board[2][6], Koma.opponent_kaku)
def hifumin(channel, message): board = ShogiInput.get_shogi_board(channel.channel_id) board_str = ShogiOutput.make_board_emoji_reverse(board) message.send(board_str)
def test_clear_for_non_exists_channnel(self): self.assertIsNone(ShogiInput.clear("channel_id_non_exists"))
def resign(channel, message): message.send("最終局面") board = ShogiInput.get_shogi_board(channel.channel_id) board_str = ShogiOutput.make_board_emoji(board) message.send(board_str) ShogiInput.clear(channel.channel_id)
def board_info(channel, message): board = ShogiInput.get_shogi_board(channel.channel_id) board_str = ShogiOutput.make_board_emoji(board) message.send(board_str)