Skip to content

Commit

Permalink
Item ID is now parsed too.
Browse files Browse the repository at this point in the history
* Decode method refactored to be a class method on TSMAccounting.
  • Loading branch information
OwlManAtt committed Sep 29, 2011
1 parent 57af599 commit 474a4ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
5 changes: 5 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
== 1.1.0 ==
* The item ID is now parsed (in the case of random-enchantment items, the ID
is parsed but the enchantment data is discarded) and emitted during CSV
creation.

== 1.0.1 ==
* Worst QA engineer. Worst.

Expand Down
59 changes: 33 additions & 26 deletions lib/tsm-accounting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
require 'csv'

module TSMAccounting
VERSION = '1.0.1'
VERSION = '1.1.0'

class Database
class InvalidDatabaseFileException < RuntimeError
Expand All @@ -53,7 +53,7 @@ def initialize(db_string)

def to_csv(output_file)
CSV.open(output_file, 'w') do |f|
f << ['Realm','Faction','Transaction Type','Time','Item','Quantity','Stack Size','Price (g)','Price (c)','Buyer','Seller']
f << ['Realm','Faction','Transaction Type','Time','Item ID','Item Name','Quantity','Stack Size','Price (g)','Price (c)','Buyer','Seller']

@data.each do |realm,factions|
factions.each do |faction,ropes|
Expand All @@ -62,6 +62,7 @@ def to_csv(output_file)
item.transactions.each do |tx|
row = [realm,faction,type]
row << tx.datetime.strftime('%Y-%m-%d %k:%M:%S')
row << item.id
row << item.name
row << tx.quantity
row << tx.stack_size
Expand Down Expand Up @@ -190,15 +191,15 @@ def parse_rope(rope,type)
end # Database

class Item
attr_reader :name, :transactions
attr_reader :id, :name, :transactions

def initialize(item,type)
encoded_item, encoded_records = item.split '!'

if encoded_item[0,1] == 'x'
@name = decode_code(encoded_item)
@id, @name = decode_code(encoded_item)
else
@name = decode_link(encoded_item)
@id, @name = decode_link(encoded_item)
end
@transactions = encoded_records.split('@').map {|record| Transaction.new(record,type) }
@transactions ||= []
Expand All @@ -207,8 +208,14 @@ def initialize(item,type)
protected
def decode_link(text)
colour, code, name = text.split '|'

# In the case of items with random enchantments (ie
# Jasper Ring of the Bedrock), the item ID is the same for
# all rings (52310) but there is an additional attribute
# seperated by a colon to identify the enchantment.

return name
id, ench = code.split ':'
return [TSMAccounting.decode(id), name]
end # decode_link

# I _think_ this will only get called if the item link cannot
Expand All @@ -217,7 +224,7 @@ def decode_link(text)
#
# In theory, this shouldn't get called...
def decode_code(text)
return text
return [nil, text]
end # decode_string
end # item

Expand All @@ -227,10 +234,10 @@ class Transaction
def initialize(encoded_string,type)
d = encoded_string.split('#')

@stack_size = decode(d[0])
@quantity = decode(d[1])
@datetime = Time.at(decode(d[2]))
@price = decode(d[3])
@stack_size = TSMAccounting.decode(d[0])
@quantity = TSMAccounting.decode(d[1])
@datetime = Time.at(TSMAccounting.decode(d[2]))
@price = TSMAccounting.decode(d[3])

if type == 'purchase'
@buyer = d[5]
Expand Down Expand Up @@ -261,21 +268,21 @@ def usable_price
return "#{parts['gold']}.#{parts['silver']}".to_f
end

protected
def decode(value)
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_="
base = alpha.length

i = value.length - 1
result = 0
value.each_char do |w|
if w.match(/([A-Za-z0-9_=])/)
result += (alpha.index(w)) * (base**i)
i -= 1
end
end # Transaction

def self.decode(value)
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_="
base = alpha.length

i = value.length - 1
result = 0
value.each_char do |w|
if w.match(/([A-Za-z0-9_=])/)
result += (alpha.index(w)) * (base**i)
i -= 1
end
end

return result
end # decode
end # Transaction
return result
end # decode
end # TSMAccounting

0 comments on commit 474a4ea

Please sign in to comment.