Skip to content

Commit 91e359c

Browse files
committed
v0.2.4: send achievements info to GLX
1 parent 34a841b commit 91e359c

File tree

4 files changed

+104
-17
lines changed

4 files changed

+104
-17
lines changed

current_version.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"tag_name": "v0.2.3",
2+
"tag_name": "v0.2.4",
33
"assets": [
44
{
5-
"browser_download_url": "https://github.com/Mixaill/galaxy-integration-gw2/archive/v0.2.3.zip",
5+
"browser_download_url": "https://github.com/Mixaill/galaxy-integration-gw2/archive/v0.2.4.zip",
66
"name": "windows.zip"
77
}
88
]

gw2_api.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ def do_GET(self):
9797
class GW2API(object):
9898

9999
API_DOMAIN = 'https://api.guildwars2.com'
100+
101+
API_URL_ACHIEVEMENTS = '/v2/achievements'
100102
API_URL_ACCOUNT = '/v2/account'
103+
API_URL_ACCOUNT_ACHIVEMENTS = '/v2/account/achievements'
101104

102105
LOCALSERVER_HOST = '127.0.0.1'
103106
LOCALSERVER_PORT = 13338
@@ -133,6 +136,28 @@ def get_owned_games(self) -> List[str]:
133136
def get_account_age(self) -> int:
134137
return self._account_info['age']
135138

139+
def get_account_achievements(self) -> Dict:
140+
result = dict()
141+
142+
(status, achievements_account) = self.__api_get_account_achievements(self._api_key)
143+
if status == 200:
144+
#select completed achievements
145+
ids_to_request = list()
146+
for achievement in achievements_account:
147+
if achievement['done'] == True:
148+
ids_to_request.append(achievement['id'])
149+
result[achievement['id']] = None
150+
151+
#get additional info
152+
(status, achievements_info) = self.__api_get_achievements_info(ids_to_request)
153+
if status == 200 or status == 206:
154+
for achievement in achievements_info:
155+
result[achievement['id']] = achievement['name']
156+
157+
158+
return result
159+
160+
136161
#
137162
# Authorization server
138163
#
@@ -202,16 +227,27 @@ def do_auth_apikey(self, api_key : str) -> GW2AuthorizationResult:
202227
return GW2AuthorizationResult.FINISHED
203228

204229

205-
def __api_get_account_info(self, api_key):
230+
231+
def __api_get_response(self, api_key, url, parameters):
206232
result = None
207233

208-
resp = requests.get(self.API_DOMAIN+self.API_URL_ACCOUNT, params={'access_token': api_key})
209-
234+
resp = requests.get(self.API_DOMAIN+url, params=parameters)
210235
try:
211236
result = json.loads(resp.text)
212237
except Exception:
213238
if resp.status_code != 502:
214-
logging.error('gw2api/__api_get_account_info: failed to parse response %s' % resp.text)
215-
239+
logging.error('gw2_api/__api_get_response: failed to parse response %s' % resp.text)
216240

217241
return (resp.status_code, result)
242+
243+
244+
def __api_get_account_info(self, api_key):
245+
return self.__api_get_response(api_key, self.API_URL_ACCOUNT, {'access_token': api_key})
246+
247+
248+
def __api_get_account_achievements(self, api_key):
249+
return self.__api_get_response(api_key, self.API_URL_ACCOUNT_ACHIVEMENTS, {'access_token': api_key})
250+
251+
252+
def __api_get_achievements_info(self, ids : List[int]):
253+
return self.__api_get_response(self, self.API_URL_ACHIEVEMENTS, {'ids': ','.join(str(i) for i in ids)})

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Galaxy Guild Wars 2 plugin",
33
"platform": "gw2",
44
"guid": "637b1fd4-b1d0-4de8-9bf6-757e639807f3",
5-
"version": "0.2.3",
5+
"version": "0.2.4",
66
"description": "Galaxy Guild Wars 2 plugin",
77
"author": "Mikhail Paulyshka",
88
"email": "[email protected]",

plugin.py

+60-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import sys
66
import time
7-
from typing import List
7+
from typing import Any, List
88

99
#expand sys.path
1010
thirdparty = os.path.join(os.path.dirname(os.path.realpath(__file__)),'3rdparty\\')
@@ -25,14 +25,19 @@
2525
from galaxy.api.errors import BackendError, InvalidCredentials
2626
from galaxy.api.consts import Platform, LicenseType, LocalGameState
2727
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
2929
from galaxy.proc_tools import process_iter
3030

3131
from gw2_api import GW2API
3232
import gw2_localgame
3333

3434
class GuildWars2Plugin(Plugin):
3535

36+
GAME_ID = 'guild_wars_2'
37+
GAME_NAME = 'Guild Wars 2'
38+
39+
SLEEP_CHECK_ACHIEVEMENTS = 1500
40+
3641
SLEEP_CHECK_INSTANCES = 60
3742

3843
SLEEP_CHECK_RUNNING = 5
@@ -43,9 +48,14 @@ def __init__(self, reader, writer, token):
4348
super().__init__(Platform(manifest['platform']), manifest['version'], reader, writer, token)
4449
self._gw2_api = GW2API()
4550
self._game_instances = None
51+
52+
self.__task_check_for_achievements = None
4653
self.__task_check_for_instances = None
4754
self._task_check_for_running = None
55+
4856
self._last_state = LocalGameState.None_
57+
self.__imported_achievements = None
58+
4959

5060
async def authenticate(self, stored_credentials=None):
5161
if not stored_credentials:
@@ -72,6 +82,7 @@ async def authenticate(self, stored_credentials=None):
7282

7383
return Authentication(self._gw2_api.get_account_id(), self._gw2_api.get_account_name())
7484

85+
7586
async def pass_login_credentials(self, step, credentials, cookies):
7687
self._gw2_api.auth_server_stop()
7788

@@ -83,14 +94,16 @@ async def pass_login_credentials(self, step, credentials, cookies):
8394
self.store_credentials({'api_key': api_key})
8495
return Authentication(self._gw2_api.get_account_id(), self._gw2_api.get_account_name())
8596

97+
8698
async def get_local_games(self):
8799
self._game_instances = gw2_localgame.get_game_instances()
88100
if len(self._game_instances) == 0:
89101
self._last_state = LocalGameState.None_
90102
return []
91103

92104
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+
94107

95108
async def get_owned_games(self):
96109
free_to_play = False
@@ -116,28 +129,53 @@ async def get_owned_games(self):
116129
if free_to_play:
117130
license_type = LicenseType.FreeToPlay
118131

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)) ]
120133

121134

122135
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+
123140
time_played = None
124141
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')
128145

129146
return GameTime(game_id = game_id, time_played = time_played, last_played_time = last_played_time)
130147

131148

132149
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)
134152
return
135153

136154
self._game_instances[0].run_game()
137155

138156

139157
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
141179

142180

143181
def tick(self):
@@ -147,6 +185,19 @@ def tick(self):
147185
if not self.__task_check_for_instances or self.__task_check_for_instances.done():
148186
self.__task_check_for_instances = self.create_task(self.task_check_for_game_instances(), "task_check_for_instances")
149187

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+
150201

151202
async def task_check_for_game_instances(self):
152203
self._game_instances = gw2_localgame.get_game_instances()

0 commit comments

Comments
 (0)