Example #1
0
 public void Online(SocketAsyncEventArgs args, PlayerEnter enter, long roomId)
 {
     Args          = args;
     Enter.Account = enter.Account;
     Enter.TokenId = enter.TokenId;
     RoomId        = roomId;
 }
Example #2
0
 void Start()
 {
     m_playerCore  = GameObject.Find("Player").GetComponentInChildren <PlayerCore>();
     m_playerEnter = m_playerCore.transform.GetComponent <PlayerEnter>();
     m_text        = GetComponent <Text>();
     m_recordKey   = YamagenLib.PlayInstructor.instance.GetLoadStage().ToString() + "Time";
     m_elapsedTime = 0.0f;
 }
Example #3
0
        /// <summary>
        /// Called when a player comes into range of this crafter.
        /// </summary>
        private void OnPlayerEnter(BasePlayer player)
        {
            if (CraftingTasks.Count > 0)
            {
                SendCraftingList(player);
            }

            PlayerEnter?.Invoke(this, player);
        }
Example #4
0
 public PlayerInfo(SocketAsyncEventArgs args, PlayerEnter enter)
 {
     IsReady = false;
     Args    = args;
     Enter   = new PlayerEnter()
     {
         Account = enter.Account,
         TokenId = enter.TokenId,
     };
 }
    void OnComplete(SocketAction action, string msg)
    {
        switch (action)
        {
        case SocketAction.Connect:
        {
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Success);
            // 登录到RoomServer
            PlayerEnter enter = new PlayerEnter();
            if (ClientManager.Instance != null)
            {
                enter.Account = ClientManager.Instance.Player.Account;
                enter.TokenId = ClientManager.Instance.Player.TokenId;
            }
            else
            {
                enter.Account = "Footman3";
                enter.TokenId = 123456;
                //enter.TokenId = 1234561;
            }
            CurrentPlayer.Init(enter.Account, enter.TokenId);
            SendMsg(ROOM.PlayerEnter, enter.ToByteArray());
            StartHeartBeat();     // 开始心跳
            Log(msg);
        }
        break;

        case SocketAction.Send:
            break;

        case SocketAction.Receive:
            break;

        case SocketAction.Close:
            StopHeartBeat();
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Error);
            UIManager.Instance.MessageBox("错误", msg, (int)PanelMessageBox.BUTTON.OK, OnClickMessageBox);
            Debug.LogWarning(msg);
            break;

        case SocketAction.Error:
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Error);
            UIManager.Instance.MessageBox("错误", msg, (int)PanelMessageBox.BUTTON.OK, OnClickMessageBox);
            Debug.LogWarning(msg);
            break;
        }
    }
    private static void PLAYER_ENTER(byte[] bytes)
    {
        PlayerEnter input = PlayerEnter.Parser.ParseFrom(bytes);
        PlayerInfo  pi    = new PlayerInfo(_args, input);

        //检测是否重复登录,如果发现曾经有人登录,则将前面的人踢掉
        var alreadyLoggedIn = ServerRoomManager.Instance.FindPlayerArgs(input.TokenId);

        if (alreadyLoggedIn != null)
        {
            var oldPlayer = ServerRoomManager.Instance.GetPlayer(alreadyLoggedIn);
            if (oldPlayer != null)
            {
                string    roomName  = "";
                RoomLogic roomLogic = ServerRoomManager.Instance.GetRoomLogic(oldPlayer.RoomId);
                if (roomLogic != null)
                {
                    roomName = roomLogic.RoomName;
                }
                LeaveRoomReply output = new LeaveRoomReply()
                {
                    RoomName = roomName,
                    Ret      = true,
                };
                ServerRoomManager.Instance.SendMsg(alreadyLoggedIn, ROOM_REPLY.LeaveRoomReply, output.ToByteArray());
                ServerRoomManager.Instance.RemovePlayer(alreadyLoggedIn, true);
                string msg = "Kick myself that priviously logged in."; // "踢掉之前登录的本用户.";
                ServerRoomManager.Instance.Log($"MSG: PLAYER_ENTER WARNING - " + msg + $" - {oldPlayer.Enter.Account}");
            }
        }

        ServerRoomManager.Instance.AddPlayer(_args, pi);

        {
            PlayerEnterReply output = new PlayerEnterReply()
            {
                Ret = true,
            };
            ServerRoomManager.Instance.SendMsg(_args, ROOM_REPLY.PlayerEnterReply, output.ToByteArray());
        }
    }
