<commit_before>package nuitinfo2013

import grails.converters.JSON
import grails.plugin.springsecurity.SpringSecurityService;
import groovy.ui.ConsoleTextEditor.UpdateCaretListener;
import nuitinfo2013.User;

class ExchangeController {
	def springSecurityService

	def currentExchange=[];
	
  def index() { }
	
	def getExchange(User connectedUser){
		def exc = null;
		def res = UserManager.findByUser(connectedUser);
		UserManager current = res.get(0);
		
		if (current.getAvailable()){
			User match = algoMatch(connectedUser);
			exc = new Exchange(A: connectedUser,B: match,initial: new Date())
			
			// update states
			current.available = false;
			def res2 = UserManager.findByUser(match);
			UserManager user2 = res2.get(0);
			user2.available = false;
			
			// save
			exc.save();
			current.save();
			user2.save();
		}
		return exc;
	}
	
	def User algoMatch(User connectedUser){
		def exc;
		def res = UserManager.getAll();
		def bestMatch = null;
		def bestElo = -1;
		res.each {
			if (it.getAvailable()){
				def user = it.getUser();
				def product  = user.currentProduct;
				if (product.id != connectedUser.currentProduct.id){
					def elo = Rating.findByUserAndProduct(connectedUser,product).elo;
					if (elo>bestElo) bestMatch=user;
				}
			}
		}
		return bestMatch;
	}
	
	
	def reponse = {
		// Récuperation de l'utilisateur
		boolean isUserOne = false
		User otherUser
		Exchange exchange
		def answeringUser = User.get(springSecurityService.authentication.principal.id)
		// Récuperation de l'échange
		exchange = Exchange.findByFirstUserAndEnded(answeringUser, false)
		if(!exchange) {
			exchange = Exchange.findBySecondUserAndEnded(answeringUser, false)
			// TODO ERROR
			if(!exchange) {
				render(contentType: "text/json") {
					resultCode = "KO"
				}
			}
			else {
				otherUser = exchange.getFirstUser()
				isUserOne = false
			}
		}
		else {
			otherUser = exchange.getSecondUser()
			isUserOne = true
		}
		
		boolean reponse = params?.reponse
		if(isUserOne) {
			exchange.setFirstUserResponse(reponse)
		}
		else {
			exchange.setSecondUserResponse(reponse)
		}
		
		if(exchange.firstUserResponse != null && exchange.secondUserResponse != null){
			if(exchange.firstUserResponse != true && exchange.secondUserResponse != true){
				//points
				RatingAlgorithmController r 
				r.update(exchange)
				//exchange products
				Product tmp
				tmp =answeringUser.currentProduct
				answeringUser.currentProduct = otherUser.currentProduct
				otherUser.currentProduct = tmp
				//save
				answeringUser.save()
				otherUser.save()
			}else{
				//update points
				RatingAlgorithmController r
				r.update(exchange)
				//keep same object
				answeringUser.save()
				otherUser.save()
			}
			
		}
		
		render(contentType: "text/json") {
				resultCode = "OK"
		}
	}
	
	def state = {
		// Récuperation de l'utilisateur
		boolean isUserOne = false
		User otherUser
		Exchange exchange
		def answeringUser = User.get(springSecurityService.authentication.principal.id)
		// Wait end timer
		
		
		// Récuperation de l'échange
		String statusTmp
		int remainingExchangeTmp
		exchange = Exchange.findByFirstUser(answeringUser)
		if(!exchange) {
			exchange = Exchange.findBySecondUser(answeringUser)
			if(!exchange) {
				render(contentType: "text/json") {
					resultCode = "KO"
				}
			}
		}
		// Send exchange
		RatingAlgorithmController rating
		rating.update(exchange)		
		
		if(exchange.firstUserResponse() == true && exchange.secondUserResponse() == true) {
			statusTmp = "VALIDATE"
		}
		else {
			statusTmp = "DENIED"
		}
		remainingExchangeTmp = answeringUser.exchangeRemaining
		// TODO REMOVE
		exchange.remove()
		
		render(contentType: "text/json") {
				resultCode = "OK"
				status = statusTmp
				remainingExchange = remainingExchangeTmp
		}
	}
	
