-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
156 lines (126 loc) · 5.94 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php /** @noinspection PhpFullyQualifiedNameUsageInspection */
/** @noinspection PhpDynamicFieldDeclarationInspection */
use App\Utils\SocketUtil;
use App\Utils\RoomUtil;
use App\GameConfig;
use Swoole\Table;
require_once __DIR__. '/vendor/autoload.php';
require_once __DIR__. '/app/helpers.php';
date_default_timezone_set('PRC');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
\Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_TCP);
/********************************** 建表 **********************************/
$fdTable = new Table(GameConfig::SERVER_MAX_FD_COUNT);
$fdTable->column('fd', Table::TYPE_INT);
$fdTable->column('uid', Table::TYPE_STRING, GameConfig::USER_ID_LENGTH);
$fdTable->column('updatedAt', Table::TYPE_INT); //活跃时间timestamp
$fdTable->create();
$userTable = new Table(GameConfig::SERVER_MAX_USER_COUNT);
$userTable->column('uid', Table::TYPE_STRING, GameConfig::USER_ID_LENGTH);
$userTable->column('activeFd', Table::TYPE_INT);
$userTable->column('name', Table::TYPE_STRING, GameConfig::USER_NAME_LENGTH);
$userTable->column('roomId', Table::TYPE_STRING, GameConfig::ROOM_ID_LENGTH); //所在房间,断线不会改变,主动退出、换房、被踢出才会改变
$userTable->column('roomStatus', Table::TYPE_INT); //当前状态 脚本会定时更新
$userTable->column('updatedAt', Table::TYPE_INT); //活跃时间timestamp
$userTable->column('ping', Table::TYPE_INT);
$userTable->column('extra', Table::TYPE_STRING, 256); //json格式
$userTable->column('lineId', Table::TYPE_STRING, GameConfig::USER_ID_LENGTH + GameConfig::ROOM_ID_LENGTH + 1); //D2W模式下的lineId
$userTable->create();
$roomTable = new Table(GameConfig::SERVER_MAX_ROOM_COUNT);
$roomTable->column('id', Table::TYPE_STRING, GameConfig::ROOM_ID_LENGTH);
$roomTable->column('name', Table::TYPE_STRING, 32);
$roomTable->column('ownerId', Table::TYPE_STRING, GameConfig::USER_ID_LENGTH); //无房主时,为空字符串
$roomTable->column('status', Table::TYPE_INT); //当前状态
$roomTable->column('round', Table::TYPE_INT); //回合
$roomTable->column('lockLexicon', Table::TYPE_INT); //锁定词库
$roomTable->column('roundNumReal', Table::TYPE_INT); //真实的轮次数(不包含-1等)
$roomTable->column('nextStateTimestamp', Table::TYPE_INT); //下一阶段的时间戳(仅用于断线重连使用)
$roomTable->column('liveQQGroupNum', Table::TYPE_INT); //直播
$roomTable->column('lexiconId', Table::TYPE_INT);
$roomTable->column('roundNum', Table::TYPE_INT);
$roomTable->column('drawSecond', Table::TYPE_INT);
$roomTable->column('guessSecond', Table::TYPE_INT);
$roomTable->column('updatedAt', Table::TYPE_INT); //活跃时间timestamp
$roomTable->column('nth', Table::TYPE_INT); //局次(场次)
$roomTable->create();
/********************************** 服务 **********************************/
$port = (int)env('SERVER_PORT', 9999);
$workerNum = (int)env('SERVER_WORKER_NUM', 2);
$server = new \App\Server("0.0.0.0", $port, SWOOLE_PROCESS);
$server->set([
'worker_num' => 2,
'heartbeat_check_interval' => 10,
'heartbeat_idle_time' => 60,
// 'dispatch_mode' => 1,
]);
$server->fdTable = $fdTable;
$server->userTable = $userTable;
$server->roomTable = $roomTable;
\App\Mode\D2W\D2W::initTable($server);
vlogPink("接龙服务器启动,当前版本:0.1.0-alpha");
//Redis
\App\Utils\RedisUtil::initServer($server);
//render(cowsay("会米画猜后端")->template('fox')->w30());
$service = new \App\Service\SocketService();
$httpService = new \App\Service\HttpService();
$lexiconService = new \App\Service\Lexicon\LexiconService();
$service->lexiconService = $lexiconService;
$server->lexiconService = $lexiconService;
//测试注入数据
//加载词库
$lexiconService->init();
//创建房间
RoomUtil::createRoom('test1', '测试房间1');
RoomUtil::createRoom('test2', '测试房间2', null, 190993837);
RoomUtil::createRoom('enazo', '绘谜', null, 602606155);
//加载游戏王房间
$ygoLexiconIndex = $lexiconService->findLexiconId('绘谜', 'ygo-vitech-common');
if ($ygoLexiconIndex) {
RoomUtil::createRoom('ygo', '游戏王', null, 586151828);
} else {
vlogWarning('没有找到游戏王热门词库!');
}
$server->on('open', function (\App\Server $server, \Swoole\Http\Request $request) use($service) {
vlogDebug("[{$request->fd}]握手成功");
SocketUtil::contextSet(SocketUtil::CTX_SERVER, $server);
SocketUtil::contextSet(SocketUtil::CTX_FD, $request->fd);
$service->onConnect($request->fd, $request->get);
});
$server->on('message', function (\App\Server $server, \Swoole\WebSocket\Frame $frame) use($service) {
SocketUtil::contextSet(SocketUtil::CTX_SERVER, $server);
SocketUtil::contextSet(SocketUtil::CTX_FD, $frame->fd);
// vlog("[{$frame->fd}]原始消息:{$frame->data}");
$service->onMessage($frame, $frame->fd);
});
$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use($httpService) {
global $server;
$httpService->response = $response;
$httpService->request = $request;
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Headers', '*');
$response->header('Access-Control-Allow-Methods', '*');
SocketUtil::contextSet(SocketUtil::CTX_SERVER, $server);
$httpService->onMessage();
});
$server->on('close', function (\App\Server $server, $fd) use($service) {
$wsStatus = $server->connection_info($fd)['websocket_status'];
if ($wsStatus !== 3) return;
vlogDebug("[{$fd}]客户端关闭");
SocketUtil::contextSet(SocketUtil::CTX_SERVER, $server);
$service->onFdClose($fd);
});
SocketUtil::contextSet(SocketUtil::CTX_SERVER, $server);
$GLOBALS['server'] = $server;
//function checkAll($server) {
// vlog("状态检查");
// echo \App\Utils\TableUtil::genTableStats();
// echo "\n";
//}
//\Swoole\Timer::after(1000, function() use($server) {
// checkAll($server);
//});
//\Swoole\Timer::tick(30 * 1000, function() use($server) {
// checkAll($server);
//});
$server->start();