加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.py 7.05 KB
一键复制 编辑 原始数据 按行查看 历史
Earth2023 提交于 2024-01-19 17:05 . 2024年1月19日17:05:52 Update main.py
from loguru import logger
import getpass, socket, os, time, json
with open("D:/Game_Data/MapName.json", "r", encoding="UTF-8") as READ_Data_MapName:
MapNameData = json.loads(READ_Data_MapName.read())
MapNameData = MapNameData["Data"]
CONST_MapNameData = {"Data": MapNameData}
map_name = "UNKNOWN"
map_difficultylevel = "UNKNONW"
map_type = "UNKNONW"
_map: list[list[str]] = []
logger.remove(handler_id=None)
logger.add("D:/Game_Log/Underground_Treasures.log")
logger.info("Game Launch.")
def CreateMap(instruct: list) -> str:
"""
Means of use: Create a new map and save it in a JSON file, confirm it as the current selected map.
"""
Error_List = [
"Error: Don't have map type.",
"Error: Difficulty level input error.",
"Error: Command parameters not fully submitted."
]
Difficulty_Level_Dict = {
"Difficulty": 100,
"Ordinary": 80,
"Simple": 50
}
if len(instruct) != 3:
logger.debug(Error_List[2])
print(Error_List[2])
return ["UNKNOWN", "UNKNOWN", "UNKNOWN", []]
logger.info("Start creating map.")
Create_Map_Type = ""
Difficulty_Level = ""
Create_Map_Name = ""
if instruct[0] == "Python_Map":
Create_Map_Type = "Python"
else:
logger.debug(Error_List[0])
print(Error_List[0])
return ["UNKNOWN", "UNKNOWN", "UNKNOWN", []]
if instruct[1] in Difficulty_Level_Dict.keys():
Difficulty_Level = instruct[1]
else:
logger.debug(Error_List[1])
print(Error_List[1])
return ["UNKNOWN", "UNKNOWN", "UNKNOWN", []]
Create_Map_Name = instruct[2]
def rate_random(data_list, rate_list):
import random
start = 0
random_num = random.random()
for idx, score in enumerate(rate_list):
start += score
if random_num <= start:
break
return data_list[idx]
"""
X: 怪物 0.1
O: 岩石(可能开出未知物品) 0.5
L: 屏障(只能通过钳子(Y)拆除) 0.06
U: 随机宝箱(必开出随机物品) 0.1
S: 中立生物/敌对生物 0.1
A、B: 宝石(
a为蓝宝石(价值20 * TEMP_Floating_Exchange_Rate) 0.06
A为珍珠(价值50 * TEMP_Floating_Exchange_Rate) 0.04
b为翡翠(价值80 * TEMP_Floating_Exchange_Rate) 0.03
B为钻石(价值120 * TEMP_Floating_Exchange_Rate) 0.01
"""
RULE_Type : dict[int : list] = {
0: "X",
1: "O",
2: "L",
3: "U",
4: "S",
5: "a",
6: "A",
7: "b",
8: "B"
}
RULE_Type_Rate: list[float] = [0.1, 0.5, 0.06, 0.1, 0.1, 0.06, 0.04, 0.03, 0.01]
Map: list[list[str]] = []
for e in range(Difficulty_Level_Dict[Difficulty_Level]):
TEMP_Map: list[str] = []
for i in range(Difficulty_Level_Dict[Difficulty_Level]):
TEMP_Map.append(RULE_Type[rate_random([0, 1, 2, 3, 4, 5, 6, 7, 8], RULE_Type_Rate)])
Map.append(TEMP_Map)
logger.info(f"Map type: {Create_Map_Type}; Map difficulty level: {Difficulty_Level}; Map name: {Create_Map_Name}.")
logger.info("Map creation completed.")
print(f"Map type: {Create_Map_Type}; Map difficulty level: {Difficulty_Level}; Map name: {Create_Map_Name}.")
print("Map creation completed.")
TEMP_MapNameData = CONST_MapNameData["Data"]
TEMP_MapNameData.append({'Map_Name': Create_Map_Name, 'Map_Type': Create_Map_Type, 'Map_Difficulty_Level': Difficulty_Level, 'Map_Data': Map})
CONST_MapNameData["Data"] = TEMP_MapNameData
return [Create_Map_Name, Create_Map_Type, Difficulty_Level, Map]
def CheckOutMap(instruct: list) -> list:
"""
Meaning of use: Searching for map related information in JSON files.
"""
MapName = ""
MapDifficultyLevel = ""
MapType = ""
Error_List = [
"Error: Command parameters not fully submitted."
]
if len(instruct) != 1:
logger.debug(Error_List[0])
print(Error_List[0])
return ["UNKNOWN", "UNKNOWN", "UNKNOWN", []]
mapNameData = CONST_MapNameData["Data"]
logger.info(f"MapNameList: {mapNameData[0: 3]}")
for i in range(len(mapNameData)):
if instruct[0] in mapNameData[i].values():
MapName = mapNameData[i]["Map_Name"]
MapType = mapNameData[i]["Map_Type"]
MapDifficultyLevel = mapNameData[i]["Map_Difficulty_Level"]
Map = mapNameData[i]["Map_Data"]
logger.info(f"Map type: {MapType}; Map difficulty level: {MapDifficultyLevel}; Map name: {MapName}.")
return [MapName, MapType, MapDifficultyLevel, Map]
def HelpInstruct(instruct: str) -> None:
logger.info("Help start.")
instruct = instruct.lower()
if instruct == "createmap":
logger.info("Help instruct: CreateMap")
print("""CreateMap <Map Type> <Map Difficulty Level> <Map Name>
Map type: Python, C++
Map difficulty level: Simple(20 x 50), Ordinary(80 x 80), Difficulty(100 x 100)""")
elif instruct == "checkoutmap":
logger.info("Help instruct: CheckoutMap")
print("CheckoutMap <Map Name>")
elif instruct == "showmap":
logger.info("Help instruct: ShowMap")
print("ShowMap <Map Name>")
def ShowMap(instruct: list) -> None:
"""
Means of use: Query map data with specified names.
"""
Difficulty_Level_Dict = {
"Difficulty": 100,
"Ordinary": 80,
"Simple": 50
}
TEMP_CheckOutMap_List = CheckOutMap([instruct[0]])
for i in range(Difficulty_Level_Dict[TEMP_CheckOutMap_List[2]]):
print(" ".join(TEMP_CheckOutMap_List[-1][i]))
return
print("Underground Treasures(地底藏宝) v0.1.1-develop")
time.sleep(0.5)
print(f"Host name: {socket.gethostname()}")
print(f"User name: {getpass.getuser()}")
tmp = f"{os.path.expanduser('~')}\.Underground_Treasures\Custom_System.message"
print(f"Is it a customized system: {os.path.exists(tmp)}")
print("Enter '^Z' or 'Exit' to End Program.")
time.sleep(1)
while True:
try:
instruct = input(f">> [{map_name}, {map_type}, {map_difficultylevel}] ")
except EOFError:
break
logger.info(f"instruct: {instruct}")
logger.info(f"Currently selected map: {map_name}")
instruct = instruct.split(" ")
instruct[0] = instruct[0].lower()
if instruct[0] == "createmap":
map_name, map_type, map_difficultylevel, _map = CreateMap(instruct[1: 4])
elif instruct[0] == "exit":
break
elif instruct[0] == "checkoutmap":
map_name, map_type, map_difficultylevel, _map = CheckOutMap([instruct[1]])
elif instruct[0] == "help":
HelpInstruct(instruct[1])
elif instruct[0] == "showmap":
ShowMap([instruct[1]])
else:
logger.debug("Error: The instruction was not found. Please re-enter the command.")
print("Error: The instruction was not found. Please re-enter the command.")
CONST_WRITE_MapNameData = json.dumps(CONST_MapNameData, indent=4, ensure_ascii=False)
with open("D:/Game_Data/MapName.json", "r+", encoding="UTF-8") as WRITE_Data_MapName:
WRITE_Data_MapName.write(CONST_WRITE_MapNameData)
logger.critical("Game exit.")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化