]> git.xn--bdkaa.com Git - where-are-you.py.git/commitdiff
finish
authora.soldatof <a.soldatof@gmail.com>
Sun, 19 May 2019 00:16:08 +0000 (03:16 +0300)
committera.soldatof <a.soldatof@gmail.com>
Sun, 19 May 2019 00:16:22 +0000 (03:16 +0300)
app.py
src/Game.js
src/GameController.js
src/Notification.js
src/createMap.js

diff --git a/app.py b/app.py
index b895bfc6d686b24d912c0ccd6a6400fb6fa4e941..314223e342c781c70b23367914d07517f47e4e5c 100644 (file)
--- a/app.py
+++ b/app.py
@@ -306,17 +306,15 @@ def move(game_id, direction):
     return {'tips': game.shown_tips}
 
 
-@post('/api/games/<game_id>/finish')
+@post('/api/games/<game_id>/finish/<latitude>/<longitude>')
 @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)
 
index 4facb6da03b4d00d84e8eb5b1658413ccb30ea0f..8e9b9bd449fc3e0591e027593e26e161028c01c1 100644 (file)
@@ -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 (
index 32aa03a3ab97b21dcc24ec251de0a7d84fb1700d..b14f2ca07abea51b5e8a27b84b7598d3d65a4a9c 100644 (file)
@@ -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
index e7b25e2dbd753983c45f221771223d560dd161a9..2a82438ac1d35b606d759f5fc577758e68b42eac 100644 (file)
@@ -11,7 +11,8 @@ export class Notification {
     this.div = div;
   }
 
-  show(text, info) {
+  show(text, info, afterHide) {
+    this.afterHide = afterHide;
     ReactDOM.render(
       <Modal onClose={this.hide} width={600}>
         <Modal.Header>{text}</Modal.Header>
@@ -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
index 9babee1acb64fa61908e938554d61632a4725cb0..2969268e1cb22c1982701a53d9b6205ab6828589 100644 (file)
@@ -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',
     {