def sign_in(): # вход пользователя _request = { 'token': request.json.get('token', ''), 'nickname': request.json.get('nickname', ''), 'password': request.json.get('password', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() user = db_sess.query(User).filter( User.nickname == _request['nickname']).first() if not user: return jsonify({'result': 'unknown nickname'}) # нет такого пользователя if not user.check_password(_request['password']): # неверный пароль return jsonify({'result': 'incorrect password'}) config.USER_ID = user.id user.get_energy() db_sess.merge(user) db_sess.commit() return jsonify({ 'result': 'OK', 'user': user.to_dict(), })
def create(): _request = { 'token': request.json.get('token', ''), 'input': request.json.get('input', ''), 'output': request.json.get('output', ''), 'input_number': request.json.get('input_number', ''), 'output_number': request.json.get('output_number', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() user = db_sess.query(User).get(config.USER_ID) resources = db_sess.query(Resource).all() new_deal = Deal() # создание сделки res = new_deal.create(_request, user, resources) if res: return jsonify(res) res = user.create_deal(new_deal) if res: return jsonify(res) db_sess.add(new_deal) db_sess.commit() db_sess.merge(user) db_sess.commit() return jsonify({'result': 'OK'})
def accept(): # принять сделку _request = { 'token': request.json.get('token', ''), 'deal_id': request.json.get('deal_id', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() deal = db_sess.query(Deal).get(_request['deal_id']) user = db_sess.query(User).get(config.USER_ID) author = db_sess.query(User).get(deal.user.id) res = user.send_deal(deal) if res: return jsonify(res) author.get_deal(deal) db_sess.delete(deal) db_sess.commit() db_sess.merge(user) db_sess.commit() db_sess.merge(author) db_sess.commit() return jsonify({'result': 'OK'})
def get_bets(): # получить ставки _request = { 'token': request.json.get('token', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() bets = db_sess.query(Bet).all() for bet in bets: if bet.is_complete(): bet.on_complete() db_sess.delete(bet) db_sess.commit() bets = db_sess.query(Bet).all() while len(bets) < random.randint(15, 25): # добавление ставок new_bet = Bet() new_bet.generate() db_sess.add(new_bet) db_sess.commit() bets = db_sess.query(Bet).all() bets = db_sess.query(Bet).all() return jsonify({ 'result': 'OK', 'data': [bet.to_dict() for bet in bets], })
def init(): # инициализация _request = { 'token': request.json.get('token', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() resources = db_sess.query(Resource).all() names = [i.name for i in resources] patterns = eval( open('static/core/resources.txt', 'r', encoding='utf8').read()) # ресурсы for pattern in patterns: if pattern['name'] not in names: # создание ресурсов new_resource = Resource() new_resource.apply_pattern(pattern) db_sess.add(new_resource) db_sess.commit() types = db_sess.query(ProductionType).all() names = [i.name for i in types] patterns = eval( open('static/core/productions.txt', 'r', encoding='utf8').read()) for pattern in patterns: if pattern['title'] not in names: new_type = ProductionType() new_type.apply_pattern(pattern) db_sess.add(new_type) db_sess.commit() return jsonify({'result': 'OK'})
def main(): while True: try: print token insert_source_company().insert_keyno() insert_company().insert_data() insert_company_investment().insert_data() insert_company_branch().insert_data() except AttributeError: print 'token 失效或账号被封' disabled_token = json.dumps(token, encoding="utf-8", ensure_ascii=False) cursor.execute( 'update token_list set token_status=0 where wx_token=%s' % disabled_token) db.commit() global token token = config.check_token() #当所有token都不可用时,发送信息告知 cursor.execute( 'select count(id) from token_list where token_status=1') remain_token = cursor.fetchone()[0] print '剩余token数为:' + str(remain_token) if remain_token == 0: print "无可用token,请添加···" config.send_msg() else: continue time.sleep(120) except NameError: print '已经爬取800条,更换token' disabled_token = json.dumps(token, encoding="utf-8", ensure_ascii=False) cursor.execute( 'update token_list set token_status=0 where wx_token=%s' % disabled_token) db.commit() global token token = config.check_token() continue
def stock_exchange(): # биржа _request = { 'token': request.json.get('token', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() deals = db_sess.query(Deal).all() return {'result': 'OK', 'data': [deal.to_dict() for deal in deals]}
def rating(): # рейтинг пользователей _request = { 'token': request.json.get('token', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() users = db_sess.query(User).all() users = sorted(users, key=lambda user: user.to_dict()['resources']['coin'], reverse=True) return jsonify({ 'result': 'OK', 'data': [user.to_dict() for user in users], })
def promote_production(): _request = { 'token': request.json.get('token', ''), 'production_id': request.json.get('production_id', ''), 'param': request.json.get('param', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() production = db_sess.query(Production).get(_request['production_id']) user = db_sess.query(User).get(config.USER_ID) res = user.promote(production) if res: return jsonify(res) production.promote(_request['param']) db_sess.merge(user) db_sess.commit() db_sess.merge(production) db_sess.commit() return jsonify({'result': 'OK'})
def sign_up(): # регистрация пользователей _request = { 'token': request.json.get('token', ''), 'nickname': request.json.get('nickname', ''), 'password': request.json.get('password', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) if len(_request['nickname']) < 4: return jsonify({'result': 'nickname too short'}) # слишком короткое имя пользователя if len(_request['password']) < 4: return jsonify({'result': 'password too short'}) db_sess = create_session() user = db_sess.query(User).filter( User.nickname == _request['nickname']).first() if user: return jsonify({'result': 'user with same nickname is already exist'}) # добавление пользователя в базу user = User() user.apply_pattern(_request) db_sess.add(user) db_sess.commit() resource_patterns = eval( open('static/core/resources.txt', 'r', encoding='utf8').read()) # добавление ресурсов for pattern in resource_patterns: users_to_resources = UsersToResources() users_to_resources.create(user, pattern) db_sess.add(users_to_resources) db_sess.commit() config.USER_ID = user.id user.get_energy() db_sess.merge(user) db_sess.commit() return jsonify({ 'result': 'OK', 'user': user.to_dict(), })
def start_production(): # начать производство _request = { 'token': request.json.get('token', ''), 'production_id': request.json.get('production_id', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() production = db_sess.query(Production).get(_request['production_id']) if not production.to_dict()['active']: return jsonify({'result': 'production already started'}) user = db_sess.query(User).get(config.USER_ID) res = user.start(production) if res: return jsonify(res) production.start(user) db_sess.merge(user) db_sess.commit() db_sess.merge(production) db_sess.commit() return jsonify({'result': 'OK'})
def get_productions(): # получение ппроизводств _request = { 'token': request.json.get('token', ''), } if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() productions = db_sess.query(Production).all() for production in productions: if production.is_outdated(): db_sess.delete(production) db_sess.commit() production.on_complete() db_sess.merge(production) db_sess.commit() patterns = eval( open('static/core/productions.txt', 'r', encoding='utf8').read()) users = len(db_sess.query(User).all()) for pattern in patterns: productions = db_sess.query(Production).filter( Production.type_id == pattern['type']).all() count = len(productions) if productions else 0 while count * pattern['count'] < users: new_production = Production() new_production.apply_pattern(pattern) db_sess.add(new_production) db_sess.commit() productions = db_sess.query(Production).filter( Production.type_id == pattern['type']).all() count = len(productions) if productions else 0 productions = db_sess.query(Production).all() return { 'result': 'OK', 'data': [production.to_dict() for production in productions] }
def do_bet(): # сделать ставку _request = { 'token': request.json.get('token', ''), 'bet_id': request.json.get('bet_id', ''), 'side': request.json.get('side', ''), 'value': request.json.get('value', ''), } print(_request['value']) if not config.check_token(_request['token']): return jsonify({'result': 'incorrect token'}) db_sess = create_session() bet = db_sess.query(Bet).get(_request['bet_id']) user = db_sess.query(User).get(config.USER_ID) res = user.do_bet(bet, _request['side'], _request['value']) if res: return jsonify(res) bet.update(_request['side'], _request['value']) db_sess.merge(user) db_sess.commit() db_sess.merge(bet) db_sess.commit() return jsonify({'result': 'OK'})