<commit_before>require 'rubygems'
require 'bcrypt'
require 'htmlentities'
require 'to_bool'
require 'time'
require 'time_difference'

class Game < XTParser

	attr_accessor :iglooMap

	def initialize(main_class)
		@parent = main_class
		@xtPackets = Hash.new
		
		@iglooMap = Hash.new
		
		@gamePuck = '0%0%0%0%'
		
		@findFourRooms = [220, 221]
		@findFourTables = [200, 201, 202, 203, 204, 205, 206, 207]
		@mancalaTables = [100, 101, 102, 103, 104]
		@treasureTables = [300, 301, 302, 303, 304, 305, 306, 307]
		
		@mancalaRoom = 111
		@treasureRoom = 422
		
		@tables = [@findFourTables, @mancalaTables, @treasureTables].reduce([], :concat)
		@tablePopulationByID = Hash[@tables.map {|tableID| [tableID, Hash.new]}]
		@playersByTableID = Hash[@tables.map {|tableID| [tableID, Array.new]}]
		@gamesByTableID = Hash[@tables.map {|tableID| [tableID, nil]}]
		
		@waddlesByID = [
			103 => ['', ''], 
			102 => ['', ''], 
			101 => ['', '', ''], 
			100 => ['', '', '', '']
		]
		
		@sledRacing = [100, 101, 102, 103]
		
		@waddleRoom = 0
		
		@waddleUsers = Hash[@waddlesByID[0].keys.map {|waddleID| [waddleID, Hash.new(0)]}]
		
		self.handleLoadPackets
	end
	
	def loadIglooMap
		igloo_map = ''
		@iglooMap.each do |userID, username|
			igloo_map << userID.to_s + '|' + username + '%'
		end
		return igloo_map
	end
	
	def handleData(data, client)
		handlingInfo = self.parseData(data)
		if handlingInfo == false
			return @parent.sock.handleRemoveClient(client.sock)
		end
		gameHandler = handlingInfo[0]['handler']
		gameHandlerArgs = handlingInfo[0]['arguments']
		@parent.hooks.each do |hook, hookClass|
			if @parent.hooks[hook].dependencies['hook_type'].downcase == 'game'
				if @parent.hooks[hook].respond_to?(gameHandler) == true && @parent.hooks[hook].callBefore == true && @parent.hooks[hook].callAfter == false
					hookClass.send(gameHandler, gameHandlerArgs, client)
				end
			end
		end
		if self.respond_to?(gameHandler) != true
			return @parent.logger.error('Unfortunately doesn\'t seem like the game method exists')
		end
		if client.username == '' || client.username.nil? != false
			@parent.logger.error('Client is trying to send an undefined bot')
			return @parent.sock.handleRemoveClient(client.sock)
		end
		self.send(gameHandler, gameHandlerArgs, client)
		@parent.hooks.each do |hook, hookClass|
			if @parent.hooks[hook].dependencies['hook_type'].downcase == 'game'
				if @parent.hooks[hook].respond_to?(gameHandler) == true && @parent.hooks[hook].callAfter == true && @parent.hooks[hook].callBefore == false
					hookClass.send(gameHandler, gameHandlerArgs, client)
				end
			end
		end
	end
	
	def handleJoinServer(gameHandlerArgs, client)
		loginKey = gameHandlerArgs[1]
		if loginKey.length < 64
			return client.sendError(101)
		end
		validKey = @parent.mysql.getLoginKey(client.username)
		cryptedKey = BCrypt::Password.new(validKey)
		if cryptedKey != loginKey
			currInvalidAttempts = @parent.mysql.getInvalidLogins(client.username)
			currInvalidAttempts += 1
			@parent.mysql.updateInvalidLogins(client.username, currInvalidAttempts)
			return client.sendError(101)
		end
		client.sendData('%xt%js%-1%0%1%' + client.ranking['isStaff'].to_s + '%0%')
		client.sendData('%xt%lp%-1%' + client.buildClientString + '%' + client.coins.to_s + '%0%1440%100%' + client.joindate.to_s + '%4%' + client.joindate.to_s + '%%7%')
		client.sendData('%xt%gps%-1%' + client.ID.to_s + '%' + client.stamps.join('|') + '%')
		@parent.mysql.updateLoginKey("", client.username)
		client.checkPuffleStats
		client.joinRoom(100)
	end
	
	def handleJoinPlayer(gameHandlerArgs, client)
		room = gameHandlerArgs[0]
		if room < 1000
			room += 1000
		end
		client.sendData('%xt%jp%-%' + room.to_s + '%')
		client.joinRoom(room)
	end
	
	def handleJoinGame(gameHandlerArgs, client)
		room = gameHandlerArgs[0]
		client.joinRoom(room)
	end
	
	def handleGetRoomSynced(gameHandlerArgs, client)
		client.sendData('%xt%grs%-1%' + client.room.to_s + '%' + client.buildClientString + '%')
	end
	
	def handleJoinRoom(gameHandlerArgs, client)
		room = gameHandlerArgs[0]
		room_x = gameHandlerArgs[1]
		room_y = gameHandlerArgs[2]
		client.joinRoom(room, room_x, room_y)
	end<commit_msg>Fix for Issue #11 from the main branch.<commit_after>require 'rubygems'
require 'bcrypt'
require 'htmlentities'
require 'to_bool'
require 'time'
require 'time_difference'

