<commit_before># -*- coding: utf-8 *-*
from GameManager import GameManager
import Common
import Constants
import random
import math

players = []
games = []
last_game_id = 1


class Player:
	def __init__(self, socket):
		self.socket = socket
		self.username = ''
		self.state = None
		self.current_game = None
		self.planets = []


class Game:
	# States of game
	NOT_STARTED = 1
	IN_PROGRESS = 2
	FINISHED = 3

	def __init__(self):
		global last_game_id
		self.players = []
		self.players_lost = []
		self.id = last_game_id
		self.state = Game.NOT_STARTED
		self.name = ''
		self.map = None
		self.tech = {}
		self.stats = Constants.STATS_TEMPLATE
		self.max_players = 0
		self.hosting_player = None
		self.manager = GameManager(self)
		last_game_id += 1

	def valid(self, player, command, is_precheck):
		"""
		Checks if command is valid for player.
		command: type, sourceId, targetId, unitCount
		is_precheck: checking only if commands are valid, to send "gameInvalidCommand" to player
		"""
		from_id = command['sourceId']
		to_id = command['targetId']
		count = command['fleetCount']

		# Move
		if command['type'] == 'move':
			if to_id not in self.map.planets: # Wrong id
				return False
			to_planet = self.map.planets[to_id]
			if from_id not in self.map.planets: # Wrong id
				return False
			from_planet = self.map.planets[from_id]
			if from_planet['player'] is not player: # Player doesn't own this planet
				return False
			if to_planet['id'] not in from_planet['links']: # Planets must be linked
				return False
			if not is_precheck:
				if from_planet['fleets'] <= count: # At least one fleet must always stay behind
					return False

		# Attack
		if command['type'] == 'attack':
			if to_id not in self.map.planets: # Wrong id
				return False
			to_planet = self.map.planets[to_id]
			if from_id not in self.map.planets: # Wrong id
				return False
			from_planet = self.map.planets[from_id]
			if from_planet['player'] is not player: # Player doesn't own this planet
				return False
			if to_planet['id'] not in from_planet['links']: # Planets must be linked
				return False
			if not is_precheck:
				if from_planet['fleets'] <= count: # At least one fleet must always stay behind
					return False

		# Deploy
		if command['type'] == 'deploy': # Deploy
			if to_id not in self.map.planets: # Wrong id
				return False
			to_planet = self.map.planets[to_id]
			if to_planet['player'] is not player: # Player doesn't own that planet
				return False

		# Tech
		if command['type'] == 'tech': # Tech
			if 'techType' not in command: # Invalid command
				return False
			if command['techType'] not in Constants.UPGRADE_BONUS: # Invalid tech
				return False
			# Player doesn't have enough tech points
			if self.tech[player]['points'] < Constants.TECH_COST[command['techType']]:
				return False

		return True

	def execute(self, player, command):
		"""
		Executes a command, validating it beforehand
		"""
		if self.valid(player, command, False):
			ret = {'player': player.username, 'type': command['type']}
			if command['type'] == 'deploy': # Deploy
				self.map.deploy(command['targetId'], command['fleetCount'])
				ret.update({
					'targetId': command['targetId'],
					'fleetCount': command['fleetCount']
				})

			# Maybe, in the course of time, target planed changed its owner
			# So it may no longer be an attack, but just a move
			if command['type'] == 'move' or command['type'] == 'attack':
				if self.map.planets[command['sourceId']]['player'] is self.map.planets[command['targetId']]['player']:
					updated_command_type = 'move'
				else:
					updated_command_type = 'attack'

				# Is it move?
				if updated_command_type == 'move':
					self.map.move(command['sourceId'], command['targetId'], command['fleetCount'])
					ret.update({
						'sourceId': command['sourceId'],
						'targetId': command['targetId'],
						'fleetCount': command['fleetCount'],
						'type': 'move'
					})

				# Or is it attack?
				if updated_command_type == 'attack':
					result = self.map.attack(command['sourceId'], command['targetId'], command['fleetCount'])
					ret.update({
						'sourceId': command['sourceId'],
						'targetId': command['targetId'],
						'fleetCount': command['fleetCount'],
						'type': 'attack'
					})
					ret.update(result)

			if command['type'] == 'tech': # Tech
				self.tech[player][command['techType']] += 1
				self.add_tech_points(player, -Constants.TECH_COST[command['techType']])
				return None # We return nothing, so nobody can see what others upgraded

			return ret
		else:
			return None
<commit_msg>Poprawiony warunek sprawdzania liczby punktow technologicznych<commit_after># -*- coding: utf-8 *-*
from GameManager import GameManager
import Common
import Constants
import random
import math

players = []
games = []
last_game_id = 1


