<commit_before>from . import version
from .constants import *
from shared.messages import *
from shared.protocol import JSONReceiver

import random

class ClientProtocol(JSONReceiver):
  def __init__(self, factory):
    JSONReceiver.__init__(self, factory)
    self.addCallback(MODE_CLIENT_AUTHENTIFICATION, MSG_CLIENT_ACCEPTED, self.clientAccepted)
    self.addCallback(MODE_CLIENT_AUTHENTIFICATION, MSG_CLIENT_REFUSED, self.clientRefused)
    self.addCallback(MODE_CLIENT_AUTHENTIFICATION, MSG_SERVER_AUTHENTIFICATION, self.serverAuthentification)
    self.addCallback(MODE_USER_AUTHENTIFICATION, MSG_USER_LOGIN, self.userLogin)
    self.addCallback(MODE_USER_AUTHENTIFICATION, MSG_USER_REGISTRATION, self.userRegistration)
    self.addCallback(MODE_INITIAL_SYNC, MSG_CURRENT_GAMES, self.currentGames)
    self.addCallback(MODE_INITIAL_SYNC, MSG_CURRENT_USERS, self.currentUsers)
    self.addCallback(MODE_INITIAL_SYNC, MSG_DATABASE_QUERY, self.databaseQuery)
    self.addCallback(MODE_INITIAL_SYNC, MSG_DATABASE_PUSH, self.databasePush)
    self.addCallback(MODE_INITIAL_SYNC, MSG_SYNC_FINISHED, self.syncFinished)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_CREATE_GAME, self.createGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_JOIN_GAME, self.joinGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_JOINED_GAME, self.joinedGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_LOGGED_IN, self.loggedIn)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_LOGGED_OFF, self.loggedOff)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_DISCONNECTED_FROM_GAME, self.disconnectedFromGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_LEFT_GAME, self.leftGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_DELETED_GAME, self.deletedGame)
    self.addCallback(MODE_IN_GAME, MSG_START_GAME, self.startGame)
    self.addCallback(MODE_IN_GAME, MSG_STARTED_GAME, self.startedGame)
    self.addCallback(MODE_IN_GAME, MSG_CREATE_GAME, self.createGame)
    self.addCallback(MODE_IN_GAME, MSG_DRAW_CARDS, self.drawCards)
    self.addCallback(MODE_IN_GAME, MSG_CZAR_CHANGE, self.czarChange)
    self.addCallback(MODE_IN_GAME, MSG_JOINED_GAME, self.joinedGame)
    self.addCallback(MODE_IN_GAME, MSG_LOGGED_IN, self.loggedIn)
    self.addCallback(MODE_IN_GAME, MSG_LOGGED_OFF, self.loggedOff)
    self.addCallback(MODE_IN_GAME, MSG_LEFT_GAME, self.leftGame)
    self.addCallback(MODE_IN_GAME, MSG_DISCONNECTED_FROM_GAME, self.disconnectedFromGame)
    self.addCallback(MODE_IN_GAME, MSG_DELETED_GAME, self.deletedGame)
    self.addCallback(MODE_IN_GAME, MSG_CHOOSE_CARDS, self.chooseCards)
    self.addCallback(MODE_IN_GAME, MSG_CHOICES, self.choices)
    self.addCallback(MODE_IN_GAME, MSG_CZAR_DECISION, self.czarDecision)
    self.setMode(MODE_CLIENT_AUTHENTIFICATION)
    self.database_hash = None
    self.identification = 'server'
    self.server_version = {'MAJOR': 0, 'MINOR': 0, 'REVISION': 0}
    self.factory.client = self
    self.user_id = 0
    self.game_id = 0

  def connectionMade(self):
    self.sendMessage(MSG_CLIENT_AUTHENTIFICATION, major=version.MAJOR, minor=version.MINOR, revision=version.REVISION)

  def serverAuthentification(self, major, minor, revision):
    self.server_version = {'MAJOR': major, 'MINOR': minor, 'REVISION': revision}

  def clientRefused(self, reason):
    self.factory.display.view.clientRefusedMessage(reason)

  def clientAccepted(self):
    username, password = self.factory.display.getLoginCredentials()
    self.sendMessage(MSG_USER_AUTHENTIFICATION, username=username, password=password)
    self.setMode(MODE_USER_AUTHENTIFICATION)
    self.factory.display.view.loginMessage()

  def userLogin(self, success, message, user_id = 0):
    if success:
      self.user_id = user_id
      self.factory.display.view.syncMessage()
      self.setMode(MODE_INITIAL_SYNC)
      self.sendMessage(MSG_DATABASE_QUERY)
    else:
      self.factory.display.view.errorMessage(message)

  def userRegistration(self, success, message):
    if not success:
      self.factory.display.view.errorMessage(message)

  def databaseQuery(self, hash):
    self.factory.card_database.loadPath(self.factory.display.server_name, hash)
    if not self.factory.card_database.loaded:
      self.sendMessage(MSG_DATABASE_PULL)
      self.database_hash = hash
    else:
      self.factory.card_database.loadCards()
      self.sendMessage(MSG_DATABASE_KNOWN)

  def databasePush(self, size):
    self.receiveRawData(size, self.databaseKnown)

  def databaseKnown(self):
    self.factory.card_database.loadData(self.raw_data, self.factory.display.server_name, self.database_hash)
    self.factory.card_database.loadCards()
    self.sendMessage(MSG_DATABASE_KNOWN)

  def syncFinished(self):
    self.setMode(MODE_FREE_TO_JOIN)
    self.factory.display.setView('OverviewView')

  def createGame(self, success=True, game_id = '', message = '', name = '', user_id = ''):
    if success:
      self.factory.addGame(game_id, name)
      if self.getMode() == MODE_FREE_TO_JOIN:
        self.factory.display.callFunction('self.view.addGame', game_id)
      if self.getMode() == MODE_FREE_TO_JOIN and user_id == self.user_id:
        self.sendMessage(MSG_JOIN_GAME, game_id=game_id)
    else:
      self.factory.display.setView('ConnectionView')
      self.factory.display.callFunction('self.view.errorMessage', message = message)

  def joinGame(self, success, message = '', game_id = 0, users = []):
    if success:
      self.game_id = game_id
      self.setMode(MODE_IN_GAME)
      self.factory.display.setView('GameView')
      self.factory.display.callFunction('self.view.player_indicators.addPlayer', self.user_id)
      for user in users:
        self.factory.display.callFunction('self.view.player_indicators.addPlayer', user)
      self.factory.display.join_game_sound.stop()
      self.factory.display.join_game_sound.play()
    else:
      self.factory.display.setView('ConnectionView')
      self.factory.display.callFunction('self.view.errorMessage', message = message)

  def startGame(self, success, message=''):
    if not success:
      self.factory.display.callFunction('self.view.writeLogError', message)

  def joinedGame(self, user_id, game_id):
    if self.getMode() == MODE_IN_GAME and game_id == self.game_id:
      self.factory.display.callFunction('self.view.writeLog', '%s joined the game'%self.factory.findUsername(user_id))
      self.factory.display.callFunction('self.view.player_indicators.addPlayer', user_id)
      self.factory.display.join_game_sound.stop()
      self.factory.display.join_game_sound.play()<commit_msg>client doesn't need to join created games manually, server will do this automatically<commit_after>from . import version
