Correction du classement #13

Merged
nemunaire merged 4 commits from fix/classement into master 2021-01-01 20:07:03 +00:00
7 changed files with 96 additions and 48 deletions

View File

@ -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';` 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 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). 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).

16
cron/classement.php Normal file
View File

@ -0,0 +1,16 @@
<?php
// Ce cron met à jour le classement des joueurs.
//Définition de la constante anti-hacking
define("INDEX", 1);
define("DEBUG", false);
//Inclusion de l'API Onyx
require_once(__DIR__ . "/../onyx2/load.php");
require_once("common.php");
$bdd = new BDD();
$updated = $bdd->update_classement();
$bdd->deconnexion();

View File

@ -28,6 +28,7 @@ services:
volumes: volumes:
- ./htdocs:/usr/src/hb-main/htdocs:rw - ./htdocs:/usr/src/hb-main/htdocs:rw
- ./onyx2:/usr/src/hb-main/onyx2: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 # make cache and compile writable + don't add file to local host
- /usr/src/hb-main/onyx2/cache - /usr/src/hb-main/onyx2/cache
- /usr/src/hb-main/onyx2/log - /usr/src/hb-main/onyx2/log

View File

@ -113,6 +113,7 @@ class User
public function addPoints($metal, $cristal, $hydrogene, $credits = 0, $demolition = false) 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; global $table_bourse_ressources;
//On charge les 3 valeurs boursières //On charge les 3 valeurs boursières
$bdd = new BDD(); $bdd = new BDD();
@ -129,6 +130,11 @@ class User
$points = bourse_calcPrixBase($bourse[0]["dispo"], $metal); $points = bourse_calcPrixBase($bourse[0]["dispo"], $metal);
$points += bourse_calcPrixBase($bourse[1]["dispo"], $cristal); $points += bourse_calcPrixBase($bourse[1]["dispo"], $cristal);
$points += bourse_calcPrixBase($bourse[2]["dispo"], $hydrogene); $points += bourse_calcPrixBase($bourse[2]["dispo"], $hydrogene);
*/
$rate = 0.7;
$points = $metal *$rate;
$points += $cristal *$rate;
$points += $hydrogene *$rate;
if ($demolition) { if ($demolition) {
$this->points -= intval($points); $this->points -= intval($points);

View File

@ -50,7 +50,9 @@ if ($cached['check'] == md5_file(ONYX.'config/root.xml').md5_file(ONYX.'modules/
//$xml->validate(); //$xml->validate();
if ($root = $xml->documentElement->getAttribute('root')) { 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); define('ROOT', $root);
$search = substr(FILE, strlen($root)); $search = substr(FILE, strlen($root));

View File

@ -193,4 +193,14 @@ class BDD
return mysqli_affected_rows($this->session); 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);
}
} }

View File

