xy 3 周之前
父節點
當前提交
e56d0ed883
共有 2 個文件被更改,包括 58 次插入7 次删除
  1. 49 0
      main.php
  2. 9 7
      src/client.php

+ 49 - 0
main.php

@@ -0,0 +1,49 @@
+<?php
+
+class ProgramMain {
+    private static ?GameClient $client = null;
+    
+    private static function getClient(): GameClient {
+        if (self::$client === null) {
+            self::$client = new GameClient();
+            echo "客户端初始化完成\n";
+        }
+        return self::$client;
+    }
+
+    public static function handleCommand(array $args): void {
+        $client = self::getClient();
+        
+        if (count($args) < 2) {
+            echo "使用方法: php client.php [命令] [参数...]\n";
+            echo "可用命令:\n";
+            echo "  login [账号] [密码] - 登录游戏\n";
+            echo "  start - 启动客户端\n";
+            return;
+        }
+
+        $command = $args[1];
+        switch ($command) {
+            case 'login':
+                if (count($args) < 4) {
+                    echo "错误:login命令需要账号和密码参数\n";
+                    echo "示例: php client.php login myaccount mypassword\n";
+                    return;
+                }
+                $account = $args[2];
+                $password = $args[3];
+                $client->login($account, $password);
+                break;
+            case 'start':
+                $client->start();
+                break;
+            default:
+                echo "未知命令: $command\n";
+        }
+    }
+}
+
+// 命令行执行
+if (php_sapi_name() === 'cli') {
+    ProgramMain::handleCommand($argv);
+}

+ 9 - 7
src/client.php

@@ -2,6 +2,7 @@
 require __DIR__.'/../generated/vendor/autoload.php';
 require_once __DIR__ . '/../vendor/autoload.php';
 require_once './msg.php';
+
 use App\WebSocketClient;
 use Common_pack\EnterHall;
 use Common_pack\ReqLogin;
@@ -17,13 +18,13 @@ class GameClient {
     private ?string $nikeName = null;
 
     public function start() {
-        $this->login();
+        // $this->login();
     }
 
-    private function login() {
+    public function login(string $account , string $password) {
         $loginReq = new ReqLogin();
-        $loginReq->setAccount("xy1");
-        $loginReq->setPassword('123');
+        $loginReq->setAccount($account);
+        $loginReq->setPassword($password);
         
         $binaryData = $loginReq->serializeToString();
 
@@ -108,7 +109,8 @@ class GameClient {
         $this->client->on(MsgID::RESHEARTBEAT, function($binaryData) {
             $response = new ResHeartBeat();
             $response->mergeFromString($binaryData);
-            echo "心跳:\n".$response->getMsg();
+            echo "心跳:".$response->getMsg();
+            echo "\n";
         });
     }
 
@@ -134,5 +136,5 @@ class GameClient {
 }
 
 // 启动客户端
-$gameClient = new GameClient();
-$gameClient->start();
+// $gameClient = new GameClient();
+// $gameClient->start();