code fix
This commit is contained in:
261
backend/app.js
261
backend/app.js
@@ -88,7 +88,7 @@ function storeToken(token) {
|
||||
try {
|
||||
fs.mkdirSync(TOKEN_DIR);
|
||||
} catch (err) {
|
||||
if (err.code != 'EEXIST') {
|
||||
if (err.code !== 'EEXIST') {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -152,7 +152,7 @@ function getAvailableNames(callback){
|
||||
try{
|
||||
let name = result.valueRanges[0].values[i][0];
|
||||
let available = result.valueRanges[1].values[i][0];
|
||||
if (name && (available=='x')){
|
||||
if (name && (available==='x')){
|
||||
names.push(name);
|
||||
}
|
||||
}catch(err){
|
||||
@@ -164,228 +164,6 @@ function getAvailableNames(callback){
|
||||
});
|
||||
}
|
||||
|
||||
function getRandomInt(min, max) {
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
}
|
||||
|
||||
function MakePairs(callback){
|
||||
executeAPI(getAvailableNames,function(names){
|
||||
executeAPI(getPairs, function(pairs){
|
||||
const numOfPairs = Math.floor(names.length / 2);
|
||||
const left_side = [];
|
||||
const right_side = [];
|
||||
|
||||
const newPairs = [];
|
||||
|
||||
for (let i=0;i<numOfPairs;i++){
|
||||
|
||||
let id1 = getRandomInt(0,names.length-1);
|
||||
left_side.push(names[id1]);
|
||||
names.splice(id1,1);
|
||||
let id2 = getRandomInt(0,names.length-1);
|
||||
right_side.push(names[id2]);
|
||||
names.splice(id2,1);
|
||||
}
|
||||
|
||||
//make pairs using both sides
|
||||
|
||||
for (let i=0;i<left_side.length;i++){
|
||||
if (i==left_side.length) break;
|
||||
for (let j=0;j<right_side.length;j++){
|
||||
let tmpName1 = left_side[i];
|
||||
let tmpName2 = right_side[j];
|
||||
let exist=false;
|
||||
pairs.filter((pair)=>{
|
||||
if (((pair.name1 == tmpName1)&&(pair.name2==tmpName2))||((pair.name1==tmpName2)&&(pair.name2==tmpName1))){
|
||||
exist = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!exist){
|
||||
newPairs.push({name1:tmpName1, name2:tmpName2});
|
||||
left_side.splice(i,1);
|
||||
right_side.splice(j,1);
|
||||
i=-1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//make pairs from names on left side
|
||||
for (let i=0;i<left_side.length;i++){
|
||||
for (let j=0;j<left_side.length;j++){
|
||||
if (i==j) continue;
|
||||
let tmpName1 = left_side[i];
|
||||
let tmpName2 = left_side[j];
|
||||
let exist=false;
|
||||
pairs.filter((pair)=>{
|
||||
if (((pair.name1 == tmpName1)&&(pair.name2==tmpName2))||((pair.name1==tmpName2)&&(pair.name2==tmpName1))){
|
||||
exist = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!exist){
|
||||
newPairs.push({name1:tmpName1, name2:tmpName2});
|
||||
left_side.splice(i,1);
|
||||
if (i>j)
|
||||
left_side.splice(j,1);
|
||||
else
|
||||
left_side.splice(j-1,1);
|
||||
i=-1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//make pairs from names on right side
|
||||
for (let i=0;i<right_side.length;i++){
|
||||
for (let j=0;j<right_side.length;j++){
|
||||
if (i==j) continue;
|
||||
let tmpName1 = right_side[i];
|
||||
let tmpName2 = right_side[j];
|
||||
let exist=false;
|
||||
pairs.filter((pair)=>{
|
||||
if (((pair.name1 == tmpName1)&&(pair.name2==tmpName2))||((pair.name1==tmpName2)&&(pair.name2==tmpName1))){
|
||||
exist = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!exist){
|
||||
newPairs.push({name1:tmpName1, name2:tmpName2});
|
||||
right_side.splice(i,1);
|
||||
if (i>j)
|
||||
right_side.splice(j,1);
|
||||
else
|
||||
right_side.splice(j-1,1);
|
||||
i=-1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//try to make pair using unused name from list of names and names on left side
|
||||
if (names.length>0){
|
||||
let tmpName1 = names[0];
|
||||
for (let i=0;i<left_side.length;i++){
|
||||
let tmpName2 = left_side[i];
|
||||
let exist=false;
|
||||
pairs.filter((pair)=>{
|
||||
if (((pair.name1 == tmpName1)&&(pair.name2==tmpName2))||((pair.name1==tmpName2)&&(pair.name2==tmpName1))){
|
||||
exist = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!exist){
|
||||
newPairs.push({name1:tmpName1, name2:tmpName2});
|
||||
names=[];
|
||||
left_side.splice(i,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//try to make pair using unused name from list of names and names on right side
|
||||
if (names.length>0){
|
||||
let tmpName1 = names[0];
|
||||
for (let i=0;i<right_side.length;i++){
|
||||
let tmpName2 = right_side[i];
|
||||
let exist=false;
|
||||
pairs.filter((pair)=>{
|
||||
if (((pair.name1 == tmpName1)&&(pair.name2==tmpName2))||((pair.name1==tmpName2)&&(pair.name2==tmpName1))){
|
||||
exist = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!exist){
|
||||
newPairs.push({name1:tmpName1, name2:tmpName2});
|
||||
names=[];
|
||||
right_side.splice(i,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pairsForSave = newPairs;
|
||||
|
||||
let withoutPair = [];
|
||||
for (let i=0;i<left_side.length;i++)
|
||||
withoutPair.push(left_side[i]);
|
||||
for (let i=0;i<right_side.length;i++)
|
||||
withoutPair.push(right_side[i]);
|
||||
if (names.length>0)
|
||||
withoutPair.push(names[0]);
|
||||
|
||||
if (withoutPair.length==0) withoutPair=null;
|
||||
|
||||
callback.send({pairs: newPairs, left: withoutPair });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function MakePairsV2(callback){
|
||||
executeAPI(getAvailableNames, function(names){
|
||||
executeAPI(getPairs, function(pairs){
|
||||
const AllPairs = [];
|
||||
const BatchPairs = [];
|
||||
|
||||
for(let i=0;i<names.length;i++){
|
||||
for (let j=i+1;j<names.length;j++){
|
||||
let exist = false;
|
||||
pairs.filter((pair)=>{
|
||||
if (((pair.name1 == names[i])&&(pair.name2 == names[j]))||((pair.name1==names[j])&&(pair.name2==names[i]))){
|
||||
exist=true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!exist){
|
||||
AllPairs.push({name1: names[i], name2: names[j]});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while(AllPairs.length>0){
|
||||
|
||||
BatchPairs.push(AllPairs[0]);
|
||||
|
||||
let tmpNames = [];
|
||||
let indexToRemove = [];
|
||||
|
||||
tmpNames.push(AllPairs[0].name1);
|
||||
tmpNames.push(AllPairs[0].name2);
|
||||
|
||||
for (let i=0;i<AllPairs.length;i++){
|
||||
if ((tmpNames.indexOf(AllPairs[i].name1)!=-1)||(tmpNames.indexOf(AllPairs[i].name2)!=-1)){
|
||||
indexToRemove.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(let i=indexToRemove.length-1; i>=0;i--){
|
||||
AllPairs.splice(indexToRemove[i],1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
for(let i=0;i<BatchPairs.length;i++){
|
||||
let index = names.indexOf(BatchPairs[i].name1);
|
||||
if (index!=-1)
|
||||
names.splice(index,1);
|
||||
index = names.indexOf(BatchPairs[i].name2);
|
||||
if (index!=-1)
|
||||
names.splice(index,1);
|
||||
}
|
||||
|
||||
if (names.length==0) names=null;
|
||||
|
||||
|
||||
pairsForSave=BatchPairs;
|
||||
|
||||
callback.send({pairs: BatchPairs, left:names});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var matrix = [];
|
||||
var tree = [];
|
||||
let done = false;
|
||||
@@ -395,19 +173,19 @@ function count_pairs(row, col, cntr){
|
||||
|
||||
matrix[row][col] = 2;
|
||||
|
||||
//oznaci zauzeta polja zbog izbora para
|
||||
//mark all fields in matrix related to row and col names
|
||||
for(let i=0;i<row;i++) matrix[row][i] = (matrix[row][i]===0)? cntr: matrix[row][i];
|
||||
for(let i=0;i<col;i++) matrix[col][i] = (matrix[col][i]===0)? cntr: matrix[col][i];
|
||||
|
||||
for(let i=row;i<matrix.length;i++) matrix[i][row] = (matrix[i][row]===0)? cntr: matrix[i][row];
|
||||
for(let i=col;i<matrix.length;i++) matrix[i][col] = (matrix[i][col]===0)? cntr: matrix[i][col];
|
||||
|
||||
//nadi sljedece slobodno
|
||||
//find next pair
|
||||
|
||||
let found = false;
|
||||
for (let i=0;i<matrix.length;i++){
|
||||
for (let j=0;j<=i;j++){
|
||||
if (matrix[i][j]==0){
|
||||
if (matrix[i][j]===0){
|
||||
count_pairs(i,j,cntr+1);
|
||||
found=true;
|
||||
}
|
||||
@@ -418,18 +196,18 @@ function count_pairs(row, col, cntr){
|
||||
let inner_tree = [];
|
||||
for (let i=0;i<matrix.length;i++){
|
||||
for (let j=0;j<=i;j++){
|
||||
if (matrix[i][j]==2){
|
||||
if (matrix[i][j]===2){
|
||||
inner_tree.push({row: i, col:j});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inner_tree.length == max_pairs) {
|
||||
if (inner_tree.length === max_pairs) {
|
||||
done=true;
|
||||
tree.push(inner_tree);
|
||||
}
|
||||
}
|
||||
|
||||
//skini oznake za zauzeta polja zbog izbora para
|
||||
//remove marks from matrix
|
||||
for(let i=0;i<row;i++) matrix[row][i] = (matrix[row][i]===cntr)? 0: matrix[row][i];
|
||||
for(let i=0;i<col;i++) matrix[col][i] = (matrix[col][i]===cntr)? 0: matrix[col][i];
|
||||
|
||||
@@ -447,17 +225,17 @@ function MakePairsV3(callback){
|
||||
|
||||
for (let i=0;i<names.length;i++){
|
||||
let matrix_row = [];
|
||||
for (let j=0;j<=i;j++) (i==j)? matrix_row.push(1):matrix_row.push(0);
|
||||
for (let j=0;j<=i;j++) (i===j)? matrix_row.push(1):matrix_row.push(0);
|
||||
matrix.push(matrix_row);
|
||||
}
|
||||
|
||||
//Oznaci postojece parove, stavljajuci 3 na polja u matrici
|
||||
//Mark existing pairs, put 3 in matrix fields
|
||||
pairs.filter((pair)=>{
|
||||
|
||||
let name1_id = names.indexOf(pair.name1);
|
||||
let name2_id = names.indexOf(pair.name2);
|
||||
|
||||
if (name1_id != -1 && name2_id != -1){
|
||||
if (name1_id !== -1 && name2_id !== -1){
|
||||
if (name1_id > name2_id){
|
||||
matrix[name1_id][name2_id] = 3;
|
||||
}else{
|
||||
@@ -480,12 +258,6 @@ function MakePairsV3(callback){
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
|
||||
/*
|
||||
for (let i=0;i<tree.length;i++){
|
||||
console.log(tree[i]);
|
||||
console.log("---");
|
||||
}*/
|
||||
|
||||
let max_count_index = 0;
|
||||
|
||||
@@ -515,7 +287,7 @@ function MakePairsV3(callback){
|
||||
names.splice(indexToRemove[i],1);
|
||||
}
|
||||
|
||||
if (names.length == 0) names=null;
|
||||
if (names.length === 0) names=null;
|
||||
pairsForSave = result_pairs;
|
||||
callback.send({pairs: result_pairs, left: names});
|
||||
});
|
||||
@@ -523,7 +295,7 @@ function MakePairsV3(callback){
|
||||
}
|
||||
|
||||
function SavePairs(callback){
|
||||
if (lastRow== null) {
|
||||
if (lastRow=== null) {
|
||||
callback.send({result:true});
|
||||
return;
|
||||
}
|
||||
@@ -540,7 +312,7 @@ function SavePairs(callback){
|
||||
|
||||
const data = [];
|
||||
|
||||
if (lastRow == 0) lastRow=-1;
|
||||
if (lastRow === 0) lastRow=-1;
|
||||
let range1= '2017!A'+(lastRow+2)+':A'+(lastRow+2+pairsForSave.length);
|
||||
let range2= '2017!B'+(lastRow+2)+':B'+(lastRow+2+pairsForSave.length);
|
||||
|
||||
@@ -593,13 +365,8 @@ app.get('/getPairs',(req,resp)=>{
|
||||
MakePairsV3(resp);
|
||||
});
|
||||
|
||||
app.get('/getAllPairs',(req,resp)=>{
|
||||
MakePairsV3(resp);
|
||||
});
|
||||
|
||||
app.get('/savePairs', (req,resp)=>{
|
||||
SavePairs(resp);
|
||||
//resp.send({result:true});
|
||||
});
|
||||
|
||||
app.listen(3005, function () {
|
||||
|
||||
Reference in New Issue
Block a user