Example #7
0
        protected override void ServerOnPlayerEnter(int clientId)
        {
            var clientInfo = ClientInfo(clientId);

            if (Config["SvSilentSpectatorMode"] && Players[clientId].Team == Team.Spectators)
            {
                clientInfo.Silent = true;
            }

            for (var i = 0; i < Players.Length; i++)
            {
                if (i == clientId || Players[i] == null || !Server.ClientInGame(i) && !Players[i].IsDummy)
                {
                    continue;
                }

                if (Server.ClientInGame(i))
                {
                    Server.SendPackMsg(clientInfo, MsgFlags.Vital | MsgFlags.NoRecord, i);
                }

                Server.SendPackMsg(ClientInfo(i), MsgFlags.Vital | MsgFlags.NoRecord, clientId);
            }

            clientInfo.Local = true;
            Server.SendPackMsg(clientInfo, MsgFlags.Vital | MsgFlags.NoRecord, clientId);

            if (false) // TODO DEMO
            {
                var msg = new GameMsg_DeClientEnter()
                {
                    Name     = clientInfo.Name,
                    Team     = clientInfo.Team,
                    ClientId = clientId
                };
                Server.SendPackMsg(msg, MsgFlags.NoSend, -1);
            }

            PlayerEnter?.Invoke(Players[clientId]);
        }
Example #8
0
    void OnComplete(SocketAction action, string msg)
    {
        switch (action)
        {
        case SocketAction.Connect:
        {
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Success);
            // 发送第一条消息,登录该用户,第一次使用Protobuf哦。。。Oct.17.2019. Liu Gang.
            PlayerEnter data = new PlayerEnter()
            {
                Account = ClientManager.Instance.Player.Account,
                TokenId = ClientManager.Instance.Player.TokenId,
            };
            SendMsg(LOBBY.PlayerEnter, data.ToByteArray());
            StartHeartBeat();     // 连接上以后开始心跳
            Debug.Log(msg);
        }
        break;

        case SocketAction.Send:
            break;

        case SocketAction.Receive:
            break;

        case SocketAction.Close:
            StopHeartBeat();
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Error);
            UIManager.Instance.MessageBox("错误", msg, (int)PanelMessageBox.BUTTON.OK, OnClickMessageBox);
            Debug.LogWarning(msg);
            break;

        case SocketAction.Error:
            UIManager.Instance.SystemTips(msg, PanelSystemTips.MessageType.Error);
            UIManager.Instance.MessageBox("错误", msg, (int)PanelMessageBox.BUTTON.OK, OnClickMessageBox);
            Debug.LogWarning(msg);
            break;
        }
    }
Example #9
0
        protected override void NetMsgEnterGame(Chunk packet, UnPacker unPacker, int clientId)
        {
            if (!packet.Flags.HasFlag(SendFlags.Vital))
            {
                return;
            }

            if (Clients[clientId].State != ServerClientState.Ready)
            {
                return;
            }

            if (!GameContext.IsClientReady(clientId))
            {
                return;
            }

            Console.Print(OutputLevel.Standard, "server", $"player has entered the game. ClientId={clientId} addr={NetworkServer.ClientEndPoint(clientId)}");
            Clients[clientId].State = ServerClientState.InGame;
            SendServerInfo(clientId);
            PlayerEnter?.Invoke(clientId);
        }
Example #10
0
 private void Awake()
 {
     _instance = this;
 }
