Created
July 19, 2014 18:21
-
-
Save AxisKriel/d94e0d5990180160070f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using TShockAPI; | |
using Terraria; | |
namespace ItemExchanger | |
{ | |
public class ILog : IDisposable | |
{ | |
public ILog(); | |
private static string path = Path.Combine(TShock.SavePath, "ItemExchanger", "ExchangeLog.txt"); | |
public StreamReader Reader { get; protected set; } | |
public StreamWriter Writer { get; protected set; } | |
public List<Exchange> Exchanges { get; protected set; } | |
public void Initialize() | |
{ | |
var dir = Path.GetDirectoryName(path); | |
if (!Directory.Exists(dir)) | |
{ | |
Directory.CreateDirectory(dir); | |
} | |
Writer = File.CreateText(path); | |
Reader = new StreamReader(path); | |
Exchanges = LoadExchanges(); | |
} | |
public void Dispose() | |
{ | |
Writer.Dispose(); | |
Reader.Dispose(); | |
} | |
public bool LogExchange(Exchange exchange) | |
{ | |
try | |
{ | |
var reqs = String.Join(",", ParseToString(exchange.Recipe.Requirements)); | |
var result = String.Join(", ", ParseToString(exchange.Recipe.Result)); | |
var str = String.Format("Player ({0}) exchanged recipe ({1}), giving ({2}) and receiving ({3}).", | |
exchange.Player.Name, | |
exchange.Recipe.Name, | |
reqs, | |
result); | |
Writer.WriteLine(str); | |
Exchanges.Add(exchange); | |
} | |
catch (Exception ex) | |
{ | |
Log.ConsoleError(ex.ToString()); | |
return false; | |
} | |
return true; | |
} | |
public List<Exchange> LoadExchanges() | |
{ | |
var list = new List<Exchange>(); | |
for (int i = 0; i < File.ReadLines(path).Count(); i++) | |
{ | |
list.Add(ParseLine(Reader.ReadLine())); | |
} | |
return list; | |
} | |
public List<Exchange> GetExchanges(TSPlayer ply) | |
{ | |
return Exchanges.Where(e => e.Player == ply).ToList(); | |
} | |
List<string> ParseToString(List<Item> items) | |
{ | |
var list = new List<string>(); | |
foreach (var item in items) | |
{ | |
list.Add(String.Format("{0} {1}", item.stack, item.name)); | |
} | |
return list; | |
} | |
Dictionary<int, int> ParseItems(string items) | |
{ | |
var dic = new Dictionary<int, int>(); | |
string name = String.Empty; | |
string stack = String.Empty; | |
int reading = 0; | |
foreach (char ch in items) | |
{ | |
switch (ch) | |
{ | |
case ',': | |
dic.Add( | |
TShock.Utils.GetItemByName(name).First().netID, | |
Int32.Parse(stack)); | |
break; | |
default: | |
if (Char.IsNumber(ch)) | |
{ | |
stack += ch; | |
} | |
else if (Char.IsWhiteSpace(ch) && reading == 0) | |
reading++; | |
else | |
name += ch; | |
break; | |
} | |
} | |
return dic; | |
} | |
Exchange ParseLine(string line) | |
{ | |
bool reading = false; | |
int pos = 0; | |
var reader = new[] | |
{ | |
String.Empty, | |
String.Empty, | |
String.Empty, | |
String.Empty | |
}; | |
var exchange = new Exchange(); | |
foreach (char ch in line) | |
{ | |
switch (ch) | |
{ | |
case '(': | |
reading = true; | |
break; | |
case ')': | |
reading = false; | |
pos++; | |
break; | |
default: | |
if (reading) | |
{ | |
reader[pos] += ch; | |
} | |
break; | |
} | |
} | |
exchange.Active = false; | |
exchange.Player = TShock.Utils.FindPlayer(reader[0]).First(); | |
exchange.Recipe = new Recipe( | |
reader[1], | |
ParseItems(reader[2]), | |
ParseItems(reader[3]), | |
null); | |
return exchange; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment