415 lines
12 KiB
PHP
Executable File
415 lines
12 KiB
PHP
Executable File
<?php
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Core\AdminBaseController;
|
|
use Db\Database;
|
|
|
|
class GameController extends AdminBaseController {
|
|
public function __construct() {
|
|
$this->checkLogin();
|
|
$this->checkAdmin();
|
|
}
|
|
|
|
public function index() {
|
|
$db = new Database();
|
|
try {
|
|
$games = $db->select('games', '*', [
|
|
'ORDER' => ['id' => 'DESC']
|
|
]);
|
|
if (!is_array($games)) {
|
|
$games = [];
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$games = [];
|
|
}
|
|
|
|
$this->render('Admin/games.php', [
|
|
'games' => $games,
|
|
'title' => '游戏管理'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取单个游戏数据(用于编辑)
|
|
*/
|
|
public function get($id) {
|
|
header('Content-Type: application/json');
|
|
|
|
if (empty($id) || !is_numeric($id)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的游戏ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
try {
|
|
$game = $db->get('games', '*', [
|
|
'id' => $id
|
|
]);
|
|
|
|
if ($game) {
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => $game
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '游戏不存在'
|
|
]);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '获取数据失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建或更新游戏
|
|
*/
|
|
public function update() {
|
|
header('Content-Type: application/json');
|
|
|
|
$data = $_POST;
|
|
if (empty($data)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '未接收到数据'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
$id = isset($data['id']) ? (int)$data['id'] : 0;
|
|
$isEditMode = $id > 0;
|
|
|
|
$name = trim($data['name'] ?? '');
|
|
$code = trim($data['code'] ?? '');
|
|
$streamUrl = trim($data['stream_url'] ?? '');
|
|
$type = trim($data['type'] ?? 'dice');
|
|
$sortOrder = isset($data['sort_order']) ? (int)$data['sort_order'] : 0;
|
|
$status = isset($data['status']) ? (int)$data['status'] : 1; // 默认启用
|
|
$featured = isset($data['featured']) ? (int)$data['featured'] : 1; // 默认显示在游戏大厅
|
|
$image = trim($data['image'] ?? '');
|
|
|
|
// 处理标签(复选框数组转逗号分隔字符串)
|
|
$tags = '';
|
|
if (isset($data['tags']) && is_array($data['tags'])) {
|
|
$tags = implode(',', array_map('trim', $data['tags']));
|
|
} elseif (isset($data['tags'])) {
|
|
$tags = trim($data['tags']);
|
|
}
|
|
|
|
if (empty($name)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '游戏名称不能为空'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// 自动生成游戏标识(如果为空)
|
|
if (empty($code)) {
|
|
// 将中文转换为拼音或使用拼音首字母,这里简化为使用时间戳+随机数
|
|
// 实际可以使用拼音库,这里使用简化方式:去除特殊字符,转为小写,空格转下划线
|
|
$code = $this->generateGameCode($name, $db, $id);
|
|
}
|
|
|
|
// 检查code是否已存在(排除当前ID)
|
|
try {
|
|
$exists = $db->get('games', 'id', [
|
|
'code' => $code,
|
|
'id[!]' => $id
|
|
]);
|
|
|
|
if ($exists) {
|
|
// 如果已存在,添加随机后缀
|
|
$code = $code . '_' . time();
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// 如果表不存在或其他错误,继续执行
|
|
}
|
|
|
|
// 准备数据
|
|
$gameData = [
|
|
'name' => $name,
|
|
'code' => $code,
|
|
'stream_url' => $streamUrl,
|
|
'type' => $type,
|
|
'tags' => $tags,
|
|
'sort_order' => $sortOrder,
|
|
'status' => $status,
|
|
'featured' => $featured,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
];
|
|
|
|
// 如果提供了图片路径,则添加
|
|
if (!empty($image)) {
|
|
$gameData['image'] = $image;
|
|
}
|
|
|
|
try {
|
|
if ($isEditMode) {
|
|
$db->update('games', $gameData, ['id' => $id]);
|
|
$message = '游戏更新成功';
|
|
} else {
|
|
$gameData['created_at'] = date('Y-m-d H:i:s');
|
|
$id = $db->insert('games', $gameData);
|
|
$message = '游戏创建成功';
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => $message,
|
|
'data' => ['id' => $id]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '操作失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除游戏
|
|
*/
|
|
public function delete($id) {
|
|
header('Content-Type: application/json');
|
|
|
|
if (empty($id) || !is_numeric($id)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的游戏ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
try {
|
|
$db->delete('games', ['id' => $id]);
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => '游戏已删除'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '删除失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 切换游戏启用状态
|
|
*/
|
|
public function toggleStatus($id) {
|
|
header('Content-Type: application/json');
|
|
|
|
if (empty($id) || !is_numeric($id)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的游戏ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
try {
|
|
$game = $db->get('games', ['status'], ['id' => $id]);
|
|
if (!$game) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '游戏不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$newStatus = $game['status'] ? 0 : 1;
|
|
$db->update('games', [
|
|
'status' => $newStatus,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
], ['id' => $id]);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => $newStatus ? '游戏已启用' : '游戏已禁用',
|
|
'data' => ['status' => $newStatus]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '操作失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取游戏赔率配置
|
|
*/
|
|
public function getOdds($gameId) {
|
|
header('Content-Type: application/json');
|
|
|
|
if (empty($gameId) || !is_numeric($gameId)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的游戏ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
try {
|
|
// Check if game exists
|
|
$game = $db->get('games', ['id', 'name'], ['id' => $gameId]);
|
|
if (!$game) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '游戏不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// Get odds
|
|
$odds = $db->select('game_odds', '*', [
|
|
'game_id' => $gameId
|
|
]);
|
|
|
|
// If no odds found, return defaults structure (or empty array)
|
|
// The frontend should handle empty/missing odds
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => [
|
|
'game' => $game,
|
|
'odds' => $odds
|
|
]
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '获取赔率失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新游戏赔率
|
|
*/
|
|
public function updateOdds() {
|
|
header('Content-Type: application/json');
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($data)) {
|
|
$data = $_POST;
|
|
}
|
|
|
|
$gameId = isset($data['game_id']) ? (int)$data['game_id'] : 0;
|
|
$oddsData = isset($data['odds']) ? $data['odds'] : [];
|
|
|
|
if (empty($gameId) || empty($oddsData)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '参数不完整'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
try {
|
|
// Begin transaction if supported (PDO usually does)
|
|
// For simple database wrapper, we just loop update
|
|
|
|
foreach ($oddsData as $item) {
|
|
$type = $item['type'];
|
|
$target = $item['target'];
|
|
$odds = floatval($item['odds']);
|
|
|
|
// Check if exists
|
|
$exists = $db->has('game_odds', [
|
|
'game_id' => $gameId,
|
|
'type' => $type,
|
|
'target' => $target
|
|
]);
|
|
|
|
if ($exists) {
|
|
$db->update('game_odds', [
|
|
'odds' => $odds,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
], [
|
|
'game_id' => $gameId,
|
|
'type' => $type,
|
|
'target' => $target
|
|
]);
|
|
} else {
|
|
$db->insert('game_odds', [
|
|
'game_id' => $gameId,
|
|
'type' => $type,
|
|
'target' => $target,
|
|
'odds' => $odds,
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => '赔率配置已保存'
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '保存失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 自动生成游戏标识
|
|
* @param string $name 游戏名称
|
|
* @param Database $db 数据库实例
|
|
* @param int $excludeId 排除的ID(编辑时使用)
|
|
* @return string 生成的游戏标识
|
|
*/
|
|
private function generateGameCode($name, $db, $excludeId = 0) {
|
|
// 去除特殊字符,转为小写,空格和中文标点转下划线
|
|
$code = strtolower($name);
|
|
// 将中文字符转为拼音(简化处理:直接使用拼音首字母或时间戳)
|
|
// 这里使用简化的方式:去除所有非字母数字字符,替换为下划线
|
|
$code = preg_replace('/[^a-z0-9_]+/', '_', $code);
|
|
$code = preg_replace('/_+/', '_', $code); // 多个下划线合并为一个
|
|
$code = trim($code, '_'); // 去除首尾下划线
|
|
|
|
// 如果转换后为空,使用时间戳
|
|
if (empty($code)) {
|
|
$code = 'game_' . time();
|
|
}
|
|
|
|
// 限制长度
|
|
if (strlen($code) > 50) {
|
|
$code = substr($code, 0, 50);
|
|
}
|
|
|
|
// 检查是否已存在,如果存在则添加随机数
|
|
try {
|
|
$exists = $db->get('games', 'id', [
|
|
'code' => $code,
|
|
'id[!]' => $excludeId
|
|
]);
|
|
|
|
if ($exists) {
|
|
$code = $code . '_' . substr(time(), -6); // 添加时间戳后缀
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// 忽略错误
|
|
}
|
|
|
|
return $code;
|
|
}
|
|
}
|
|
|