Example #11
0
    public void Online(SocketAsyncEventArgs args, PlayerEnter enter, long roomId)
    {
        var piir = GetPlayerInRoom(enter.TokenId);

        if (piir == null)
        { // 如果房间里实际没有这个玩家(的存盘), 则表明这是一个新用户, 要从表格中读取初始数据
            piir = new PlayerInfoInRoom();
            var csv            = CsvDataManager.Instance.GetTable("battle_init");
            int wood           = csv.GetValueInt(1, "PlayerInit_Wood");
            int food           = csv.GetValueInt(1, "PlayerInit_Food");
            int iron           = csv.GetValueInt(1, "PlayerInit_Iron");
            int actionPointMax = csv.GetValueInt(1, "MaxActionPoint");
            piir.AddWood(wood);
            piir.AddFood(food);
            piir.AddIron(iron);
            piir.SetActionPointMax(actionPointMax);
            piir.AddActionPoint(actionPointMax);
            piir.TimeSinceLastSave = DateTime.Now.ToFileTime();
            piir.TimeSinceLastRestoreActionPoint = 0;
            PlayersInRoom[enter.TokenId]         = piir;
        }
        // 把本玩家记录到Room的Redis里, 便于以后查找, 这里保存的就是房间内所有的玩家的id
        AddPlayerIdToRedis(piir);

        // 玩家上线
        piir.Online(args, enter, roomId);

        // 玩家进入以后,根据该玩家[离开游戏]的时间,到[现在]的时间差(秒),计算出应该恢复多少的行动点数, 一次性恢复之
        piir.RestoreActionPointAfterLoading();

        // 这段逻辑很重要: 这是要把所有房间内的玩家的代理权限交给第一个进入房间的玩家, 以后, 如果这些玩家也上线了, 则把代理权交还给真正的玩家
        // 这是因为, 服务器并没有对这些玩家的逻辑进行AI处理, 而是由客户端自己来控制自己的.
        // 未来, 如果服务器要把AI代理权限收回来,这些地方都要做修改.
        if (piir.AiRights == 0 || piir.AiRights != piir.Enter.TokenId)
        { // 我之前曾经被别人代理过, 把代理权收回来
            var piirAi = GetPlayerInRoom(piir.AiRights);
            if (piirAi != null && piirAi.IsOnline)
            {
                piir.AiRights = piir.Enter.TokenId;
                ChangeAiRightsReply output = new ChangeAiRightsReply()
                {
                    RoomId      = piirAi.RoomId,
                    OwnerId     = piirAi.Enter.TokenId,
                    AiPlayerId  = piir.Enter.TokenId,
                    AiAccount   = piir.Enter.Account,
                    ControlByMe = false,
                    Ret         = true,
                };
                ServerRoomManager.Instance.SendMsg(piirAi.Args, ROOM_REPLY.ChangeAiRightsReply, output.ToByteArray());
                ServerRoomManager.Instance.Log($"RoomLogic Online OK - AI Rights of {piir.Enter.TokenId} is no longer controlled by {piir.Enter.Account}");
            }
            else
            {
                ServerRoomManager.Instance.Log($"RoomLogic Online Error - When trying to handle the AI controlled rights, the original player is offline! {piir.Enter.Account} - {piir.Enter.Account}");
            }
        }

        // 遍历所有玩家, 把所有: AI没有被代理, 且不在线的玩家, 权限都给它. 这种情况仅发生在第一个进入的玩家
        foreach (var keyValue in PlayersInRoom)
        {
            var piirAi = keyValue.Value;
            if (piirAi.AiRights == 0)
            { // 我自己不需要发送此消息, 我自己的AiRights此时肯定不是0了
                piirAi.AiRights = piir.Enter.TokenId;
                ChangeAiRightsReply output = new ChangeAiRightsReply()
                {
                    RoomId      = piir.RoomId,
                    OwnerId     = piir.Enter.TokenId,
                    AiPlayerId  = piirAi.Enter.TokenId,
                    AiAccount   = piirAi.Enter.Account,
                    ControlByMe = true,
                    Ret         = true,
                };
                ServerRoomManager.Instance.SendMsg(piir.Args, ROOM_REPLY.ChangeAiRightsReply, output.ToByteArray());
                ServerRoomManager.Instance.Log($"RoomLogic Online OK - AI Rights of {piirAi.Enter.Account} belongs to {piir.Enter.Account} now!");
            }
        }

        _curPlayerCount = PlayersInRoom.Count;
        ServerRoomManager.Instance.Log($"RoomLogic Online OK - Player entered the battlefield! Player:{piir.Enter.Account}"); // 玩家进入战场
    }
Example #12
0
 public PlayerInfoInRoom()
 {
     AiRights = 0;
     Enter    = new PlayerEnter();
 }