|
| 1 | +from threading import Thread |
| 2 | +import threading |
| 3 | +from mrmine.keypress_detector import detect_keypress_nonblocking |
| 4 | +from mrmine.gui import update_GUI, scroll, update_GUI_func, GUI_send, get_save |
| 5 | +import os, time, random, math |
| 6 | +global save_data |
| 7 | +# Make the relay of keys more accessible |
| 8 | +key_pressed = None |
| 9 | + |
| 10 | +def keypress_listener(): |
| 11 | + global key_pressed |
| 12 | + while True: |
| 13 | + key = detect_keypress_nonblocking() |
| 14 | + if key: |
| 15 | + key_pressed = key.lower() |
| 16 | + if key_pressed == '\x03': # Ctrl+C for interrupt |
| 17 | + raise KeyboardInterrupt |
| 18 | + |
| 19 | +def tick(): |
| 20 | + global key_pressed |
| 21 | + while True: |
| 22 | + if key_pressed: |
| 23 | + key = key_pressed |
| 24 | + key_pressed = False |
| 25 | + print(f"Key registered: {key}") |
| 26 | + if key == 'up' or key == 'down': |
| 27 | + scroll(key) |
| 28 | + update_GUI() |
| 29 | + elif key in ["k", "q", "c", "s", "h", "u", " ", "p", "r"]: |
| 30 | + update_GUI_func(key) |
| 31 | + else: |
| 32 | + time.sleep(0.1) |
| 33 | + update_GUI() |
| 34 | + |
| 35 | +def drill_percent(): |
| 36 | + global save_data |
| 37 | + while True: |
| 38 | + drill_data=save_data['DRILL_DATA'] |
| 39 | + time_to_new_depth=drill_data[0]/save_data['LAYER_HARDNESS'] |
| 40 | + start_new_depth=time.time() |
| 41 | + end_new_depth=start_new_depth+time_to_new_depth |
| 42 | + while time.time()<=end_new_depth: |
| 43 | + percent=round(((time.time()-start_new_depth)/time_to_new_depth)*100, 2) |
| 44 | + GUI_send(percent, "percent_to_new_layer") |
| 45 | + time.sleep(0.5) |
| 46 | + save_data['DEPTH']+=1 |
| 47 | + save_data['LAYER_HARDNESS']=float((1.57**(1+(save_data['DEPTH']/50)))-0.57) |
| 48 | + GUI_send('new_depth', 'signal') |
| 49 | +def miner_monitor(): |
| 50 | + global save_data |
| 51 | + while True: |
| 52 | + i=1 |
| 53 | + resource_input={ |
| 54 | + 1: 0, |
| 55 | + 2: 0, |
| 56 | + 3: 0, |
| 57 | + 4: 0, |
| 58 | + 5: 0, |
| 59 | + 6: 0, |
| 60 | + 7: 0, |
| 61 | + 8: 0, |
| 62 | + 9: 0, |
| 63 | + 10: 0, |
| 64 | + 11: 0, |
| 65 | + 12: 0, |
| 66 | + 13: 0, |
| 67 | + 14: 0, |
| 68 | + 15: 0 |
| 69 | + } |
| 70 | + while i<=save_data['DEPTH']: |
| 71 | + x100depth=math.ceil(i/100) |
| 72 | + #1-100km: 0.2 coal / sec, 0.07 copper / sec, 0.003 iron / sec |
| 73 | + #101-200km: 0.2 copper / sec, 0.07 iron / sec, 0.003 silver / sec |
| 74 | + #etc. |
| 75 | + if x100depth*100<=save_data['DEPTH']: |
| 76 | + resource_input[x100depth]+=(0.2)*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*100 |
| 77 | + resource_input[x100depth+1]+=(0.07)*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*100 |
| 78 | + resource_input[x100depth+2]+=(0.003)*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*100 |
| 79 | + else: |
| 80 | + resource_input[x100depth]+=(0.2)*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*(save_data['DEPTH']-((x100depth-1)*100)) |
| 81 | + resource_input[x100depth+1]+=(0.07)*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*(save_data['DEPTH']-((x100depth-1)*100)) |
| 82 | + resource_input[x100depth+2]+=(0.003)*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*(save_data['DEPTH']-((x100depth-1)*100)) |
| 83 | + i+=100 |
| 84 | + for mineral in resource_input: |
| 85 | + save_data['MINERALS'][mineral-1]+=resource_input[mineral] |
| 86 | + save_data['MINERALS'][mineral-1]=int(round(save_data['MINERALS'][mineral-1], 0)) |
| 87 | + time.sleep(0.1) |
| 88 | +drill_thread=threading.Thread(target=drill_percent) |
| 89 | +miner_thread=threading.Thread(target=miner_monitor) |
| 90 | +def read_save(): |
| 91 | + save_data = { |
| 92 | +'SAVE_NUMBER': 1, |
| 93 | +'END_TIME': 0, |
| 94 | +'STORAGE': 0, |
| 95 | +'UPGRADES': [], |
| 96 | +'MINERALS': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 97 | +'FLUIDS': [0, 0, 0], |
| 98 | +'SCIENTIST_DATA': [], |
| 99 | +'CAVE_DATA': [], |
| 100 | +'CHEST_DATA': [], |
| 101 | +'MONEY': 0, |
| 102 | +'TP_DATA': [], |
| 103 | +'MANAGER': (0.00, 0), |
| 104 | +'ATTACK': (0, 0), |
| 105 | +'MINER_SPEED': 1, |
| 106 | +'MINER_EFFICIENCY': 1, |
| 107 | +'DEPTH': 10, |
| 108 | +'DRILL_DATA': [], |
| 109 | +'RIG_DATA': [], |
| 110 | +'LAYER_HARDNESS': 1, |
| 111 | +'FORGE_STATUS': 'none', |
| 112 | +'BROADCAST': '', |
| 113 | +'BROADCAST_TYPE': 'none', |
| 114 | +'DATAID': 0, |
| 115 | +'PLANET': 1 |
| 116 | + } |
| 117 | + with open('/workspaces/coolmathgames/v1.0/mrmine/mrmine_save.txt', 'r') as file: |
| 118 | + for line in file: |
| 119 | + line = line.strip() |
| 120 | + if '=' in line: # Ensuring there's a key-value split |
| 121 | + key, value = line.split('=', 1) # Split on first '=' to handle complex values |
| 122 | + try: |
| 123 | + # Attempt to evaluate the value for correct data types |
| 124 | + save_data[key] = eval(value) |
| 125 | + except Exception: |
| 126 | + print("Error reading save file key") |
| 127 | + # If eval fails, it's likely a string or ambiguous format |
| 128 | + save_data[key] = value |
| 129 | + return save_data |
| 130 | +def send_data_to_GUI(): |
| 131 | + while True: |
| 132 | + get_save(save_data) |
| 133 | + time.sleep(0.02) |
| 134 | +send_thread=threading.Thread(target=send_data_to_GUI) |
| 135 | +def write_save(save_data): |
| 136 | + success=False |
| 137 | + i=1 |
| 138 | + while i<=5 and not success: |
| 139 | + try: |
| 140 | + with open('/workspaces/coolmathgames/v1.0/mrmine/mrmine_save.txt', 'w') as file: |
| 141 | + print("opened save file") |
| 142 | + strv='''''' |
| 143 | + for item in save_data: |
| 144 | + print("writing item",item) |
| 145 | + strv += item + '=' + str(save_data[item]) + '\n' |
| 146 | + file.writelines(strv) |
| 147 | + print("success") |
| 148 | + success=True |
| 149 | + break |
| 150 | + except Exception: |
| 151 | + print(f"Error while saving the game. (Attempt {i}/5)") |
| 152 | + i+=1 |
| 153 | + |
| 154 | +def mrmine_start_game(): |
| 155 | + global save_data |
| 156 | + os.system('clear') |
| 157 | + try: |
| 158 | + save_data=read_save() |
| 159 | + save_data['DATAID']=str(random.randint(1, 1000000000)) |
| 160 | + write_save(save_data) |
| 161 | + save_data=read_save() |
| 162 | + except Exception: |
| 163 | + print("Error while loading / writing save file.") |
| 164 | + save_data=None |
| 165 | + print("Exiting the game...") |
| 166 | + exit() |
| 167 | + if save_data['BROADCAST_TYPE'] == "message": |
| 168 | + print("Message from devs: "+str(save_data['BROADCAST'])) |
| 169 | + time.sleep(1) |
| 170 | + os.system('clear') |
| 171 | + try: |
| 172 | + listener_thread = Thread(target=keypress_listener) |
| 173 | + listener_thread.daemon = True |
| 174 | + listener_thread.start() |
| 175 | + drill_thread.start() |
| 176 | + miner_thread.start() |
| 177 | + send_thread.start() |
| 178 | + update_GUI() |
| 179 | + while True: |
| 180 | + tick() |
| 181 | + except KeyboardInterrupt: |
| 182 | + print("Game interrupted, exiting...") |
| 183 | + exit() |
0 commit comments