	def newExchange () {
		// Récuperation de l'utilisateur
		boolean estFirstUser = false
		def answeringUser = User.get(springSecurityService.authentication.principal.id)
		
		UserManager.findByUser(answeringUser).available = true
		
		Exchange exchange = getExchange(answeringUser)
		
		def estFirstUser = (exchange.firstUser == answeringUser)
		
		if(estFirstUser) {
			render(contentType: "text/json") {
				resultCode = "OK"
				myProduct = {
					name = exchange.firstUser.currentProduct.name
					descriptif = exchange.firstUser.currentProduct.name 
				}
				yourProduct = {
					name = exchange.secondUser.currentProduct.name
					descriptif = exchange.secondUser.currentProduct.name
				}
			}
		}
		else {
			render(contentType: "text/json") {
				resultCode = "OK"
				yourProduct = {
					name = exchange.firstUser.currentProduct.name
					descriptif = exchange.firstUser.currentProduct.name
				}
				myProduct = {
					name = exchange.secondUser.currentProduct.name<commit_msg>Repair mistake in newExchange<commit_after>package nuitinfo2013

import grails.converters.JSON
import grails.plugin.springsecurity.SpringSecurityService;
import groovy.ui.ConsoleTextEditor.UpdateCaretListener;
import nuitinfo2013.User;

class ExchangeController {
	def springSecurityService

	def currentExchange=[];
	
  def index() { }
	
	def getExchange(User connectedUser){
		def exc = null;
		def res = UserManager.findByUser(connectedUser);
		UserManager current = res.get(0);
		
		if (current.getAvailable()){
			User match = algoMatch(connectedUser);
			exc = new Exchange(A: connectedUser,B: match,initial: new Date())
			
			// update states
			current.available = false;
			def res2 = UserManager.findByUser(match);
			UserManager user2 = res2.get(0);
			user2.available = false;
			
			// save
			exc.save();
			current.save();
			user2.save();
		}
		return exc;
	}
	
	def User algoMatch(User connectedUser){
		def exc;
		def res = UserManager.getAll();
		def bestMatch = null;
		def bestElo = -1;
		res.each {
			if (it.getAvailable()){
				def user = it.getUser();
				def product  = user.currentProduct;
				if (product.id != connectedUser.currentProduct.id){
					def elo = Rating.findByUserAndProduct(connectedUser,product).elo;
					if (elo>bestElo) bestMatch=user;
				}
			}
		}
		return bestMatch;
	}
	
	
	def reponse = {
		// Récuperation de l'utilisateur
		boolean isUserOne = false
		User otherUser
		Exchange exchange
		def answeringUser = User.get(springSecurityService.authentication.principal.id)
		// Récuperation de l'échange
		exchange = Exchange.findByFirstUserAndEnded(answeringUser, false)
		if(!exchange) {
			exchange = Exchange.findBySecondUserAndEnded(answeringUser, false)
			// TODO ERROR
			if(!exchange) {
				render(contentType: "text/json") {
					resultCode = "KO"
				}
			}
			else {
				otherUser = exchange.getFirstUser()
				isUserOne = false
			}
		}
		else {
			otherUser = exchange.getSecondUser()
			isUserOne = true
		}
		
		boolean reponse = params?.reponse
		if(isUserOne) {
			exchange.setFirstUserResponse(reponse)
		}
		else {
			exchange.setSecondUserResponse(reponse)
		}
		
		if(exchange.firstUserResponse != null && exchange.secondUserResponse != null){
			if(exchange.firstUserResponse != true && exchange.secondUserResponse != true){
				//points
				RatingAlgorithmController r 
				r.update(exchange)
				//exchange products
				Product tmp
				tmp =answeringUser.currentProduct
				answeringUser.currentProduct = otherUser.currentProduct
				otherUser.currentProduct = tmp
				//save
				answeringUser.save()
				otherUser.save()
			}else{
				//update points
				RatingAlgorithmController r
				r.update(exchange)
				//keep same object
				answeringUser.save()
				otherUser.save()
			}
			
		}
		
		render(contentType: "text/json") {
				resultCode = "OK"
		}
	}
	
	def state = {
		// Récuperation de l'utilisateur
		boolean isUserOne = false
		User otherUser
		Exchange exchange
		def answeringUser = User.get(springSecurityService.authentication.principal.id)
		// Wait end timer
		
		
		// Récuperation de l'échange
		String statusTmp
		int remainingExchangeTmp
		exchange = Exchange.findByFirstUser(answeringUser)
		if(!exchange) {
			exchange = Exchange.findBySecondUser(answeringUser)
			if(!exchange) {
				render(contentType: "text/json") {
					resultCode = "KO"
				}
			}
		}
		// Send exchange
		RatingAlgorithmController rating
		rating.update(exchange)		
		
		if(exchange.firstUserResponse() == true && exchange.secondUserResponse() == true) {
			statusTmp = "VALIDATE"
		}
		else {
			statusTmp = "DENIED"
		}
		remainingExchangeTmp = answeringUser.exchangeRemaining
		// TODO REMOVE
		exchange.remove()
		
		render(contentType: "text/json") {
				resultCode = "OK"
				status = statusTmp
				remainingExchange = remainingExchangeTmp
		}
	}
	
	def newExchange () {
		// Récuperation de l'utilisateur
		boolean isFirstUser = false
		def answeringUser = User.get(springSecurityService.authentication.principal.id)
		
		UserManager.findByUser(answeringUser).available = true
		
		Exchange exchange = getExchange(answeringUser)
		
		isFirstUser = (exchange.firstUser == answeringUser)
		
		if(isFirstUser) {
			render(contentType: "text/json") {
				resultCode = "OK"
				myProduct = {
					name = exchange.firstUser.currentProduct.name
					descriptif = exchange.firstUser.currentProduct.name 
				}
				yourProduct = {
					name = exchange.secondUser.currentProduct.name
					descriptif = exchange.secondUser.currentProduct.name
				}
			}
		}
		else {
			render(contentType: "text/json") {
				resultCode = "OK"
				yourProduct = {
					name = exchange.firstUser.currentProduct.name
					descriptif = exchange.firstUser.currentProduct.name
				}
				myProduct = {
					name = exchange.secondUser.currentProduct.name
{'fraction_non_alphanumeric': 0.10217912626420603, 'fraction_numerical': 0.0033364612657699926, 'mean_word_length': 3.3659535730541648, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '255845', 'n_tokens_mistral': 3770, 'n_tokens_neox': 2964, 'n_words': 589}