;================================================================
; mahjong/mahjong_play 麻将游戏 实际业务逻辑
; mahjong_run_round.erb 进行单局游戏（kyoku）
;================================================================


;;;初始化单局麻将游戏
;;;@return 没有返回值
@MahjongInitGame
    CALL PrintLine
    CALL MahjongShuffleTilesWall(MJ_GAME_RULE)  ; 重新洗牌
    MJ_KAN_COUNT = 0                            ; 重置开杠数量
    VARSET MJ_GAIN_HISTORY:0:0, ""              ; 重置各家进张记录
    VARSET MJ_DISCARD_PILE:0:0, ""              ; 重置各家牌河
    MJ_LAST_DISCARD = 0
    MJ_LAST_DISCARD_SEAT = -1
    CALL MahjongDealStartingHand
    MJ_CURRENT_PLAYER = MJ_ROUND_WIND
    MJ_LAST_DISCARD = 0
    MJ_LAST_DISCARD_SEAT = -1
    CALL PrintLog(@"已初始化本局麻将游戏，你是%MJ_ENUM_SEAT:MJ_PLAYER_SEAT%家。")
    ; TODO debug 调试用 开启透视
    MJ_CHEAT_X_RAY = 1
    RETURN


;;;处理玩家输入
;;;@return 1处理完成 0处理未完成
@MahjongHandleInput(button_value)
    #DIM button_value
    #DIMS DYNAMIC discard_code
    SELECTCASE button_value
        CASE IS >= 0xb200
            SELECTCASE button_value - 0xb200
                CASE MJ_ENUM_WIN
                    MJ_LOG_TEXT '= "选择和牌"
                    RETURN 1
                CASE MJ_ENUM_RIICHI
                    MJ_LOG_TEXT '= "选择立直"
                    RETURN 1
                CASE MJ_ENUM_KAN
                    MJ_LOG_TEXT '= "试图杠"
                    RETURN 1
                CASE MJ_ENUM_PON
                    MJ_LOG_TEXT '= "试图碰"
                    RETURN 1
                CASE MJ_ENUM_CHII
                    MJ_LOG_TEXT '= "试图吃"
                    RETURN 1
                CASE MJ_ENUM_SKIP
                    MJ_LOG_TEXT '= "选择跳过"
                    RETURN 1
                CASE MJ_ENUM_DRAW
                    MJ_LOG_TEXT '= "选择流局（九种九牌）"
                    RETURN 1
                CASEELSE
                    MJ_LOG_TEXT '= "未知选项"
                    RETURN 0
            ENDSELECT
        CASE IS >= 0xb100
            button_value = button_value - 0xb100
            discard_code '= mj_to_code(MJ_HAND:MJ_PLAYER_SEAT:button_value)
            IF mj_discard_by(MJ_PLAYER_SEAT, discard_code)
                PLAYSOUND "mahjong/default/dahai.wav"
                MJ_LOG_TEXT '= @"自家切掉第 {button_value + 1} 张手牌「%mj_humanize(discard_code)%」"
                RETURN 1
            ENDIF
        CASEELSE
            MJ_KAN_COUNT = LIMIT(RESULT, 0, 4)
            MJ_LOG_TEXT '= @"输入开杠数量（当前={MJ_KAN_COUNT}）以调试宝牌/海底牌/岭上牌"
    ENDSELECT

    ; TODO
    CLEARLINE LINECOUNT - MJ_LINE_FLAG
    CALL MahjongPrintButtons
    PRINTL
    CALL MahjongPrintSelfHand
    CALL PrintLine
    CALL PrintLog(MJ_LOG_TEXT)

    RETURN 0


;;;各家处理自家摸牌
;;;@return 没有返回值
@MahjongEveryoneHandleDraw
    IF MJ_PLAYER_SEAT == MJ_CURRENT_PLAYER
        WHILE 1
            INPUT
            CLEARLINE 1
            CALL MahjongHandleInput(RESULT)
            SIF RESULT
                BREAK
        WEND
    ELSE
        CALL MahjongBotHandleDraw(MJ_CURRENT_PLAYER)
    ENDIF
    RETURN


;;;各家处理别家切牌
;;;@return 0无事发生 1鸣牌 3荣和
@MahjongEveryoneHandleDiscard
    #DIM DYNAMIC i
    #DIMS DYNAMIC choose, 4
    #DIM DYNAMIC priority, 4
    #DIM DYNAMIC execute
    FOR i, 0, MJ_PLAYER_COUNT
        IF i == MJ_CURRENT_PLAYER
            CONTINUE
        ELSEIF mj_get_options(i) > 0
            IF i == MJ_PLAYER_SEAT
                ; 重新打印按钮
                CALL MahjongPrintDesk
                WHILE 1
                    INPUT
                    CLEARLINE 1
                    CALL MahjongHandleInput(RESULT)
                    SIF RESULT
                        BREAK
                WEND
            ELSE
                CALL MahjongBotHandleDiscard(i)
                priority:i = RESULT
                choose:i '= RESULTS
            ENDIF
        ENDIF
    NEXT
    ; 判断操作优先级
    ; 和 > 碰/杠 > 吃 > 正常轮换
    execute = MAXARRAY(priority)
    ; TODO 并进行处理
    RETURN 0


;;;单局麻将游戏的主循环
;;;（轮流摸切）
;;;@return 没有返回值
@MahjongMainLoop
    CALL MahjongSortHand()
    CALL MahjongPrintDesk
    ; TODO 判断天和/两立直/暗杠/九种九牌流局
    CALL MahjongEveryoneHandleDraw
    WHILE 1
        CALL MahjongEveryoneHandleDiscard
        SELECTCASE RESULT
            CASE 1  ; 有人鸣牌（大明杠/碰/吃）
                MJ_CURRENT_PLAYER = 1  ; TODO 牌权切换到鸣牌的那家
                CALL MahjongEveryoneHandleDraw
                CONTINUE
            CASE 2  ; 有人放铳 本局游戏结束
                BREAK
            CASEELSE  ; 无事发生 正常轮换
                MJ_LAST_DISCARD = 0  ; 清理舍张标记
                MJ_CURRENT_PLAYER = mj_get_next_seat(MJ_CURRENT_PLAYER)
        ENDSELECT
        SIF !mj_count_wall()  ; 荒牌流局
            BREAK
        CALL MahjongSortHand()
        ; 摸一张牌
        CALL MahjongDrawTo(MJ_CURRENT_PLAYER)
        SIF !RESULT  ; 摸牌失败
            THROW THROW %MJ_ENUM_SEAT:MJ_CURRENT_PLAYER%家 %mj_get_hand_code(MJ_CURRENT_PLAYER)%({mj_count_hand(MJ_CURRENT_PLAYER)}) 试图摸牌失败
        CALL MahjongPrintDesk
        ; TODO 判断自摸/立直/暗杠/加杠/九种九牌流局（仅限第一巡）
        CALL MahjongEveryoneHandleDraw
    WEND
    RETURN


;;;结算单局麻将游戏
;;;@return 没有返回值
@MahjongSettleGame
    ; TODO 有人和了/流局 进行结算
    RETURN
