From 640d4bbec3f92b8c2e1ee422bd05ecdcebb3cbbe Mon Sep 17 00:00:00 2001 From: Nigel Sheldon Date: Fri, 1 Jan 2021 14:25:11 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Ajoute=20un=20syst=C3=A8me=20de=20cron=20po?= =?UTF-8?q?ur=20calculer=20le=20classement=20tous=20les=20jours,=20voir=20?= =?UTF-8?q?plus=20souvent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ cron/classement.php | 16 ++++++++++++++++ docker-compose.yml | 1 + onyx2/load.php | 4 +++- onyx2/modules/db/mysql.class.php | 10 ++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 cron/classement.php diff --git a/README.md b/README.md index 9b5071a..291969a 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,6 @@ vous connecter au serveur MySQL via `docker exec -it game_mariadb_1 mysql --user et récupérer votre jeton d'activation via `select id_activ from user_inscriptions WHERE pseudo LIKE 'VOTRE_PSEUDO_A_REMPLACER';` une fois l'ID récupérer, rendez-vous à l'adresse http://localhost:8080/?p=validation&i=VOTRE_ID (pensez bien à modifier l'id dans l'URL). + +## Installer +Un cron doit être rajouté sur la machine hôte pour calculer le classement des joueurs, en éxécutant le fichier `cron/classement.php` aussi souvent qu'il vous plaira (ou aussi souvent que votre CPU le permettra). diff --git a/cron/classement.php b/cron/classement.php new file mode 100644 index 0000000..6f938ec --- /dev/null +++ b/cron/classement.php @@ -0,0 +1,16 @@ +update_classement(); +$bdd->deconnexion(); diff --git a/docker-compose.yml b/docker-compose.yml index 1f8e5f5..9c34834 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,6 +28,7 @@ services: volumes: - ./htdocs:/usr/src/hb-main/htdocs:rw - ./onyx2:/usr/src/hb-main/onyx2:rw + - ./cron:/usr/src/hb-main/cron:rw # make cache and compile writable + don't add file to local host - /usr/src/hb-main/onyx2/cache - /usr/src/hb-main/onyx2/log diff --git a/onyx2/load.php b/onyx2/load.php index 536ada1..7a977a7 100644 --- a/onyx2/load.php +++ b/onyx2/load.php @@ -50,7 +50,9 @@ if ($cached['check'] == md5_file(ONYX.'config/root.xml').md5_file(ONYX.'modules/ //$xml->validate(); if ($root = $xml->documentElement->getAttribute('root')) { - if ($root == substr(FILE, 0, strlen($root))) { + // Autorise les fichiers dans les dossiers htdocs/ et cron/ + $cron = str_replace("htdocs", "cron", $root); + if ($root == substr(FILE, 0, strlen($root)) || $cron == substr(FILE, 0, strlen($cron))) { define('ROOT', $root); $search = substr(FILE, strlen($root)); diff --git a/onyx2/modules/db/mysql.class.php b/onyx2/modules/db/mysql.class.php index d355fc8..fedbc8a 100644 --- a/onyx2/modules/db/mysql.class.php +++ b/onyx2/modules/db/mysql.class.php @@ -193,4 +193,14 @@ class BDD return mysqli_affected_rows($this->session); } + + public function update_classement() + { + if (!$this->session) { + return false; + } + global $table_user; + $query = "SET @p=0; UPDATE $table_user SET place_points = (@p:=@p+1) ORDER BY points DESC;"; + mysqli_multi_query($this->session, $query); + } } -- 2.45.2 From 52e99ddea8d7da8a9e44c1b7ff6bb011e8750043 Mon Sep 17 00:00:00 2001 From: Nigel Sheldon Date: Fri, 1 Jan 2021 14:25:59 +0100 Subject: [PATCH 2/4] Lintage du fichier postgres.class.php pour la BDD --- onyx2/modules/db/postgresql.class.php | 94 +++++++++++++-------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/onyx2/modules/db/postgresql.class.php b/onyx2/modules/db/postgresql.class.php index 16cc9d0..6c800ea 100644 --- a/onyx2/modules/db/postgresql.class.php +++ b/onyx2/modules/db/postgresql.class.php @@ -3,74 +3,74 @@ class BDD { public $connected; - + private $session; - + private $reponse; - + public $host; - + public $user; - + private $password; - + public $database; - + public $num_rows; - + public $nodb; - - + + public function __construct($profile=null) { if ($profile === false) { return false; } - + global $db_config; - + if (empty($profile)) { if (!$db_config['profile']) { return false; } $profile = &$db_config['profile']; } - + if (!ctype_alnum($profile)) { trigger_error('Le nom du profil contient des caracteres illegaux', E_USER_ERROR); } - + if ($db_config['profile']) { require(ONYX.'db/'.$profile.'.profile.php'); - + $db = &$___profile['db']; $host = &$___profile['host']; $user = &$___profile['user']; $pass = &$___profile['pass']; } - + if ($db_config['crypt']) { $pass = dbpass($pass, $db_config['crypt']); } - + return $this->connexion($host, $user, $pass, $db); } - + public function connexion($host, $user, $pass, $db=null) { if ($this->session) { $this->deconnexion(); } - + $this->reponse = null; - + $host = pg_escape_string($host); $user = pg_escape_string($user); $pass = pg_escape_string($pass); $db = pg_escape_string($db); - + $this->session = pg_connect("host='$host' port=5432 dbname='$db' user='$user' password='$pass'"); - + if (!$this->session) { elog('Connexion impossible a la base de donnee : '.$this->erreur(), 2); if (function_exists($this->nodb)) { @@ -78,54 +78,54 @@ class BDD } return false; } - + pg_setclientencoding($this->session, 'UTF8'); - + $this->host = $host; $this->user = $user; $this->password = $pass; $this->database = $db; - + $this->connected = true; } - + public function reconnexion() { if (!empty($this->host) && !empty($this->user) && !empty($this->password) && !empty($this->database)) { $this->connexion($this->host, $this->user, $this->password, $this->database); } } - + public function deconnexion() { if (!$this->session) { return false; } - + $r = pg_close($this->session); $this->session = false; $this->connected = false; return $r; } - + public function erreur() { if (!$this->session) { return false; } - + return pg_last_error($this->session); } - + public function db($db) { if (!$this->session) { return false; } - + return $this->database = pg_query($this->session, "\\connect ".pg_escape_string($db)) ? $db : $this->database; } - + public function escape(&$var) { if (!$this->session) { @@ -134,23 +134,23 @@ class BDD $var = pg_escape_string($this->session, $var); return $var; } - + public function query($query) { if (!$this->session) { return false; } - + $this->reponse = pg_query($this->session, $query); - + global $db_config; - + if (!$this->reponse && $db_config['log']) { elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requète: { '.$query.' }.', 1); } - + $this->num_rows = pg_num_rows($this->reponse); - + if ($this->num_rows == 0) { return null; } elseif ($this->num_rows >= 1) { @@ -162,23 +162,23 @@ class BDD return false; } } - + public function unique_query($query) { if (!$this->session) { return false; } - + $this->reponse = pg_query($this->session, $query); - + global $db_config; - + if (!$this->reponse && $db_config['log']) { elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requète: { '.$query.' }.', 1); } - + $this->num_rows = pg_num_rows($this->reponse); - + if ($this->num_rows == 0) { return null; } elseif ($this->num_rows >= 1) { @@ -187,13 +187,13 @@ class BDD return false; } } - + public function affected() { if (!$this->session) { return false; } - + return pg_affected_rows($this->reponse); } } -- 2.45.2 From 4cda71a3375b47a4e33f9a1b905244be93058ece Mon Sep 17 00:00:00 2001 From: Nigel Sheldon Date: Fri, 1 Jan 2021 14:26:24 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Ajout=20du=20code=20pour=20mettre=20=C3=A0?= =?UTF-8?q?=20jour=20le=20classement=20via=20postgresql=20aussi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- onyx2/modules/db/postgresql.class.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/onyx2/modules/db/postgresql.class.php b/onyx2/modules/db/postgresql.class.php index 6c800ea..0d13051 100644 --- a/onyx2/modules/db/postgresql.class.php +++ b/onyx2/modules/db/postgresql.class.php @@ -196,4 +196,14 @@ class BDD return pg_affected_rows($this->reponse); } + + public function update_classement() + { + if (!$this->session) { + return false; + } + global $table_user; + $query = "UPDATE $table_user SET place_points=subquery.rn FROM (SELECT id, points, place_points, row_number() over (order by points DESC) as rn FROM $table_user) AS subquery WHERE $table_user.id=subquery.id;"; + pg_query($this->session, $query); + } } -- 2.45.2 From afa97638fa5840533d02d283e482cd8b79d34d44 Mon Sep 17 00:00:00 2001 From: Nigel Sheldon Date: Fri, 1 Jan 2021 14:27:52 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Le=20calcul=20des=20points=20c'=C3=A9tait?= =?UTF-8?q?=20n'importe=20quoi.=20J'ai=20simplifi=C3=A9=20c'est=20d=C3=A9s?= =?UTF-8?q?ormais:=20le=20nombre=20de=20ressources=20consomm=C3=A9es=20x?= =?UTF-8?q?=200.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- onyx2/include/Class/user.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/onyx2/include/Class/user.php b/onyx2/include/Class/user.php index 6571675..4492209 100644 --- a/onyx2/include/Class/user.php +++ b/onyx2/include/Class/user.php @@ -113,6 +113,7 @@ class User public function addPoints($metal, $cristal, $hydrogene, $credits = 0, $demolition = false) { + /* Ancien code, fait des calculs boursier dans le système de points, wtf ? global $table_bourse_ressources; //On charge les 3 valeurs boursières $bdd = new BDD(); @@ -129,6 +130,11 @@ class User $points = bourse_calcPrixBase($bourse[0]["dispo"], $metal); $points += bourse_calcPrixBase($bourse[1]["dispo"], $cristal); $points += bourse_calcPrixBase($bourse[2]["dispo"], $hydrogene); + */ + $rate = 0.7; + $points = $metal *$rate; + $points += $cristal *$rate; + $points += $hydrogene *$rate; if ($demolition) { $this->points -= intval($points); -- 2.45.2