---
# System prepended metadata

title: UnitTest_FileProcess.py
tags: [UnitTest_FileProcess]

---

# UnitTest_FileProcess.py
###### tags: `UnitTest_FileProcess`
```
import os
import MJ_tiletest_new
from MJ_tiletest_new import hu_test
# C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn3_0327\\
# C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\enter\\Board\\Mahjong\\33test\\
#dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn3_0327\\'
'''
dir_dict = {'a1': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\open\\Board\\Mahjong\\a1_vs_a3_vs_a4_vs_a2_2022_03_21_03_09_54\\',
       'a3': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\enter2\\Board\\Mahjong\\a1_vs_a3_vs_a4_vs_a2_2022_03_21_03_09_54\\',
       'a4': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\enter3\\Board\\Mahjong\\a1_vs_a3_vs_a4_vs_a2_2022_03_21_03_09_54\\',
       'a2': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\windows_MJ_new\\windows\\enter\\Board\\Mahjong\\500contest\\'}

out_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\turn4_0330\\'
'''

dir_dict = {'a1': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\a1\\',
       'a2': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\a2\\',
       'a3': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\a3\\',
       'a4': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\a4\\'}
out_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\2973r\\test\\'

#dir_dict = {'a1': 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\debug\\old_file\\'}
#out_dir = 'C:\\Users\\chsta\\Desktop\\Majong_Project\\test\\debug\\new_file\\'


def check_chucker(txt_list):
    player_game_wind = txt_list[2].split()[2]
    for i in range(len(txt_list)-1, 0, -1):
        if txt_list[i].find('/throw') != -1:
            chucker_game_wind = txt_list[i].split()[1]
            if player_game_wind == chucker_game_wind:
                return True
            else:
                break
    return False

def print_info(txt_list, player_card):
    print('  out: ' + outfilename)
    #print("  game_wind = " + str(txt_list[2].split()[2]))
    #print("  txt_list = " + str(txt_list))
    #print("  txt_list_len = " + str(len(txt_list)))
    #print("  player = " + dir_k)
    print("  player_card =", player_card)
    
def get_player_card(txt_list, wind, master):
    begin = 1
    if wind  == master:
        end = 18
    else:
        end = 17
    player_card = txt_list[4].split()[begin:end]
    #player_card_int = [int(c) for c in player_card]
    return player_card

def modify_player_card(player_card, txt_list, wind, file):
    #print('  last txt_list =', txt_list[-1])
    for i in range(0, len(txt_list)):
        line_list = txt_list[i].split()
        #print(i,line_list)
        if line_list[0] == '/throw' and line_list[1] == str(wind):
            player_card.remove(line_list[2])
        elif line_list[0] == '/mo':
            player_card.append(line_list[1])
        elif line_list[0] == '/eat' and line_list[1] == str(wind):
            player_card.remove(line_list[2])
            player_card.remove(line_list[4])
        elif line_list[0] == '/pong' and line_list[1] == str(wind):
            player_card.remove(line_list[3])
            player_card.remove(line_list[4])
        elif line_list[0] == '/gong' and line_list[1] == str(wind) and line_list[2] == 4:
            player_card.remove(line_list[4])
            player_card.remove(line_list[5])
            player_card.remove(line_list[6])
        elif line_list[0] == '/gong' and line_list[1] == str(wind) and line_list[2] == 0:
            player_card.remove(line_list[3])
            player_card.remove(line_list[4])
            player_card.remove(line_list[5])
            player_card.remove(line_list[6])
        elif line_list[0] == '/gong' and line_list[1] == str(wind) and line_list[2] == 1:
            player_card.remove(line_list[3])
    return player_card

def is_ready_hand(player_card, file):
    #player_card.pop()
    player_card_int = [int(c) for c in player_card]
    copy_card = player_card_int.copy()
    for j in range(len(player_card_int)):
        copy_card = player_card_int.copy()
        copy_card.pop(j)
        for i in range(11, 48):
            if i%10 == 0: continue
            x = i*10 +1
            if hu_test(copy_card, x):
                return True
    return False

def read_file(file_fullpath):
    f = open(file_fullpath, 'r')
    txt_list = f.read()
    txt_list = txt_list.split('\n')
    f.close()
    return txt_list

def modify_file(txt_list):
    # process the last hand and the follwing commands
    for i in range(len(txt_list)-1, 0, -1):
        if txt_list[i].find('/ask throw') != -1: #not find is -1
            break
        else:
            txt_list.pop()
    # process the commands with '/ask' and '>>'
    for i in range(len(txt_list)-1, 0, -1):
        line_list = txt_list[i].split()
        if line_list[0] == '/ask':
            txt_list.pop(i)
        if line_list[0] == '/throw':
            if ((line_list[1] != '1') and (line_list[1] != '2') and (line_list[1] != '3') and (line_list[1] != '4')):
                txt_list.pop(i)
        if line_list[0] == '/eat':
            if ((line_list[1] != '1') and (line_list[1] != '2') and (line_list[1] != '3') and (line_list[1] != '4')):
                txt_list.pop(i)
        if line_list[0] == '/pong':
            if ((line_list[1] != '1') and (line_list[1] != '2') and (line_list[1] != '3') and (line_list[1] != '4')):
                txt_list.pop(i)
        if line_list[0] == '/gong':
            if ((line_list[1] != '1') and (line_list[1] != '2') and (line_list[1] != '3') and (line_list[1] != '4')):
                txt_list.pop(i)
        if line_list[0] == '/ready':
            txt_list.pop(i)
        if line_list[0] == '/pass':
            txt_list.pop(i)
    return txt_list

def write_file(txt_list, out_file_fullpath, file):
    f = open(out_file_fullpath, 'w')
    # save the old file name at the top
    f.write('>>' + file + '\n')
    f.write('\n'.join(txt_list))
    #f.write('\n')
    f.close()
    # save the test number to file
    f1 = open(out_dir + 'test_number.txt', 'a')
    f1.write(file.split()[0] + '\n')
    f1.close()


# main
i = 0
for file in os.listdir(dir_dict['a1']):
    file_fullpath = dir_dict['a1']+file
    # check (is file) and (the filename have chucker) and (is .command)
    if (os.path.isfile(file_fullpath)) and ('.command' in file) and ('chucker' in file):
        #print("filename = " + file)
        outfilename = 'round_' + str(i) + '.command'
        #print('  file_fullpath =',file_fullpath)
        # find chucker and modify
        for (dir_k, dir_v) in dir_dict.items():
            file_fullpath = dir_v + file
            # read file to get txt_list(list of line)
            txt_list = read_file(file_fullpath)
            #print('  txt_list =',txt_list[4])
            # check the file is the chucker vision
            is_chucker = check_chucker(txt_list)
            if is_chucker:
                wind = txt_list[2].split()[2]
                master = txt_list[3].split()[4]
                # get player cards
                player_card = get_player_card(txt_list, wind, master)
                # modify the file to the state before chucker
                txt_list = modify_file(txt_list)
                # modify the player cards to the state before chucker
                player_card = modify_player_card(player_card, txt_list, wind, file)
                # check is ready hand
                if not is_ready_hand(player_card, file):
                    if len(txt_list)-4 > 55:
                        print('not ready hand', file)
                        # write the modified file to the new place
                        out_file_fullpath = out_dir + outfilename
                        write_file(txt_list, out_file_fullpath, file)
                        # print_info for debug
                        print_info(txt_list, player_card)
                        i = i + 1
                else:
                    print('is ready hand', file)