Ejemplo n.º 1
0
        /// <summary>登录后进入当前场景</summary>
        public ASObject CommandStart(SocketServer.TGGSession session, ASObject data)
        {
            try
            {
#if DEBUG
                XTrace.WriteLine("{0}:{1} - 玩家:{2}", "LOGIN_ENTER_SCENE", "登录后进入当前场景", session.Player.User.player_name);
#endif
                const int result    = (int)ResultType.SUCCESS;
                var       userid    = session.Player.User.id;
                var       userscene = session.Player.Scene;
                if (userscene == null)
                {
                    return(new ASObject(Common.GetInstance().BuildData((int)ResultType.SCENE_NOFIND, 0, 0, 0, null)));
                }
#if DEBUG
                XTrace.WriteLine("玩家{0}当前场景---{1}", session.Player.User.player_name, userscene.scene_id);
                XTrace.WriteLine("玩家{0}当前坐标---{1},{2}", session.Player.User.player_name, userscene.X, userscene.Y);
#endif

                var key = string.Format("{0}_{1}", (int)ModuleNumber.SCENE, userid);
                Variable.SCENCE.AddOrUpdate(key, userscene, (m, n) => n); //加入到内存中
                if (userscene.model_number != (int)ModuleNumber.SCENE)
                {
                    return(new ASObject(Common.GetInstance().BuildData(result, userscene.scene_id, userscene.X, userscene.Y, new List <view_scene_user>())));
                }
                var otherplays = Core.Common.Scene.GetOtherPlayers(userid, userscene.scene_id, (int)ModuleNumber.SCENE);//同场景内的其他玩家
                foreach (var item in otherplays)
                {
#if DEBUG
                    XTrace.WriteLine("向{0}玩家推送{1}进入当前场景---{2}", item.player_name, session.Player.User.player_name, userscene.scene_id);
#endif
                    var temp = new Common.ScenePush()
                    {
                        user_id       = userid,
                        other_user_id = item.user_id,
                        user_scene    = userscene
                    };
                    var ts = new CancellationTokenSource();
                    Task.Factory.StartNew(m =>
                    {
                        var t = m as Common.ScenePush;
                        if (t == null)
                        {
                            return;
                        }
                        new PLAYER_ENTER_SCENE().CommandStart(t.user_scene, t.user_id, t.other_user_id);
                        ts.Cancel();
                    }, temp, ts.Token);
                }

                return(new ASObject(Common.GetInstance().BuildData(result, userscene.scene_id, userscene.X, userscene.Y, otherplays)));
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
                return(new ASObject());
            }
        }
Ejemplo n.º 2
0
        /// <summary>切换场景</summary>
        public ASObject CommandStart(SocketServer.TGGSession session, ASObject data)
        {
#if DEBUG
            XTrace.WriteLine("{0}:{1} - {2}", "ENTER_SCENE", "切换场景", session.Player.User.player_name);
#endif
            var result    = (int)ResultType.SUCCESS;
            var sceneid   = Convert.ToInt32(data.FirstOrDefault(m => m.Key == "id").Value);
            var userid    = session.Player.User.id;
            var userscene = session.Player.Scene;
#if DEBUG
            XTrace.WriteLine("玩家{0}当前场景---{1}\t", session.Player.User.player_name, userscene.scene_id);
            XTrace.WriteLine("玩家{0}当前坐标---{1},{2}\t", session.Player.User.player_name, userscene.X, userscene.Y);
#endif
            var basescene = Variable.BASE_SCENE.FirstOrDefault(q => q.id == sceneid);
            if (basescene == null || userscene == null)
            {
                return(BuildData((int)ResultType.SCENE_BASEDATA_WRONG, null));
            }
            if (userscene.scene_id == sceneid)
            {
                return(BuildData((int)ResultType.SCENE_ENTER_SAME, null));
            }
            //1.表示开启 0表示未开启
            if (basescene.enabled == 0)
            {
                return(BuildData((int)ResultType.SCENE_CITY_UNOPEN, null));
            }
            var basebornpoint = Variable.BASE_ROLEBORNPOINT.FirstOrDefault(q => q.id == basescene.bornPoint);
            if (basebornpoint == null)
            {
                return(BuildData((int)ResultType.SCENE_BASEDATA_WRONG, null));
            }

            var otherplays = Core.Common.Scene.GetOtherPlayers(userid, userscene.scene_id, (int)ModuleNumber.SCENE); //同场景内的其他玩家
            //推送玩家离开场景信息
            foreach (var item in otherplays)                                                                         //调用推送
            {
#if DEBUG
                XTrace.WriteLine("向{0}玩家推送{1}离开场景---{2}\t\n", item.player_name, session.Player.User.player_name, userscene.scene_id);
#endif
                var tokenTest = new CancellationTokenSource();
                var temp      = new Common.ScenePush()
                {
                    user_id       = userid,
                    other_user_id = item.user_id
                };
                Task.Factory.StartNew(m =>
                {
                    var t = m as Common.ScenePush;
                    if (t == null)
                    {
                        return;
                    }
                    new PLAYER_EXIT_SCENET().CommandStart(t.user_id, t.other_user_id);
                    tokenTest.Cancel();
                }, temp, tokenTest.Token);
            }
            SceneDataSave(sceneid, userscene, session.Player.Scene, basebornpoint);
            var newotherplays = Core.Common.Scene.GetOtherPlayers(userid, userscene.scene_id, (int)ModuleNumber.SCENE); //同场景内的其他玩家

            foreach (var item in newotherplays)                                                                         //向新场景玩家推送玩家进入
            {
#if DEBUG
                XTrace.WriteLine("玩家{0}当前坐标---{1},{2}\t", session.Player.User.player_name, userscene.X, userscene.Y);
                XTrace.WriteLine("向{0}玩家推送{1}进入当前场景---{2}\t", item.player_name, session.Player.User.player_name, userscene.scene_id);
#endif
                var tokenTest = new CancellationTokenSource();
                var temp      = new Common.ScenePush()
                {
                    user_id       = userid,
                    other_user_id = item.user_id,
                    user_scene    = userscene
                };
                Task.Factory.StartNew(m =>
                {
                    var t = m as Common.ScenePush;
                    if (t == null)
                    {
                        return;
                    }
                    new PLAYER_ENTER_SCENE().CommandStart(t.user_scene, t.user_id, t.other_user_id);
                    tokenTest.Cancel();
                }, temp, tokenTest.Token);
            }
            return(BuildData(result, newotherplays));
        }
