forked from jheidel/scrabble-opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
116 lines (99 loc) · 4.16 KB
/
webserver.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import tornado.httpserver
import tornado.ioloop
import tornado.web
import webbrowser
from threading import Thread
from scoring import *
import cgi
import twl
import os
PORT = 80
#This global state is bad practice; TODO: Fix
global global_scoreboard
global global_board
global handle_count
handle_count = 0
class MainHandler(tornado.web.RequestHandler):
def post(self, val):
params = dict(cgi.parse_qsl(self.request.body))
addr = self.request.remote_ip
global handle_count
global global_scoreboard, global_board #TODO: Fix
self.set_header("Content-Type", "text/html")
if params["m"] == "check":
self.write(str(handle_count))
elif params["m"] == "scores":
#Generate scoreboard dynamically
scores = global_scoreboard.get_scores()
sb = "<table>"
for (x,y) in scores:
sb += "<tr>"
sb += "<td align=right><b>%s:</b></td><td>%s</td>" % (str(x), str(y))
sb += "</tr>"
sb += "</table>"
self.write(sb)
elif params["m"] == "board":
sg = "<table width=\"418px\" height=\"451px\" style=\"background-image: url('scrabble.jpg')\" cellpadding=0 cellspacing=0>"
for j in range(0,15):
sg += "<tr height=\"28\" valign=\"middle\">"
for i in range(0,15):
p = global_board.get(i,j)
at = " bgcolor=\"#FFCC66\"" if p is not None else ""
ltr_txt = ("<b><font color=%s>%s</font></b>" % ("black" if not isinstance(p, Blank) else "blue", str(p).upper()) if p is not None else "")
pts = str(get_letter_points(p)) if p is not None else ""
sg += "<td width=\"29\" valign=\"middle\" align=\"center\"><table cellpadding=0 cellspacing=0 height=23 width=23%s><tr valign=middle><td align=center valign=middle>%s</td><td valign=bottom><font size=1>%s</font></td></tr></tr></table></td>" % (at, ltr_txt, pts)
sg += "</tr>"
sg += "</table>"
self.write(sg)
elif params["m"] == "dict":
word = params["d"]
if twl.check(word.strip().lower()):
os.system("beep -f 150 -l 10 &>/dev/null &") #dictionary lookup beep
self.write("The word %s is in the dictionary." % word)
else:
os.system("beep -f 100 -l 10 &>/dev/null &") #dictionary lookup beep
self.write("The word %s IS NOT the dictionary." % word)
def get(self, pth):
addr = self.request.remote_ip
if pth.endswith(".html") or pth.endswith(".htm") or pth.endswith(".txt") or pth=="":
self.set_header("Content-Type", "text/html")
if pth.endswith(".png"):
self.set_header("Content-Type", "image/png")
if pth.endswith(".jpg"):
self.set_header("Content-Type", "image/jpg")
if "index" in pth or pth == "":
ipage = open("web/index.html", "rb")
ihtml = ipage.read()
self.write(ihtml)
else:
try:
#Grab the path from the web dir
page = open("web/%s" % pth, "rb")
self.write(page.read())
except IOError:
self.set_header("Content-Type", "text/html")
self.set_status(200)
self.write("404: Not found")
class ScrabbleServer(Thread):
def __init__(self, game_board, scoreboard):
Thread.__init__(self)
self.io_loop = None
global global_scoreboard, global_board #TODO: Fix
global_scoreboard = scoreboard
global_board = game_board
def kill(self):
self.io_loop.stop()
def refresh(self):
global handle_count
handle_count += 1
def run(self):
application = tornado.web.Application([
(r"/(.*)", MainHandler),
])
#start server
print "Starting Scrabble Server"
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(PORT)
self.io_loop = tornado.ioloop.IOLoop.instance()
self.io_loop.start()
print "Scrabble server stopped"