summaryrefslogtreecommitdiffstats
path: root/webapp/app/views.py
blob: a13bf12ce12e460c7f72f019c3c8da546c3f5a64 (plain) (blame)
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
from flask import render_template, request, send_from_directory
from app import app, sio, send_queue, recv_queue
import time

import threading

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html',
                           title='F16 Group1')

@app.route('/static/<path:path>')
def send_static(path):
    return send_from_directory('static', path)

connections = set()

@sio.on('connect', namespace='/spawn')
def connect(sid, environ):
    print("connect ", sid)
    connections.add(sid)

@sio.on('spawn', namespace='/spawn')
def message(sid, data):
    print("message ", data)
    send_queue.put(('spawn', data))

@sio.on('ready', namespace='/spawn')
def ready(sid):
    sio.emit('fish state', fish_state, room=sid, namespace='/spawn')

@sio.on('disconnect', namespace='/spawn')
def disconnect(sid):
    print('disconnect', sid)
    connections.remove(sid)

fish_state = {
        'jellyfish': 0,
        'dolphin': 0,
        'boat': 0,
        'nemo': 0,
        'dory': 0,
        'whale': 0,
        'eel': 0,
        'stingray': 0,
        'shark': 0
}

def recv_events():
    while True:
        event = recv_queue.get()
        if event[0] == 'enable image':
            fish_state[event[1]['name']] = 1
        elif event[0] == 'disable image':
            fish_state[event[1]['name']] = 0

        sio.emit('fish state', fish_state, namespace='/spawn')


thread = sio.start_background_task(recv_events)