Blame game.py

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())
3c1a85
        self.beginturn()
3c1a85
        players[self.id] = self
3c1a85
3c1a85
    def beginturn(self):
3c1a85
        self.word = ""
3c1a85
        self.ready = False
3c1a85
        self.selection = -1
3c1a85
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]
3c1a85
        friendId = friend.id if self.game.players[0] == self else None
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 {
3c1a85
            "turn": self.game.turn,
3c1a85
            "score": self.game.score,
3c1a85
            "word": self.game.word,
3c1a85
            "wordSource": self.game.wordSource,
3c1a85
            "cards": cards,
3c1a85
            "selected": self.game.selected,
3c1a85
            "host": config.externalHost,
3c1a85
            "prefix": config.prefix,
3c1a85
            "cardsPrefix": config.cardsPrefix,
3c1a85
            "waiting": (self.selection >= 0 and friendSelection < 0) or (self.ready and not friend.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:
3c1a85
    def __init__(self):
3c1a85
        self.players = ( Player(self, "Player1"), Player(self, "Player2") )
3c1a85
        self.turn = 1
3c1a85
        self.score = 0
3c1a85
        self.cards = []
3c1a85
        self.beginturn()
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
3c1a85
        w0 = self.players[0].word
3c1a85
        w1 = self.players[1].word
3c1a85
        if not w0 and not w1:
3c1a85
            self.word = random.choice(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
3c1a85
    def playerSelect(self):
3c1a85
        if not self.selected and self.players[0].selection >= 0 and self.players[1].selection >= 0:
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):
3c1a85
        if self.selected and self.players[0].ready and self.players[1].ready:
3c1a85
            self.turn = self.turn + 1
3c1a85
            self.beginturn()
3c1a85
3c1a85
3c1a85
def mergeCards(path):
3c1a85
    for f in os.scandir(path):
3c1a85
        if f.is_file() and f.name.endswith(".jpg"):
3c1a85
            cards.append(f.name)
3c1a85
3c1a85
def mergeWords(path):
3c1a85
    global words
3c1a85
    ws = set(words)
3c1a85
    with open(path, "r") as f:
3c1a85
        for word in f.readlines():
3c1a85
            w = word.strip()
3c1a85
            if w != "":
3c1a85
                ws.add(w)
3c1a85
    words.clear()
3c1a85
    words += ws
3c1a85
3c1a85
players = {}
3c1a85
cards = []
3c1a85
words = []
3c1a85