Skip to content

Commit

Permalink
fix client update issue, make grid size adjustable, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Avantol13 committed Jun 20, 2019
1 parent 1cbb19e commit 17034b1
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 90 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [x] Can Create/Delete entire entities
- [x] Simple navbar
- [x] Any entity update (movement/attribute change) reflected on all clients using websockets
- [x] Adjustable grid size

## Milestone 2: Map Mode

Expand Down
16 changes: 11 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from flask import Flask, render_template, jsonify
from flask import Flask, render_template, jsonify, make_response
from flask_socketio import SocketIO, emit
from mongoengine import *
from mongo import Entity, DoesNotExist
from config import config
import json
import logging

# Establishing a Connection
connect("mongoengine_test", host="localhost", port=27017)
Expand All @@ -12,10 +13,13 @@
app.config["SECRET_KEY"] = config.SECRET_KEY
socketio = SocketIO(app)

logging.basicConfig(level=(logging.DEBUG if config.DEBUG else logging.INFO))
logging.getLogger('flask_cors').level = logging.DEBUG

@app.route("/")
def index():
return render_template("index.html")
response = make_response(render_template("index.html"))
return response


@app.route("/api/entities", methods=["GET"])
Expand All @@ -26,7 +30,7 @@ def get_entities():
@socketio.on("delete entity", namespace="/test")
def delete_entity(entity_id):
Entity.objects(entity_id=entity_id).delete()

logging.info(f"deleted entity {entity_id}")
emit("deleted entity", entity_id, broadcast=True)


Expand All @@ -35,7 +39,7 @@ def update_entity(data):
data_dict = json.loads(data)
entity_id = data_dict.get("attrs", {}).get("id", {})
if not entity_id:
print(f"error. {data_dict} has no 'attrs.id'")
logging.error(f"error. {data_dict} has no 'attrs.id'")
return

# "flatten", get attrs on first level and then other things
Expand All @@ -59,6 +63,8 @@ def update_entity(data):
Entity.objects(entity_id=entity_id).update(**new, upsert=True)
entity = Entity.objects.get(entity_id=entity_id)

logging.info(f"emit updated entity {entity.to_json().get('id')}")
logging.debug(entity.to_json())
emit("updated entity", entity.to_json(), broadcast=True)


Expand All @@ -69,7 +75,7 @@ def test_connect():

@socketio.on("disconnect", namespace="/test")
def test_disconnect():
print("Client disconnected")
logging.info("Client disconnected")


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Config(object):
SECRET_KEY = os.environ.get("SECRET_KEY") or "you-will-never-guess"
DEBUG = os.environ.get("DEBUG") or True


config = Config()
9 changes: 6 additions & 3 deletions static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ body {
margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
background-color: red;
.navbar a:hover,
.dropdown:hover .dropbtn,
.dropbtn:focus {
/*background-color: red;*/
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}

Expand Down Expand Up @@ -76,6 +78,7 @@ body {
border: 1px dotted gray;
padding-right: 15px;
}

.new-attribute-value {
border: 1px dotted gray;
padding-right: 15px;
Expand Down
Loading

0 comments on commit 17034b1

Please sign in to comment.