forked from madcowfred/evething
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontracts.py
More file actions
309 lines (251 loc) · 13.3 KB
/
Copy pathcontracts.py
File metadata and controls
309 lines (251 loc) · 13.3 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# ------------------------------------------------------------------------------
# Copyright (c) 2010-2013, EVEthing team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
import datetime
from decimal import *
from .apitask import APITask
from thing.models import Alliance, Character, Contract, ContractItem, Corporation, Event, Station, APIKey
# ---------------------------------------------------------------------------
class Contracts(APITask):
name = 'thing.contracts'
def run(self, url, taskstate_id, apikey_id, character_id):
if self.init(taskstate_id, apikey_id) is False:
return
# Make sure the character exists
try:
character = Character.objects.select_related('details').get(pk=character_id)
except Character.DoesNotExist:
self.log_warn('Character %s does not exist!', character_id)
return
now = datetime.datetime.now()
# Initialise for corporate query
if self.apikey.key_type == APIKey.CORPORATION_TYPE:
c_filter = Contract.objects.filter(corporation=self.apikey.corporation)
# Initialise for character query
else:
c_filter = Contract.objects.filter(character=character, corporation__isnull=True)
params = { 'characterID': character_id }
if self.fetch_api(url, params) is False or self.root is None:
return
# Retrieve a list of this user's characters and corporations
#user_chars = list(Character.objects.filter(apikeys__user=self.apikey.user).values_list('id', flat=True))
#user_corps = list(APIKey.objects.filter(user=self.apikey.user).exclude(corpasdasd_character=None).values_list('corpasd_character__corporation__id', flat=True))
# First we need to get all of the acceptor and assignee IDs
contract_ids = set()
station_ids = set()
lookup_ids = set()
lookup_corp_ids = set()
contract_rows = []
# <row contractID="58108507" issuerID="2004011913" issuerCorpID="751993277" assigneeID="401273477"
# acceptorID="0" startStationID="60014917" endStationID="60003760" type="Courier" status="Outstanding"
# title="" forCorp="0" availability="Private" dateIssued="2012-08-02 06:50:29" dateExpired="2012-08-09 06:50:29"
# dateAccepted="" numDays="7" dateCompleted="" price="0.00" reward="3000000.00" collateral="0.00" buyout="0.00"
# volume="10000"/>
for row in self.root.findall('result/rowset/row'):
if self.apikey.key_type == APIKey.CORPORATION_TYPE:
# corp keys don't care about non-corp orders
if row.attrib['forCorp'] == '0':
continue
# corp keys don't care about orders they didn't issue - another fun
# bug where corp keys see alliance contracts they didn't make :ccp:
if self.apikey.corporation.id not in (int(row.attrib['issuerCorpID']),
int(row.attrib['assigneeID']), int(row.attrib['acceptorID'])):
#logger.info('Skipping non-corp contract :ccp:')
continue
# non-corp keys don't care about corp orders
if self.apikey.key_type != APIKey.CORPORATION_TYPE and row.attrib['forCorp'] == '1':
continue
contract_ids.add(int(row.attrib['contractID']))
station_ids.add(int(row.attrib['startStationID']))
station_ids.add(int(row.attrib['endStationID']))
lookup_ids.add(int(row.attrib['issuerID']))
lookup_corp_ids.add(int(row.attrib['issuerCorpID']))
if row.attrib['assigneeID'] != '0':
lookup_ids.add(int(row.attrib['assigneeID']))
if row.attrib['acceptorID'] != '0':
lookup_ids.add(int(row.attrib['acceptorID']))
contract_rows.append(row)
# Fetch bulk data
char_map = Character.objects.in_bulk(lookup_ids)
corp_map = Corporation.objects.in_bulk(lookup_ids | lookup_corp_ids)
alliance_map = Alliance.objects.in_bulk(lookup_ids)
station_map = Station.objects.in_bulk(station_ids)
# Add missing IDs as *UNKNOWN* Characters for now
new = []
for new_id in lookup_ids.difference(char_map, corp_map, alliance_map, lookup_corp_ids):
char = Character(
id=new_id,
name="*UNKNOWN*",
)
new.append(char)
char_map[new_id] = char
if new:
Character.objects.bulk_create(new)
# Add missing Corporations too
new = []
for new_id in lookup_corp_ids.difference(corp_map):
corp = Corporation(
id=new_id,
name="*UNKNOWN*",
)
new.append(corp)
corp_map[new_id] = corp
if new:
Corporation.objects.bulk_create(new)
# Fetch station data
# Fetch all existing contracts
c_map = {}
for contract in c_filter.filter(contract_id__in=contract_ids):
c_map[contract.contract_id] = contract
# Finally, after all of that other bullshit, we can actually deal with
# our goddamn contract rows
new_contracts = []
new_events = []
# <row contractID="58108507" issuerID="2004011913" issuerCorpID="751993277" assigneeID="401273477"
# acceptorID="0" startStationID="60014917" endStationID="60003760" type="Courier" status="Outstanding"
# title="" forCorp="0" availability="Private" dateIssued="2012-08-02 06:50:29" dateExpired="2012-08-09 06:50:29"
# dateAccepted="" numDays="7" dateCompleted="" price="0.00" reward="3000000.00" collateral="0.00" buyout="0.00"
# volume="10000"/>
for row in contract_rows:
contractID = int(row.attrib['contractID'])
issuer_char = char_map.get(int(row.attrib['issuerID']))
if issuer_char is None:
self.log_warn('Invalid issuerID %s', row.attrib['issuerID'])
continue
issuer_corp = corp_map.get(int(row.attrib['issuerCorpID']))
if issuer_corp is None:
self.log_warn('Invalid issuerCorpID %s', row.attrib['issuerCorpID'])
continue
start_station = station_map.get(int(row.attrib['startStationID']))
if start_station is None:
self.log_warn('Invalid startStationID %s', row.attrib['startStationID'])
continue
end_station = station_map.get(int(row.attrib['endStationID']))
if end_station is None:
self.log_warn('Invalid endStationID %s', row.attrib['endStationID'])
continue
assigneeID = int(row.attrib['assigneeID'])
acceptorID = int(row.attrib['acceptorID'])
dateIssued = self.parse_api_date(row.attrib['dateIssued'])
dateExpired = self.parse_api_date(row.attrib['dateExpired'])
dateAccepted = row.attrib['dateAccepted']
if dateAccepted:
dateAccepted = self.parse_api_date(dateAccepted)
else:
dateAccepted = None
dateCompleted = row.attrib['dateCompleted']
if dateCompleted:
dateCompleted = self.parse_api_date(dateCompleted)
else:
dateCompleted = None
type = row.attrib['type']
if type == 'ItemExchange':
type = 'Item Exchange'
contract = c_map.get(contractID, None)
# Contract exists, maybe update stuff
if contract is not None:
if contract.status != row.attrib['status']:
text = "Contract %s changed status from '%s' to '%s'" % (
contract, contract.status, row.attrib['status'])
new_events.append(Event(
user_id=self.apikey.user.id,
issued=now,
text=text,
))
contract.status = row.attrib['status']
contract.date_accepted = dateAccepted
contract.date_completed = dateCompleted
contract.acceptor_id = acceptorID
contract.save()
# Contract does not exist, make a new one
else:
contract = Contract(
character=character,
contract_id=contractID,
issuer_char=issuer_char,
issuer_corp=issuer_corp,
assignee_id=assigneeID,
acceptor_id=acceptorID,
start_station=station_map[int(row.attrib['startStationID'])],
end_station=station_map[int(row.attrib['endStationID'])],
type=type,
status=row.attrib['status'],
title=row.attrib['title'],
for_corp=(row.attrib['forCorp'] == '1'),
public=(row.attrib['availability'].lower() == 'public'),
date_issued=dateIssued,
date_expired=dateExpired,
date_accepted=dateAccepted,
date_completed=dateCompleted,
num_days=int(row.attrib['numDays']),
price=Decimal(row.attrib['price']),
reward=Decimal(row.attrib['reward']),
collateral=Decimal(row.attrib['collateral']),
buyout=Decimal(row.attrib['buyout']),
volume=Decimal(row.attrib['volume']),
)
if self.apikey.key_type == APIKey.CORPORATION_TYPE:
contract.corporation = self.apikey.corporation
new_contracts.append(contract)
# If this contract is a new contract in a non-completed state, log an event
if contract.status in ('Outstanding', 'InProgress'):
#if assigneeID in user_chars or assigneeID in user_corps:
assignee = char_map.get(assigneeID, corp_map.get(assigneeID, alliance_map.get(assigneeID)))
if assignee is not None:
text = "Contract %s was created from '%s' to '%s' with status '%s'" % (
contract, contract.get_issuer_name(), assignee.name, contract.status)
new_events.append(Event(
user_id=self.apikey.user.id,
issued=now,
text=text,
))
# And save the damn things
Contract.objects.bulk_create(new_contracts)
Event.objects.bulk_create(new_events)
# Force the queryset to update
# c_filter.update()
# # Now go fetch items for each contract
# items_url = url.replace('Contracts', 'ContractItems')
# new = []
# seen_contracts = []
# # Apparently courier contracts don't have ContractItems support? :ccp:
# for contract in c_filter.filter(retrieved_items=False).exclude(type='Courier'):
# params['contractID'] = contract.contract_id
# if self.fetch_api(items_url, params) is False or self.root is None:
# continue
# for row in self.root.findall('result/rowset/row'):
# new.append(ContractItem(
# contract_id=contract.contract_id,
# item_id=row.attrib['typeID'],
# quantity=row.attrib['quantity'],
# raw_quantity=row.attrib.get('rawQuantity', 0),
# singleton=row.attrib['singleton'] == '1',
# included=row.attrib['included'] == '1',
# ))
# seen_contracts.append(contract.contract_id)
# if new:
# ContractItem.objects.bulk_create(new)
# c_filter.filter(contract_id__in=seen_contracts).update(retrieved_items=True)
return True
# ---------------------------------------------------------------------------