|
|
3c1a85 |
|
|
|
3c1a85 |
import config
|
|
|
3c1a85 |
|
|
|
3c1a85 |
import os
|
|
|
3c1a85 |
import uuid
|
|
|
3c1a85 |
import random
|
|
|
3c1a85 |
|
|
|
3c1a85 |
|
|
|
3c1a85 |
class Player:
|
|
|
3c1a85 |
def __init__(self, game, name):
|
|
|
3c1a85 |
self.game = game
|
|
|
3c1a85 |
self.name = name
|
|
|
3c1a85 |
self.id = str(uuid.uuid4())
|
|
|
33a545 |
self.joined = False
|
|
|
33a545 |
self.word = ""
|
|
|
33a545 |
self.ready = False
|
|
|
33a545 |
self.selection = -1
|
|
|
3c1a85 |
players[self.id] = self
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def beginturn(self):
|
|
|
3c1a85 |
self.word = ""
|
|
|
3c1a85 |
self.ready = False
|
|
|
3c1a85 |
self.selection = -1
|
|
|
3c1a85 |
|
|
|
33a545 |
def join(self, word):
|
|
|
33a545 |
if not self.joined:
|
|
|
33a545 |
del players[self.id]
|
|
|
33a545 |
self.id = str(uuid.uuid4())
|
|
|
33a545 |
players[self.id] = self
|
|
|
33a545 |
self.word = str(word).strip()
|
|
|
33a545 |
self.joined = True
|
|
|
33a545 |
self.game.playerJoin()
|
|
|
33a545 |
|
|
|
3c1a85 |
def select(self, i):
|
|
|
3c1a85 |
if self.selection < 0 and i >= 0 and i < len(self.game.cards):
|
|
|
3c1a85 |
self.selection = i
|
|
|
3c1a85 |
self.game.playerSelect()
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def nextturn(self, word):
|
|
|
3c1a85 |
if self.game.selected and not self.ready:
|
|
|
3c1a85 |
self.word = str(word).strip()
|
|
|
3c1a85 |
self.ready = True
|
|
|
3c1a85 |
self.game.playerReady()
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def status(self):
|
|
|
3c1a85 |
friend = self.game.players[1] if self.game.players[0] == self else self.game.players[0]
|
|
|
33a545 |
friendId = None if friend.joined else friend.id
|
|
|
3c1a85 |
friendSelection = friend.selection if self.game.selected else -1
|
|
|
3c1a85 |
cards = []
|
|
|
3c1a85 |
for i in range(0, len(self.game.cards)):
|
|
|
3c1a85 |
cards.append({ "index": i, "name": self.game.cards[i], "my": self.selection == i, "friends": friendSelection == i })
|
|
|
3c1a85 |
return {
|
|
|
9ae076 |
"vocabulary": self.game.vocabulary,
|
|
|
3c1a85 |
"turn": self.game.turn,
|
|
|
3c1a85 |
"score": self.game.score,
|
|
|
3c1a85 |
"word": self.game.word,
|
|
|
3c1a85 |
"wordSource": self.game.wordSource,
|
|
|
3c1a85 |
"cards": cards,
|
|
|
33a545 |
"started": self.game.started,
|
|
|
3c1a85 |
"selected": self.game.selected,
|
|
|
3c1a85 |
"host": config.externalHost,
|
|
|
3c1a85 |
"prefix": config.prefix,
|
|
|
3c1a85 |
"cardsPrefix": config.cardsPrefix,
|
|
|
33a545 |
"waiting": (self.joined and not self.game.started) or (self.selection >= 0 and not self.game.selected) or self.ready,
|
|
|
3c1a85 |
"win": self.game.selected and self.selection == friend.selection,
|
|
|
3c1a85 |
"lose": self.game.selected and self.selection != friend.selection,
|
|
|
3c1a85 |
"me": {
|
|
|
3c1a85 |
"id": self.id,
|
|
|
3c1a85 |
"name": self.name,
|
|
|
3c1a85 |
"selection": self.selection,
|
|
|
3c1a85 |
"selected": self.selection >= 0,
|
|
|
3c1a85 |
"ready": self.ready },
|
|
|
3c1a85 |
"friend": {
|
|
|
3c1a85 |
"id": friendId,
|
|
|
3c1a85 |
"selection": friendSelection } }
|
|
|
3c1a85 |
|
|
|
3c1a85 |
|
|
|
3c1a85 |
class Game:
|
|
|
9ae076 |
def __init__(self, vocabulary, word):
|
|
|
9ae076 |
self.vocabulary = vocabulary
|
|
|
9ae076 |
self.words = words.get(vocabulary, ["no words for choose, sorry..."])
|
|
|
3c1a85 |
self.players = ( Player(self, "Player1"), Player(self, "Player2") )
|
|
|
3c1a85 |
self.turn = 1
|
|
|
3c1a85 |
self.score = 0
|
|
|
33a545 |
self.started = False
|
|
|
33a545 |
self.selected = False
|
|
|
3c1a85 |
self.cards = []
|
|
|
33a545 |
self.word = ""
|
|
|
33a545 |
self.wordSource = ""
|
|
|
33a545 |
self.players[0].join(word)
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def beginturn(self):
|
|
|
3c1a85 |
assert(cards)
|
|
|
3c1a85 |
self.cards.clear()
|
|
|
3c1a85 |
for i in range(0, config.cardsCount):
|
|
|
3c1a85 |
for j in range(0, 100):
|
|
|
3c1a85 |
card = cards[ random.randrange(0, len(cards)) ]
|
|
|
3c1a85 |
if not card in self.cards:
|
|
|
3c1a85 |
self.cards.append(card)
|
|
|
3c1a85 |
break
|
|
|
c463bf |
self.cards.append(skipname)
|
|
|
3c1a85 |
w0 = self.players[0].word
|
|
|
3c1a85 |
w1 = self.players[1].word
|
|
|
3c1a85 |
if not w0 and not w1:
|
|
|
9ae076 |
self.word = random.choice(self.words)
|
|
|
3c1a85 |
self.wordSource = "choosen by random"
|
|
|
3c1a85 |
elif not w1:
|
|
|
3c1a85 |
self.word = w0
|
|
|
3c1a85 |
self.wordSource = "suggested by player1"
|
|
|
3c1a85 |
elif not w0:
|
|
|
3c1a85 |
self.word = w1
|
|
|
3c1a85 |
self.wordSource = "suggested by player2"
|
|
|
3c1a85 |
elif w0 == w1:
|
|
|
3c1a85 |
self.word = w0
|
|
|
3c1a85 |
self.wordSource = "suggested by both players"
|
|
|
3c1a85 |
elif random.randrange(0, 1):
|
|
|
3c1a85 |
self.word = w0
|
|
|
3c1a85 |
self.wordSource = "suggested by player1 (player2 may be lucky next time)"
|
|
|
3c1a85 |
else:
|
|
|
3c1a85 |
self.word = w1
|
|
|
3c1a85 |
self.wordSource = "suggested by player2 (player1 may be lucky next time)"
|
|
|
3c1a85 |
self.players[0].beginturn()
|
|
|
3c1a85 |
self.players[1].beginturn()
|
|
|
3c1a85 |
self.selected = False
|
|
|
3c1a85 |
|
|
|
33a545 |
def playerJoin(self):
|
|
|
33a545 |
if not self.started and all(p.joined for p in self.players):
|
|
|
33a545 |
self.started = True
|
|
|
33a545 |
self.beginturn()
|
|
|
33a545 |
|
|
|
3c1a85 |
def playerSelect(self):
|
|
|
33a545 |
if self.started and not self.selected and all(p.selection >= 0 for p in self.players):
|
|
|
3c1a85 |
self.selected = True
|
|
|
3c1a85 |
if self.players[0].selection == self.players[1].selection:
|
|
|
3c1a85 |
self.score = self.score + 1
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def playerReady(self):
|
|
|
33a545 |
if self.started and self.selected and all(p.ready for p in self.players):
|
|
|
3c1a85 |
self.turn = self.turn + 1
|
|
|
3c1a85 |
self.beginturn()
|
|
|
3c1a85 |
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def mergeCards(path):
|
|
|
3c1a85 |
for f in os.scandir(path):
|
|
|
9ae076 |
if f.is_file() and f.name.endswith(".jpg") and f.name != skipname:
|
|
|
3c1a85 |
cards.append(f.name)
|
|
|
3c1a85 |
|
|
|
3c1a85 |
def mergeWords(path):
|
|
|
9ae076 |
for f in os.scandir(path):
|
|
|
9ae076 |
if f.is_file() and f.name.endswith(".txt"):
|
|
|
9ae076 |
n = f.name[:-4]
|
|
|
9ae076 |
ws = set(words.get(n, []))
|
|
|
9ae076 |
with open(path + "/" + f.name, "r") as ff:
|
|
|
9ae076 |
for word in ff.readlines():
|
|
|
9ae076 |
w = word.strip()
|
|
|
9ae076 |
if w != "":
|
|
|
9ae076 |
ws.add(w)
|
|
|
9ae076 |
if ws:
|
|
|
9ae076 |
if not n in words:
|
|
|
9ae076 |
words[n] = []
|
|
|
9ae076 |
words[n].clear()
|
|
|
9ae076 |
words[n] += ws
|
|
|
9ae076 |
|
|
|
9ae076 |
skipname = "skip.jpg"
|
|
|
3c1a85 |
players = {}
|
|
|
3c1a85 |
cards = []
|
|
|
9ae076 |
words = {}
|
|
|
3c1a85 |
|