Esempio n. 1
0
    private PlayerContext NewPlayerContext(string playerId, Commands.StructVector3 pos, DirectionEnum dir)
    {
        var playerContext = new PlayerContext(playerId, pos, dir);
        var auto          = new Spawning <PlayerContext, List <PlayerContext> >(clientFrame, playerContext);

        playerContext.auto = auto;

        var prefab = Resources.Load("Chara") as GameObject;

        playerModels[playerId] = Instantiate(prefab, new Vector3(playerContext.x, playerContext.height, playerContext.z), Quaternion.identity) as GameObject;
        return(playerContext);
    }
Esempio n. 2
0
    public PlayerContext(string playerId, Commands.StructVector3 pos, DirectionEnum dir)
    {
        this.playerId = playerId;
        this.forward  = DirectionEnum.North;
        this.x        = pos.x;
        this.z        = pos.z;
        this.height   = pos.height;
        this.forward  = dir;

        this.stackedDummyAutos = new List <AutoInfo>();
        this.stackedCommands   = new List <Commands.BaseData>();
    }
    /**
     *      ServerContext Core.
     *      このレイヤがXrossPeerの外側一枚。
     */
    private void InputToXrossPeer(int frame, string playerId, byte[] data)
    {
        /*
         *      validate data.
         */
        var commandAndPlayerId = Commands.ReadCommandAndSourceId(data);
        var command            = commandAndPlayerId.command;
        var commandPlayerId    = commandAndPlayerId.playerId;

        if (commandPlayerId != playerId)
        {
            XrossPeer.Log("実行者と送付者のidがマッチしない、BANする");
            return;
        }

        /*
         *      extract.
         *      この部分で、
         *      ・XrossPeer内の対象のAutoが状況変化を受けいれること
         *      ・対象変化を受け入れた場合、そのコマンドを全員に配ること
         *      ・受け入れない場合ここで黙殺すること
         *      を決める。
         */

        /*
         *      サーバでしか受け取らず、サーバでないと他のユーザーに反射しないシリーズ。
         *      ユーザーの有無っていう究極的な可否を判定する根っこ。
         *
         *      だいたいConnectionServerが吐いてる
         */
        switch (command)
        {
        case Commands.CommandEnum.OnConnected: {
            var onConnected         = Commands.FromData <Commands.OnConnected>(data);
            var onConnectedPlayerId = onConnected.playerId;

            XrossPeer.Log("connected playerId:" + onConnectedPlayerId);

            /*
             *      このタイミングで、サーバへのプレイヤーのログインが完了してる。
             */
            {
                var pos = new Commands.StructVector3(0, 0, 30);
                // 適当な位置をでっち上げる
                var newPlayer = new PlayerContext(onConnectedPlayerId, pos, DirectionEnum.East);
                world.AddPlayer(newPlayer);
                StackPublish(new Commands.EntriedId(onConnectedPlayerId, pos, newPlayer.forward), AllConnectedIds());
            }


            /*
             *      ダミーを10人くらい降らせよう。
             */
            if (AllConnectedIds().Length == 1)
            {
                XrossPeer.Log("最初の接続者 onConnectedPlayerId:" + onConnectedPlayerId + " が来たので、ダミーを降らせる。ランダムシードがすげえーー怪しい時があるな。");

                var dummyCount = 2;

                var xRand = new byte[dummyCount];
                var zRand = new byte[dummyCount];

                var r1 = new System.Random();
                r1.NextBytes(xRand);

                var r2 = new System.Random();
                r2.NextBytes(zRand);


                {
                    var dummyPlayerId = Guid.NewGuid().ToString();
                    var dir           = DirectionEnum.South;
                    var pos           = new Commands.StructVector3(10, 10, 30);
                    var dummyPlayer   = new PlayerContext(dummyPlayerId, pos, dir);
                    dummyPlayer.isDummy = true;
                    world.AddPlayer(dummyPlayer);
                }

                {
                    var dummyPlayerId = Guid.NewGuid().ToString();
                    var dir           = DirectionEnum.South;
                    var pos           = new Commands.StructVector3(10, 0, 30);
                    var dummyPlayer   = new PlayerContext(dummyPlayerId, pos, dir);
                    dummyPlayer.isDummy = true;
                    world.AddPlayer(dummyPlayer);
                }
            }

            /*
             *      receive world info.
             */
            var playersInfos = world.PlayersInfos();
            StackPublish(new Commands.WorldData(onConnectedPlayerId, playersInfos), new string[] { ConnectionIdFromPlayerId(onConnectedPlayerId) });

            return;
        }

        case Commands.CommandEnum.OnDisconnected: {
            var onDisconnected       = Commands.FromData <Commands.OnDisconnected>(data);
            var disconnectedPlayerId = onDisconnected.playerId;
            var reason = onDisconnected.reason;

            var reasonCode = 0;

            XrossPeer.Log("disconnected この時点で通信対象リストからは外されている。 disconnectedPlayerId:" + disconnectedPlayerId + " reason:" + reason + " まだなんにもしてない。");
            // StackPublish(new Commands.PlayerLeft(disconnectedPlayerId, reasonCode), AllConnectedIds());
            return;
        }
        }


        switch (command)
        {
        case Commands.CommandEnum.SpawnRequest: {
            var spawnData = Commands.FromData <Commands.SpawnRequest>(data);

            var spawnPlayerId = spawnData.playerId;
            XrossPeer.Log("spawnがきた spawnPlayerId:" + spawnPlayerId);

            /*
             *      すでにSpawnしてないかとかみて、OKだったらSpawnを返す
             */
            StackPublish(new Commands.Spawn(spawnPlayerId), AllConnectedIds());
            return;
        }

        // 移動データとかを扱う。
        case Commands.CommandEnum.Walk: {
            var walkData        = Commands.FromData <Commands.Walk>(data);
            var walkingPlayerId = walkData.playerId;
            var walkingDir      = walkData.direction;
            var walkBasePos     = walkData.pos;

            StackPublish(new Commands.Walk(walkingPlayerId, walkingDir, walkBasePos), AllConnectedIds());


            if (!world.IsDummyPlayer(walkingPlayerId))
            {
                var playerInfo = world.GetPlayerInfo(walkingPlayerId);
                playerInfo.auto = new Walk <PlayerContext, List <PlayerContext> >(gameFrame, playerInfo);
            }
            return;
        }

        case Commands.CommandEnum.Messaging: {
            var messageData    = Commands.FromData <Commands.Messaging>(data);
            var senderPlayerId = messageData.playerId;
            var targetPlayerId = messageData.targetPlayerId;
            var message        = messageData.message;

            // サーバまで届いているので、そのことの反射をする。
            var senderConnectionId = ConnectionIdFromPlayerId(senderPlayerId);
            StackPublish(new Commands.Messaging(senderPlayerId, senderPlayerId, "我:" + senderPlayerId + ":" + message), new string[] { senderConnectionId });

            XrossPeer.Log("message:" + message + " from playerId:" + senderPlayerId + " to targetPlayerId:" + targetPlayerId);

            /*
             *      is bot, message turns to "order".
             */
            if (world.IsDummyPlayer(targetPlayerId))
            {
                GenerateAnswer(targetPlayerId, senderPlayerId, message, senderConnectionId);
                return;
            }

            var connectionId = ConnectionIdFromPlayerId(targetPlayerId);
            StackPublish(new Commands.Messaging(senderPlayerId, targetPlayerId, "相手:" + senderPlayerId + ":" + message), new string[] { connectionId });
            return;
        }

        default: {
            XrossPeer.Log("gameContextLayer unhandled command:" + command);
            break;
        }
        }
    }