예제 #1
0
        public bool AcctOffLine(ServerSession session)
        {
            bool   suc = true;
            string act = onlineAccountReversDic[session];

            suc = suc ? onlineAccountDic.Remove(act) : false;
            suc = suc ? onlineAccountReversDic.Remove(session) : false;
            suc = suc ? onlineSessionDic.Remove(session) : false;
            if (!suc)
            {
                NETCommon.Log("Clear Cahce Error", NETLogLevel.Error);
            }
            return(suc);
        }
예제 #2
0
        public void ClearCache(ServerSession session)
        {
            PlayerData pd = cacheSev.GetPlayerDataBySession(session);

            if (pd != null)
            {
                pd.time = timeSev.GetNowTime();
                if (!cacheSev.UpdatePlayerData(pd))
                {
                    NETCommon.Log("Update Offline time Error", NETLogLevel.Error);
                }
            }
            cacheSev.AcctOffLine(session);
        }
예제 #3
0
        private void CalcPowerAdd(int id)
        {
            GameMsg msg = new GameMsg
            {
                cmd      = (int)CMD.PshPower,
                pshPower = new PshPower()
            };

            // 向所有在线玩家推送
            Dictionary <ServerSession, PlayerData> onlineDic = cacheSev.GetOnlinePlayers();

            foreach (var pair in onlineDic)
            {
                PlayerData    pd      = pair.Value;
                ServerSession session = pair.Key;

                int powerMax = NETCommon.GetPowerLimit(pd.lv);
                if (pd.power >= powerMax)
                {
                    continue;
                }

                pd.power += NETCommon.PowerAddCount;
                if (pd.power > powerMax)
                {
                    pd.power = powerMax;
                }

                pd.time = timeSev.GetNowTime();

                if (!cacheSev.UpdatePlayerData(pd))
                {
                    msg.err = (int)ErrorCode.UpdateDBError;
                }
                else
                {
                    msg.pshPower.power = pd.power;
                }
                session.SendMsg(msg);
            }
        }
예제 #4
0
 public MsgPack(ServerSession session, GameMsg msg)
 {
     this.session = session;
     this.msg     = msg;
 }
예제 #5
0
 /// <summary>
 /// 账号上线时缓存数据
 /// </summary>
 /// <param name="account"></param>
 /// <param name="session"></param>
 /// <param name="playerData"></param>
 public void CacheAccountOnline(string account, ServerSession session, PlayerData playerData)
 {
     onlineAccountDic.Add(account, session);
     onlineAccountReversDic.Add(session, account);
     onlineSessionDic.Add(session, playerData);
 }
예제 #6
0
        public void ReqStrong(MsgPack pack)
        {
            ServerSession session = pack.session;
            ReqStrong     data    = pack.msg.reqStrong;

            GameMsg msg = new GameMsg
            {
                cmd = (int)CMD.RspStrong
            };

            PlayerData pd      = cacheSev.GetPlayerDataBySession(session);
            int        curStar = pd.strongArr[data.pos];
            StrongCfg  nextSc  = cfgSev.GetStrongCfgData(data.pos, curStar + 1);

            // 条件判断
            if (pd.lv < nextSc.minLv)
            {
                msg.err = (int)ErrorCode.LackLevel;
                session.SendMsg(msg);
                return;
            }
            if (pd.coin < nextSc.coin)
            {
                msg.err = (int)ErrorCode.LackCoin;
                session.SendMsg(msg);
                return;
            }
            if (pd.crystal < nextSc.crystal)
            {
                msg.err = (int)ErrorCode.LackCrystal;
                session.SendMsg(msg);
                return;
            }

            // 资源扣除
            pd.coin    -= nextSc.coin;
            pd.crystal -= nextSc.crystal;

            // 属性增益
            pd.strongArr[data.pos] += 1;
            pd.hp    += nextSc.addHp;
            pd.ad    += nextSc.addHurt * (1 + 1 / (data.pos + 1));
            pd.ap    += nextSc.addHurt;
            pd.addef += nextSc.addDef;
            pd.apdef += nextSc.addDef;

            // 任务进度更新
            msg.pshTaskPrgs = TaskSystem.Instance.CalcTaskPrg(pd, 3);

            // 更新数据库
            if (!cacheSev.UpdatePlayerData(pd))
            {
                msg.err = (int)ErrorCode.UpdateDBError;
                session.SendMsg(msg);
                return;
            }

            // 返回消息
            msg.rspStrong = new RspStrong
            {
                coin      = pd.coin,
                crystal   = pd.crystal,
                hp        = pd.hp,
                ad        = pd.ad,
                ap        = pd.ap,
                adddef    = pd.addef,
                apdef     = pd.apdef,
                strongArr = pd.strongArr
            };
            session.SendMsg(msg);
        }
