<commit_before>var uuid = require("uuid");
//var couchbase = require("couchbase");
//var N1qlQuery = require('couchbase').N1qlQuery;
var db = require("../app").db_connection;
var logdb = require("../app").logdb_connection;
var async = require("async");

function StateModel() { };

// ھ 
StateModel.saveScore = function(uid, score, callback) {

	var previousRank, currentRank;
	var isNewRecord = false;

	//   ߰ϴ Լ
	var addMyScore = function(callback) {
		db.query("INSERT INTO Score SET ?", { uid: uid, score: score }, function(err, result) {
			if(err) throw err;

			callback(null);
		});
	};

	//   ϴ Լ
	var updateMyScore = function(callback) {
		//   
		db.query("SELECT score FROM Score WHERE uid = ?", uid, function(err, result) {
			if(err) throw err;
			
			//     Ŭ (ű)
			if(result[0].score < score) {
				isNewRecord = true;
				// ο  
				db.query("UPDATE Score SET score = ? WHERE uid = ?", [score, uid], function(err, result) {
					if(err) throw err;

					callback(null);
				});
			} else {
				callback(null);
			}
		});
	};

	// DB   Լ
	var getScoreDataInDB = function(callback) {
		db.query("SELECT uid, score, (SELECT COUNT(*) + 1 FROM Score WHERE score > t.score) AS rank FROM Score AS t ORDER BY rank ASC;", function(err, result) {
			if(err) throw err;

			//   ְ  
			callback(null, result);
		});
	};

	// ŷ  Լ
	var getRank = function(scoreData, callback) {
		for(var i in scoreData) {
			if(scoreData[i].uid == uid) {
				// ŷ  ְ  
				callback(null, scoreData[i].rank);
			}
		}
	};

	//  Լ
	var final = function(err, result) {
		currentRank = result;
		// ó   ȭ Ƿ 0    ũ ־
		if(previousRank == null) {
			previousRank = currentRank;
			isNewRecord = true;
		}

		var r = {
            "result": true,
            
			"isNewRecord": isNewRecord, "rank": currentRank, "rankChange": previousRank - currentRank,
			
			"error": 0
		};

		// DB previous_rank  ־
		db.query("UPDATE User SET previous_rank = ? WHERE uid = ?", [currentRank, uid], function(err, result) {
			if(err) throw err;

			//  
			callback(r);
		});
	};
	// -------------------------------

	//  ŷ 
	db.query("SELECT previous_rank FROM User WHERE uid = ?", uid, function(err, result) {
		if(err) throw err;<commit_msg>스코어 로그 기록 안 되던 버그 수정<commit_after>var uuid = require("uuid");
//var couchbase = require("couchbase");
//var N1qlQuery = require('couchbase').N1qlQuery;
var db = require("../app").db_connection;
var logdb = require("../app").logdb_connection;
var async = require("async");

function StateModel() { };

// ھ 
StateModel.saveScore = function(uid, score, callback) {

	var previousRank, currentRank;
	var isNewRecord = false;

	//   ߰ϴ Լ
	var addMyScore = function(callback) {
		db.query("INSERT INTO Score SET ?", { uid: uid, score: score }, function(err, result) {
			if(err) throw err;

			callback(null);
		});
	};

	//   ϴ Լ
	var updateMyScore = function(callback) {
		//   
		db.query("SELECT score FROM Score WHERE uid = ?", uid, function(err, result) {
			if(err) throw err;
			
			//     Ŭ (ű)
			if(result[0].score < score) {
				isNewRecord = true;
				// ο  
				db.query("UPDATE Score SET score = ? WHERE uid = ?", [score, uid], function(err, result) {
					if(err) throw err;

					callback(null);
				});
			} else {
				callback(null);
			}
		});
	};

	// DB   Լ
	var getScoreDataInDB = function(callback) {
		db.query("SELECT uid, score, (SELECT COUNT(*) + 1 FROM Score WHERE score > t.score) AS rank FROM Score AS t ORDER BY rank ASC;", function(err, result) {
			if(err) throw err;

			//   ְ  
			callback(null, result);
		});
	};

	// ŷ  Լ
	var getRank = function(scoreData, callback) {
		for(var i in scoreData) {
			if(scoreData[i].uid == uid) {
				// ŷ  ְ  
				callback(null, scoreData[i].rank);
			}
		}
	};

	//  Լ
	var final = function(err, result) {
		currentRank = result;
		// ó   ȭ Ƿ 0    ũ ־
		if(previousRank == null) {
			previousRank = currentRank;
			isNewRecord = true;
		}

		// α׿ 
		var log = { uid: session.uid, score: score, get_date: new Date() };
		logdb.query("INSERT INTO Log_Score SET ?", log);

		var r = {
            "result": true,
            
			"isNewRecord": isNewRecord, "rank": currentRank, "rankChange": previousRank - currentRank,
			
			"error": 0
		};

		// DB previous_rank  ־
		db.query("UPDATE User SET previous_rank = ? WHERE uid = ?", [currentRank, uid], function(err, result) {
			if(err) throw err;

			//  
			callback(r);
		});
	};
	// -------------------------------

	//  ŷ 
	db.query("SELECT previous_rank FROM User WHERE uid = ?", uid, function(err, result) {
		if(err) throw err;
{'fraction_non_alphanumeric': 0.17192290845378888, 'fraction_numerical': 0.002628120893561104, 'mean_word_length': 2.5875883739198744, 'pattern_counts': {'":': 10, '<': 5, '<?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': '4138016', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1628, 'n_words': 424}