-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-leaders.py
122 lines (95 loc) · 3.42 KB
/
get-leaders.py
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
from __future__ import print_function
import boto3
from boto3.dynamodb.conditions import Key, Attr
from decimal import *
# Helper for quering DynamoDB table
def dynamo_request(table_obj, request_dict):
resp = table_obj.get_item(Key=request_dict)
if resp.has_key('Item') and resp['Item']:
return resp['Item']
else:
return None
# Helper for quering DynamoDB table
def dynamo_query(table_obj, key, value):
resp = table_obj.query(KeyConditionExpression=Key(key).eq(value))
if resp.has_key('Items') and resp['Items']:
return resp['Items']
else:
return None
# Helper for put new item to DynamoDB table
def dynamo_put(table_obj, item_dict):
try:
resp = table_obj.put_item(Item=item_dict)
if resp:
if resp['ResponseMetadata']['HTTPStatusCode'] != 200:
return None
except:
return None
return True
# Helper for updating item in DynamoDB table
def dynamo_update(table_obj, request_dict, value_dict):
resp = table_obj.get_item(Key=request_dict)
if resp.has_key('Item') and resp['Item']:
item = resp['Item']
for key in value_dict.keys():
item[key] = value_dict[key]
try:
resp = table_obj.put_item(Item=item)
if resp:
if resp['ResponseMetadata']['HTTPStatusCode'] != 200:
return None
except:
return None
else:
return None
return True
# Constance for gamification
NEW_SQUARE = 10
NEW_POI = 20
# Constance for calculating of grid
LAT_SHIFT = Decimal("0.001")
LON_SHIFT = Decimal("0.0015")
NUM_LAT = 180 / LAT_SHIFT
NUM_LON = 360 / LON_SHIFT
# Function for calculating lower left corner of sqare as a Decimal
def calculate_sqare(lat, lon):
grid_lat = ((Decimal(str(lat)) + 90) // LAT_SHIFT) * LAT_SHIFT - 90
grid_lon = ((Decimal(str(lon)) + 180) // LON_SHIFT) * LON_SHIFT - 180
return (grid_lat, grid_lon)
# Function for calculating id of the square
def calculate_id(grid_lat, grid_lon):
return (grid_lat + 90) / LAT_SHIFT * NUM_LON + (grid_lon + 180) / LON_SHIFT
# Function for getting lat lon from square id
def calculate_grid(id):
lat = (id // NUM_LON) * LAT_SHIFT - 90
lon = (id % NUM_LON) * LON_SHIFT - 180
return (lat, lon)
# Dummy function, in future filtration of abuse usage
def filter(user, square_id):
return True
def lambda_handler(event, context):
'''Provide an event that contains the following keys:
- token: user token
- id: user id
- self: return user own data inside
'''
dyn_users = boto3.resource('dynamodb').Table('mapexplorer-users')
user = dynamo_request(dyn_users, {'id': event['id']})
if user:
if user['token'] == event['token']:
answer = []
resp = dyn_users.query(
IndexName='lastLocation-xp-index',
ScanIndexForward=False,
KeyConditionExpression=Key('lastLocation').eq(0) & Key('xp').gte(0))
if resp.has_key('Items'):
for i in resp['Items']:
rec = {'id': i['id'], 'name': i['name'], 'color': i['color'], 'xp': i['xp']}
answer.append(rec)
return answer
else:
raise Exception("Bad Request: Error quering users data")
else:
raise Exception("Unauthorized: Wrong token")
else:
raise Exception("Not Found: User not found")