class Player:
	def __init__(self, socket):
		self.socket = socket
		self.username = ''
		self.state = None
		self.current_game = None
		self.planets = []


class Game:
	# States of game
	NOT_STARTED = 1
	IN_PROGRESS = 2
	FINISHED = 3

	def __init__(self):
		global last_game_id
		self.players = []
		self.players_lost = []
		self.id = last_game_id
		self.state = Game.NOT_STARTED
		self.name = ''
		self.map = None
		self.tech = {}
		self.stats = Constants.STATS_TEMPLATE
		self.max_players = 0
		self.hosting_player = None
		self.manager = GameManager(self)
		last_game_id += 1

	def valid(self, player, command, is_precheck):
		"""
		Checks if command is valid for player.
		command: type, sourceId, targetId, unitCount
		is_precheck: checking only if commands are valid, to send "gameInvalidCommand" to player
		"""
		from_id = command['sourceId']
		to_id = command['targetId']
		count = command['fleetCount']

		# Move
		if command['type'] == 'move':
			if to_id not in self.map.planets: # Wrong id
				return False
			to_planet = self.map.planets[to_id]
			if from_id not in self.map.planets: # Wrong id
				return False
			from_planet = self.map.planets[from_id]
			if from_planet['player'] is not player: # Player doesn't own this planet
				return False
			if to_planet['id'] not in from_planet['links']: # Planets must be linked
				return False
			if not is_precheck:
				if from_planet['fleets'] <= count: # At least one fleet must always stay behind
					return False

		# Attack
		if command['type'] == 'attack':
			if to_id not in self.map.planets: # Wrong id
				return False
			to_planet = self.map.planets[to_id]
			if from_id not in self.map.planets: # Wrong id
				return False
			from_planet = self.map.planets[from_id]
			if from_planet['player'] is not player: # Player doesn't own this planet
				return False
			if to_planet['id'] not in from_planet['links']: # Planets must be linked
				return False
			if not is_precheck:
				if from_planet['fleets'] <= count: # At least one fleet must always stay behind
					return False

		# Deploy
		if command['type'] == 'deploy': # Deploy
			if to_id not in self.map.planets: # Wrong id
				return False
			to_planet = self.map.planets[to_id]
			if to_planet['player'] is not player: # Player doesn't own that planet
				return False

		# Tech
		if command['type'] == 'tech': # Tech
			if 'techType' not in command: # Invalid command
				return False
			if command['techType'] not in Constants.UPGRADE_BONUS: # Invalid tech
				return False
			# Tech maxed
			if self.tech[player][command['techType']] >= 2:
				return False
			# Player doesn't have enough tech points
			if self.tech[player]['points'] < Constants.TECH_COST[self.tech[player][command['techType']]+1]:
				return False

		return True

	def execute(self, player, command):
		"""
		Executes a command, validating it beforehand
		"""
		if self.valid(player, command, False):
			ret = {'player': player.username, 'type': command['type']}
			if command['type'] == 'deploy': # Deploy
				self.map.deploy(command['targetId'], command['fleetCount'])
				ret.update({
					'targetId': command['targetId'],
					'fleetCount': command['fleetCount']
				})

			# Maybe, in the course of time, target planed changed its owner
			# So it may no longer be an attack, but just a move
			if command['type'] == 'move' or command['type'] == 'attack':
				if self.map.planets[command['sourceId']]['player'] is self.map.planets[command['targetId']]['player']:
					updated_command_type = 'move'
				else:
					updated_command_type = 'attack'

				# Is it move?
				if updated_command_type == 'move':
					self.map.move(command['sourceId'], command['targetId'], command['fleetCount'])
					ret.update({
						'sourceId': command['sourceId'],
						'targetId': command['targetId'],
						'fleetCount': command['fleetCount'],
						'type': 'move'
					})

				# Or is it attack?
				if updated_command_type == 'attack':
					result = self.map.attack(command['sourceId'], command['targetId'], command['fleetCount'])
					ret.update({
						'sourceId': command['sourceId'],
						'targetId': command['targetId'],
						'fleetCount': command['fleetCount'],
						'type': 'attack'
					})
					ret.update(result)

			if command['type'] == 'tech': # Tech
				self.tech[player][command['techType']] += 1
				self.add_tech_points(player, -Constants.TECH_COST[self.tech[player][command['techType']]])
				return None # We return nothing, so nobody can see what others upgraded

			return ret
		else:
			return None

{'fraction_non_alphanumeric': 0.1270759669988214, 'fraction_numerical': 0.0019286403085824494, 'mean_word_length': 3.5889872173058013, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 4, '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': '2901199', 'n_tokens_mistral': 3666, 'n_tokens_neox': 2995, 'n_words': 868}