server_controller.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 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. ownable_name = request.json['ownable']
  70. ownable_id = model.ownable_id_by_name(ownable_name)
  71. user_id = model.get_user_id_by_session_id(session_id)
  72. model.own(user_id, ownable_name)
  73. ownership_id = model.get_ownership_id(ownable_id, user_id)
  74. limit = None
  75. if 'limit' in request.json:
  76. limit = request.json['limit']
  77. stop_loss = 'stop_loss' in request.json and bool(request.json['stop_loss'])
  78. # TODO test if stop loss works
  79. if sell:
  80. if not model.user_owns_at_least(user_id, amount, ownership_id):
  81. return bad_request('You can not sell more than you own.')
  82. model.place_order(buy, ownership_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 transactions():
  96. missing = missing_attributes(['session_id', 'ownable_id'])
  97. if missing:
  98. return bad_request(missing)
  99. if not model.ownable_name_exists(request.json['ownable']):
  100. return bad_request('This kind of object can not have transactions.')
  101. return {'data': model.transactions()}
  102. def not_found(msg=''):
  103. response.status = 404
  104. if debug:
  105. msg = str(response.status) + ': ' + msg
  106. response.content_type = 'application/json'
  107. return json.dumps({"error_message": msg})
  108. def forbidden(msg=''):
  109. response.status = 403
  110. if debug:
  111. msg = str(response.status) + ' Forbidden: ' + msg
  112. response.content_type = 'application/json'
  113. return json.dumps({"error_message": msg})
  114. def bad_request(msg=''):
  115. response.status = 400
  116. if debug:
  117. msg = str(response.status) + ' Bad request: ' + msg
  118. response.content_type = 'application/json'
  119. return json.dumps({"error_message": msg})