01-08-2018, 02:24 AM
(Modification du message : 01-08-2018, 11:52 AM par Sephi-Chan.)
Une proposition de système alternatif simple tel que proposé plus haut (c'est codé de tête et j'ai pas écrit de PHP depuis la nuit des temps).
Note : C'est la merde, les balises code ne marchent plus.
<?php
define("DEFAULT_HEALTH", 100);
function try_to_start_a_fight() {
$results = fetch(
"SELECT level, COUNT(*) AS count, GROUP_CONCAT(id) AS player_ids
FROM players
WHERE looking_for_a_fight = 1
GROUP BY level
HAVING count = 6;"
);
if (empty($results)) {
return null;
}
else {
$level = $results[0]['level'];
$player_ids = $results[0]['player_ids'];
$game_id = create_game($level, $player_ids);
return $game_id;
}
}
function create_game($level, $player_ids) {
$game_id = insert("INSERT INTO games (level) VALUES(?)", $level);
players_stops_looking_for_a_fight($player_ids);
foreach ($player_ids as $player_id) {
insert(
"INSERT INTO fighters (game_id, player_id, health, max_health)
VALUES (?, ?, ?, ?)",
$game_id,
$player_id,
DEFAULT_HEALTH,
DEFAULT_HEALTH
);
};
return $game_id;
}
function player_starts_looking_for_a_fight($player_id) {
query(
"UPDATE players
SET looking_for_a_fight = 1
WHERE id = ?",
$player_id
);
return $try_to_start_a_fight();
}
function players_stops_looking_for_a_fight($player_ids) {
query(
"UPDATE players
SET looking_for_a_fight = 1
WHERE id IN (?)",
join(", ", $player_ids)
);
}
/*
CREATE TABLE players (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
level INT NOT NULL,
looking_for_a_fight TINYINT NOT NULL
) ENGINE=InnoDB;
CREATE TABLE games (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
level INT NOT NULL,
) ENGINE=InnoDB;
CREATE TABLE fighters (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
game_id INT NOT NULL,
player_id INT NOT NULL,
health INT NOT NULL,
max_health INT NOT NULL,
) ENGINE=InnoDB;
INSERT INTO players (id, name, level, looking_for_a_fight) VALUES(NULL, "Corwin", 1, 1);
INSERT INTO players (id, name, level, looking_for_a_fight) VALUES(NULL, "Mandor", 1, 1);
INSERT INTO players (id, name, level, looking_for_a_fight) VALUES(NULL, "Random", 1, 1);
INSERT INTO players (id, name, level, looking_for_a_fight) VALUES(NULL, "Eric", 1, 1);
INSERT INTO players (id, name, level, looking_for_a_fight) VALUES(NULL, "Caine", 1, 1);
INSERT INTO players (id, name, level, looking_for_a_fight) VALUES(NULL, "Benedict", 1, 1);
Note : C'est la merde, les balises code ne marchent plus.