ConvertAllLevelConfigs.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import RedHelper
  2. import FileUtils
  3. if __name__ == "__main__":
  4. # init
  5. print("convert all red file to json...")
  6. red_helper = RedHelper.RedHelper()
  7. file_utils = FileUtils.FileUtils()
  8. file_utils.greet()
  9. all_level_red_file_full_path = file_utils.get_all_level_red_file_full_paths()
  10. # print(len(all_level_red_file_full_path), all_level_red_file_full_path)
  11. level_id_count = 1
  12. for level_path in all_level_red_file_full_path:
  13. level_one_raw_data = file_utils.xml_to_py_object(file_utils.get_root_of_red_file(level_path))
  14. # print(level_one_raw_data)
  15. template_id_list = red_helper.get_template_IDs(level_one_raw_data)
  16. # print(template_id_list)
  17. template_file_full_paths = file_utils.get_template_file_full_path(template_id_list)
  18. # print(template_file_full_paths)
  19. template_file_data_list = [
  20. file_utils.xml_to_py_object(file_utils.get_root_of_red_file(file_name))
  21. for file_name in template_file_full_paths
  22. ]
  23. # print(len(template_file_data_list), template_file_data_list)
  24. plate_configs_of_this_level = [red_helper.get_plate_configs(template) for template in template_file_data_list]
  25. # 最终生成数据的时候还要使用这个 还没生成screw数据
  26. plate_configs_of_this_level = [
  27. plate_config for plates_of_template in plate_configs_of_this_level for plate_config in plates_of_template
  28. ]
  29. # print(len(plate_configs_of_this_level) , plate_configs_of_this_level)
  30. plate_file_full_paths = file_utils.get_plate_file_full_path(plate_configs_of_this_level)
  31. # print(plate_file_full_paths)
  32. plate_conditions = [
  33. file_utils.xml_to_py_object(file_utils.get_root_of_red_file(file_name)) for file_name in plate_file_full_paths
  34. ]
  35. # print(plate_conditions)
  36. max_screws_of_plate_list = [red_helper.get_max_screws_of_plate(plate_condition) for plate_condition in
  37. plate_conditions]
  38. # print(max_screws_of_plate_list)
  39. # 从这开始模拟C++关卡生成模块
  40. conditions_selected = [red_helper.generate_condition_id(ID) for ID in max_screws_of_plate_list]
  41. # print(conditions_selected)
  42. # 看一下condition_selected能容纳的总钉子个数 不满足三消条件要调整
  43. conditions_selected = red_helper.adjust_conditions_selected(plate_conditions, conditions_selected)
  44. # 此时 plate_condition 和 conditions_selected 中的数据是下标对应的
  45. # 这里 +1 是为了对应生成总数量 不加对应condition的下标
  46. # 盘子情况的顺序最好是钉子数从小到大的 以防万一
  47. # 给每个盘子分配了随机种类的钉子
  48. # screw_types_list = [
  49. # red_helper.generate_screw_types_for_plate(conditions_selected[i] + 1) for i in range(len(conditions_selected))
  50. # ]
  51. n_screws_of_conditions = red_helper.get_n_screw_of_conditions(plate_conditions, conditions_selected)
  52. screw_types_list = red_helper.generate_screw_types_for_plates(conditions_selected, n_screws_of_conditions)
  53. # TODO: 看一下screw_types_list是否每种钉子出现次数为3的倍数 不是的话要调整
  54. # 如果需要调整 那么需要调整的钉子种类至少是成对出现的 因为之前已经满足钉子总个数为3的倍数
  55. # 如果一种钉子出现次数不满足3的倍数 势必有其他钉子出现次数也不满足要求
  56. # print(screw_types_list)
  57. # print(plate_conditions)
  58. # print(conditions_selected)
  59. # print(screw_types_list)
  60. screw_configs = [
  61. red_helper.generate_screw_configs(
  62. plate_conditions[i],
  63. conditions_selected[i],
  64. screw_types_list[i]
  65. ) for i in range(len(plate_conditions))
  66. ]
  67. # print(screw_configs)
  68. plate_configs_of_this_level = red_helper.correct_plate_zorders(plate_configs_of_this_level)
  69. final_level_config = red_helper.generate_final_level_config(plate_configs_of_this_level, screw_configs)
  70. # print(final_level_config)
  71. final_json_file_name = file_utils.generate_level_json_file_full_path(str(level_id_count))
  72. level_id_count += 1
  73. # print(final_json_file_name)
  74. file_utils.write_dict_to_json(final_level_config, final_json_file_name)