From: a.soldatof Date: Sun, 19 May 2019 00:16:08 +0000 (+0300) Subject: finish X-Git-Url: https://git.xn--bdkaa.com/?a=commitdiff_plain;h=bde8e9936eb36457d470d35ece5dd40074b3aad1;p=where-are-you.py.git finish --- diff --git a/app.py b/app.py index b895bfc..314223e 100644 --- a/app.py +++ b/app.py @@ -306,17 +306,15 @@ def move(game_id, direction): return {'tips': game.shown_tips} -@post('/api/games//finish') +@post('/api/games//finish//') @enable_cors -def finish_game(game_id): +def finish_game(game_id, latitude, longitude): if not game_id in games: return bottle.HTTPResponse(status=404, body='game not found') game = games[game_id] - answer = request.json - - answer_coordinates = (answer["latitude"], answer["longitude"]) + answer_coordinates = (float(latitude), float(longitude)) d = distance(game.current_coordinates, answer_coordinates) diff --git a/src/Game.js b/src/Game.js index 4facb6d..8e9b9bd 100644 --- a/src/Game.js +++ b/src/Game.js @@ -24,7 +24,7 @@ export class Game extends Component { tips: this.gameController.tips(), loading: false }, () => { - createMap('map'); + createMap('map', this.onMapClick); }) }).catch(error => { this.notification.show( @@ -60,6 +60,23 @@ export class Game extends Component { }); }; + onMapClick = (e) => { + this.gameController.tryFinish(`${e.latlng.lat}/${e.latlng.lng}`).then(json => { + this.notification.show( + 'Игра закончена.', + `Неплохая попытка! Вы оказались по адресу: ${json.data.address}, а промахнулись на ${Math.round(json.data.distance)} метров.`, + () => { + window.location = '/'; + } + ); + }).catch(error => { + this.notification.show( + 'Что-то сломалось, зовите разраба', + error && JSON.stringify(error) + ); + }); + }; + render() { if(this.state.loading) { return ( diff --git a/src/GameController.js b/src/GameController.js index 32aa03a..b14f2ca 100644 --- a/src/GameController.js +++ b/src/GameController.js @@ -40,4 +40,8 @@ export class GameController { this._tips = json.data.tips; }); } + + tryFinish(ltLng) { + return api.post(`/games/${this.gameId}/finish/${ltLng}`); + } } \ No newline at end of file diff --git a/src/Notification.js b/src/Notification.js index e7b25e2..2a82438 100644 --- a/src/Notification.js +++ b/src/Notification.js @@ -11,7 +11,8 @@ export class Notification { this.div = div; } - show(text, info) { + show(text, info, afterHide) { + this.afterHide = afterHide; ReactDOM.render( {text} @@ -26,5 +27,6 @@ export class Notification { hide = () => { this.div && ReactDOM.render(null, this.div); + this.afterHide && this.afterHide(); } } \ No newline at end of file diff --git a/src/createMap.js b/src/createMap.js index 9babee1..2969268 100644 --- a/src/createMap.js +++ b/src/createMap.js @@ -5,7 +5,7 @@ const ekb = { SW: ['56.768982', '60.491112'] }; -export function createMap(elementId) { +export function createMap(elementId, onMapClick) { const neLat = ekb.NE[0]; const neLng = ekb.NE[1]; const swLat = ekb.SW[0]; @@ -13,6 +13,8 @@ export function createMap(elementId) { const map = new L.Map(elementId); + map.on('click', onMapClick); + const service = new L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {