Commit 30cf2abd authored by Jakub Wolf's avatar Jakub Wolf
Browse files

Initial commit

+ class Racer and RacersList,
+ RacersList running in a separate thread and check for update every
10sec
+ Flask framework is running
+ database model
+ css stylesheet created
parents
.db
\ No newline at end of file
from flask import Flask, url_for, render_template
from racer import *
import hashlib
import sqlite3
DBPATH = '/Users/ku3o/Dropbox/Apps/Alleycat/www/database.db'
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', title='main')
@app.route('/racers')
def racers():
conn = sqlite3.connect(db)
c = conn.cursor()
res = c.execute('SELECT ')
return render_template('racers.table.html', racersList=racersList)
@app.route('/data')
def data(version='0'):
json = race.getJson(version)
print hashlib.md5(json).hexdigest()
return json
# url_for('static', filename='style.css')
if __name__ == '__main__':
race = RacersList(DBPATH)
race.start()
app.run(debug=True)
race.stop()
\ No newline at end of file
CREATE TABLE CHECKPOINT_RACER(racer_id INTEGER, checkpoint_id INTEGER);
CREATE TABLE RACER(name TEXT, id INT);
CREATE TABLE CHECKPOINT(id INTEGER, name TEXT, lat REAL, lon REAL);
INSERT INTO RACER VALUES('Jakub Wolf', 123);
INSERT INTO RACER VALUES('Sona Rumlerová', 345);
INSERT INTO RACER VALUES('Štěpán Žampach', 125);
INSERT INTO RACER VALUES('Jozef', 301);
INSERT INTO RACER VALUES('Sabit', 903);
INSERT INTO CHECKPOINT VALUES(1, 'Na Mendlaku', 9, 10);
INSERT INTO CHECKPOINT VALUES(2, 'Na Stojaka', 9, 10);
INSERT INTO CHECKPOINT VALUES(3, 'Doma', 9, 10);
INSERT INTO CHECKPOINT VALUES(4, 'Na Draze', 9, 10);
INSERT INTO CHECKPOINT VALUES(5, 'Budka', 9, 10);
INSERT INTO CHECKPOINT_RACER VALUES(123,1);
INSERT INTO CHECKPOINT_RACER VALUES(123,3);
INSERT INTO CHECKPOINT_RACER VALUES(123,4);
INSERT INTO CHECKPOINT_RACER VALUES(345,3);
INSERT INTO CHECKPOINT_RACER VALUES(345,1);
INSERT INTO CHECKPOINT_RACER VALUES(345,5);
INSERT INTO CHECKPOINT_RACER VALUES(345,4);
INSERT INTO CHECKPOINT_RACER VALUES(125,1);
INSERT INTO CHECKPOINT_RACER VALUES(301,1);
INSERT INTO CHECKPOINT_RACER VALUES(301,2);
INSERT INTO CHECKPOINT_RACER VALUES(301,3);
INSERT INTO CHECKPOINT_RACER VALUES(903,2);
INSERT INTO CHECKPOINT_RACER VALUES(903,3);
INSERT INTO CHECKPOINT_RACER VALUES(903,4);
import sqlite3
import json
import hashlib
import threading
import time
class Racer:
def __init__(self, db, racerID):
self.racerID = racerID
# self.db = sqlite3.connect(dbPath)
# self.db.row_factory = sqlite3.Row
#get racer name from Database
self.db = db
c = self.db.cursor()
c.execute('SELECT name FROM racer WHERE id = ?', [self.racerID])
r = c.fetchone()
self.name = r['name']
#call method to update checkpoint list
self.ckPointList = []
self.update()
def __str__(self):
return self.name
def __unicode__(self):
return self.name
def getData(self):
return {'name': self.name, 'ckPointList': self.ckPointList}
def update(self):
newCkPointList = []
c = self.db.cursor()
# find all passed checkpoints
c.execute('SELECT c.id FROM checkpoint c LEFT JOIN checkpoint_racer cr on cr.checkpoint_id = c.id WHERE cr.racer_id = ?', [self.racerID])
# create a list from db result
for ckPoint in c:
newCkPointList.append(ckPoint['id'])
if self.ckPointList != newCkPointList:
self.ckPointList = newCkPointList
return True
else:
return False
class RacersList(threading.Thread):
def __init__(self, dbPath):
threading.Thread.__init__(self)
self._dbPath = dbPath
self.dataHashValue = 0
def __del__(self):
self.stop()
def __str__(self):
res = ''
for racer in self.racersList:
res += str(racer)
return res
def __unicode__(self):
res = ''
for racer in self.racersList:
res += unicode(racer) + '\n'
return res
def __iter__(self):
return iter(self.racersList)
def run(self):
self.db = sqlite3.connect(self._dbPath)
self.db.row_factory = sqlite3.Row
self.createList()
self.update(force=True)
self.running = True
while self.running:
print "Updating"
self.update()
time.sleep(10.0)
def stop(self):
self.running = False
def createList(self):
newRacersList = []
c = self.db.cursor()
c.execute('SELECT id FROM racer')
for racer in c:
newRacersList.append( Racer(self.db, racer['id']) )
self.racersList = newRacersList
def update(self, force=False):
for racer in self.racersList:
if (racer.update() or force):
resultdata = {'data': []}
for r in self.racersList:
resultdata['data'].append(r.getData())
self.racersListJSON = json.dumps(resultdata, ensure_ascii=False).encode('utf8')
self.dataHashValue = hashlib.md5(self.racersListJSON).hexdigest()
return True
return False
def getJson(self, h):
if self.dataHashValue != h:
return self.racersListJSON
else:
return False
# r = RacersList('/Users/ku3o/Dropbox/Apps/Alleycat/www/database.db')
# print r.getJson(1)
File added
#flex-wrapper, .checkpoint-list {
display: flex;
flex-wrap: wrap;
}
#flex-wrapper {
}
.checkpoint-list {
margin: 0 auto;
}
.flex-item {
background-color: #ECECEC;
margin: 5px;
padding: 5px;
flex-grow: 0;
width: 150px;
height: 50px;
border-radius: 5px;
}
.checkpoint {
flex-grow: 0;
margin: 2px 6px 2px 0px;
width: 15px;
height: 15px;
border-radius: 50%;
}
.checkpoint:nth-child(1) { background: #765A5E;}
.checkpoint:nth-child(2) { background: #12A18D ;}
.checkpoint:nth-child(3) { background: #ACB41B ;}
.checkpoint:nth-child(4) { background: #E9B822 ;}
.checkpoint:nth-child(5) { background: #FE9631 ;}
.empty { background: #ECECEC !important; box-shadow: inset 0 0 10px #B4B3AB;}
.racer-name {
font-family: 'Karla', sans-serif;
font-size: 20px;
color: #25020F;
}
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %} {{ title }} {% endblock %} | Alleycat</title>
<meta name="description" content="Allyecat live feed">
<meta name="author" content="KU3O">
<link href='http://fonts.googleapis.com/css?family=Karlan' rel='stylesheet' type='text/css'>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<link rel="stylesheet" href="styles.css">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<header>
<nav class="side-nav">
<ul>
<li><a href="stat"></a></li>
</ul>
</nav>
</header>
<div id="content-wrap">
{% block content %}
Welcome to the Alleycat Page
{% endblock %}
</div>
<footer>
{% block contact_info %}
Created by KU3O
{% endblock %}
</footer>
<script src="scripts.js"></script>
</body>
</html>
\ No newline at end of file
{% extends "index.html" %}
<div id="flex-wrapper">
{% for racer in racersList %}
<div class="flex-item">
<span class="racer-name">{{ racer.name }}</span>
<div class="checkpoint-list">
{% for checkpoint in racer.checkpoints %}
<div class="checkpoint {% '' if checkpoint else 'empty' %}" ></div>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment