public void Download(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq mediaid, DataTable dt) { CoCAPI.CoCMediaSetInfo cms = null; if ( (dt.Rows.Count == 0) || ((cms = CoCMediaSet.GetMediaSet(mediaid)) == null) ) { return; } try { this._CheckMediaDir(cms); foreach (DataRow dr in dt.Rows) { if ((dr["ico"] == null) || (string.IsNullOrWhiteSpace((string)dr["ico"]))) { continue; } foreach (int i in cms.msize) { string mediaPath = Path.Combine( this._basepath, cms.mpath[0], cms.mpath[1], cms.mdir, i.ToString(), (string)dr["ico"] + ".png" ); if (File.Exists(mediaPath)) { continue; } this._MediaDownload( string.Format( @"{0}/{1}/{2}/{3}.png", Properties.Settings.Default.CoCMediaURL, cms.mdir, i.ToString(), (string)dr["ico"] ), mediaPath ); } } } catch (Exception e) { throw e; } }
private static bool _CheckReturnString(string jsonOut, stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, stCore.IMessage iLog = null) { if (string.IsNullOrWhiteSpace(jsonOut)) { string serverError = string.Format( Properties.Resources.CoCGetUrlEmpty, type.ToString() ); stCore.LogException.Error(serverError, iLog); return(false); } #if DEBUG_CHECKCURL stConsole.WriteHeader("Json Out (CheckReturnString): " + jsonOut); #endif if (jsonOut.Contains("\"reason\":")) { string serverError = String.Empty; DataTable dt1 = jsonOut.JsonToDataTable(); #if DEBUG_CHECKCURL dt1.DataTableToPrint(); #endif if ((dt1 != null) && (dt1.Rows.Count > 0)) { serverError = string.Format( Properties.Resources.CoCGetUrlServerError, ((dt1.Rows[0].Table.Columns.Contains("reason")) ? stConsole.SplitCapitalizeString((string)dt1.Rows[0]["reason"]) : Properties.Resources.CurlReturnReason ), ((dt1.Rows[0].Table.Columns.Contains("message")) ? dt1.Rows[0]["message"] : Properties.Resources.CurlReturnReason ) ); } else { serverError = string.Format( Properties.Resources.CoCGetUrlEmpty, type.ToString() ); } throw new CoCDBExceptionReason(serverError); } return(true); }
private static bool _CheckArgs(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string key, string clanTag, stCore.IMessage iLog = null) { if (type == stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.None) { stCore.LogException.Error(Properties.Resources.CoCRequestIdEmpty, iLog); return(false); } if (string.IsNullOrWhiteSpace(clanTag)) { stCore.LogException.Error(Properties.Resources.CoCClanIdEmpty, iLog); return(false); } if (string.IsNullOrWhiteSpace(key)) { stCore.LogException.Error(Properties.Resources.CoCKeyEmpty, iLog); return(false); } return(true); }
public static string GetCoCUrlSingle(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string key, string clanTag, string curlexe, string rootpath, stCore.IMessage iLog = null) { if (!CoCRequest._CheckArgs(type, key, clanTag, iLog)) { return(String.Empty); } string reqUrl = CoCRequest.MakeCoCUrl(type, clanTag); if (string.IsNullOrWhiteSpace(reqUrl)) { stCore.LogException.Error(Properties.Resources.CoCUrlEmpty, iLog); return(String.Empty); } #if DEBUG_CHECKCURL stConsole.WriteHeader("Get URL: " + reqUrl); #endif dynamic ccl = null; try { ccl = CoCRequest._InitCUrlObj(curlexe, key, rootpath, iLog); string jsonOut = ccl.GetJson(reqUrl, ((stRuntime.isRunTime()) ? null : key)); if (!CoCRequest._CheckReturnString(jsonOut, type, iLog)) { return(String.Empty); } return(jsonOut); } catch (Exception e) { stCore.LogException.Error(e, iLog); return(String.Empty); } finally { if (ccl != null) { ccl.Dispose(); } } }
private static string MakeCoCUrl(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string clanTag) { bool[] opUrl; switch (type) { case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.None: { return(String.Empty); } case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Clan: case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Members: case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Warlog: { opUrl = new bool[] { true, true }; break; } case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.ClanRank: { opUrl = new bool[] { true, false }; break; } case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Leagues: case stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Locations: { opUrl = new bool[] { false, false }; break; } default: { opUrl = new bool[] { false, true }; break; } } return(string.Format( cocTemplateUrl[type], cocBaseUrl, ((opUrl[0]) ? ((opUrl[1]) ? CoCDataUtils.TagToUrl(clanTag) : clanTag) : "") )); }
public static string GetCoCUrlMulti(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string key, string clanTag, dynamic curlobj, stCore.IMessage iLog = null) { if (curlobj == null) { stCore.LogException.Error(Properties.Resources.CurlObjEmpty, iLog); return(String.Empty); } if (!CoCRequest._CheckArgs(type, key, clanTag, iLog)) { return(String.Empty); } string reqUrl = CoCRequest.MakeCoCUrl(type, clanTag); if (string.IsNullOrWhiteSpace(reqUrl)) { stCore.LogException.Error(Properties.Resources.CoCUrlEmpty, iLog); return(String.Empty); } #if DEBUG_CHECKCURL stConsole.WriteHeader("Get URL: " + reqUrl); #endif try { string jsonOut = curlobj.GetJson(reqUrl, ((stRuntime.isRunTime()) ? null : key)); if (!CoCRequest._CheckReturnString(jsonOut, type, iLog)) { return(String.Empty); } return(jsonOut); } catch (Exception e) { stCore.LogException.Error(e, iLog); return(String.Empty); } }
private void _updateAllList <T>(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string jsonIn) where T : class, new() { DataTable dt1 = null, dt2 = null; string query = string.Format( @"SELECT * FROM {0}", typeof(T).Name ); CoCAPI.CoCMediaDownload md = null; try { dt1 = stSqlite.SqliteConvertExtension.JsonToDataTable <T>(jsonIn, true, false); if ((dt1 == null) || (dt1.Rows.Count == 0)) { stCore.LogException.Error( string.Format( Properties.Resources.DataTableEmptyIn, typeof(T).Name ), this._parent._ilog ); return; } dt2 = this._parent.dbMgr.Query(query); if (dt2 == null) { dt2 = SqliteConvertExtension.MapToDataTable <T>(); } foreach (DataRow row in dt1.Rows) { DataRow dr = dt2.AsEnumerable() .Where(r => (Convert.ToInt32(r["id"]) == Convert.ToInt32(row["id"]))) .FirstOrDefault(); if (dr == null) { dr = dt2.NewRow(); this._setDataTableRow(ref dr, row, dt1.Columns); dt2.Rows.Add(dr); } else { this._setDataTableRow(ref dr, row, dt1.Columns, "id"); } } dt1.Clear(); if (!this._parent.dbMgr.Update(query, dt2)) { throw new ArgumentException(typeof(T).Name); } md = new CoCAPI.CoCMediaDownload(this._ccl, this._parent.RootPath, this._parent); md.Download(type, dt2); #if DEBUG_LIST dt2.DataTableToPrint(); #endif dt2.Clear(); } catch (Exception e) { #if DEBUG_LIST stConsole.WriteHeader(e.ToString()); throw e; #else stCore.LogException.Error(e, this._parent._ilog); return; #endif } finally { if (md != null) { md.Dispose(); } } }
private void _updateWarlog(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string jsonIn) { DataTable dt1 = null, dt2 = null; string query = string.Format( Properties.Settings.Default.DBSysUpdateSelect, typeof(WarLog).Name ); CoCAPI.CoCMediaDownload md = null; try { dt1 = stSqlite.SqliteConvertExtension.JsonToDataTable <WarLog>(jsonIn, true, false); if ((dt1 == null) || (dt1.Rows.Count == 0)) { stCore.LogException.Error( string.Format( Properties.Resources.DataTableEmptyIn, typeof(WarLog).Name ), this._parent._ilog ); return; } dt2 = this._parent.dbMgr.Query(query); if (dt2 == null) { dt2 = SqliteConvertExtension.MapToDataTable <WarLog>(); } #if DEBUG_WARLOG else { if (dt2.Rows.Count > 0) { dt2.DataTableToPrint(); } } #endif foreach (DataRow row in dt1.Rows) { DataRow dr = dt2.AsEnumerable() .Where(r => r.Field <DateTime>("dtend").ToString() .Equals(row.Field <string>("dtend"))) .FirstOrDefault(); if (dr == null) { dr = dt2.NewRow(); this._setDataTableRow(ref dr, row, dt1.Columns, "dtend"); dr["dtend"] = stCoCAPI.CoCAPI.CoCDataUtils.FieldConvertDateTime((string)row["dtend"]); dr["cdestruct"] = CoCDataUtils.DestructionWar((double)row["cdestruct"]); dr["opdestruct"] = CoCDataUtils.DestructionWar((double)row["opdestruct"]); dt2.Rows.Add(dr); } } dt1.Clear(); if (this._parent._cocNotifier != null) { this._parent.NotifyEvent(type, dt2); } if (!this._parent.dbMgr.Update(query, dt2)) { throw new ArgumentException(typeof(WarLog).Name); } md = new CoCAPI.CoCMediaDownload(this._ccl, this._parent.RootPath, this._parent); md.Download(CoCEnum.CoCFmtReq.Warlog, dt2); #if DEBUG_WARLOG dt2.DataTableToPrint(); #endif dt2.Clear(); } catch (Exception e) { #if DEBUG_WARLOG stConsole.WriteHeader(e.ToString()); throw e; #else stCore.LogException.Error(e, this._parent._ilog); return; #endif } finally { if (md != null) { md.Dispose(); } } }
private void _updateClan(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string jsonIn) { DataTable dt1 = null, dt2 = null; CoCAPI.CoCMediaDownload md = null; int[] intSeason = stCoCAPI.CoCAPI.CoCSeason.GetSeasonDateInt(); try { dt1 = stSqlite.SqliteConvertExtension.JsonToDataTable <ClanInfo>(jsonIn, true, false); if ((dt1 == null) || (dt1.Rows.Count == 0)) { stCore.LogException.Error( string.Format( Properties.Resources.DataTableEmptyIn, typeof(ClanInfo).Name ), this._parent._ilog ); return; } dt2 = this._parent.dbMgr.Query(Properties.Settings.Default.DBSysUpdateClanInfo); if (dt2 == null) { dt2 = SqliteConvertExtension.MapToDataTable <ClanInfo>(); } #if DEBUG_CLAN else { if (dt2.Rows.Count > 0) { dt2.DataTableToPrint(); } } #endif foreach (DataRow row in dt1.Rows) { DataRow dr = dt2.AsEnumerable() .Where(r => r.Field <string>("tag") .Equals(row.Field <string>("tag"))) .FirstOrDefault(); if (dr == null) { dr = dt2.NewRow(); this._setDataTableRow(ref dr, row, dt1.Columns); dr["dtup"] = DateTime.Now; dt2.Rows.Add(dr); } else { this._setDataTableRow(ref dr, row, dt1.Columns, "tag"); dr["dtup"] = DateTime.Now; } } dt1.Clear(); if (this._parent._cocNotifier != null) { this._parent.NotifyEvent(type, dt2); } if (!this._parent.dbMgr.Update(Properties.Settings.Default.DBSysUpdateClanInfo, dt2)) { throw new ArgumentException(typeof(ClanMember).Name); } md = new CoCAPI.CoCMediaDownload(this._ccl, this._parent.RootPath, this._parent); md.Download(CoCEnum.CoCFmtReq.Clan, dt2); #if DEBUG_CLAN dt2.DataTableToPrint(); #endif dt2.Clear(); } catch (Exception e) { #if DEBUG_CLAN stConsole.WriteHeader(e.ToString()); throw e; #else stCore.LogException.Error(e, this._parent._ilog); return; #endif } finally { if (md != null) { md.Dispose(); } } }
private void _updateMember(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string jsonIn) { DataTable dt1 = null, dt2 = null; int[] intSeason = stCoCAPI.CoCAPI.CoCSeason.GetSeasonDateInt(); try { dt1 = stSqlite.SqliteConvertExtension.JsonToDataTable <ClanMember>(jsonIn, true, false); if ((dt1 == null) || (dt1.Rows.Count == 0)) { stCore.LogException.Error( string.Format( Properties.Resources.DataTableEmptyIn, typeof(ClanMember).Name ), this._parent._ilog ); return; } dt2 = this._parent.dbMgr.Query(Properties.Settings.Default.DBSysUpdateMembers); if (dt2 == null) { dt2 = SqliteConvertExtension.MapToDataTable <ClanMember>(); } #if DEBUG_MEMBERS else { if (dt2.Rows.Count > 0) { dt2.DataTableToPrint(); } } #endif bool isNewTable = ((dt2.Rows.Count > 0) ? false : true); if (!isNewTable) { dt2.AsEnumerable().Where(dr => Convert.ToInt32(dr["status"]) > 0) .Select(dri => dri["status"] = 2) .ToList(); } foreach (DataRow row in dt1.Rows) { DataRow dr = dt2.AsEnumerable() .Where(r => r.Field <string>("tag") .Equals(row.Field <string>("tag"))) .FirstOrDefault(); double ratio = CoCDataUtils.DonationsRatio((int)row["send"], (int)row["receive"]); if (dr == null) { dr = dt2.NewRow(); this._setDataTableRow(ref dr, row, dt1.Columns); dr["status"] = 1; dr["ratio"] = ratio; dr["season"] = intSeason[1]; dr["year"] = intSeason[0]; dr["dtin"] = DateTime.Now; dr["note"] = ""; dt2.Rows.Add(dr); } else { this._setDataTableRow(ref dr, row, dt1.Columns, "tag"); dr["status"] = 1; dr["ratio"] = ratio; } } dt1.Clear(); if (!isNewTable) { dt2.AsEnumerable().Where(r => Convert.ToInt32(r["status"]) == 2) .Select(dr => { dr["status"] = 0; dr["dtout"] = DateTime.Now; return(dr); }).ToList(); } if (this._parent._cocNotifier != null) { this._parent.NotifyEvent(type, dt2); } if (!this._parent.dbMgr.Update(Properties.Settings.Default.DBSysUpdateMembers, dt2)) { throw new ArgumentException(typeof(ClanMember).Name); } #if DEBUG_MEMBERS dt2.DataTableToPrint(); #endif dt2.Clear(); } catch (Exception e) { #if DEBUG_MEMBERS stConsole.WriteHeader(e.ToString()); throw e; #else stCore.LogException.Error(e, this._parent._ilog); return; #endif } }
public static void InformerWebRequest(string url, object ctx, object udata) { stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq reqtype = stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.None; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, String.Empty, context ); return; } Int32 idx = 0, resize = 2; byte[] msg = null; string[] urlPart = url.Split('/'); urlPart = urlPart.Skip(1).Concat(urlPart.Take(1)).ToArray(); Array.Resize(ref urlPart, urlPart.Length - 1); string memberId = "", langid = stNet.stWebServerUtil.HttpUtil.GetHttpPreferedLanguage(context.Request.UserLanguages); try { if (urlPart.Length < 3) { throw new ArgumentOutOfRangeException(); } if (!Int32.TryParse(urlPart[2], out idx)) { throw new ArgumentOutOfRangeException(); } switch (urlPart[1]) { case "info": case "clan": { reqtype = stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Clan; if (urlPart.Length != 4) { throw new ArgumentOutOfRangeException(); } break; } case "player": case "member": { reqtype = stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Members; if (urlPart.Length != 5) { throw new ArgumentOutOfRangeException(); } if (urlPart[3].Equals("random")) { urlPart[1] = urlPart[3]; resize = 3; } else { if ((urlPart[3].Length > 12) || (urlPart[3].Length < 6)) { throw new ArgumentOutOfRangeException(); } urlPart[2] = urlPart[3]; } memberId = urlPart[3]; break; } default: { throw new ArgumentOutOfRangeException(); } } Array.Resize(ref urlPart, urlPart.Length - resize); } catch (Exception) { conf.HttpSrv.BadRequestRaw(conf.Api.InformerImageError(reqtype, langid), context, (int)HttpStatusCode.BadRequest, HttpUtil.MimeType.MimePng); return; } try { stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq cReq = stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None; string query = string.Empty; try { query = conf.Api.GetQueryString(urlPart, ref cReq, conf.Opt.SQLDBFilterMemberTag.collection, conf.ILog.LogError); if ( (cReq == stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None) || (string.IsNullOrWhiteSpace(query)) ) { throw new ArgumentNullException(); } } catch (Exception) { throw new ArgumentException(); } #if DEBUG_PrintWebRequest stConsole.WriteHeader("InformerWebRequest -> URL: (" + url + ") Query: (" + query + ")"); #endif DataTable dt = conf.Api.QueryData(query); if ((dt == null) || (dt.Rows.Count == 0)) { throw new ArgumentNullException(); } #if DEBUG_PrintJson stConsole.WriteHeader("InformerWebRequest -> JSON: " + dt.ToJson(false, false)); #endif #if DEBUG_PrintDataTable dt.DataTableToPrint(); #endif msg = conf.Api.InformerImageGet(dt.Rows[0], reqtype, idx, memberId, langid, conf.Opt.WEBCacheEnable.bval); #if DEBUG_PrintImageInfo stConsole.WriteHeader("InformerWebRequest -> ImageInfo: " + msg.Length); #endif } catch (Exception) { conf.HttpSrv.BadRequestRaw( conf.Api.InformerImageError(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Auth, langid), context, (int)HttpStatusCode.InternalServerError, HttpUtil.MimeType.MimePng ); return; } try { #if DEBUG_NoCache context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "no-cache"); context.Response.AddHeader(conf.HttpSrv.httpAccelBuffering, "no"); #else context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "max-age=" + conf.Api.UpdateNextSeconds.ToString() + ", public"); context.Response.AddHeader(conf.HttpSrv.httpAccelBuffering, "yes"); context.Response.AddHeader(conf.HttpSrv.httpLastModified, conf.Api.UpdateLastTime.ToString("R")); #endif context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimePng)); context.Response.AddHeader(conf.HttpSrv.httpAccessControlAllowOrigin, "*"); context.Response.ContentLength64 = msg.Length; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "Informer"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); }