-
Notifications
You must be signed in to change notification settings - Fork 4
/
games.py
241 lines (190 loc) · 7.57 KB
/
games.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
#from __future__ import standard_library
#standard_library.install_aliases()
import re
import languageTemplate
import main
import wordsearch
from userDB import getUserInfo
import userDB
import json
import logging
import os
import sys
#import urllib.request, urllib.parse, urllib.error
import webapp2
from google.appengine.api import users
from google.appengine.ext.webapp import template
class WordSearchHandler(webapp2.RequestHandler):
def get(self):
logging.info('games WordSearchHandler')
#user_info = getUserInfo(self.request.url)
user = users.get_current_user()
rawWordList = self.request.get('words', '')
#wordList = re.findall(r"[\w']+", rawWordList)
#logging.info('games WordSearchHandler wordList = %s' % wordList)
#grid, answers, words = wordsearch.generateWordsGrid(wordList)
#logging.info('games WordSearchHandler grid = %s' % grid)
#logging.info('games WordSearchHandler answers = %s' % answers)
#logging.info('games WordSearchHandler words = %s' % words)
wordData = [];
language = 'Sylheti'
fonts = []
template_values = {
# 'user_nickname': user_info[1],
# 'user_logout': user_info[2],
# 'user_login_url': user_info[3],
'language': Language,
'fontFamilies': fonts,
'wordTestData': wordData,
'maxunicode': sys.maxunicode,
}
path = os.path.join(os.path.dirname(__file__), 'wordsearch.html')
self.response.out.write(template.render(path, template_values))
class GenerateWordSearchHandler(webapp2.RequestHandler):
def get(self):
#user_info = getUserInfo(self.request.url)
user = users.get_current_user()
langInfo = self.app.config.get('langInfo')
rawWordList = self.request.get('words', [])
fillList = self.request.get('fillList', []).split('||')
gridSize = self.request.get('size', 10)
diacritics = self.request.get('diacritics', '').split('||')
tokenGroups = self.request.get('tokenGroups', [])
gridFactor = self.request.get('gridFactor', 1.4)
randomSeed = self.request.get('seed', None)
# Get words separate by spaces, not commas, returns, or tabs
# TODO: remove other punctuation, e.g., periods, parens, etc.
wordList = rawWordList.replace(",", " ").replace("\r", " ").replace("\t", " ").replace(".", '').split()
# TODO: move to wordsearch object
wordSearchObj = wordsearch.WordSearch(gridSize)
wordSearchObj.setFillLetters(fillList)
wordSearchObj.setDiacritics(diacritics)
wordSearchObj.setMode(True) # It's indeed a word search
grid, answers, words, grid_width, attempts = wordsearch.generateWordsGrid(
wordList, fillList, diacritics, randomSeed)
if not grid:
message = 'Cannot create grid'
else:
message = 'Created a grid of size %s' % grid_width
language = 'Sylheti'
fonts = []
template_values = {
'message': message,
#'user_nickname': user_info[1],
#'user_logout': user_info[2],
# 'user_login_url': user_info[3],
'language': language,
#'fontFamilies': fonts,
'grid': grid,
'grid_width': grid_width,
'answers': answers,
'words': words,
'maxunicode': sys.maxunicode,
'attempts' : attempts
}
self.response.out.write(json.dumps(template_values))
# Calls the depth first search method, along with parameters to support it.
class GenerateWordSearchDFSHandler(webapp2.RequestHandler):
def get(self):
logging.info('games GenerateWordSearchDFSHandler')
# user_info = getUserInfo(self.request.url)
user = users.get_current_user()
rawWordList = self.request.get('words', '')
# Suggested size for the grid
raw_size = self.request.get('size', '0')
logging.info('games WordSearchHandler raw_size = >%s<' % raw_size)
if not raw_size or raw_size is '' or raw_size is ' ':
grid_width = 0
else:
grid_width = int(raw_size)
# A measure of when to quit the search
max_tries = self.request.get('max_tries', 1000)
# How many solutions to generated
max_solution_count = self.request.get('max_solution_count', 1)
# logging.info('games WordSearchHandler rawWordList = %s' % rawWordList)
# Strip out white space.
wordList = rawWordList.replace(",", " ").replace("\r", " ").replace("\t", " ").split()
# logging.info('games WordSearchDFS Handler wordList = %s' % wordList)
logging.info('games WordSearchDFS Handler size = %s' % grid_width)
ws = wordsearch.generateDFSWordSearch(wordList,
grid_width, max_tries, max_solution_count)
grid = ws.grid
if not grid:
message = 'Cannot create grid'
else:
message = 'Created a grid of size %s' % grid_width
#logging.info('games WordSearchHandler grid = %s' % grid)
#logging.info('games WordSearchHandler answers = %s' % answers)
#logging.info('games WordSearchHandler words = %s' % words)
template_values = {
# 'user_nickname': user_info[1],
# 'user_logout': user_info[2],
# 'user_login_url': user_info[3],
'language': main.Language,
'fontFamilies': main.OsageFonts,
'grid': grid,
'answers': ws.formatAnswers(),
'words': ws.words,
'grid_width': ws.size,
'maxunicode': sys.maxunicode,
}
self.response.out.write(json.dumps(template_values))
class CrosswordHandler(webapp2.RequestHandler):
def get(self):
logging.info('games CrosswordHandler')
#user_info = getUserInfo(self.request.url)
user = users.get_current_user()
template_values = {
# 'user_nickname': user_info[1],
# 'user_logout': user_info[2],
# 'user_login_url': user_info[3],
'language': main.Language,
'fontFamilies': main.OsageFonts,
}
path = os.path.join(os.path.dirname(__file__), 'crossword.html')
self.response.out.write(template.render(path, template_values))
class GenerateCrosswordHandler(webapp2.RequestHandler):
def get(self):
logging.info('games GenerateCrosswordHandler')
# user_info = getUserInfo(self.request.url)
user = users.get_current_user()
rawWordList = self.request.get('words', '')
logging.info('games CrossWordHandler rawWordList = %s' % rawWordList)
wordList = rawWordList.replace(",", " ").replace("\r", " ").replace("\t", " ").split()
logging.info('games CrossWordHandler wordList = %s' % wordList)
logging.info('games CrossWordHandler CALLING')
grid, answers, words, grid_width = wordsearch.generateCrosswordsGrid(wordList)
if not grid:
message = 'Cannot create grid'
else:
message = 'Created a grid of size %s' % grid_width
logging.info('games WordSearchHandler grid = %s' % grid)
logging.info('games WordSearchHandler answers = %s' % answers)
logging.info('games WordSearchHandler words = %s' % words)
template_values = {
# 'user_nickname': user_info[1],
# 'user_logout': user_info[2],
# 'user_login_url': user_info[3],
'language': main.Language,
'fontFamilies': main.OsageFonts,
'grid': grid,
'answers': answers,
'words': words,
'grid_width': grid_width,
'maxunicode': sys.maxunicode,
}
self.response.out.write(json.dumps(template_values))
class TestHandler(webapp2.RequestHandler):
def get(self):
logging.info('games TestHandler')
app = webapp2.WSGIApplication([
('/games/wordsearch/', WordSearchHandler),
('/games/crossword/', CrosswordHandler),
('/games/generatewordsearch/', GenerateWordSearchHandler),
('/games/generatewordsearchDFS/', GenerateWordSearchDFSHandler),
('/games/generatecrossword/', GenerateCrosswordHandler),
('/games/test/', TestHandler),
], debug=True)