Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit 9e65a50

Browse files
committedApr 6, 2020
Move dictionary file loading out of game class
1 parent bb956db commit 9e65a50

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed
 

‎server/__init__.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,16 @@ def on_create(data):
9797
gm = game.Info(
9898
size=data['size'],
9999
teams=data['teams'],
100-
mix=data['dictionaryOptions']['mixPercentages'])
100+
mix=data['dictionaryOptions']['mixPercentages'],
101+
dictionaries=DICTIONARIES)
102+
101103
# handle standard single dictionary
102104
else:
103105
gm = game.Info(
104106
size=data['size'],
105107
teams=data['teams'],
106-
dictionary=data['dictionaryOptions']['dictionaries'])
108+
dictionary=data['dictionaryOptions']['dictionaries'],
109+
dictionaries=DICTIONARIES)
107110

108111
room = gm.game_id
109112
ROOMS[room] = gm
@@ -151,7 +154,29 @@ def on_regenerate(data):
151154
def list_dictionaries():
152155
"""send a list of dictionary names"""
153156
# send dict list to client
154-
emit('list_dictionaries', {'dictionaries': list(game.DICTIONARIES.keys())})
157+
emit('list_dictionaries', {'dictionaries': list(DICTIONARIES.keys())})
158+
159+
def __load_dictionaries():
160+
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
161+
FILE_ROOT = os.path.join(APP_ROOT, 'dictionaries')
162+
163+
DICTIONARIES = {}
164+
DICTIONARIES["English"] = FILE_ROOT + "/english.txt"
165+
DICTIONARIES["Czech"] = FILE_ROOT + "/czech.txt"
166+
DICTIONARIES["French"] = FILE_ROOT + "/french.txt"
167+
DICTIONARIES["German"] = FILE_ROOT + "/german.txt"
168+
DICTIONARIES["Greek"] = FILE_ROOT + "/greek.txt"
169+
DICTIONARIES["Italian"] = FILE_ROOT + "/italian.txt"
170+
DICTIONARIES["Portuguese"] = FILE_ROOT + "/portuguese.txt"
171+
DICTIONARIES["Russian"] = FILE_ROOT + "/russian.txt"
172+
DICTIONARIES["Spanish"] = FILE_ROOT + "/spanish.txt"
173+
DICTIONARIES["Cards Against Humanity"] = FILE_ROOT + "/cards_against_humanity.txt"
174+
def load_dictionary(path):
175+
with open(path, 'r') as words_file:
176+
return [elem for elem in words_file.read().split('\n') if len(elem.strip()) > 0]
177+
return {k: load_dictionary(v) for k,v in DICTIONARIES.items()}
155178

156179
if __name__ == '__main__':
180+
global DICTIONARIES
181+
DICTIONARIES = __load_dictionaries()
157182
socketio.run(app, host='0.0.0.0', debug=True)

‎server/codenames/game.py

+5-26
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@
77
import os
88

99
# dictionaries
10-
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
11-
FILE_ROOT = os.path.join(APP_ROOT, '..', 'dictionaries')
12-
13-
DICTIONARIES = {}
14-
DICTIONARIES["English"] = FILE_ROOT + "/english.txt"
15-
DICTIONARIES["Czech"] = FILE_ROOT + "/czech.txt"
16-
DICTIONARIES["French"] = FILE_ROOT + "/french.txt"
17-
DICTIONARIES["German"] = FILE_ROOT + "/german.txt"
18-
DICTIONARIES["Greek"] = FILE_ROOT + "/greek.txt"
19-
DICTIONARIES["Italian"] = FILE_ROOT + "/italian.txt"
20-
DICTIONARIES["Portuguese"] = FILE_ROOT + "/portuguese.txt"
21-
DICTIONARIES["Russian"] = FILE_ROOT + "/russian.txt"
22-
DICTIONARIES["Spanish"] = FILE_ROOT + "/spanish.txt"
23-
DICTIONARIES["Cards Against Humanity"] = FILE_ROOT + "/cards_against_humanity.txt"
24-
2510
# colors per team
2611
RED = 'R'
2712
BLUE = 'B'
@@ -36,7 +21,7 @@
3621
class Info(object):
3722
# pylint: disable=too-many-instance-attributes
3823
"""Object for tracking game stats"""
39-
def __init__(self, dictionary='English', size='normal', teams=2, wordbank=False, mix=False):
24+
def __init__(self, dictionary='English', size='normal', teams=2, wordbank=False, mix=False, dictionaries=[]):
4025
self.wordbank = wordbank
4126
self.game_id = self.generate_room_id()
4227
self.starting_color = RED
@@ -46,8 +31,8 @@ def __init__(self, dictionary='English', size='normal', teams=2, wordbank=False,
4631
self.size = size
4732
self.teams = teams
4833
self.dictionary = dictionary
34+
self.dictionaries = dictionaries
4935
self.mix = mix
50-
self.dictionaries = DICTIONARIES.keys()
5136
self.minWords = BOARD_SIZE[self.size]
5237

5338
# gererate board
@@ -120,31 +105,25 @@ def playtime(self):
120105

121106
def __get_words(self, size):
122107
"""Generate a list of words"""
123-
if not self.dictionary in DICTIONARIES.keys():
124-
print("Error: dictionary '" + self.dictionary + "' doesn't exist")
125-
return None
126108
# override words with the wordbank
127109
words = self.wordbank
128110
if not self.wordbank:
129111
if self.mix:
130112
words = []
131113
for key in self.mix:
132114
# load and shuffle current dict
133-
tempWords = self.__load_words(key)
115+
tempWords = self.dictionaries[key]
134116
random.shuffle(tempWords)
135117
# get word ratio (rounded up)
136118
numWords = int(math.ceil((self.mix[key]/100.0)*BOARD_SIZE[size]))
137119
words = words + tempWords[0:numWords]
138120
else:
139-
words = self.__load_words(self.dictionary)
121+
print(self.dictionary)
122+
words = self.dictionaries[self.dictionary]
140123
random.shuffle(words)
141124
final_words = words[0:BOARD_SIZE[size]]
142125
return final_words
143126

144-
def __load_words(self, d):
145-
with open(DICTIONARIES.get(d), 'r') as words_file:
146-
return [elem for elem in words_file.read().split('\n') if len(elem.strip()) > 0]
147-
148127
def __get_layout(self, size, teams):
149128
"""Randomly generate a card layout"""
150129
size = BOARD_SIZE[size]

0 commit comments

Comments
 (0)