class Game < XTParser

	attr_accessor :iglooMap

	def initialize(main_class)
		@parent = main_class
		@xtPackets = Hash.new
		
		@iglooMap = Hash.new
		
		@gamePuck = '0%0%0%0%'
		
		@findFourRooms = [220, 221]
		@findFourTables = [200, 201, 202, 203, 204, 205, 206, 207]
		@mancalaTables = [100, 101, 102, 103, 104]
		@treasureTables = [300, 301, 302, 303, 304, 305, 306, 307]
		
		@mancalaRoom = 111
		@treasureRoom = 422
		
		@tables = [@findFourTables, @mancalaTables, @treasureTables].reduce([], :concat)
		@tablePopulationByID = Hash[@tables.map {|tableID| [tableID, Hash.new]}]
		@playersByTableID = Hash[@tables.map {|tableID| [tableID, Array.new]}]
		@gamesByTableID = Hash[@tables.map {|tableID| [tableID, nil]}]
		
		@waddlesByID = [
			103 => ['', ''], 
			102 => ['', ''], 
			101 => ['', '', ''], 
			100 => ['', '', '', '']
		]
		
		@sledRacing = [100, 101, 102, 103]
		
		@waddleRoom = 0
		
		@waddleUsers = Hash[@waddlesByID[0].keys.map {|waddleID| [waddleID, Hash.new(0)]}]
		
		self.handleLoadPackets
	end
	
	def loadIglooMap
		igloo_map = ''
		@iglooMap.each do |userID, username|
			igloo_map << userID.to_s + '|' + username + '%'
		end
		return igloo_map
	end
	
	def handleData(data, client)
		handlingInfo = self.parseData(data)
		if handlingInfo == false
			return @parent.sock.handleRemoveClient(client.sock)
		end
		gameHandler = handlingInfo[0]['handler']
		gameHandlerArgs = handlingInfo[0]['arguments']
		@parent.hooks.each do |hook, hookClass|
			if @parent.hooks[hook].dependencies['hook_type'].downcase == 'game'
				if @parent.hooks[hook].respond_to?(gameHandler) == true && @parent.hooks[hook].callBefore == true && @parent.hooks[hook].callAfter == false
					hookClass.send(gameHandler, gameHandlerArgs, client)
				end
			end
		end
		if self.respond_to?(gameHandler) != true
			return @parent.logger.error('Unfortunately doesn\'t seem like the game method exists')
		end
		if client.username == '' || client.username.nil? != false
			@parent.logger.error('Client is trying to send an undefined bot')
			return @parent.sock.handleRemoveClient(client.sock)
		end
		self.send(gameHandler, gameHandlerArgs, client)
		@parent.hooks.each do |hook, hookClass|
			if @parent.hooks[hook].dependencies['hook_type'].downcase == 'game'
				if @parent.hooks[hook].respond_to?(gameHandler) == true && @parent.hooks[hook].callAfter == true && @parent.hooks[hook].callBefore == false
					hookClass.send(gameHandler, gameHandlerArgs, client)
				end
			end
		end
	end
	
	def handleJoinServer(gameHandlerArgs, client)
		loginKey = gameHandlerArgs[1]
		if loginKey.length < 64
			return client.sendError(101)
		end
		validKey = @parent.mysql.getLoginKey(client.username)
		cryptedKey = BCrypt::Password.new(validKey)
		if cryptedKey != loginKey
			currInvalidAttempts = @parent.mysql.getInvalidLogins(client.username)
			currInvalidAttempts += 1
			@parent.mysql.updateInvalidLogins(client.username, currInvalidAttempts)
			return client.sendError(101)
		end
		client.sendData('%xt%js%-1%0%1%' + client.ranking['isStaff'].to_s + '%0%')
		client.sendData('%xt%lp%-1%' + client.buildClientString + '%' + client.coins.to_s + '%0%1440%100%' + client.joindate.to_s + '%4%' + client.joindate.to_s + '%%7%')
		client.sendData('%xt%gps%-1%' + client.ID.to_s + '%' + client.stamps.join('|') + '%')
		@parent.mysql.updateLoginKey("", client.username)
		client.checkPuffleStats
		client.joinRoom(100)
	end
	
	def handleJoinPlayer(gameHandlerArgs, client)
		room = gameHandlerArgs[0]
		if room < 1000
			room += 1000
		end
		client.sendData('%xt%jp%-%' + room.to_s + '%')
		client.joinRoom(room)
	end
	
	def handleJoinGame(gameHandlerArgs, client)
		room = gameHandlerArgs[0]
		client.joinRoom(room)
	end
	
	def handleGetRoomSynced(gameHandlerArgs, client)
		client.sendData('%xt%grs%-1%' + client.room.to_s + '%' + client.buildClientString + '%')
	end
	
	def handleJoinRoom(gameHandlerArgs, client)
		room = gameHandlerArgs[0]
		room_x = gameHandlerArgs[1]
		room_y = gameHandlerArgs[2]
		if  room == 230 && defined? client.waddleRoom
			client.tableID = nil
			client.waddleRoom = nil
			client.waddleID = nil
			return client.joinRoom(room, room_x, room_y)
		end
		client.joinRoom(room, room_x, room_y)
	end
{'fraction_non_alphanumeric': 0.15293980401306578, 'fraction_numerical': 0.03581427904806346, 'mean_word_length': 4.391823899371069, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '6904976', 'n_tokens_mistral': 3641, 'n_tokens_neox': 3038, 'n_words': 594}