-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
start_worker.php
60 lines (55 loc) · 1.81 KB
/
start_worker.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
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \Server\Utils;
use \Server\Player;
use \Server\WorldServer;
// 自动加载类
require_once __DIR__ . '/vendor/autoload.php';
// BrowserQuest Server
$ws_worker = new Worker('Websocket://0.0.0.0:8000');
$ws_worker->name = 'BrowserQuestWorker';
$ws_worker->onWorkerStart = function($ws_worker)
{
$ws_worker->server = new \Server\Server();
$ws_worker->config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
$ws_worker->worlds = array();
foreach(range(0, $ws_worker->config['nb_worlds']-1) as $i)
{
$world = new WorldServer('world'. ($i+1), $ws_worker->config['nb_players_per_world'], $ws_worker);
$world->run($ws_worker->config['map_filepath']);
$ws_worker->worlds[] = $world;
}
};
$ws_worker->onConnect = function($connection) use ($ws_worker)
{
$connection->server = $ws_worker->server;
if(isset($server->connectionCallback))
{
call_user_func($ws_worker->server->connectionCallback);
}
$world = Utils::detect($ws_worker->worlds, function($world)use($ws_worker)
{
return $world->playerCount < $ws_worker->config['nb_players_per_world'];
});
$world->updatePopulation(null);
if($world && isset($world->connectCallback))
{
call_user_func($world->connectCallback, new Player($connection, $world));
}
};
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}