Ejemplo n.º 3
0
        /// <summary>玩家移动</summary>
        public ASObject CommandStart(SocketServer.TGGSession session, ASObject data)
        {
#if DEBUG
            XTrace.WriteLine("{0}:{1} - {2}", "MOVING", "玩家移动", session.Player.User.player_name);
#endif
            try
            {
                var dic       = new Dictionary <string, object>();
                var result    = (int)ResultType.SUCCESS;
                var x         = Convert.ToInt32(data.FirstOrDefault(q => q.Key == "x").Value);
                var y         = Convert.ToInt32(data.FirstOrDefault(q => q.Key == "y").Value);
                var userid    = session.Player.User.id;
                var userscene = session.Player.Scene;
#if DEBUG
                XTrace.WriteLine("玩家{0}当前场景---{1}\t", session.Player.User.player_name, userscene.scene_id);
                XTrace.WriteLine("玩家{0}当前坐标---{1},{2}\t", session.Player.User.player_name, userscene.X, userscene.Y);
#endif
                if (userscene == null)
                {
                    dic.Add("result", (int)ResultType.SCENE_BASEDATA_WRONG);
                    return(new ASObject(dic));
                }

                userscene.X            = x;
                userscene.Y            = y;
                session.Player.Scene.X = x;
                session.Player.Scene.Y = y;
                #region 测试数据
                //var otherplays = Core.Common.Scene.GetOtherPlayersByArea(userid, userscene.scene_id, (int)ModuleNumber.SCENE, userscene.X, userscene.Y, session.window.X, session.window.Y);//同场景内的其他玩家
                //var otherplays = new Share.TGTask().TestOhters(userid, userscene.scene_id, (int)ModuleNumber.SCENE);
                #endregion
                var otherplays = Core.Common.Scene.GetOtherPlayers(userid, userscene.scene_id, (int)ModuleNumber.SCENE);//同场景内的其他玩家
#if DEBUG
                XTrace.WriteLine("玩家{0}新坐标---{1},{2}\t\n", session.Player.User.player_name, userscene.X, userscene.Y);
#endif

                foreach (var item in otherplays)// 推送玩家进入信息
                {
#if DEBUG
                    XTrace.WriteLine("向{0}玩家推送{1}移动---{2}\t\n", item.player_name, session.Player.User.player_name, userscene.scene_id);
#endif
                    var tokenTest = new CancellationTokenSource();
                    var temp      = new Common.ScenePush()
                    {
                        user_id       = userid,
                        other_user_id = item.user_id,
                        user_scene    = userscene
                    };
                    Task.Factory.StartNew(m =>
                    {
                        var t = m as Common.ScenePush;
                        if (t == null)
                        {
                            return;
                        }
                        PlayerMoving(t.user_scene.X, t.user_scene.Y, t.user_id, t.other_user_id);
                        tokenTest.Cancel();
                    }, temp, tokenTest.Token);
                }
                dic.Add("result", result);
                return(new ASObject(dic));
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
                return(new ASObject());
            }
        }