from .constants import *
from shared.messages import *
from shared.protocol import JSONReceiver

import random

class ClientProtocol(JSONReceiver):
  def __init__(self, factory):
    JSONReceiver.__init__(self, factory)
    self.addCallback(MODE_CLIENT_AUTHENTIFICATION, MSG_CLIENT_ACCEPTED, self.clientAccepted)
    self.addCallback(MODE_CLIENT_AUTHENTIFICATION, MSG_CLIENT_REFUSED, self.clientRefused)
    self.addCallback(MODE_CLIENT_AUTHENTIFICATION, MSG_SERVER_AUTHENTIFICATION, self.serverAuthentification)
    self.addCallback(MODE_USER_AUTHENTIFICATION, MSG_USER_LOGIN, self.userLogin)
    self.addCallback(MODE_USER_AUTHENTIFICATION, MSG_USER_REGISTRATION, self.userRegistration)
    self.addCallback(MODE_INITIAL_SYNC, MSG_CURRENT_GAMES, self.currentGames)
    self.addCallback(MODE_INITIAL_SYNC, MSG_CURRENT_USERS, self.currentUsers)
    self.addCallback(MODE_INITIAL_SYNC, MSG_DATABASE_QUERY, self.databaseQuery)
    self.addCallback(MODE_INITIAL_SYNC, MSG_DATABASE_PUSH, self.databasePush)
    self.addCallback(MODE_INITIAL_SYNC, MSG_SYNC_FINISHED, self.syncFinished)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_CREATE_GAME, self.createGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_JOIN_GAME, self.joinGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_JOINED_GAME, self.joinedGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_LOGGED_IN, self.loggedIn)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_LOGGED_OFF, self.loggedOff)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_DISCONNECTED_FROM_GAME, self.disconnectedFromGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_LEFT_GAME, self.leftGame)
    self.addCallback(MODE_FREE_TO_JOIN, MSG_DELETED_GAME, self.deletedGame)
    self.addCallback(MODE_IN_GAME, MSG_START_GAME, self.startGame)
    self.addCallback(MODE_IN_GAME, MSG_STARTED_GAME, self.startedGame)
    self.addCallback(MODE_IN_GAME, MSG_CREATE_GAME, self.createGame)
    self.addCallback(MODE_IN_GAME, MSG_DRAW_CARDS, self.drawCards)
    self.addCallback(MODE_IN_GAME, MSG_CZAR_CHANGE, self.czarChange)
    self.addCallback(MODE_IN_GAME, MSG_JOINED_GAME, self.joinedGame)
    self.addCallback(MODE_IN_GAME, MSG_LOGGED_IN, self.loggedIn)
    self.addCallback(MODE_IN_GAME, MSG_LOGGED_OFF, self.loggedOff)
    self.addCallback(MODE_IN_GAME, MSG_LEFT_GAME, self.leftGame)
    self.addCallback(MODE_IN_GAME, MSG_DISCONNECTED_FROM_GAME, self.disconnectedFromGame)
    self.addCallback(MODE_IN_GAME, MSG_DELETED_GAME, self.deletedGame)
    self.addCallback(MODE_IN_GAME, MSG_CHOOSE_CARDS, self.chooseCards)
    self.addCallback(MODE_IN_GAME, MSG_CHOICES, self.choices)
    self.addCallback(MODE_IN_GAME, MSG_CZAR_DECISION, self.czarDecision)
    self.setMode(MODE_CLIENT_AUTHENTIFICATION)
    self.database_hash = None
    self.identification = 'server'
    self.server_version = {'MAJOR': 0, 'MINOR': 0, 'REVISION': 0}
    self.factory.client = self
    self.user_id = 0
    self.game_id = 0

  def connectionMade(self):
    self.sendMessage(MSG_CLIENT_AUTHENTIFICATION, major=version.MAJOR, minor=version.MINOR, revision=version.REVISION)

  def serverAuthentification(self, major, minor, revision):
    self.server_version = {'MAJOR': major, 'MINOR': minor, 'REVISION': revision}

  def clientRefused(self, reason):
    self.factory.display.view.clientRefusedMessage(reason)

  def clientAccepted(self):
    username, password = self.factory.display.getLoginCredentials()
    self.sendMessage(MSG_USER_AUTHENTIFICATION, username=username, password=password)
    self.setMode(MODE_USER_AUTHENTIFICATION)
    self.factory.display.view.loginMessage()

  def userLogin(self, success, message, user_id = 0):
    if success:
      self.user_id = user_id
      self.factory.display.view.syncMessage()
      self.setMode(MODE_INITIAL_SYNC)
      self.sendMessage(MSG_DATABASE_QUERY)
    else:
      self.factory.display.view.errorMessage(message)

  def userRegistration(self, success, message):
    if not success:
      self.factory.display.view.errorMessage(message)

  def databaseQuery(self, hash):
    self.factory.card_database.loadPath(self.factory.display.server_name, hash)
    if not self.factory.card_database.loaded:
      self.sendMessage(MSG_DATABASE_PULL)
      self.database_hash = hash
    else:
      self.factory.card_database.loadCards()
      self.sendMessage(MSG_DATABASE_KNOWN)

  def databasePush(self, size):
    self.receiveRawData(size, self.databaseKnown)

  def databaseKnown(self):
    self.factory.card_database.loadData(self.raw_data, self.factory.display.server_name, self.database_hash)
    self.factory.card_database.loadCards()
    self.sendMessage(MSG_DATABASE_KNOWN)

  def syncFinished(self):
    self.setMode(MODE_FREE_TO_JOIN)
    self.factory.display.setView('OverviewView')

  def createGame(self, success=True, game_id = '', message = '', name = '', user_id = ''):
    if success:
      self.factory.addGame(game_id, name)
      if self.getMode() == MODE_FREE_TO_JOIN:
        self.factory.display.callFunction('self.view.addGame', game_id)
    else:
      self.factory.display.setView('ConnectionView')
      self.factory.display.callFunction('self.view.errorMessage', message = message)

  def joinGame(self, success, message = '', game_id = 0, users = []):
    if success:
      self.game_id = game_id
      self.setMode(MODE_IN_GAME)
      self.factory.display.setView('GameView')
      self.factory.display.callFunction('self.view.player_indicators.addPlayer', self.user_id)
      for user in users:
        self.factory.display.callFunction('self.view.player_indicators.addPlayer', user)
      self.factory.display.join_game_sound.stop()
      self.factory.display.join_game_sound.play()
    else:
      self.factory.display.setView('ConnectionView')
      self.factory.display.callFunction('self.view.errorMessage', message = message)

  def startGame(self, success, message=''):
    if not success:
      self.factory.display.callFunction('self.view.writeLogError', message)

  def joinedGame(self, user_id, game_id):
    if self.getMode() == MODE_IN_GAME and game_id == self.game_id:
      self.factory.display.callFunction('self.view.writeLog', '%s joined the game'%self.factory.findUsername(user_id))
      self.factory.display.callFunction('self.view.player_indicators.addPlayer', user_id)
      self.factory.display.join_game_sound.stop()
      self.factory.display.join_game_sound.play()
{'fraction_non_alphanumeric': 0.10111409911640415, 'fraction_numerical': 0.0010756819054936612, 'mean_word_length': 5.133836003770028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '872098', 'n_tokens_mistral': 4506, 'n_tokens_neox': 4478, 'n_words': 615}