server_controller.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import json
  2. from bottle import request, response
  3. import model
  4. from util import debug
  5. def missing_attributes(attributes):
  6. for attr in attributes:
  7. if attr not in request.json:
  8. if str(attr) == 'session_id':
  9. return 'You are not signed in.'
  10. return 'Missing value for attribute ' + str(attr)
  11. if str(attr) == 'session_id':
  12. if not model.valid_session_id(request.json['session_id']):
  13. return 'You are not signed in.'
  14. return False
  15. def login():
  16. missing = missing_attributes(['username', 'password'])
  17. if missing:
  18. return bad_request(missing)
  19. username = request.json['username']
  20. password = request.json['password']
  21. session_id = model.login(username, password)
  22. if session_id:
  23. return {'session_id': session_id}
  24. else:
  25. return forbidden('Invalid login data')
  26. def depot():
  27. missing = missing_attributes(['session_id'])
  28. if missing:
  29. return bad_request(missing)
  30. data = model.get_user_ownership(model.get_user_id_by_session_id(request.json['session_id']))
  31. return {'data': data}
  32. def register():
  33. missing = missing_attributes(['username', 'password'])
  34. if missing:
  35. return bad_request(missing)
  36. username = request.json['username']
  37. password = request.json['password']
  38. if model.user_exists(username):
  39. return forbidden('User already exists.')
  40. game_key = ''
  41. if 'game_key' in request.json:
  42. game_key = request.json['game_key'].strip().upper()
  43. if game_key != '' and game_key not in model.unused_keys():
  44. return bad_request('Game key is not valid.')
  45. if model.register(username, password, game_key):
  46. return {'message': "successfully registered user"}
  47. else:
  48. return bad_request('registration not successful')
  49. def activate_key():
  50. missing = missing_attributes(['key', 'session_id'])
  51. if missing:
  52. return bad_request(missing)
  53. if model.valid_key(request.json['key']):
  54. user_id = model.get_user_id_by_session_id(request.json['session_id'])
  55. model.activate_key(request.json['key'], user_id)
  56. return {'message': "successfully activated key"}
  57. else:
  58. return bad_request('Invalid key.')
  59. def order():
  60. missing = missing_attributes(['buy', 'session_id', 'amount', 'ownable'])
  61. if missing:
  62. return bad_request(missing)
  63. buy = request.json['buy']
  64. sell = not buy
  65. session_id = request.json['session_id']
  66. amount = request.json['amount']
  67. ownable_name = request.json['ownable']
  68. ownable_id = model.ownable_id_by_name(ownable_name)
  69. user_id = model.get_user_id_by_session_id(session_id)
  70. model.own(user_id, ownable_id)
  71. ownership_id = model.ownership_id(ownable_id, user_id)
  72. limit = None
  73. if 'limit' in request.json:
  74. limit = request.json['limit']
  75. stop_loss = 'stop_loss' in request.json and bool(request.json['stop_loss']) # TODO is this a string or a bool?
  76. if sell:
  77. if not model.user_owns_at_least(user_id, amount, ownership_id):
  78. return bad_request('You can not sell more than you own.')
  79. if buy:
  80. if not model.user_owns_at_least(user_id, amount, model.kollar_id()):
  81. return bad_request('You do not have the money to buy that much.')
  82. model.place_order(buy, user_id, ownable_id, limit, stop_loss, amount)
  83. return {'message': "Order placed."}
  84. def orders():
  85. missing = missing_attributes(['session_id'])
  86. if missing:
  87. return bad_request(missing)
  88. data = model.get_user_orders(model.get_user_id_by_session_id(request.json['session_id']))
  89. return {'data': data}
  90. def news():
  91. missing = missing_attributes(['session_id'])
  92. if missing:
  93. return bad_request(missing)
  94. return {'data': model.news()}
  95. def not_found(msg=''):
  96. response.status = 404
  97. if debug:
  98. msg = str(response.status) + ': ' + msg
  99. response.content_type = 'application/json'
  100. return json.dumps({"error_message": msg})
  101. def forbidden(msg=''):
  102. response.status = 403
  103. if debug:
  104. msg = str(response.status) + ' Forbidden: ' + msg
  105. response.content_type = 'application/json'
  106. return json.dumps({"error_message": msg})
  107. def bad_request(msg=''):
  108. response.status = 400
  109. if debug:
  110. msg = str(response.status) + ' Bad request: ' + msg
  111. response.content_type = 'application/json'
  112. return json.dumps({"error_message": msg})