Permalink
Browse files

Initial commit

Incomplete - start of an API from an afternoon's hackday coding. Putting 
it here to keep it safe until I get a chance to tidy and finish it.
  • Loading branch information...
dalelane committed Sep 18, 2016
1 parent a2bfd58 commit 4b9249b79f960fd6706a134155b0f93f6e563a72
Showing with 733 additions and 0 deletions.
  1. +1 −0 Procfile
  2. +78 −0 bots.py
  3. +44 −0 data/decks/kingsandqueens.csv
  4. +83 −0 decks.py
  5. +9 −0 fileutils.py
  6. +8 −0 manifest.yml
  7. +7 −0 requirements.txt
  8. +1 −0 runtime.txt
  9. +209 −0 server.py
  10. BIN static/images/newapp-icon.png
  11. +21 −0 static/index.html
  12. +88 −0 static/stylesheets/style.css
  13. +184 −0 training.py
View
@@ -0,0 +1 @@
web: python server.py
View
78 bots.py
@@ -0,0 +1,78 @@
from os.path import exists, isfile
from glob import glob
import re
import fileutils
import decks
BOTNAME_PATTERN = re.compile("^[A-Za-z0-9]{1,32}$")
def isValidName (botname):
global BOTNAME_PATTERN
return BOTNAME_PATTERN.match(botname) is not None
def getFileName (botname, deckname):
return "./data/training/" + botname + "-" + deckname + ".csv"
def countBotTraining (botname):
filenamepattern = "./data/training/" + botname + "-*.csv"
training = {}
trainingfiles = glob(filenamepattern)
for trainingfile in trainingfiles:
deckname = trainingfile[len("./data/training/" + botname + "-"):-(len(".csv"))]
traininglines = fileutils.line_count(trainingfile)
if traininglines > 1:
training[deckname] = int((traininglines - 1) / 2)
else:
training[deckname] = 0
return training
def appendToTrainingData (botname, deckname, newtraining):
deck = decks.get(deckname)
rules = deck["rules"]
trainingfilepath = getFileName(botname, deckname)
if isfile(trainingfilepath) == False:
header = ""
for attribute in rules.keys():
header += "\"" + attribute + "\","
header += "choice,outcome"
with open(trainingfilepath, "a") as trainingfile:
trainingfile.write(header + "\n")
trainingline = ""
for newtrainingdata in newtraining:
for attribute in rules.keys():
if attribute in newtrainingdata["card"]:
trainingline += str(newtrainingdata["card"][attribute])
else:
trainingline += "0"
trainingline += ","
trainingline += str(rules.keys().index(newtrainingdata["choice"]))
trainingline += ","
trainingline += str(newtrainingdata["outcome"])
trainingline += "\n"
with open(trainingfilepath, "a") as trainingfile:
trainingfile.write(trainingline)
@@ -0,0 +1,44 @@
name,picture,reign,ascension,death,wives,children
,,higher,lower,higher,higher,higher
William I (1066-1087),https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/King_William_I_%28%27The_Conqueror%27%29_from_NPG.jpg/341px-King_William_I_%28%27The_Conqueror%27%29_from_NPG.jpg,20,38,59,1,10
William II (1087-1100),https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/British_School_-_William_Rufus_-_Google_Art_Project.jpg/356px-British_School_-_William_Rufus_-_Google_Art_Project.jpg,13,30,43,0,0
Henry I (1100-1135),https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/British_-_Henry_I_-_Google_Art_Project.jpg/357px-British_-_Henry_I_-_Google_Art_Project.jpg,35,31,67,2,4
"Stephen (1135-1141, 1141-1154)",https://upload.wikimedia.org/wikipedia/commons/thumb/a/a3/King_Stephen_from_NPG.jpg/368px-King_Stephen_from_NPG.jpg,19,38,57,1,5
Matilda (1141),https://upload.wikimedia.org/wikipedia/commons/5/54/Empress_Matilda.jpg,0,38,65,2,3
Henry II (1154-1189),https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Henry_II_of_England_-_Illustration_from_Cassell%27s_History_of_England_-_Century_Edition_-_published_circa_1902.jpg/385px-Henry_II_of_England_-_Illustration_from_Cassell%27s_History_of_England_-_Century_Edition_-_published_circa_1902.jpg,34,21,56,1,8
Richard I (1189-1199),https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/British_-_Richard_I_-_Google_Art_Project.jpg/351px-British_-_Richard_I_-_Google_Art_Project.jpg,9,31,41,1,0
John (1199-1216),https://upload.wikimedia.org/wikipedia/commons/c/cc/BL_MS_Royal_14_C_VII_f.9_%28John%29.jpg,17,31,48,2,5
Henry III (1216-1272),https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Henry_III_of_England_-_Illustration_from_Cassell%27s_History_of_England_-_Century_Edition_-_published_circa_1902.jpg/396px-Henry_III_of_England_-_Illustration_from_Cassell%27s_History_of_England_-_Century_Edition_-_published_circa_1902.jpg,56,9,65,1,9
Edward I (1272-1307),https://upload.wikimedia.org/wikipedia/commons/b/b2/Gal_nations_edward_i.jpg,34,33,68,2,19
Edward II (1307-1327),https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/King_Edward_II.jpg/366px-King_Edward_II.jpg,19,23,43,1,4
Edward III (1327-1377),https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/King_Edward_III_from_NPG.jpg/363px-King_Edward_III_from_NPG.jpg,50,14,64,1,13
Richard II (1377-1399),https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/King_Richard_II_from_NPG_%282%29.jpg/364px-King_Richard_II_from_NPG_%282%29.jpg,22,10,33,2,0
Henry IV (1399-1413),https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/King_Henry_IV_from_NPG_%282%29.jpg/367px-King_Henry_IV_from_NPG_%282%29.jpg,13,33,45,2,7
Henry V (1413-1422),https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/King_Henry_V_from_NPG.jpg/344px-King_Henry_V_from_NPG.jpg,9,25,34,1,1
"Henry VI (1422-1461, 1470-1471)",https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/King_Henry_VI_from_NPG_%282%29.jpg/376px-King_Henry_VI_from_NPG_%282%29.jpg,39,0,49,1,1
"Edward IV (1461-1470, 1471-1483)",https://upload.wikimedia.org/wikipedia/commons/thumb/4/48/King_Edward_IV.jpg/373px-King_Edward_IV.jpg,21,18,40,1,10
Edward V (1483),https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/King_Edward_V_from_NPG.jpg/374px-King_Edward_V_from_NPG.jpg,0,12,12,0,0
Richard III (1483-1485),https://upload.wikimedia.org/wikipedia/commons/0/09/Richard_III_earliest_surviving_portrait.jpg,2,30,33,1,1
Henry VII (1485-1509),https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/King_Henry_VII.jpg/325px-King_Henry_VII.jpg,23,28,52,1,8
Henry VIII (1509-1547),https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Workshop_of_Hans_Holbein_the_Younger_-_Portrait_of_Henry_VIII_-_Google_Art_Project.jpg/273px-Workshop_of_Hans_Holbein_the_Younger_-_Portrait_of_Henry_VIII_-_Google_Art_Project.jpg,37,17,55,6,10
Edward VI (1547-1553),https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Portrait_of_Edward_VI_of_England.jpg/379px-Portrait_of_Edward_VI_of_England.jpg,6,9,15,0,0
Lady Jane Grey (1553),https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Streathamladyjayne.jpg/371px-Streathamladyjayne.jpg,0,15,15,1,0
Mary I (1553-1558),https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Maria_Tudor1.jpg/363px-Maria_Tudor1.jpg,5,37,42,1,0
Elizabeth I (1558-1603),https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Darnley_stage_3.jpg/328px-Darnley_stage_3.jpg,44,25,69,0,0
James I (1603-1625),https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/JamesIEngland.jpg/284px-JamesIEngland.jpg,21,36,58,1,9
Charles I (1625-1649),https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/King_Charles_I_after_original_by_van_Dyck.jpg/278px-King_Charles_I_after_original_by_van_Dyck.jpg,23,24,48,1,9
Charles II (1660-1685),https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/King_Charles_II_by_John_Michael_Wright_or_studio.jpg/376px-King_Charles_II_by_John_Michael_Wright_or_studio.jpg,24,30,54,1,3
James II (1685-1688),https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/King_James_II_by_Sir_Godfrey_Kneller%2C_Bt.jpg/290px-King_James_II_by_Sir_Godfrey_Kneller%2C_Bt.jpg,3,51,67,2,20
William III (1689-1702),https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/King_William_III_of_England%2C_%281650-1702%29_%28lighter%29.jpg/378px-King_William_III_of_England%2C_%281650-1702%29_%28lighter%29.jpg,13,38,51,1,3
Mary II (1689-1694),https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Mary_II_-_Kneller_1690.jpg/294px-Mary_II_-_Kneller_1690.jpg,5,26,32,1,3
Anne (1702-1714),https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Anne1705.jpg/290px-Anne1705.jpg,12,37,49,1,19
George I (1714-1727),https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/King_George_I_by_Sir_Godfrey_Kneller%2C_Bt_%283%29.jpg/338px-King_George_I_by_Sir_Godfrey_Kneller%2C_Bt_%283%29.jpg,12,54,67,1,2
George II (1727-1760),https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/George_II_by_Thomas_Hudson.jpg/290px-George_II_by_Thomas_Hudson.jpg,33,43,76,1,9
George III (1760-1820),https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/George%2C_Prince_of_Wales%2C_later_George_III%2C_1754_by_Liotard.jpg/374px-George%2C_Prince_of_Wales%2C_later_George_III%2C_1754_by_Liotard.jpg,59,22,81,1,15
George IV (1820-1830),https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/George_IV_1821_color.jpg/329px-George_IV_1821_color.jpg,10,57,67,2,1
William IV (1830-1837),https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/William_IV_by_Sir_Martin_Archer_Shee.jpg/317px-William_IV_by_Sir_Martin_Archer_Shee.jpg,6,64,71,1,6
Victoria (1837-1901),https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Queen_Victoria_by_Bassano.jpg/340px-Queen_Victoria_by_Bassano.jpg,63,18,81,1,9
Edward VII (1901-1910),https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Edward_VII_in_coronation_robes.jpg/340px-Edward_VII_in_coronation_robes.jpg,9,59,68,1,6
George V (1910-1936),https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/King_George_V_1911_color-crop.jpg/350px-King_George_V_1911_color-crop.jpg,25,44,70,1,6
Edward VIII (1936),https://upload.wikimedia.org/wikipedia/commons/thumb/a/a3/HRH_The_Prince_of_Wales_No_4_%28HS85-10-36416%29.jpg/356px-HRH_The_Prince_of_Wales_No_4_%28HS85-10-36416%29.jpg,0,41,77,1,0
George VI (1936-1952),https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/King_George_VI_of_England%2C_formal_photo_portrait%2C_circa_1940-1946_-_edit.jpg/339px-King_George_VI_of_England%2C_formal_photo_portrait%2C_circa_1940-1946_-_edit.jpg,15,40,56,1,2
View
@@ -0,0 +1,83 @@
import os
import csv
import random
decks = None
def getFileNameWithoutExtension(filename):
return os.path.splitext(filename)[0]
def loadDeckData ():
global decks
decks = {}
decknames = os.listdir("./data/decks")
for deckname in decknames:
attributes = []
rules = {}
cards = []
with open("./data/decks/" + deckname, "rb") as csvfile:
reader = csv.reader(csvfile)
attributes = reader.next()
idx = 0
rulesrow = reader.next()
for attribute in attributes:
if attribute != "name" and attribute != "picture":
rules[attribute] = rulesrow[idx]
idx += 1
for row in reader:
card = {}
idx = 0
for attribute in attributes:
if attribute != "name" and attribute != "picture":
card[attribute] = eval(row[idx])
else:
card[attribute] = row[idx]
idx += 1
cards.append(card)
decks[getFileNameWithoutExtension(deckname)] = { "rules" : rules, "cards" : cards }
def shuffleAndDeal (deck):
allcards = deck["cards"][:]
random.shuffle(allcards)
half = len(allcards) / 2
return allcards[:half], allcards[half:]
def list ():
global decks
if decks is None:
loadDeckData()
return map(getFileNameWithoutExtension, decks)
def get (deckname):
global decks
if decks is None:
loadDeckData()
if deckname in decks:
return decks[deckname]
return None
View
@@ -0,0 +1,9 @@
def line_count(filename):
try:
with open(filename) as f:
for i, l in enumerate(f):
pass
return i + 1
except:
return 0
View
@@ -0,0 +1,8 @@
applications:
- path: .
memory: 128M
instances: 1
domain: eu-gb.mybluemix.net
name: toptrumps
host: toptrumps
disk_quota: 512M
View
@@ -0,0 +1,7 @@
CherryPy==8.1.0
requests==2.7.0
scipy==0.16.0
sklearn==0.0
NumPy==1.9.3
pylru==1.0.9
scikit-learn==0.18rc2
View
@@ -0,0 +1 @@
python-2.7.11
Oops, something went wrong.

0 comments on commit 4b9249b

Please sign in to comment.