From c8ae45d091f8a97c5a94d1d8032721984dff94c4 Mon Sep 17 00:00:00 2001 From: wushin Date: Fri, 26 Sep 2014 12:14:03 -0500 Subject: Making ManaMarket Public --- stats/process_salelog/Readme | 1 + stats/process_salelog/main.py | 59 +++++++++++++++ stats/process_salelog/main_stat.py | 151 +++++++++++++++++++++++++++++++++++++ stats/process_salelog/utils.py | 82 ++++++++++++++++++++ stats/update_sales.sh | 5 ++ 5 files changed, 298 insertions(+) create mode 100644 stats/process_salelog/Readme create mode 100755 stats/process_salelog/main.py create mode 100755 stats/process_salelog/main_stat.py create mode 100644 stats/process_salelog/utils.py create mode 100755 stats/update_sales.sh (limited to 'stats') 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 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 + Copyright 2011, Stefan Beller + + 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(' \n') + out_obj.write(' \n') + out_obj.write(' \n') + out_obj.write('ManaMarket Sales \n') + out_obj.write('

ManaMarket Sales

') + out_obj.write(' \n') + out_obj.write('\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('\n') + sale_total += int(line[2]) + items_sold += int(line[1]) + + out_obj.write('
Item Name Amount Price Time
'+ItemDB.getItem(int(line[0])).name+' '+locale.format("%d", int(line[1]), grouping=True)+' '+locale.format("%d", unit_price, grouping=True)+' '+time.asctime(t_time)+'
\n') + out_obj.write('
Total sales: '+str(sale_total)+' GP
') + out_obj.write('Total number of items sold: '+str(items_sold)+'
') + out_obj.write('Updated: '+time.asctime(time.gmtime())) + out_obj.write(' \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 + Copyright 2011, Stefan Beller + + 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(' \n \ + \n \ + ManaMarket Statistics \n \ + \n \ + \n \ + \n \ +

ManaMarket Statistics

\n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \n \ + \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('') + + 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,''+''+ \ + ''+''+''+ ''+''+'']) + + 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('') + out_obj.write(str_list[pos][1]) + str_list.pop(pos) + + + out_obj.write(' \n') + + out_obj.write('
Item Name Total Amount SoldPriceLast Sold
MinMaxAverage (Week)Average (Month)Average (Overall)
'+ItemDB.getItem(item).name+''+locale.format("%d", average_all_time_amount, grouping=True)+''+ locale.format("%d", min_price, grouping=True) +''+ locale.format("%d", max_price, grouping=True) +''+ \ + locale.format("%d", average_week, grouping=True) +''+ locale.format("%d", average_month, grouping=True) + \ + ''+ locale.format("%d", average_all_time, grouping=True)+''+ time.asctime(time.gmtime(last_sold)) +'
\n') + out_obj.write('
Total sales: '+str(sale_total)+' GP
') + out_obj.write('Updated: '+time.asctime(time.gmtime())+'
') + out_obj.write('For a detailed page showing all sales '+'click here') + + out_obj.write(' \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 + Copyright 2011, Stefan Beller + + 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 -- cgit v1.2.3-70-g09d2