summaryrefslogtreecommitdiff
path: root/stats
diff options
context:
space:
mode:
authorwushin <pasekei@gmail.com>2014-09-26 12:14:03 -0500
committerwushin <pasekei@gmail.com>2014-09-26 12:14:03 -0500
commitc8ae45d091f8a97c5a94d1d8032721984dff94c4 (patch)
tree76c5e8ed65baee2add9916ae8769debfaa00184a /stats
downloadmanamarket-c8ae45d091f8a97c5a94d1d8032721984dff94c4.tar.gz
manamarket-c8ae45d091f8a97c5a94d1d8032721984dff94c4.tar.bz2
manamarket-c8ae45d091f8a97c5a94d1d8032721984dff94c4.tar.xz
manamarket-c8ae45d091f8a97c5a94d1d8032721984dff94c4.zip
Making ManaMarket Public
Diffstat (limited to 'stats')
-rw-r--r--stats/process_salelog/Readme1
-rwxr-xr-xstats/process_salelog/main.py59
-rwxr-xr-xstats/process_salelog/main_stat.py151
-rw-r--r--stats/process_salelog/utils.py82
-rwxr-xr-xstats/update_sales.sh5
5 files changed, 298 insertions, 0 deletions
diff --git a/stats/process_salelog/Readme b/stats/process_salelog/Readme
new file mode 100644
index 0000000..6b80ea7
--- /dev/null
+++ b/stats/process_salelog/Readme
@@ -0,0 +1 @@
+Usage: python main.py sales.log <output file>
diff --git a/stats/process_salelog/main.py b/stats/process_salelog/main.py
new file mode 100755
index 0000000..4ad2dd3
--- /dev/null
+++ b/stats/process_salelog/main.py
@@ -0,0 +1,59 @@
+#!/usr/bin/python
+"""
+
+ Copyright 2011, Dipesh Amin <yaypunkrock@gmail.com>
+ Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
+
+ tradey, a package, which implements an Automated Market Bot for "The Mana World" a 2D MMORPG.
+
+"""
+
+import logging
+import socket
+import sys
+import time
+import string
+import utils
+import locale
+
+ItemDB = utils.ItemDB()
+
+def main():
+ in_file = sys.argv[1]
+ out_file = sys.argv[2]
+ locale.setlocale(locale.LC_ALL, '')
+
+ out_obj = open(out_file, 'w')
+ in_obj = open(in_file, 'r')
+
+ out_obj.write('<html> \n')
+ out_obj.write('<body> \n')
+ out_obj.write('<head> \n')
+ out_obj.write('<title>ManaMarket Sales</title> \n')
+ out_obj.write('<h2>ManaMarket Sales</h2>')
+ out_obj.write('<table border="1" cellpadding="5" cellspacing="0"> \n')
+ out_obj.write('<tr bgcolor="6699FF"> <td>Item Name</td> <td>Amount</td> <td>Price</td> <td>Time</td> </tr>\n')
+
+ sales = in_obj.readlines()
+
+ sale_total = 0
+ items_sold = 0
+ for line in sales:
+ line = line.split()
+ t_time = time.gmtime(float(line[3]))
+ unit_price = int(line[2])/int(line[1])
+ out_obj.write('<tr> <td>'+ItemDB.getItem(int(line[0])).name+'</td> <td>'+locale.format("%d", int(line[1]), grouping=True)+'</td> <td>'+locale.format("%d", unit_price, grouping=True)+'</td> <td>'+time.asctime(t_time)+'</td> </tr>\n')
+ sale_total += int(line[2])
+ items_sold += int(line[1])
+
+ out_obj.write('</table> \n')
+ out_obj.write('</br><b>Total sales: '+str(sale_total)+' GP</b> </br>')
+ out_obj.write('<b>Total number of items sold: '+str(items_sold)+'</b></br>')
+ out_obj.write('Updated: '+time.asctime(time.gmtime()))
+ out_obj.write('</body></html> \n')
+ in_obj.close()
+ out_obj.close()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/stats/process_salelog/main_stat.py b/stats/process_salelog/main_stat.py
new file mode 100755
index 0000000..9ff71d5
--- /dev/null
+++ b/stats/process_salelog/main_stat.py
@@ -0,0 +1,151 @@
+#!/usr/bin/python
+"""
+
+ Copyright 2011, Dipesh Amin <yaypunkrock@gmail.com>
+ Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
+
+ tradey, a package, which implements an Automated Market Bot for "The Mana World" a 2D MMORPG.
+
+"""
+
+import logging
+import socket
+import sys
+import time
+import string
+import utils
+import locale
+
+class SaleStat:
+ pass
+
+ItemDB = utils.ItemDB()
+
+def main():
+ in_file = sys.argv[1]
+ out_file = sys.argv[2]
+ locale.setlocale(locale.LC_ALL, '')
+
+ out_obj = open(out_file, 'w')
+ in_obj = open(in_file, 'r')
+
+ out_obj.write('<html> \n \
+ <head> \n \
+ <title>ManaMarket Statistics</title> \n \
+ <style type="text/css"> \n \
+ td, th { border: 1px solid black } \n \
+ table {border: 1px solid black} \n \
+ </style> \n \
+ </head> \n \
+ <body> \n \
+ <h2> ManaMarket Statistics </h2> \n \
+ <table cellpadding="5" cellspacing="0"> \n \
+ <tr> \n \
+ <th rowspan="2"> Item Name</th> \n \
+ <th rowspan="2"> Total Amount Sold</th> \n \
+ <th colspan="5">Price</th> \n \
+ <th rowspan="2">Last Sold</th> \n \
+ </tr> \n \
+ <tr> \n \
+ <td>Min</td> \n \
+ <td>Max</td> \n \
+ <td>Average (Week)</td> \n \
+ <td>Average (Month)</td> \n \
+ <td>Average (Overall)</td> \n \
+ </tr> \n ')
+
+ sales = in_obj.readlines()
+
+ sale_dict = {}
+
+ # Put sales date into a dict.
+ sale_total = 0
+ for line in sales:
+ line = line.split()
+ item_id = int(line[0])
+ name = ItemDB.getItem(int(line[0])).name
+ amount = int(line[1])
+ price = int(line[2])
+ t_time = float(line[3])
+ sale_total += int(line[2])
+
+ if item_id not in sale_dict:
+ sale_dict[item_id] = []
+
+ sale_dict[item_id].append([amount, price, t_time])
+
+ str_list = []
+ # calculate the stats
+ for item in sale_dict:
+ average_week = 0
+ average_week_amount = 0
+ average_month = 0
+ average_month_amount = 0
+ average_all_time = 0
+ average_all_time_amount = 0
+ min_price = 0
+ max_price = 0
+ last_sold = 0
+
+ for n in range(len(sale_dict[item])):
+
+ #out_obj.write('<tr>')
+
+ if min_price > sale_dict[item][n][1]/sale_dict[item][n][0] or min_price == 0:
+ min_price = sale_dict[item][n][1]/sale_dict[item][n][0]
+
+ if max_price < sale_dict[item][n][1]/sale_dict[item][n][0] or max_price == 0:
+ max_price = sale_dict[item][n][1]/sale_dict[item][n][0]
+
+ if last_sold < sale_dict[item][n][2] or last_sold == 0:
+ last_sold = sale_dict[item][n][2]
+
+ if (time.time()-sale_dict[item][n][2]) < 7*24*60*60:
+ average_week_amount += sale_dict[item][n][0]
+ average_week += sale_dict[item][n][1]
+
+ if (time.time()-sale_dict[item][n][2]) < 30*24*60*60:
+ average_month_amount += sale_dict[item][n][0]
+ average_month += sale_dict[item][n][1]
+
+
+ average_all_time_amount += sale_dict[item][n][0]
+ average_all_time += sale_dict[item][n][1]
+
+ average_all_time /= average_all_time_amount
+ if average_week_amount > 0:
+ average_week /= average_week_amount
+ if average_month_amount > 0:
+ average_month /= average_month_amount
+
+ str_list.append([average_all_time_amount,'<td>'+ItemDB.getItem(item).name+'</td>'+'<td>'+locale.format("%d", average_all_time_amount, grouping=True)+'</td>'+ \
+ '<td>'+ locale.format("%d", min_price, grouping=True) +'</td>'+'<td>'+ locale.format("%d", max_price, grouping=True) +'</td>'+'<td>'+ \
+ locale.format("%d", average_week, grouping=True) +'</td>'+ '<td>'+ locale.format("%d", average_month, grouping=True) + \
+ '</td>'+'<td>'+ locale.format("%d", average_all_time, grouping=True)+'</td>'+'<td>'+ time.asctime(time.gmtime(last_sold)) +'</td>'])
+
+ while len(str_list) > 0:
+ pos = 0
+ max_amount = 0
+ for m in range(len(str_list)):
+ if max_amount < str_list[m][0] or max_amount == 0:
+ max_amount = str_list[m][0]
+ pos = m
+ out_obj.write('<tr>')
+ out_obj.write(str_list[pos][1])
+ str_list.pop(pos)
+
+
+ out_obj.write('</tr> \n')
+
+ out_obj.write('</table> \n')
+ out_obj.write('</br><b>Total sales: '+str(sale_total)+' GP</b> </br>')
+ out_obj.write('Updated: '+time.asctime(time.gmtime())+'</br>')
+ out_obj.write('For a detailed page showing all sales '+'<a href="manamarket.html">click here</a>')
+
+ out_obj.write('</body></html> \n')
+ in_obj.close()
+ out_obj.close()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/stats/process_salelog/utils.py b/stats/process_salelog/utils.py
new file mode 100644
index 0000000..fd4e432
--- /dev/null
+++ b/stats/process_salelog/utils.py
@@ -0,0 +1,82 @@
+#!/usr/bin/python
+"""
+ Copyright 2011, Dipesh Amin <yaypunkrock@gmail.com>
+ Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
+
+ This file is part of tradey, a trading bot in the mana world
+ see www.themanaworld.org
+"""
+from xml.etree.ElementTree import ElementTree
+import time
+
+class Item:
+ pass
+
+# Process a recieved ip address.
+def parse_ip(a):
+ return "%s.%s.%s.%s" % ((a % 256),((a >> 8) % 256),((a >> 16) % 256),((a >> 24) % 256))
+
+# Remove colors from a message
+def remove_colors(msg):
+ if len(msg) > 2:
+ for f in range(len(msg)-2):
+ while (len(msg) > f + 2) and (msg[f] == "#")\
+ and (msg[f+1] == "#"):
+ msg = msg[0:f]+msg[f+3:]
+ return msg
+
+# Encode string - used with 4144 shop compatibility.
+def encode_str(value, size):
+ output = ''
+ base = 94
+ start = 33
+ while value:
+ output += chr(value % base + start)
+ value /= base
+
+ while len(output) < size:
+ output += chr(start)
+
+ return output
+
+class ItemDB:
+ """
+ A simple class to look up information from the items.xml file.
+ """
+ def __init__(self):
+ print "Loading ItemDB"
+ self.item_names = {}
+ self.itemdb_file = ElementTree(file="../data/items.xml")
+
+ for item in self.itemdb_file.getroot():
+ if item.get('id') > 500:
+ item_struct = Item()
+ item_struct.name = item.get('name')
+ if item.get('weight'):
+ item_struct.weight = item.get('weight')
+ if item.get('type'):
+ item_struct.type = item.get('type')
+ item_struct.description = item.get('description')
+ self.item_names[int(item.get('id'))] = item_struct
+
+ def getItem(self, item_id):
+ return self.item_names[item_id]
+
+ def findId(self, name):
+ for item_id in self.item_names:
+ if self.item_names[item_id].name == name:
+ return item_id
+ return -10 #Not found
+
+class ItemLog:
+ """ Writes all sales to a log file, for later processing."""
+ def __init__(self):
+ self.log_file = 'data/logs/sale.log'
+
+ def add_item(self, item_id, amount, price):
+ file_node = open(self.log_file, 'a')
+ file_node.write(str(item_id)+" "+str(amount)+" "+str(price)+" "+str(time.time())+"\n")
+ file_node.close()
+
+if __name__ == '__main__':
+ print "Do not run this file directly. Run main.py"
diff --git a/stats/update_sales.sh b/stats/update_sales.sh
new file mode 100755
index 0000000..62a4e1e
--- /dev/null
+++ b/stats/update_sales.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+cd /home/tmwserver/ManaMarket/stats
+python /home/tmwserver/ManaMarket/stats/process_salelog/main.py /home/tmwserver/ManaMarket/data/logs/sale.log /var/www/tmw-homepage/server/manamarket.html
+python /home/tmwserver/ManaMarket/stats/process_salelog/main_stat.py /home/tmwserver/ManaMarket/data/logs/sale.log /var/www/tmw-homepage/server/manamarket_stats.html
+chmod 644 /var/www/tmw-homepage/server/manamarket.html /var/www/tmw-homepage/server/manamarket_stats.html