from tg import RestController
from formencode import validators
from mahjong.hand_calculating.hand import HandCalculator
from mahjong.tile import TilesConverter
from mahjong.hand_calculating.hand_config import HandConfig
from mahjong.meld import Meld
# RootController of our web app, in charge of serving content for /
class RootController(RestController):
@expose('json')
@validate({"man":validators.String(not_empty=True), "pin":validators.String(not_empty=True), "sou":validators.String(not_empty=True), "win":validators.String(not_empty=True)})
def calc(self, man, pin, sou, **kw):
# validation_status = tg.request.validation
# errors = [{key, value} in validation_status.errors.iteritems()]
# values = validation_status.values
calculator = HandCalculator()
# tiles = TilesConverter.string_to_136_array(man='22444', pin='333567', sou='444')
tiles = TilesConverter.string_to_136_array(man=man, pin=pin, sou=sou)
win_tile = TilesConverter.string_to_136_array(sou=sou)[0]
result = calculator.estimate_hand_value(tiles, win_tile)
# return dict(han=result.han, fu=result.fu, errors=str(errors), values=str(values))
return dict(han=result.han, fu=result.fu)
# Configure a new minimal application with our root controller.
config = MinimalApplicationConfigurator()
config.update_blueprint({
'root_controller': RootController()
})
# Serve the newly configured web application.
print("Serving on port 8080...")
httpd = make_server('', 8080, config.make_wsgi_app())
httpd.serve_forever()