server_controller.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 or request.json[attr] == '':
  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', 'time_until_expiration'])
  61. if missing:
  62. return bad_request(missing)
  63. if not model.ownable_name_exists(request.json['ownable']):
  64. return bad_request('This kind of object can not be ordered.')
  65. buy = request.json['buy']
  66. sell = not buy
  67. session_id = request.json['session_id']
  68. amount = request.json['amount']
  69. amount = float(amount)
  70. ownable_name = request.json['ownable']
  71. time_until_expiration = float(request.json['time_until_expiration'])
  72. if time_until_expiration < 0:
  73. return bad_request('Invalid expiration time.')
  74. ownable_id = model.ownable_id_by_name(ownable_name)
  75. user_id = model.get_user_id_by_session_id(session_id)
  76. model.own(user_id, ownable_name)
  77. ownership_id = model.get_ownership_id(ownable_id, user_id)
  78. try:
  79. if request.json['limit'] == '':
  80. limit = None
  81. elif request.json['limit'] is None:
  82. limit = None
  83. else:
  84. limit = float(request.json['limit'])
  85. except ValueError: # for example when float fails
  86. limit = None
  87. except KeyError: # for example when limit was not specified
  88. limit = None
  89. try:
  90. if request.json['stop_loss'] == '':
  91. stop_loss = None
  92. elif request.json['stop_loss'] is None:
  93. stop_loss = None
  94. else:
  95. stop_loss = 'stop_loss' in request.json and request.json['stop_loss']
  96. if stop_loss is not None and limit is None:
  97. return bad_request('Can only set stop loss for limit orders')
  98. except KeyError: # for example when stop_loss was not specified
  99. stop_loss = None
  100. # TODO test if stop loss works
  101. if sell:
  102. if not model.user_owns_at_least(amount, user_id, ownable_id):
  103. return bad_request('You can not sell more than you own.')
  104. model.place_order(buy, ownership_id, limit, stop_loss, amount, time_until_expiration)
  105. return {'message': "Order placed."}
  106. def orders():
  107. missing = missing_attributes(['session_id'])
  108. if missing:
  109. return bad_request(missing)
  110. data = model.get_user_orders(model.get_user_id_by_session_id(request.json['session_id']))
  111. return {'data': data}
  112. def orders_on():
  113. missing = missing_attributes(['session_id', 'ownable'])
  114. if missing:
  115. return bad_request(missing)
  116. if not model.ownable_name_exists(request.json['ownable']):
  117. return bad_request('This kind of object can not be ordered.')
  118. data = model.get_ownable_orders(model.ownable_id_by_name(request.json['ownable']))
  119. return {'data': data}
  120. def cancel_order():
  121. missing = missing_attributes(['session_id', 'order_id'])
  122. if missing:
  123. return bad_request(missing)
  124. if not model.user_has_order_with_id(request.json['session_id'], request.json['order_id']):
  125. return bad_request('You do not have an order with that number.')
  126. model.delete_order(request.json['order_id'])
  127. return {'message': "Successfully deleted order"}
  128. def news():
  129. missing = missing_attributes(['session_id'])
  130. if missing:
  131. return bad_request(missing)
  132. return {'data': model.news()}
  133. def transactions():
  134. missing = missing_attributes(['session_id', 'ownable'])
  135. if missing:
  136. return bad_request(missing)
  137. if not model.ownable_name_exists(request.json['ownable']):
  138. return bad_request('This kind of object can not have transactions.')
  139. return {'data': model.transactions(model.ownable_id_by_name(request.json['ownable']))}
  140. def leaderboard():
  141. missing = missing_attributes(['session_id'])
  142. if missing:
  143. return bad_request(missing)
  144. return {'data': model.leaderboard()}
  145. def not_found(msg=''):
  146. response.status = 404
  147. if debug:
  148. msg = str(response.status) + ': ' + msg
  149. response.content_type = 'application/json'
  150. return json.dumps({"error_message": msg})
  151. def forbidden(msg=''):
  152. response.status = 403
  153. if debug:
  154. msg = str(response.status) + ': ' + msg
  155. response.content_type = 'application/json'
  156. return json.dumps({"error_message": msg})
  157. def bad_request(msg=''):
  158. response.status = 400
  159. if debug:
  160. msg = str(response.status) + ': ' + msg
  161. response.content_type = 'application/json'
  162. return json.dumps({"error_message": msg})