forked from ArchipelagoMW/Archipelago
- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathChecksFinderClient.py
More file actions
Latest commit
172 lines (144 loc) · 6.53 KB
/
Copy pathChecksFinderClient.py
File metadata and controls
172 lines (144 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ importannotations
importos
importsys
importasyncio
importshutil
importModuleUpdate
ModuleUpdate.update()
importUtils
if__name__=="__main__":
Utils.init_logging("ChecksFinderClient", exception_logger="Client")
fromNetUtilsimportNetworkItem, ClientStatus
fromCommonClientimportgui_enabled, logger, get_base_parser, ClientCommandProcessor, \
CommonContext, server_loop
classChecksFinderClientCommandProcessor(ClientCommandProcessor):
def_cmd_resync(self):
"""Manually trigger a resync."""
self.output(f"Syncing items.")
self.ctx.syncing=True
classChecksFinderContext(CommonContext):
command_processor: int=ChecksFinderClientCommandProcessor
game="ChecksFinder"
items_handling=0b111# full remote
def__init__(self, server_address, password):
super(ChecksFinderContext, self).__init__(server_address, password)
self.send_index: int=0
self.syncing=False
self.awaiting_bridge=False
# self.game_communication_path: files go in this path to pass data between us and the actual game
if"localappdata"inos.environ:
self.game_communication_path=os.path.expandvars(r"%localappdata%/ChecksFinder")
else:
# not windows. game is an exe so let's see if wine might be around to run it
if"WINEPREFIX"inos.environ:
wineprefix=os.environ["WINEPREFIX"]
elifshutil.which("wine") orshutil.which("wine-stable"):
wineprefix=os.path.expanduser("~/.wine") # default root of wine system data, deep in which is app data
else:
msg="ChecksFinderClient couldn't detect system type. Unable to infer required game_communication_path"
logger.error("Error: "+msg)
Utils.messagebox("Error", msg, error=True)
sys.exit(1)
self.game_communication_path=os.path.join(
wineprefix,
"drive_c",
os.path.expandvars("users/$USER/Local Settings/Application Data/ChecksFinder"))
asyncdefserver_auth(self, password_requested: bool=False):
ifpassword_requestedandnotself.password:
awaitsuper(ChecksFinderContext, self).server_auth(password_requested)
awaitself.get_username()
awaitself.send_connect()
asyncdefconnection_closed(self):
awaitsuper(ChecksFinderContext, self).connection_closed()
forroot, dirs, filesinos.walk(self.game_communication_path):
forfileinfiles:
iffile.find("obtain") <=-1:
os.remove(root+"/"+file)
@property
defendpoints(self):
ifself.server:
return [self.server]
else:
return []
asyncdefshutdown(self):
awaitsuper(ChecksFinderContext, self).shutdown()
forroot, dirs, filesinos.walk(self.game_communication_path):
forfileinfiles:
iffile.find("obtain") <=-1:
os.remove(root+"/"+file)
defon_package(self, cmd: str, args: dict):
ifcmdin {"Connected"}:
ifnotos.path.exists(self.game_communication_path):
os.makedirs(self.game_communication_path)
forssinself.checked_locations:
filename=f"send{ss}"
withopen(os.path.join(self.game_communication_path, filename), 'w') asf:
f.close()
ifcmdin {"ReceivedItems"}:
start_index=args["index"]
ifstart_index!=len(self.items_received):
foriteminargs['items']:
filename=f"AP_{str(NetworkItem(*item).location)}PLR{str(NetworkItem(*item).player)}.item"
withopen(os.path.join(self.game_communication_path, filename), 'w') asf:
f.write(str(NetworkItem(*item).item))
f.close()
ifcmdin {"RoomUpdate"}:
if"checked_locations"inargs:
forssinself.checked_locations:
filename=f"send{ss}"
withopen(os.path.join(self.game_communication_path, filename), 'w') asf:
f.close()
defrun_gui(self):
"""Import kivy UI system and start running it as self.ui_task."""
fromkvuiimportGameManager
classChecksFinderManager(GameManager):
logging_pairs= [
("Client", "Archipelago")
]
base_title="Archipelago ChecksFinder Client"
self.ui=ChecksFinderManager(self)
self.ui_task=asyncio.create_task(self.ui.async_run(), name="UI")
asyncdefgame_watcher(ctx: ChecksFinderContext):
fromworlds.checksfinder.Locationsimportlookup_id_to_name
whilenotctx.exit_event.is_set():
ifctx.syncing==True:
sync_msg= [{'cmd': 'Sync'}]
ifctx.locations_checked:
sync_msg.append({"cmd": "LocationChecks", "locations": list(ctx.locations_checked)})
awaitctx.send_msgs(sync_msg)
ctx.syncing=False
sending= []
victory=False
forroot, dirs, filesinos.walk(ctx.game_communication_path):
forfileinfiles:
iffile.find("send") >-1:
st=file.split("send", -1)[1]
sending=sending+[(int(st))]
iffile.find("victory") >-1:
victory=True
ctx.locations_checked=sending
message= [{"cmd": 'LocationChecks', "locations": sending}]
awaitctx.send_msgs(message)
ifnotctx.finished_gameandvictory:
awaitctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}])
ctx.finished_game=True
awaitasyncio.sleep(0.1)
if__name__=='__main__':
asyncdefmain(args):
ctx=ChecksFinderContext(args.connect, args.password)
ctx.server_task=asyncio.create_task(server_loop(ctx), name="server loop")
ifgui_enabled:
ctx.run_gui()
ctx.run_cli()
progression_watcher=asyncio.create_task(
game_watcher(ctx), name="ChecksFinderProgressionWatcher")
awaitctx.exit_event.wait()
ctx.server_address=None
awaitprogression_watcher
awaitctx.shutdown()
importcolorama
parser=get_base_parser(description="ChecksFinder Client, for text interfacing.")
args, rest=parser.parse_known_args()
colorama.init()
asyncio.run(main(args))
colorama.deinit()