157 lines
3.3 KiB
C#
157 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace NewsCrawler
|
|
{
|
|
static class Config
|
|
{
|
|
static Dictionary<string, string> m_Data = new Dictionary<string, string>();
|
|
|
|
static Random m_Random = new Random();
|
|
static int m_iDartAPIKeyCnt = 0;
|
|
static bool[] m_abDartAPIKeyLimit = null;
|
|
|
|
public static void Init()
|
|
{
|
|
m_Data.Add("manual-price", "100000");
|
|
m_Data.Add("buy-price", "1000000");
|
|
m_Data.Add("supply-contract-rate", "5.0");
|
|
Load();
|
|
|
|
int iIdx = 1;
|
|
while(true)
|
|
{
|
|
if(m_Data.ContainsKey("dart-api-key"+iIdx) == false)
|
|
break;
|
|
iIdx++;
|
|
}
|
|
m_iDartAPIKeyCnt = iIdx-1;
|
|
|
|
m_abDartAPIKeyLimit = new bool[m_iDartAPIKeyCnt];
|
|
for(int i = 0; i<m_iDartAPIKeyCnt; i++)
|
|
m_abDartAPIKeyLimit[i] = false;
|
|
}
|
|
|
|
static void Load()
|
|
{
|
|
string strPath = Util.GetConfigPath()+"/config.ini";
|
|
if(File.Exists(strPath) == false)
|
|
return;
|
|
|
|
string[] aLines = File.ReadAllLines(strPath);
|
|
foreach(string strLine in aLines)
|
|
{
|
|
if(strLine.Trim().Length <= 0)
|
|
continue;
|
|
|
|
string[] aTokens = strLine.Trim().Split('=');
|
|
if(aTokens.Length < 2)
|
|
continue;
|
|
|
|
if(m_Data.ContainsKey(aTokens[0]) == true)
|
|
m_Data[aTokens[0]] = aTokens[1];
|
|
else
|
|
m_Data.Add(aTokens[0], aTokens[1]);
|
|
}
|
|
}
|
|
|
|
static void Save()
|
|
{
|
|
string strContents = "";
|
|
foreach(KeyValuePair<string, string> pair in m_Data)
|
|
strContents += pair.Key + "=" + pair.Value + Environment.NewLine;
|
|
|
|
string strPath = Util.GetConfigPath()+"/config.ini";
|
|
File.WriteAllText(strPath, strContents, new UTF8Encoding(true));
|
|
}
|
|
|
|
public static int GetManualPrice()
|
|
{
|
|
int iPrice;
|
|
int.TryParse(m_Data["manual-price"], out iPrice);
|
|
return iPrice;
|
|
}
|
|
|
|
public static void SetManualPrice(int iPrice)
|
|
{
|
|
m_Data["manual-price"] = iPrice.ToString();
|
|
Save();
|
|
}
|
|
|
|
public static int GetBuyPrice()
|
|
{
|
|
int iPrice;
|
|
int.TryParse(m_Data["buy-price"], out iPrice);
|
|
return iPrice;
|
|
}
|
|
|
|
public static void SetBuyPrice(int iPrice)
|
|
{
|
|
m_Data["buy-price"] = iPrice.ToString();
|
|
Save();
|
|
}
|
|
|
|
public static void SetAccount(string strAccount, string strAccountSub)
|
|
{
|
|
m_Data["account"] = strAccount;
|
|
m_Data["sub-account"] = strAccountSub;
|
|
Save();
|
|
}
|
|
|
|
public static string GetAccount()
|
|
{
|
|
return m_Data["account"];
|
|
}
|
|
|
|
public static string GetSubAccount()
|
|
{
|
|
return m_Data["sub-account"];
|
|
}
|
|
|
|
public static void SetSupplyContractRate(float fRate)
|
|
{
|
|
m_Data["supply-contract-rate"] = fRate.ToString();
|
|
Save();
|
|
}
|
|
|
|
public static float GetSupplyContractRate()
|
|
{
|
|
float fRate;
|
|
float.TryParse(m_Data["supply-contract-rate"], out fRate);
|
|
return fRate;
|
|
}
|
|
|
|
public static string GetDartAPIKey()
|
|
{
|
|
if(m_iDartAPIKeyCnt <= 0)
|
|
return "";
|
|
|
|
if(m_abDartAPIKeyLimit.All(s => s == true) == true)
|
|
return "";
|
|
|
|
int iNum = -1;
|
|
while(iNum < 0 || m_abDartAPIKeyLimit[iNum-1] == true)
|
|
iNum = m_Random.Next(0, 10000)%m_iDartAPIKeyCnt + 1;
|
|
|
|
return m_Data["dart-api-key"+iNum];
|
|
}
|
|
|
|
public static void SetDartAPIKeyLimit(string strKey)
|
|
{
|
|
for(int i=0; i<m_iDartAPIKeyCnt; i++)
|
|
{
|
|
if(m_Data["dart-api-key"+(i+1).ToString()] == strKey)
|
|
{
|
|
m_abDartAPIKeyLimit[i] = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|