Фиксили телеграф, немного дописали чат...

This commit is contained in:
lopar 2021-02-01 03:39:50 +02:00
parent da410d57c8
commit 7fa1720c40
2 changed files with 36 additions and 32 deletions

View File

@ -5,48 +5,52 @@
* Project name: Battles-Game
*/
use Battles\Database\DBPDO;
use Battles\Template;
session_start();
require_once "config.php";
$msg = $_POST['msg'] ?? null;
$uid = $_SESSION['uid'] ?? null;
if ($msg) {
try {
db::c()->query('INSERT INTO `chat` (`user_id`, `msg`) VALUES (?i, "?s")', $uid, $msg);
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
}
$db = new DBPDO();
$db->execute('INSERT INTO chat (user_id,msg) VALUES (?,?)', [$uid, $msg]);
}
function show_messages()
{
try {
$chat = db::c()->query('
SELECT
`msg`,
`msgdate`,
(SELECT `login` FROM `users` WHERE `users`.`id` = `user_id`) AS `from`,
`type`
FROM `chat` ORDER BY `id` LIMIT 50');
while ($message = $chat->fetch_assoc()) {
$d = new DateTime($message['msgdate']);
$m = htmlspecialchars($message['msg']);
if ($message['type'] == 'sys') { /* Системка */
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
} elseif ($message['type'] == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span><br>', $d->format('H:i'), $m);
} else {
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $message['from'], $m);
$db = new DBPDO();
$chat = $db->ofetchALL('SELECT msg,msgdate,type,s.login AS sender, r.login AS receiver, s.id AS sid, r.id AS rid FROM chat
LEFT JOIN users s on s.id = chat.user_id
LEFT JOIN users r on r.id = chat.receiver_id
WHERE r.id = ? OR r.id IS NULL OR s.id = ? ORDER BY chat.id', [$_SESSION['uid'], $_SESSION['uid']]);
$i = 0;
while ($i < count($chat)) {
$d = new DateTime($chat[$i]->msgdate);
$m = htmlspecialchars($chat[$i]->msg);
if ($chat[$i]->type == 'sys') { /* Системка */
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
} elseif ($chat[$i]->rid == $_SESSION['uid']) { /* С указанным получателем */
if ($chat[$i]->type == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">%s Телеграмма от [%s]: %s</span><br>', $d->format('d.m.Y H:i'), $chat[$i]->sender, $m);
} elseif ($chat[$i]->type == 'private') { /* Приват */
echo sprintf('<span style="background:#efe;">%s [%s] → [%s]: %s</span><br>', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
} else { /* Общак */
echo sprintf('%s [%s] → [%s]: %s<br>', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
}
} else { /* Без указанного получателя */
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $chat[$i]->sender, $m);
}
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
$i++;
}
unset($i, $chat, $db);
}
Template::header('chat');
show_messages();
\Battles\Template::header('chat');
?>
<style>
form {

View File

@ -925,17 +925,17 @@ function err($t)
/**
* @param $name
* @param $text
* @param int $userId
* @param string $text
*
* @throws \Krugozor\Database\Mysql\Exception
*/
function telegraph($userId, $text)
function telegraph(int $userId, string $text)
{
db::c()->query('SELECT 1 FROM `users` WHERE `id` = ?i', $userId)->fetch_assoc();
if (db::c()->getAffectedRows()) {
db::c()->query('INSERT INTO `telegraph` (receiver, text) VALUES (?i,"?s")', $userId, $text);
$db = DBPDO::INIT();
if ($db->ofetch('SELECT 1 FROM users WHERE id = ?', $userId)) {
$db->execute('INSERT INTO chat (user_id,receiver_id,msg,type) VALUES (-1,?,?,?)', [$userId, $text, 'sms']);
}
unset($db);
}
function get_meshok()