From a61171bb17fbcb56509d5d959c3059052fdb9ee8 Mon Sep 17 00:00:00 2001
From: TonyFord <tonyford@strategy-investor.de>
Date: Fri, 28 Apr 2023 21:04:24 +0200
Subject: [PATCH] add voting feature

---
 html/42.php                         |   1 +
 html/_layouts/default.php           |  10 +-
 html/assets/css/custom.scss         |  12 +++
 html/assets/js/script.js            |  26 ++++-
 html/functions/db_methods.php       | 142 +++++++++++++++++++---------
 html/functions/get_managervoted.php |   4 +
 html/vote.php                       |  22 +++++
 7 files changed, 166 insertions(+), 51 deletions(-)
 create mode 100644 html/functions/get_managervoted.php
 create mode 100644 html/vote.php

diff --git a/html/42.php b/html/42.php
index 8014158..2519d83 100644
--- a/html/42.php
+++ b/html/42.php
@@ -6,6 +6,7 @@ ligaspieltag: 5
 step: 1
 finals_step: 2
 finalteams: 8
+managervoting: false
 startliste:
   -
     - fr
diff --git a/html/_layouts/default.php b/html/_layouts/default.php
index 60ccfd5..723c4cb 100644
--- a/html/_layouts/default.php
+++ b/html/_layouts/default.php
@@ -55,7 +55,7 @@ if( !empty($csv) ){
   <meta http-equiv="pragma" content="no-cache" />
   <meta http-equiv="expires" content="0" />
   <link rel="stylesheet" href="assets/css/all.min.css">
-  <link rel="stylesheet" href="assets/css/custom.css">
+  <link rel="stylesheet" href="assets/css/custom.css?2">
   <script src="assets/js/jquery.min.js"></script>
   <script src="assets/js/utils.js"></script>
   <script src="assets/js/script.js"></script>
@@ -68,6 +68,9 @@ if( !empty($csv) ){
     var DATA={{ site.data | jsonify }};
     var SAISON='{{ page.saison }}';
     var SAISON_NOW='{{ site.data.global.saison_now }}';
+    var MANAGERVOTING={{ page.managervoting | default: false }};
+    var SAISON_TEAMVOTED=json_load('functions/get_managervoted.php?saison='+SAISON);
+    SAISON_TEAMVOTED.push([]);
 
     var TEAMS=json_load('functions/get_teams.php');
     var T2ID={};
@@ -342,7 +345,6 @@ if( !empty($csv) ){
       {% endfor %}
     </div>
     <h4 class="tabelle">Tabelle <button>Zweikampf</button><button>Assists</button><button>Tore</button><button class="punktestand active">Punktestand</button></h2>
-
     <table id="tabelle_P" class="tabelle">
       <thead>
         <tr>
@@ -461,6 +463,4 @@ if( !empty($csv) ){
     </table>
   </div>
 
-</body>
-
-</html>
+</body>
\ No newline at end of file
diff --git a/html/assets/css/custom.scss b/html/assets/css/custom.scss
index 784671c..623f327 100644
--- a/html/assets/css/custom.scss
+++ b/html/assets/css/custom.scss
@@ -286,6 +286,8 @@ td.n { color: firebrick; }
 .fa-crown { color: goldenrod; }
 .fa-trophy { color: goldenrod; }
 .fa-skull { color: #a42; }
+.fa-user-md { color: rgb(51, 102, 153); }
+.fa-chess-king { color: rgb(21, 123, 162); }
 
 
 #saisons {
@@ -430,3 +432,13 @@ i.fas {
 .tabelle img.flags {
   margin-right: 5px;
 }
+
+
+
+.fa-thumbs-up {
+  cursor: pointer;
+  color: grey;
+  &.done {
+    color: green;
+  }
+}
\ No newline at end of file
diff --git a/html/assets/js/script.js b/html/assets/js/script.js
index 59decdc..ce0f24f 100644
--- a/html/assets/js/script.js
+++ b/html/assets/js/script.js
@@ -452,7 +452,7 @@ function calculatePunktestand(saison,runde,show=true){
       var meistertitel=M[v.nt.toLowerCase()] ? M[v.nt.toLowerCase()] : 0;
 
       tmp+='<td class="'+border+'">'+S+'</td>';
-      tmp+='<td class="'+border+'"><img class="flags" src="./images/flags/'+(( v.flag ) ? v.flag : v.nt.toLowerCase())+'.svg"> <a href="javascript:calculateTeam('+((saison == 0 && false) ? SAISON_NOW : saison)+',\''+v.nt+'\')">'+v.name.replace(/\(NL\)/g,'<sup>'+v.nt.toUpperCase()+'</sup></a>')+' <a href="'+v.link+'" target="ultrasoccer"><i class="fas fa-link"></i></a>'+(( trophy.toLowerCase() == v.nt.toLowerCase() ) ? ' <i class="fas fa-trophy"></i>' : '') +
+      tmp+='<td class="'+border+'">'+(( MANAGERVOTING ) ? '<i class="fas fa-thumbs-up'+(( SAISON_TEAMVOTED.reduce((a,b) => (a.concat(b))).indexOf(v.nt) > -1 ) ? ' done" title="Manager dieses Teams hat sein Voting abgegeben!"' : '" title="Manager dieses Teams hat noch kein Voting abgegeben!"' )+' onclick="teamVoting(\''+v.nt+'\')"></i> ' : '') + '<img class="flags" src="./images/flags/'+(( v.flag ) ? v.flag : v.nt.toLowerCase())+'.svg"> <a href="javascript:calculateTeam('+((saison == 0 && false) ? SAISON_NOW : saison)+',\''+v.nt+'\')">'+v.name.replace(/\(NL\)/g,'<sup>'+v.nt.toUpperCase()+'</sup></a>')+' <a href="'+v.link+'" target="ultrasoccer"><i class="fas fa-link"></i></a>'+(( trophy.toLowerCase() == v.nt.toLowerCase() ) ? ' <i class="fas fa-trophy"></i>' : '') +
       ' '+(( master.toLowerCase() == v.nt.toLowerCase() ) ? ' <i class="fas fa-crown"></i>' : '') +'</td>';
       tmp+='<td class="'+border+'">'+((pokal>0) ? pokal : '')+'</td>';
       tmp+='<td class="'+border+'">'+((meistertitel>0) ? meistertitel : '')+'</td>';
@@ -845,3 +845,27 @@ function calculateMeistertitel(){
   return M;
 
 }
+
+
+function teamVoting(nt){
+  var team=TEAMS.filter((a) => (a.nt == nt && a.ende == 0))[0];
+  var cd=prompt('TeamVote für \n\n' + team.name + '\n\n -> Bitte AuthCode eingeben!');
+  $.ajax({
+    'type':"POST",
+    'async': false,
+    'url': 'vote.php',
+    'data' : { 'teamvote' : nt, 'secret' : cd },
+    'dataType': 'json',
+    'success': function (data) {
+        json = data;
+    },
+    'error': function (data) {
+      json = data;
+    }
+  });
+  if( json[3] ){
+    alert( 'Teamvoting erfolgreich gesetzt!' );
+  } else {
+    console.log( json );
+  }
+}
\ No newline at end of file
diff --git a/html/functions/db_methods.php b/html/functions/db_methods.php
index 56684bb..8ce585e 100644
--- a/html/functions/db_methods.php
+++ b/html/functions/db_methods.php
@@ -32,51 +32,6 @@ function db_addErgebnisse( $csv, $secret ){
   unset($db);
   return $result;
 }
-function db_addSpieler_old( $csv, $secret ){
-
-  $C=preg_split('/,/',$csv);
-  $C[1]='"'.$C[1].'"';
-  $C[2]='0x'.$C[2];
-  $C[3]='"'.$C[3].'"';
-  $C[4]='"'.$C[4].'"';
-  unset($C[6]);
-  unset($C[7]);
-  unset($C[8]);
-  unset($C[9]);
-  unset($C[10]);
-  unset($C[11]);
-  unset($C[12]);
-  unset($C[13]);
-
-  $C[]=SAISON_NOW;
-  $user=db_checkSecret($secret);
-  if( empty( $user ) ) return false;
-
-  $C[]=$user['id'];
-
-  if( $S=db_getFromSpielerById($C[2]) ){
-    $C[]='"'.$S['zeitstempel'].'"';
-  } else {
-    $C[]='NOW()';
-  }
-
-
-  $csv=implode(',',$C);
-
-  $db=new db;
-
-  $sql = "REPLACE INTO Spieler VALUES ( ".$csv." )";
-
-  if ( $db->query( $sql ) === TRUE) {
-      // success
-      $result=array( 'success' => true, 'message' => 'Spieler erfolgreich hinzugefügt!', 'data' => [] );
-  } else {
-      echo 'SQL-Error: '.$sql.'<br>';
-  }
-
-  unset($db);
-  return $result;
-}
 function db_addSpieler( $csv, $secret ){
 
   //CSV: SpielerID,Spieler,Alter,Nt,Saison,Liga,TeamID,Team,S,T,A,Z+,Z-,Zd,Bk,Bi+,Bi-,Attr,Face,Karriereende
@@ -181,6 +136,31 @@ function db_addStatistik( $csv, $secret ){
   unset($db);
   return $result;
 }
+function db_addTeamVote( $secret, $saison, $teamvote ){
+
+  $teams=db_checkTeamSecret($secret);
+  if( empty( $teams ) ) return false;
+  if( empty( $teams[0]['manager'] ) ) return false;
+  foreach( $teams as $team ){
+    if( $team['nt'] == $teamvote ) return false;
+  }
+
+  $manager=$teams[0]['manager'];
+
+  $db=new db;
+
+  $sql = "REPLACE INTO Voting ( manager, saison, teamvote ) VALUES ( '".$manager."',".$saison.",'".$teamvote."' )";
+
+  if ( $db->query( $sql ) === TRUE) {
+      // success
+      $result=array( 'success' => true, 'message' => 'Spieler erfolgreich hinzugefügt!', 'data' => [] );
+  } else {
+      echo 'SQL-Error: '.$sql.'<br>';
+  }
+
+  unset($db);
+  return $result;
+}
 
 function db_removeErgebnisse( $csv, $secret ){
 
@@ -221,6 +201,24 @@ function db_checkSecret($secret){
   unset($db);
   return $row;
 
+}
+function db_checkTeamSecret($secret){
+
+  $db=new db;
+
+  $sql = 'SELECT * FROM Teams WHERE secret="'.$secret.'" AND ende = 0';
+
+  $result = $db->query($sql);
+
+  $ROW=Array();
+
+  while( $row = $result->fetch_assoc() ){
+    $ROW[]=$row;
+  }
+
+  unset($db);
+  return $ROW;
+
 }
 function db_getFromTeams(){
 
@@ -252,6 +250,24 @@ function db_getFromTeamsById($teamID){
   return $row;
 
 }
+function db_getFromTeamsByManager($manager){
+
+  $db=new db;
+
+  $sql = 'SELECT nt FROM Teams WHERE manager="'.$manager.'"';
+  $result = $db->query($sql);
+
+  $ROW=Array();
+
+  while( $row = $result->fetch_assoc() ){
+    $ROW[]=$row['nt'];
+  }
+
+  unset($db);
+  return $ROW;
+
+}
+
 function db_getIdNtFromTeams(){
 
   $db=new db;
@@ -444,4 +460,40 @@ function db_getUnknownSpieler($teamID){
 
 }
 
+function db_getActiveManager($saison){
+
+  $db=new db;
+  $sql='SELECT manager FROM Teams WHERE start <= '.$saison.' AND ende = 0 AND manager != "Dummy" GROUP BY manager';
+
+  $result = $db->query($sql);
+
+  $ROW=Array();
+
+  while( $row = $result->fetch_assoc() ){
+    $ROW[]=$row;
+  }
+
+  unset($db);
+  return $ROW;
+
+}
+
+function db_getTeamvotedManagerBySaison($saison){
+  $db=new db;
+  $sql='SELECT manager FROM `Voting` WHERE saison='.$saison.' GROUP BY manager; ';
+
+  $result = $db->query($sql);
+
+  $ROW=Array();
+
+  while( $row = $result->fetch_assoc() ){
+    $ROW[]=db_getFromTeamsByManager($row['manager']);
+  }
+
+  unset($db);
+  return $ROW;
+  
+}
+
+
 ?>
diff --git a/html/functions/get_managervoted.php b/html/functions/get_managervoted.php
new file mode 100644
index 0000000..a00df9e
--- /dev/null
+++ b/html/functions/get_managervoted.php
@@ -0,0 +1,4 @@
+---
+layout: query
+---
+<?=json_encode( db_getTeamvotedManagerBySaison($_GET['saison']) )?>
diff --git a/html/vote.php b/html/vote.php
new file mode 100644
index 0000000..df0d496
--- /dev/null
+++ b/html/vote.php
@@ -0,0 +1,22 @@
+---
+---
+<?php
+
+include_once('functions/db_connect.php');
+include_once('functions/db_methods.php');
+
+
+//$M=db_getActiveManager({{ page.saison }});
+
+$secret=$_POST['secret'];
+$saison={{ site.data.global.saison_now }};
+$teamvote=$_POST['teamvote'];
+
+if( $saison*1 != {{ site.data.global.saison_now }}*1 ) exit;
+
+$result=db_addTeamVote( $secret, $saison, $teamvote );
+
+$A=[ $secret, $saison, $teamvote, $result ];
+
+echo json_encode($A);
+?>
\ No newline at end of file
-- 
GitLab