forked from CAWCAWCAW/Lottery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cs
119 lines (113 loc) · 4.03 KB
/
Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using System.Data;
using System.Linq;
using System.Threading;
using TShockAPI;
using Terraria;
using TerrariaApi.Server;
using Newtonsoft.Json;
using Wolfje.Plugins.SEconomy;
namespace Lottery
{
public class Configfile
{
public static Config config;
private static void CreateConfig()
{
string filepath = Path.Combine(TShock.SavePath, "LotteryConfig.json");
try
{
using (var stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (var sr = new StreamWriter(stream))
{
config = new Config();
var configString = JsonConvert.SerializeObject(config, Formatting.Indented);
sr.Write(configString);
}
stream.Close();
}
}
catch (Exception ex)
{
Log.ConsoleError(ex.Message);
}
}
public static bool SaveConfig()
{
string filepath = Path.Combine(TShock.SavePath, "LotteryConfig.json");
if (File.Exists(filepath))
{
File.WriteAllText(filepath, JsonConvert.SerializeObject(config, Formatting.Indented));
Log.ConsoleInfo("[Lottery] The lottery total has been saved.");
return true;
}
else
{
Log.ConsoleError("Lottery config not found. Creating new one...");
CreateConfig();
return false;
}
}
public static bool ReadConfig()
{
string filepath = Path.Combine(TShock.SavePath, "LotteryConfig.json");
try
{
if (File.Exists(filepath))
{
using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var sr = new StreamReader(stream))
{
var configString = sr.ReadToEnd();
config = JsonConvert.DeserializeObject<Config>(configString);
}
stream.Close();
}
return true;
}
else
{
Log.ConsoleError("Lottery config not found. Creating new one...");
CreateConfig();
return false;
}
}
catch (Exception ex)
{
Log.ConsoleError(ex.Message);
}
return false;
}
public class Config
{
public int MinimumLotteryNumber = 0;
public int MaximumLotteryNumber = 5000;
public int LotteryReminderTimeMinutes = 1;
public string RandomHintRanges = "The range below is plus or minus, and is randomly assigned each lottery.";
public int RandomHintRange = 200;
public int GeneralHelpTimerInMinutes = 1;
public int GeneralHelpRange = 50;
public int ReductionAmount = 100;
public int MinimumCashVoteAmount = 0;
public string LotteryTotalStartAmount = "0p0g0s0c";
public string HelpMessage = "Example of how to guess: </lottery guess 210 25g5s> or /lottery guess 234 25500000";
public bool LotteryCanSave = true;
public int LotterySaveTimerMinutes = 5;
//public bool AwardOnPartialCorrect = false;
}
private void Reload_Config(CommandArgs args)
{
if (ReadConfig())
{
args.Player.SendMessage("Lottery config reloaded sucessfully.", Color.Yellow);
}
else
{
args.Player.SendErrorMessage("Lottery config reloaded unsucessfully. Check logs for details.");
}
}
}
}