move code into mhapi package

main
Bryce Allen 11 years ago
parent e92b5c71ad
commit 661a055d5c

@ -3,8 +3,8 @@
from __future__ import print_function from __future__ import print_function
import codecs import codecs
import mhdb from mhapi.db import MHDB
import mhprob from mhapi import stats
SKILL_CARVING = "carving" SKILL_CARVING = "carving"
SKILL_CAP = "cap" SKILL_CAP = "cap"
@ -56,7 +56,7 @@ class QuestReward(object):
self.skill_delta = 0 self.skill_delta = 0
self.evs = self._calculate_ev() self.evs = self._calculate_ev()
def expected_value(self, luck_skill=mhprob.LUCK_SKILL_NONE, def expected_value(self, luck_skill=stats.LUCK_SKILL_NONE,
cap_skill=None, carving_skill=None): cap_skill=None, carving_skill=None):
return self.evs[luck_skill] return self.evs[luck_skill]
@ -70,9 +70,9 @@ class QuestReward(object):
self.skill_delta = 0 self.skill_delta = 0
else: else:
# variable reward, expected number of draws depends on luck skill # variable reward, expected number of draws depends on luck skill
counts = [mhprob.quest_reward_expected_c(self.slot, skill) counts = [stats.quest_reward_expected_c(self.slot, skill)
for skill in xrange(mhprob.LUCK_SKILL_NONE, for skill in xrange(stats.LUCK_SKILL_NONE,
mhprob.LUCK_SKILL_GREAT+1)] stats.LUCK_SKILL_GREAT+1)]
evs = [((count - self.fixed_rewards) evs = [((count - self.fixed_rewards)
@ -121,7 +121,7 @@ class QuestItemExpectedValue(object):
return (len(self.slot_rewards["A"]) > 0 return (len(self.slot_rewards["A"]) > 0
or len(self.slot_rewards["B"]) > 0) or len(self.slot_rewards["B"]) > 0)
def expected_value(self, luck_skill=mhprob.LUCK_SKILL_NONE, def expected_value(self, luck_skill=stats.LUCK_SKILL_NONE,
cap_skill=None, carving_skill=None): cap_skill=None, carving_skill=None):
return self.total_expected_values[luck_skill] return self.total_expected_values[luck_skill]
@ -177,8 +177,8 @@ class HuntReward(object):
self.evs = self._calculate_evs() self.evs = self._calculate_evs()
def expected_value(self, strategy, luck_skill=None, def expected_value(self, strategy, luck_skill=None,
cap_skill=mhprob.CAP_SKILL_NONE, cap_skill=stats.CAP_SKILL_NONE,
carving_skill=mhprob.CARVING_SKILL_NONE): carving_skill=stats.CARVING_SKILL_NONE):
if strategy == STRAT_CAP: if strategy == STRAT_CAP:
if not self.cap: if not self.cap:
return 0 return 0
@ -213,9 +213,9 @@ class HuntReward(object):
self.cap = False self.cap = False
self.kill = True self.kill = True
counts = [ counts = [
3 + mhprob.carve_delta_expected_c(skill) 3 + stats.carve_delta_expected_c(skill)
for skill in xrange(mhprob.CARVING_SKILL_PRO, for skill in xrange(stats.CARVING_SKILL_PRO,
mhprob.CARVING_SKILL_GOD+1) stats.CARVING_SKILL_GOD+1)
] ]
elif self.condition == "Body Carve (Apparent Death)": elif self.condition == "Body Carve (Apparent Death)":
# assume one carve, it's dangerous to try for two # assume one carve, it's dangerous to try for two
@ -227,18 +227,18 @@ class HuntReward(object):
self.cap = True self.cap = True
self.kill = True self.kill = True
counts = [ counts = [
1 + mhprob.carve_delta_expected_c(skill) 1 + stats.carve_delta_expected_c(skill)
for skill in xrange(mhprob.CARVING_SKILL_PRO, for skill in xrange(stats.CARVING_SKILL_PRO,
mhprob.CARVING_SKILL_GOD+1) stats.CARVING_SKILL_GOD+1)
] ]
elif self.condition == "Capture": elif self.condition == "Capture":
self.skill = SKILL_CAP self.skill = SKILL_CAP
self.cap = True self.cap = True
self.kill = False self.kill = False
counts = [ counts = [
mhprob.capture_reward_expected_c(skill) stats.capture_reward_expected_c(skill)
for skill in xrange(mhprob.CAP_SKILL_NONE, for skill in xrange(stats.CAP_SKILL_NONE,
mhprob.CAP_SKILL_GOD+1) stats.CAP_SKILL_GOD+1)
] ]
else: else:
counts = [1] counts = [1]
@ -276,8 +276,8 @@ class HuntItemExpectedValue(object):
self._set_rewards(hunt_rewards) self._set_rewards(hunt_rewards)
def expected_value(self, strategy, luck_skill=None, def expected_value(self, strategy, luck_skill=None,
cap_skill=mhprob.CAP_SKILL_NONE, cap_skill=stats.CAP_SKILL_NONE,
carving_skill=mhprob.CARVING_SKILL_NONE): carving_skill=stats.CARVING_SKILL_NONE):
ev = 0 ev = 0
for reward in self.matching_rewards: for reward in self.matching_rewards:
ev += reward.expected_value(strategy, ev += reward.expected_value(strategy,
@ -317,11 +317,11 @@ def print_monsters_and_rewards(db, item_row, out):
kill_ev = [0, 0] kill_ev = [0, 0]
kill_ev[0] = hunt_item.expected_value(STRAT_KILL) kill_ev[0] = hunt_item.expected_value(STRAT_KILL)
kill_ev[1] = hunt_item.expected_value(STRAT_KILL, kill_ev[1] = hunt_item.expected_value(STRAT_KILL,
carving_skill=mhprob.CARVING_SKILL_GOD) carving_skill=stats.CARVING_SKILL_GOD)
cap_ev = [0, 0] cap_ev = [0, 0]
cap_ev[0] = hunt_item.expected_value(STRAT_CAP) cap_ev[0] = hunt_item.expected_value(STRAT_CAP)
cap_ev[1] = hunt_item.expected_value(STRAT_CAP, cap_ev[1] = hunt_item.expected_value(STRAT_CAP,
cap_skill=mhprob.CAP_SKILL_GOD) cap_skill=stats.CAP_SKILL_GOD)
shiny_ev = hunt_item.expected_value(STRAT_SHINY) shiny_ev = hunt_item.expected_value(STRAT_SHINY)
out.write(" %20s\n" % "= Totals") out.write(" %20s\n" % "= Totals")
out.write(" %20s %s / 100\n" out.write(" %20s %s / 100\n"
@ -367,10 +367,10 @@ def print_quests_and_rewards(db, item_row, out):
kill_ev[0] += hunt_item.expected_value(STRAT_KILL) kill_ev[0] += hunt_item.expected_value(STRAT_KILL)
kill_ev[1] += hunt_item.expected_value(STRAT_KILL, kill_ev[1] += hunt_item.expected_value(STRAT_KILL,
carving_skill=mhprob.CARVING_SKILL_GOD) carving_skill=stats.CARVING_SKILL_GOD)
cap_ev[0] += hunt_item.expected_value(STRAT_CAP) cap_ev[0] += hunt_item.expected_value(STRAT_CAP)
cap_ev[1] += hunt_item.expected_value(STRAT_CAP, cap_ev[1] += hunt_item.expected_value(STRAT_CAP,
cap_skill=mhprob.CAP_SKILL_GOD) cap_skill=stats.CAP_SKILL_GOD)
shiny_ev = hunt_item.expected_value(STRAT_SHINY) shiny_ev = hunt_item.expected_value(STRAT_SHINY)
if kill_ev[0] == 0 and cap_ev[0] == 0 and shiny_ev == 0: if kill_ev[0] == 0 and cap_ev[0] == 0 and shiny_ev == 0:
@ -406,8 +406,8 @@ if __name__ == '__main__':
# TODO: doesn't work if script is symlinked # TODO: doesn't work if script is symlinked
db_path = os.path.dirname(sys.argv[0]) db_path = os.path.dirname(sys.argv[0])
db_path = os.path.join(db_path, "db", "mh4u.db") db_path = os.path.join(db_path, "..", "db", "mh4u.db")
db = mhdb.MHDB(db_path) db = MHDB(db_path)
item_row = find_item(db, item_name, err_out) item_row = find_item(db, item_name, err_out)
if item_row is None: if item_row is None:

@ -2,7 +2,7 @@
from flup.server.fcgi import WSGIServer from flup.server.fcgi import WSGIServer
from mhapi_wsgi import application from mhapi.web.wsgi import application
if __name__ == '__main__': if __name__ == '__main__':
WSGIServer(application).run() WSGIServer(application).run()

@ -6,8 +6,8 @@ import threading
from webob import Request, Response, exc from webob import Request, Response, exc
import mhdb from mhapi.db import MHDB
import mhrewards from mhapi import rewards
DB_VERSION = "20150313" DB_VERSION = "20150313"
PREFIX = "/mhapi/" PREFIX = "/mhapi/"
@ -21,7 +21,7 @@ logging.basicConfig(filename="/tmp/reward_webapp.log", level=logging.INFO)
class ThreadLocalDB(threading.local): class ThreadLocalDB(threading.local):
def __init__(self, path): def __init__(self, path):
threading.local.__init__(self) threading.local.__init__(self)
self._db = mhdb.MHDB(path) self._db = MHDB(path)
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self._db, name) return getattr(self._db, name)
@ -30,7 +30,8 @@ class ThreadLocalDB(threading.local):
class App(object): class App(object):
def __init__(self): def __init__(self):
self.web_path = os.path.dirname(__file__) self.web_path = os.path.dirname(__file__)
self.project_path = os.path.abspath(os.path.join(self.web_path, "..")) self.project_path = os.path.abspath(os.path.join(self.web_path,
"..", ".."))
db_path = os.path.join(self.project_path, "db", "mh4u.db") db_path = os.path.join(self.project_path, "db", "mh4u.db")
self.db = ThreadLocalDB(db_path) self.db = ThreadLocalDB(db_path)
@ -78,9 +79,9 @@ class App(object):
if not item_name: if not item_name:
resp.body = "Please enter an item name" resp.body = "Please enter an item name"
else: else:
item_row = mhrewards.find_item(self.db, item_name, resp.body_file) item_row = rewards.find_item(self.db, item_name, resp.body_file)
if item_row is not None: if item_row is not None:
mhrewards.print_quests_and_rewards(self.db, item_row, rewards.print_quests_and_rewards(self.db, item_row,
resp.body_file) resp.body_file)
return resp return resp
Loading…
Cancel
Save