@ -3,74 +3,74 @@
class BDD class BDD
{ {
public $connected; public $connected;
private $session; private $session;
private $reponse; private $reponse;
public $host; public $host;
public $user; public $user;
private $password; private $password;
public $database; public $database;
public $num_rows; public $num_rows;
public $nodb; public $nodb;
public function __construct($profile=null) public function __construct($profile=null)
{ {
if ($profile === false) { if ($profile === false) {
return false; return false;
} }
global $db_config; global $db_config;
if (empty($profile)) { if (empty($profile)) {
if (!$db_config['profile']) { if (!$db_config['profile']) {
return false; return false;
} }
$profile = &$db_config['profile']; $profile = &$db_config['profile'];
} }
if (!ctype_alnum($profile)) { if (!ctype_alnum($profile)) {
trigger_error('Le nom du profil contient des caracteres illegaux', E_USER_ERROR); trigger_error('Le nom du profil contient des caracteres illegaux', E_USER_ERROR);
} }
if ($db_config['profile']) { if ($db_config['profile']) {
require(ONYX.'db/'.$profile.'.profile.php'); require(ONYX.'db/'.$profile.'.profile.php');
$db = &$___profile['db']; $db = &$___profile['db'];
$host = &$___profile['host']; $host = &$___profile['host'];
$user = &$___profile['user']; $user = &$___profile['user'];
$pass = &$___profile['pass']; $pass = &$___profile['pass'];
} }
if ($db_config['crypt']) { if ($db_config['crypt']) {
$pass = dbpass($pass, $db_config['crypt']); $pass = dbpass($pass, $db_config['crypt']);
} }
return $this->connexion($host, $user, $pass, $db); return $this->connexion($host, $user, $pass, $db);
} }
public function connexion($host, $user, $pass, $db=null) public function connexion($host, $user, $pass, $db=null)
{ {
if ($this->session) { if ($this->session) {
$this->deconnexion(); $this->deconnexion();
} }
$this->reponse = null; $this->reponse = null;
$host = pg_escape_string($host); $host = pg_escape_string($host);
$user = pg_escape_string($user); $user = pg_escape_string($user);
$pass = pg_escape_string($pass); $pass = pg_escape_string($pass);
$db = pg_escape_string($db); $db = pg_escape_string($db);
$this->session = pg_connect("host='$host' port=5432 dbname='$db' user='$user' password='$pass'"); $this->session = pg_connect("host='$host' port=5432 dbname='$db' user='$user' password='$pass'");
if (!$this->session) { if (!$this->session) {
elog('Connexion impossible a la base de donnee : '.$this->erreur(), 2); elog('Connexion impossible a la base de donnee : '.$this->erreur(), 2);
if (function_exists($this->nodb)) { if (function_exists($this->nodb)) {
@ -78,54 +78,54 @@ class BDD
} }
return false; return false;
} }
pg_setclientencoding($this->session, 'UTF8'); pg_setclientencoding($this->session, 'UTF8');
$this->host = $host; $this->host = $host;
$this->user = $user; $this->user = $user;
$this->password = $pass; $this->password = $pass;
$this->database = $db; $this->database = $db;
$this->connected = true; $this->connected = true;
} }
public function reconnexion() public function reconnexion()
{ {
if (!empty($this->host) && !empty($this->user) && !empty($this->password) && !empty($this->database)) { if (!empty($this->host) && !empty($this->user) && !empty($this->password) && !empty($this->database)) {
$this->connexion($this->host, $this->user, $this->password, $this->database); $this->connexion($this->host, $this->user, $this->password, $this->database);
} }
} }
public function deconnexion() public function deconnexion()
{ {
if (!$this->session) { if (!$this->session) {
return false; return false;
} }
$r = pg_close($this->session); $r = pg_close($this->session);
$this->session = false; $this->session = false;
$this->connected = false; $this->connected = false;
return $r; return $r;
} }
public function erreur() public function erreur()
{ {
if (!$this->session) { if (!$this->session) {
return false; return false;
} }
return pg_last_error($this->session); return pg_last_error($this->session);
} }
public function db($db) public function db($db)
{ {
if (!$this->session) { if (!$this->session) {
return false; return false;
} }
return $this->database = pg_query($this->session, "\\connect ".pg_escape_string($db)) ? $db : $this->database; return $this->database = pg_query($this->session, "\\connect ".pg_escape_string($db)) ? $db : $this->database;
} }
public function escape(&$var) public function escape(&$var)
{ {
if (!$this->session) { if (!$this->session) {
@ -134,23 +134,23 @@ class BDD
$var = pg_escape_string($this->session, $var); $var = pg_escape_string($this->session, $var);
return $var; return $var;
} }
public function query($query) public function query($query)
{ {
if (!$this->session) { if (!$this->session) {
return false; return false;
} }
$this->reponse = pg_query($this->session, $query); $this->reponse = pg_query($this->session, $query);
global $db_config; global $db_config;
if (!$this->reponse && $db_config['log']) { if (!$this->reponse && $db_config['log']) {
elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requète: { '.$query.' }.', 1); elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requète: { '.$query.' }.', 1);
} }
$this->num_rows = pg_num_rows($this->reponse); $this->num_rows = pg_num_rows($this->reponse);
if ($this->num_rows == 0) { if ($this->num_rows == 0) {
return null; return null;
} elseif ($this->num_rows >= 1) { } elseif ($this->num_rows >= 1) {
@ -162,23 +162,23 @@ class BDD
return false; return false;
} }
} }
public function unique_query($query) public function unique_query($query)
{ {
if (!$this->session) { if (!$this->session) {
return false; return false;
} }
$this->reponse = pg_query($this->session, $query); $this->reponse = pg_query($this->session, $query);
global $db_config; global $db_config;
if (!$this->reponse && $db_config['log']) { if (!$this->reponse && $db_config['log']) {
elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requète: { '.$query.' }.', 1); elog('Erreur PostgreSQL: " '.$this->erreur().' ", avec la requète: { '.$query.' }.', 1);
} }
$this->num_rows = pg_num_rows($this->reponse); $this->num_rows = pg_num_rows($this->reponse);
if ($this->num_rows == 0) { if ($this->num_rows == 0) {
return null; return null;
} elseif ($this->num_rows >= 1) { } elseif ($this->num_rows >= 1) {
@ -187,13 +187,23 @@ class BDD
return false; return false;
} }
} }
public function affected() public function affected()
{ {
if (!$this->session) { if (!$this->session) {
return false; return false;
} }
return pg_affected_rows($this->reponse); 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);
}
} }