<commit_before>Db = require 'db'
Event = require 'event'
Plugin = require 'plugin'
Timer = require 'timer'
Util = require 'util'
Black = require 'black'
White = require 'white'
{tr} = require 'i18n'

handsize = 10
maxtrashcards = 1
maxWinnerStackLength = 1

### Standard Functions ###

exports.onInstall = (config) !->
	if config?
		onConfig config

exports.onConfig = onConfig = (config) !->
	if config?
		# Do things
		if not Db.shared.get 'rounds'
			exports.startgame()
	else
		# Defaults for template groups

exports.onUpgrade = !->
	log 'Upgraded the Plugin'

	deck = Db.shared.get 'answerdeck'
	log 'Cards in deck:', deck.length
	if not deck.length
		refillAnswerDeck()
	
	# Add new cards to both decks
	if (Db.shared.get 'questiondecksize') < Black.numcards()
		newCards = [Db.shared.get('questiondecksize')..Black.numcards() - 1]
		oldCards = Db.shared.get 'questiondeck'
		Db.shared.set 'questiondeck', oldCards.concat newCards
		Db.shared.set 'questiondecksize', Black.numcards()
	
	if (Db.shared.get 'answerdecksize') < White.numcards()
		newCards = [Db.shared.get('answerdecksize')..White.numcards() - 1]
		oldCards = Db.shared.get 'answerdeck'
		Db.shared.set 'answerdeck', oldCards.concat newCards
		Db.shared.set 'answerdecksize', White.numcards()

	log 'Cards in answerdeck after upgrade:', (Db.shared.get 'answerdeck').length
	log 'Cards in questiondeck after upgrade:', (Db.shared.get 'questiondeck').length

	for userId in Plugin.userIds()
		if !Db.personal(userId).get 'showpatchinfo'
			Db.personal(userId).set 'showpatchinfo', 1

	answerCards = White.cards()
	lookupAnswerCardId = (text) !->
		for id,c of answerCards
			if c.text is text
				return id
		return -1
		
	questionCards = Black.cards()
	lookupQuestionCardId = (text) !->
		for id,card of questionCards
			if card.text is text
				return id
		return -1

	# Change all card texts/objects to IDs.
	isNonId = false
	rounds = Object.keys (Db.shared.get 'rounds')
	firstround = Db.shared.get 'rounds', rounds[0]
	if firstround.question.text
		isNonId = true

	if isNonId<commit_msg>Small fix to prepare a new round if none is prepared on upgrade.<commit_after>Db = require 'db'
Event = require 'event'
Plugin = require 'plugin'
Timer = require 'timer'
Util = require 'util'
Black = require 'black'
White = require 'white'
{tr} = require 'i18n'

handsize = 10
maxtrashcards = 1
maxWinnerStackLength = 1

### Standard Functions ###

exports.onInstall = (config) !->
	if config?
		onConfig config

exports.onConfig = onConfig = (config) !->
	if config?
		# Do things
		if not Db.shared.get 'rounds'
			exports.startgame()
	else
		# Defaults for template groups

exports.onUpgrade = !->
	log 'Upgraded the Plugin'

	rounds = Db.shared.get 'rounds'
	if rounds
		activeRounds = 0
		for i in [(Object.keys rounds).length - 1..0]
			roundId = Object.keys(rounds)[i]
			if (Db.shared.get 'rounds', roundId, 'phase') in ['draw', 'play', 'vote']
				activeRounds += 1
		if activeRounds == 0
			log 'No Active Rounds!'
			prepareNewRound()

	deck = Db.shared.get 'answerdeck'
	log 'Cards in deck:', deck.length
	if not deck.length
		refillAnswerDeck()
	
	# Add new cards to both decks
	if (Db.shared.get 'questiondecksize') < Black.numcards()
		newCards = [Db.shared.get('questiondecksize')..Black.numcards() - 1]
		oldCards = Db.shared.get 'questiondeck'
		Db.shared.set 'questiondeck', oldCards.concat newCards
		Db.shared.set 'questiondecksize', Black.numcards()
	
	if (Db.shared.get 'answerdecksize') < White.numcards()
		newCards = [Db.shared.get('answerdecksize')..White.numcards() - 1]
		oldCards = Db.shared.get 'answerdeck'
		Db.shared.set 'answerdeck', oldCards.concat newCards
		Db.shared.set 'answerdecksize', White.numcards()

	log 'Cards in answerdeck after upgrade:', (Db.shared.get 'answerdeck').length
	log 'Cards in questiondeck after upgrade:', (Db.shared.get 'questiondeck').length

	for userId in Plugin.userIds()
		if !Db.personal(userId).get 'showpatchinfo'
			Db.personal(userId).set 'showpatchinfo', 1

	answerCards = White.cards()
	lookupAnswerCardId = (text) !->
		for id,c of answerCards
			if c.text is text
				return id
		return -1
		
	questionCards = Black.cards()
	lookupQuestionCardId = (text) !->
		for id,card of questionCards
			if card.text is text
				return id
		return -1

	# Change all card texts/objects to IDs.
	log 'Rounds:', Db.shared.get 'rounds'
	isNonId = false
	rounds = Object.keys (Db.shared.get 'rounds')
	firstround = Db.shared.get 'rounds', rounds[0]
	if firstround.question.text
		isNonId = true

	if isNonId
{'fraction_non_alphanumeric': 0.12819947043248014, 'fraction_numerical': 0.006398940864960283, 'mean_word_length': 3.9703947368421053, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3773040', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1553, 'n_words': 433}