Exemple #1
0
        /// <summary>
        /// 升级设备 根据deviceId
        /// </summary>
        /// <param name="upgradeInfos">待处理列表</param>
        /// <returns></returns>
        public static IEnumerable <DeviceErr> UpgradeClient(UpgradeInfos upgradeInfos)
        {
            var res = new List <DeviceErr>();

            if (upgradeInfos.Infos.Any())
            {
                switch (upgradeInfos.Type)
                {
                case 1:
                    res.AddRange(UpgradeScript(upgradeInfos.Infos));
                    break;

                case 2:
                    res.AddRange(UpgradeFirmware(upgradeInfos.Infos));
                    break;

                case 3:
                    res.AddRange(upgradeInfos.Infos.Select(x => new DeviceErr(x.DeviceId, Error.ParamError)));
                    break;

                default:
                    res.AddRange(upgradeInfos.Infos.Select(x => new DeviceErr(x.DeviceId, Error.ParamError)));
                    break;
                }
            }
            return(res);
        }
        private static IEnumerable <DeviceErr> HttpResponseErr(UpgradeInfos upgradeInfos)
        {
            var res = new List <DeviceErr>();

            //不存在设备列表
            res.AddRange(upgradeInfos.Infos.Where(x => !_clients.ContainsKey(x.DeviceId)).Select(y => new DeviceErr(y.DeviceId, Error.DeviceNotExist)));
            var leftInfos = upgradeInfos.Infos.Where(x => _clients.ContainsKey(x.DeviceId));

            if (leftInfos.Any())
            {
                var devicesList = _clients.Values.Where(x => leftInfos.Any(y => y.DeviceId == x.DeviceId));
                var serverList  = devicesList.GroupBy(x => x.ServerId).Select(y => y.Key);
                //根据serverId分组
                foreach (var deviceGroup in devicesList.GroupBy(x => x.ServerId))
                {
                    var serverId = deviceGroup.Key;
                    var devices  = devicesList.Where(x => x.ServerId == serverId);
                    //检查serverId是否存在
                    if (!_serversUrl.ContainsKey(serverId))
                    {
                        res.AddRange(devices.Select(device => new DeviceErr(device.DeviceId, Error.NpcServerNotExist)));
                        continue;
                    }
                    var serverInfo = _serversUrl[serverId];

                    var url = serverInfo.Url + UrlMappings.Urls[UrlMappings.batchUpgrade];
                    //向NpcProxyLink请求数据
                    var serverClientInfo = leftInfos.Where(x => devices.Any(y => x.DeviceId == y.DeviceId));
                    var resp             = HttpServer.Post(url, new UpgradeInfos {
                        Type = upgradeInfos.Type, Infos = serverClientInfo.ToList()
                    }.ToJSON());
                    if (resp == "fail")
                    {
                        res.AddRange(devices.Select(device => new DeviceErr(device.DeviceId, Error.ExceptionHappen)));
                        continue;
                    }

                    var funName = "UpgradeClient";
                    try
                    {
                        var result = JsonConvert.DeserializeObject <DataErrResult>(resp);
                        res.AddRange(result.datas);
                    }
                    catch (Exception e)
                    {
                        res.AddRange(devices.Select(device => new DeviceErr(device.DeviceId, Error.AnalysisFail)));
                        Log.ErrorFormat("{0} Res:{1}, Error:{2}", funName, resp, e.Message);
                    }
                }
                if (res.Any(x => x.errno == Error.Success))
                {
                    LoadClients(serverList);
                }
            }
            return(res);
        }
        public IEnumerable <DeviceErr> UpgradeClient(UpgradeInfos upgradeInfos)
        {
            var res = new List <DeviceErr>();

            if (upgradeInfos.Infos.Any())
            {
                res.AddRange(HttpResponseErr(upgradeInfos));
            }
            return(res);
        }
 void SetOnClickListener()
 {
     GetComponent <Button>().onClick.AddListener(delegate
     {
         if (UpgradeInfos.EnoughCoinsForUpgrade(upgradeType))
         {
             GameManager.refrence.coinsInfo.ReduceCoins(UpgradeSlotGUI.UpgradeInfos.GetUpgradeCost(upgradeType));
             UpgradeInfos.Upgrade(upgradeType);
             AudioManager.refrence.PlaySound(AudioManager.E_AUDIO.CASH);
             //if max lvl reached disable button
             DisableSlot();
         }
     });
 }
        public UpgradeResult Upgrade([FromBody] UpgradeInfos upgradeInfos)
        {
            if (upgradeInfos == null || !upgradeInfos.Infos.Any())
            {
                return(Result.GenError <UpgradeResult>(Error.ParamError));
            }

            var result = new UpgradeResult {
                Type = upgradeInfos.Type
            };

            result.datas.AddRange(ClientManager.UpgradeClient(upgradeInfos));
            return(result);
        }
    void UpdateSlotGUI()
    {
        //add unique info about what will happen on upgrade
        switch (upgradeType)
        {
        case E_UPGRADE.PER_CLICK: { txt_upgradeInfo.text = Global.GetSeparatedNumber(UpgradeInfos.GetNextUpgradeValue(upgradeType)) + "/click"; } break;

        case E_UPGRADE.PER_SEC: { txt_upgradeInfo.text = Global.GetSeparatedNumber(UpgradeInfos.GetNextUpgradeValue(upgradeType)) + "/sec"; } break;

        case E_UPGRADE.BONUS_PER_CLICK: { txt_upgradeInfo.text = "X" + Global.GetSeparatedNumber(UpgradeInfos.GetNextUpgradeValue(upgradeType)) + "/click"; } break;

        case E_UPGRADE.BONUS_PER_SEC: { txt_upgradeInfo.text = "X" + Global.GetSeparatedNumber(UpgradeInfos.GetNextUpgradeValue(upgradeType)) + "/sec"; } break;

        case E_UPGRADE.BONUS_STUN: { txt_upgradeInfo.text = "Sloth stunned for " + Global.GetSeparatedNumber(UpgradeInfos.GetNextUpgradeValue(upgradeType)) + " sec"; } break;
        }
        //add text info about level of upgrade
        txt_upgradeLvl.text = "LVL:" + UpgradeInfos.lvlsDictionary[upgradeType];
        //set text for price
        txt_upgradePrice.text = Global.GetSeparatedNumber(UpgradeInfos.GetUpgradeCost(upgradeType));
    }
        private static IEnumerable <DeviceErr> SocketResponseErr(UpgradeInfos upgradeInfos)
        {
            var res = new List <DeviceErr>();

            //不存在设备列表
            res.AddRange(upgradeInfos.Infos.Where(x => !_clients.ContainsKey(x.DeviceId)).Select(y => new DeviceErr(y.DeviceId, Error.DeviceNotExist)));
            var leftInfos = upgradeInfos.Infos.Where(x => _clients.ContainsKey(x.DeviceId));

            if (leftInfos.Any())
            {
                var devicesList   = _clients.Values.Where(x => leftInfos.Any(y => y.DeviceId == x.DeviceId));
                var clientSockets = _clientSockets.Where(x => x.SocketState == SocketState.Connected).GroupBy(x => x.ServerId)
                                    .ToDictionary(x => x.Key, x => x.First());
                //根据serverId分组
                foreach (var deviceGroup in devicesList.GroupBy(x => x.ServerId))
                {
                    var serverId = deviceGroup.Key;
                    var devices  = devicesList.Where(x => x.ServerId == serverId);
                    //检查serverId是否存在
                    if (!clientSockets.ContainsKey(serverId))
                    {
                        res.AddRange(devices.Select(device => new DeviceErr(device.DeviceId, Error.NpcServerNotExist)));
                        continue;
                    }

                    var serverClientInfo = leftInfos.Where(x => devices.Any(y => x.DeviceId == y.DeviceId));
                    var clientSocket     = clientSockets[serverId];
                    var guid             = StringHelper.CreateGuid();
                    var msgType          = NpcSocketMsgType.UpgradeClient;
                    var funName          = msgType.ToString();
                    var msg = new NpcSocketMsg
                    {
                        Guid    = guid,
                        MsgType = msgType,
                        Body    = new UpgradeInfos {
                            Type = upgradeInfos.Type, Infos = serverClientInfo.ToList()
                        }.ToJSON()
                    };
                    Console.WriteLine($"{DateTime.Now} {msg.MsgType} {(serverClientInfo.Select(x => x.DeviceId).ToJSON())} -----------{serverId} Send");
                    clientSocket.Send(msg);
                    var          t            = 0;
                    var          sw           = Stopwatch.StartNew();
                    NpcSocketMsg npcSocketMsg = null;
                    while (sw.ElapsedMilliseconds < _tryCount)
                    {
                        if (NpcSocketMsgs.ContainsKey(guid))
                        {
                            npcSocketMsg = NpcSocketMsgs[guid];
                            NpcSocketMsgs.TryRemove(guid, out _);
                            break;
                        }
                        //if (t > _tryCount)
                        //{
                        //    break;
                        //}
                        //t++;
                        Thread.Sleep(1);
                    }

                    if (npcSocketMsg != null)
                    {
                        try
                        {
                            var result = JsonConvert.DeserializeObject <DataErrResult>(npcSocketMsg.Body);
                            res.AddRange(result.datas);
                        }
                        catch (Exception e)
                        {
                            res.AddRange(devices.Select(device => new DeviceErr(device.DeviceId, Error.AnalysisFail)));
                            Log.ErrorFormat("{0} Res:{1}, Error:{2}", funName, npcSocketMsg.Body, e.Message);
                        }
                    }
                    else
                    {
                        res.AddRange(devices.Select(device => new DeviceErr(device.DeviceId, Error.Fail)));
                    }
                }
                if (res.Any(x => x.errno == Error.Success))
                {
                    GetFromServer(clientSockets.Keys);
                }

                //if (res.Any(x => x.errno == Error.Success))
                //{
                //    switch (upgradeInfos.Type)
                //    {
                //        case 1:
                //            foreach (var r in res)
                //            {
                //                _clients[r.DeviceId].DeviceState = DeviceState.UpgradeScript;
                //            }
                //            break;
                //        case 2:
                //            foreach (var r in res)
                //            {
                //                _clients[r.DeviceId].DeviceState = DeviceState.UpgradeFirmware;
                //            }
                //            break;
                //        case 3:
                //            break;
                //        default:
                //            break;
                //    }
                //}
            }
            return(res);
        }