4
4
import os
5
5
import sys
6
6
import time
7
- from typing import List
7
+ from typing import Any , List
8
8
9
9
#expand sys.path
10
10
thirdparty = os .path .join (os .path .dirname (os .path .realpath (__file__ )),'3rdparty\\ ' )
25
25
from galaxy .api .errors import BackendError , InvalidCredentials
26
26
from galaxy .api .consts import Platform , LicenseType , LocalGameState
27
27
from galaxy .api .plugin import Plugin , create_and_run_plugin
28
- from galaxy .api .types import Authentication , NextStep , Dlc , LicenseInfo , Game , GameTime , LocalGame
28
+ from galaxy .api .types import Achievement , Authentication , NextStep , Dlc , LicenseInfo , Game , GameTime , LocalGame
29
29
from galaxy .proc_tools import process_iter
30
30
31
31
from gw2_api import GW2API
32
32
import gw2_localgame
33
33
34
34
class GuildWars2Plugin (Plugin ):
35
35
36
+ GAME_ID = 'guild_wars_2'
37
+ GAME_NAME = 'Guild Wars 2'
38
+
39
+ SLEEP_CHECK_ACHIEVEMENTS = 1500
40
+
36
41
SLEEP_CHECK_INSTANCES = 60
37
42
38
43
SLEEP_CHECK_RUNNING = 5
@@ -43,9 +48,14 @@ def __init__(self, reader, writer, token):
43
48
super ().__init__ (Platform (manifest ['platform' ]), manifest ['version' ], reader , writer , token )
44
49
self ._gw2_api = GW2API ()
45
50
self ._game_instances = None
51
+
52
+ self .__task_check_for_achievements = None
46
53
self .__task_check_for_instances = None
47
54
self ._task_check_for_running = None
55
+
48
56
self ._last_state = LocalGameState .None_
57
+ self .__imported_achievements = None
58
+
49
59
50
60
async def authenticate (self , stored_credentials = None ):
51
61
if not stored_credentials :
@@ -72,6 +82,7 @@ async def authenticate(self, stored_credentials=None):
72
82
73
83
return Authentication (self ._gw2_api .get_account_id (), self ._gw2_api .get_account_name ())
74
84
85
+
75
86
async def pass_login_credentials (self , step , credentials , cookies ):
76
87
self ._gw2_api .auth_server_stop ()
77
88
@@ -83,14 +94,16 @@ async def pass_login_credentials(self, step, credentials, cookies):
83
94
self .store_credentials ({'api_key' : api_key })
84
95
return Authentication (self ._gw2_api .get_account_id (), self ._gw2_api .get_account_name ())
85
96
97
+
86
98
async def get_local_games (self ):
87
99
self ._game_instances = gw2_localgame .get_game_instances ()
88
100
if len (self ._game_instances ) == 0 :
89
101
self ._last_state = LocalGameState .None_
90
102
return []
91
103
92
104
self ._last_state = LocalGameState .Installed
93
- return [ LocalGame (game_id = 'guild_wars_2' , local_game_state = self ._last_state )]
105
+ return [ LocalGame (game_id = self .GAME_ID , local_game_state = self ._last_state ) ]
106
+
94
107
95
108
async def get_owned_games (self ):
96
109
free_to_play = False
@@ -116,28 +129,53 @@ async def get_owned_games(self):
116
129
if free_to_play :
117
130
license_type = LicenseType .FreeToPlay
118
131
119
- return [ Game (game_id = 'guild_wars_2' , game_title = 'Guild Wars 2' , dlcs = dlcs , license_info = LicenseInfo (license_type = license_type )) ]
132
+ return [ Game (game_id = self . GAME_ID , game_title = self . GAME_NAME , dlcs = dlcs , license_info = LicenseInfo (license_type = license_type )) ]
120
133
121
134
122
135
async def get_game_time (self , game_id , context ):
136
+ if game_id != self .GAME_ID :
137
+ logging .warn ('plugin/get_game_time: unknown game_id %s' % game_id )
138
+ return None
139
+
123
140
time_played = None
124
141
last_played_time = None
125
- if game_id == 'guild_wars_2' :
126
- time_played = int (self ._gw2_api .get_account_age () / 60 )
127
- last_played_time = self .persistent_cache .get ('last_played' )
142
+
143
+ time_played = int (self ._gw2_api .get_account_age () / 60 )
144
+ last_played_time = self .persistent_cache .get ('last_played' )
128
145
129
146
return GameTime (game_id = game_id , time_played = time_played , last_played_time = last_played_time )
130
147
131
148
132
149
async def launch_game (self , game_id ):
133
- if game_id != 'guild_wars_2' :
150
+ if game_id != self .GAME_ID :
151
+ logging .warn ('plugin/launch_game: unknown game_id %s' % game_id )
134
152
return
135
153
136
154
self ._game_instances [0 ].run_game ()
137
155
138
156
139
157
async def install_game (self , game_id ):
140
- pass
158
+ if game_id != self .GAME_ID :
159
+ logging .warn ('plugin/install_game: unknown game_id %s' % game_id )
160
+ return
161
+
162
+
163
+ async def get_unlocked_achievements (self , game_id : str , context : Any ) -> List [Achievement ]:
164
+ result = list ()
165
+
166
+ if game_id != self .GAME_ID :
167
+ logging .warn ('plugin/get_unlocked_achievements: unknown game_id %s' % game_id )
168
+ return result
169
+
170
+ if not self .__imported_achievements :
171
+ self .__imported_achievements = list ()
172
+
173
+ self .__imported_achievements .clear ()
174
+ for key , value in self ._gw2_api .get_account_achievements ().items ():
175
+ self .__imported_achievements .append (key )
176
+ result .append (Achievement (0 , key , value ))
177
+
178
+ return result
141
179
142
180
143
181
def tick (self ):
@@ -147,6 +185,19 @@ def tick(self):
147
185
if not self .__task_check_for_instances or self .__task_check_for_instances .done ():
148
186
self .__task_check_for_instances = self .create_task (self .task_check_for_game_instances (), "task_check_for_instances" )
149
187
188
+ if not self .__task_check_for_achievements or self .__task_check_for_achievements .done ():
189
+ self .__task_check_for_achievements = self .create_task (self .task_check_for_achievements (), "task_check_for_achievements" )
190
+
191
+
192
+ async def task_check_for_achievements (self ):
193
+ if self .__imported_achievements :
194
+ for key , value in self ._gw2_api .get_account_achievements ().items ():
195
+ if key not in self .__imported_achievements :
196
+ self .__imported_achievements .append (key )
197
+ self .unlock_achievement (self .GAME_ID , Achievement (0 , key , value ))
198
+
199
+ await asyncio .sleep (self .SLEEP_CHECK_ACHIEVEMENTS )
200
+
150
201
151
202
async def task_check_for_game_instances (self ):
152
203
self ._game_instances = gw2_localgame .get_game_instances ()
0 commit comments