Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Added exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlan committed Nov 21, 2019
1 parent 797d0dc commit f39605d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
31 changes: 25 additions & 6 deletions WotStatistics/Execute/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static async Task MainTask(string[] args)
discord.MessageCreated += async e =>
{
string message = e.Message.Content;
if(message.StartsWith("&"))
if (message.StartsWith("&"))
{
if (message.ToLower().StartsWith(HELP))
{
Expand Down Expand Up @@ -90,21 +90,40 @@ await e.Message.RespondAsync(e.Author.Mention + "\n```Игрок: " + currentPla
" \n" + playerStatistics.ToString() + "```");
}
}
else if(message.ToLower().StartsWith(INFO_CLAN))
else if (message.ToLower().StartsWith(INFO_CLAN))
{
ClanInfo ops = new ClanInfo();
Clan clan = new Clan();
clan.ClanName = parseName(message, INFO_CLAN, typeOfGame, false);
bool resultOperation = ops.GetClan(clan);
if(resultOperation)
try
{
ops.GetClan(clan);
}
catch (ClanNotFound ex)
{
await e.Message.RespondAsync(e.Author.Mention + "\n" + ex.Message);
}
catch
{
string infoMsg = e.Author.Mention + "\nНапишите GRAF или hunterlan об ошибке и пропишите все ваши действия.";
await e.Message.RespondAsync(infoMsg);
}
try
{
ops.GetStat(clan);
await e.Message.RespondAsync(e.Author.Mention + clan.ToString());
}
else
catch (ClanNotFound ex)
{
await e.Message.RespondAsync(e.Author.Mention + "\n" + ex.Message);
}
catch
{
await e.Message.RespondAsync("Невозможно получить данные на этот запрос");
string infoMsg = e.Author.Mention + "\nНапишите GRAF или hunterlan об ошибке и пропишите все ваши действия.";
await e.Message.RespondAsync(infoMsg);
}


}
}
}
Expand Down
14 changes: 14 additions & 0 deletions WotStatistics/WargaminAPI/Exceptions/ClanNotFound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace WargaminAPI.Exceptions
{
public class ClanNotFound : Exception
{
public ClanNotFound(string message): base(message)
{

}
}
}
48 changes: 24 additions & 24 deletions WotStatistics/WargaminAPI/WoT/ClanInfo.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using WargaminAPI.Exceptions;
using WargaminAPI.Model;

namespace WargaminAPI.WoT
{
public class ClanInfo : Info
{
public bool GetClan(Clan currentClan)
public void GetClan(Clan currentClan)
{
urlRequest = resourceMan.GetString("uri_get_clan_by_name") + appID + "&search=" + currentClan.ClanName;
string resultResponse = GetResponse(urlRequest);
JObject parsed = JObject.Parse(resultResponse);
urlRequest = resourceMan.GetString("uri_get_clan_by_name") + appID + "&search=" + currentClan.ClanName;
string resultResponse = GetResponse(urlRequest);
JObject parsed = JObject.Parse(resultResponse);

string status = (string)parsed["status"];
if (status == "ok")
{
try
{
currentClan.ID = int.Parse((string)parsed["data"][0]["clan_id"]);
currentClan.ClanTag = (string)parsed["data"][0]["tag"];
currentClan.CountMembers = int.Parse((string)parsed["data"][0]["members_count"]);
currentClan.CreatedAt = ConvertFromTimestamp(
int.Parse((string)parsed["data"][0]["created_at"]));
}
catch(Exception ex)
string status = (string)parsed["status"];
if (status == "ok")
{
int count = int.Parse((string)parsed["meta"]["count"]);
if(count > 0)
{

}
return true;
currentClan.ID = int.Parse((string)parsed["data"][0]["clan_id"]);
currentClan.ClanTag = (string)parsed["data"][0]["tag"];
currentClan.CountMembers = int.Parse((string)parsed["data"][0]["members_count"]);
currentClan.CreatedAt = ConvertFromTimestamp(
int.Parse((string)parsed["data"][0]["created_at"]));
}
else
{
return false;
throw new ClanNotFound("Клан не найден!");
}
}
else
{
throw new ClanNotFound("Ошибка запроса");
}
}

public string GetNameClan(string id)
Expand Down Expand Up @@ -61,7 +62,7 @@ public string GetNameClan(string id)
}


public bool GetStat(Clan currentClan)
public void GetStat(Clan currentClan)
{
urlRequest = resourceMan.GetString("uri_get_stat_clan") + appID + "&clan_id=" + currentClan.ID;
string resultResponse = GetResponse(urlRequest);
Expand Down Expand Up @@ -90,18 +91,17 @@ public bool GetStat(Clan currentClan)
currentClan.GmEloRating = -1;
}
if (fbStat != null)
{
{
currentClan.FbEloRating = (float)parsed["data"][currentClan.ID.ToString()]["fb_elo_rating"]["value"];
}
else
{
currentClan.FbEloRating = -1;
}
return true;
}
else
{
return false;
throw new ClanNotFound("Неизвестная ошибка");
}
}
}
Expand Down

0 comments on commit f39605d

Please sign in to comment.