예제 #7
0
        public static JObject SendResponse(HttpListenerRequest request)
        {
            string[] uriSegments = request.Url.Segments;
            string   endpoint    = uriSegments[uriSegments.Length - 1];

            string text;

            using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
            {
                text = reader.ReadToEnd();
            }

            JObject responseJson = new JObject();

            if (text.Length == 0)
            {
                return(responseJson);
            }
            JObject requestJson = JObject.Parse(text);

            byte[] sessionKey = new byte[0];

            switch (endpoint)
            {
            case "map/":
                Console.WriteLine("maps were requested");
                return(createDefaultMapsObject());

            case "login/":
                string user = (string)requestJson["user"];
                Console.WriteLine("login attempt from: " + user);
                string pass = (string)requestJson["pass"];
                if (ServerSession.getInstance().validateUser(user, pass))
                {
                    byte[] newSessionKey = ServerSession.getInstance().createNewSessionKey(user);
                    responseJson.Add("session", newSessionKey);
                    Console.WriteLine("login successful");

                    return(responseJson);
                }
                else
                {
                    responseJson.Add("session", null);
                    Console.WriteLine("login failed");
                    return(responseJson);
                }

            case "logout/":
                Console.WriteLine("logout attempt");
                sessionKey = (byte[])requestJson["session"];
                ServerSession.getInstance().endSession(sessionKey);
                Console.WriteLine("logout successful");

                return(responseJson);

            case "valid/":
                Console.WriteLine("validate session");
                sessionKey = (byte[])requestJson["session"];
                responseJson.Add("isValid", ServerSession.getInstance().isSessionValid(sessionKey));
                return(responseJson);

            case "save/":
                sessionKey = (byte[])requestJson["session"];
                validateSession(sessionKey);
                string name = (string)requestJson["name"];
                Console.WriteLine("Attempt to save map: " + name);

                using (StreamWriter file = File.CreateText(Path.Combine(getDir(), name + ".json")))
                    using (JsonTextWriter writer = new JsonTextWriter(file))
                    {
                        requestJson.WriteTo(writer);
                    }
                return(responseJson);

            default:
                Console.WriteLine("Path requested");
                sessionKey = (byte[])requestJson["session"];

                validateSession(sessionKey);
                int[][] mapArray;

                JArray arr = (JArray)requestJson["map"];

                int algorithm = (requestJson["algorithm"] != null) ? (int)requestJson["algorithm"] : 0;

                if (requestJson["settings"] != null)
                {
                    JArray settings = (JArray)requestJson["settings"];
                    mapArray = applyCustomOptions(arr.ToObject <int[][]>(), settings.ToObject <int[]>());
                }
                else
                {
                    mapArray = arr.ToObject <int[][]>();
                }

                switch (algorithm)
                {
                case 0:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculatePathRandom(mapArray)));
                    return(responseJson);

                case 1:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculateAStar(mapArray)));
                    return(responseJson);

                case 2:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculateDijkstra(mapArray)));
                    return(responseJson);

                case 3:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculateGeneric(mapArray)));
                    return(responseJson);

                case 4:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculatePathOwn(mapArray)));
                    return(responseJson);

                case 5:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculatePathDumb(mapArray)));
                    return(responseJson);

                default:
                    responseJson.Add("data", JArray.FromObject(PathfindingApi.calculatePathRandom(mapArray)));
                    return(responseJson);
                }
            }
        }