From: zharkovstas Date: Sat, 18 May 2019 19:21:12 +0000 (+0500) Subject: Moving API X-Git-Url: https://git.xn--bdkaa.com/?a=commitdiff_plain;h=1a1f2f79fe6d66f5afba3a3ab4101117b0d7c133;p=where-are-you.py.git Moving API --- diff --git a/app.py b/app.py index 7e2d418..4df0094 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,7 @@ from random import shuffle, random from bottle import get, post, run, request import uuid -from geo import distance +from geo import distance, move_coordinate from street_predictor import parse_summary from osm.osm import describe_objects @@ -20,6 +20,16 @@ class Game: self.tips = [] self.shown_tips = [] + def move(self, direction, value=100): + if direction == 'north': + self.current_coordinates = move_coordinate(self.current_coordinates[0], value), self.current_coordinates[1] + elif direction == 'south': + self.current_coordinates = move_coordinate(self.current_coordinates[0], -value), self.current_coordinates[1] + elif direction == 'east': + self.current_coordinates = self.current_coordinates[0], move_coordinate(self.current_coordinates[1], value) + elif direction == 'west': + self.current_coordinates = self.current_coordinates[0], move_coordinate(self.current_coordinates[1], -value) + def add_tips(game): radius = 0.0025 @@ -42,7 +52,8 @@ def add_tips(game): if len(buildings) > 0: game.tips.append( - 'Вы рядом с ' + convert_building_type(buildings[0]['building_type']) + ' высотой в ' + buildings[0]['levels'] + ' этажей') + 'Вы рядом с ' + convert_building_type(buildings[0]['building_type']) + ' высотой в ' + buildings[0][ + 'levels'] + ' этажей') shuffle(game.tips) @@ -130,6 +141,25 @@ def get_game(game_id): return {'tips': game.shown_tips} +@post('/api/games//move') +def get_game(game_id): + game = games[game_id] + + direction = request.json['direction'] + + game.move(direction) + + game.tips = [] + + add_tips(game) + + game.shown_tips.append('Вы переместились на 100 м на ' + direction) + + show_tips(game, 1) + + return {'tips': game.shown_tips} + + @post('/api/games//finish') def finish_game(game_id): game = games[game_id] diff --git a/geo.py b/geo.py index 2e13c6b..92af2ab 100644 --- a/geo.py +++ b/geo.py @@ -16,3 +16,8 @@ def distance(point1, point2): c = 2 * atan2(sqrt(a), sqrt(1 - a)) return R * c + + +def move_coordinate(cordinate, meters): + meters_in_gradus = 111_111 + return cordinate + meters / meters_in_gradus