def receiveFriendShipPoint(): result = dict( type=ProtocolTypes.ReceiveFriendShipPoint, result=ResultCodes.Success ) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'friend_id'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_me = Friend.query.filter_by( user_id=got_user.id, friend_id=got_data['friend_id']).first() if find_me: if not find_me.friendship_sent_date: result['result'] = ResultCodes.InputParamError else: if not find_me.friendship_received_date: find_me.friendship_received_date = datetime.datetime.now() db_session.add(find_me) friendship_point = Config.query.filter_by(str_key='receiving_friendship_point').first() if friendship_point: got_user.friendship_point += int(friendship_point.str_value) db_session.add(got_user) result['added_friendship_point'] = int(friendship_point.str_value) result['result'] = commitData() else: if find_me.friendship_sent_date == datetime.date.today(): if not find_me.friendship_received_date or ( find_me.friendship_received_date != datetime.date.today()): find_me.friendship_received_date = datetime.date.today() db_session.add(find_me) friendship_point = Config.query.filter_by(str_key='receiving_friendship_point').first() if friendship_point: got_user.friendship_point += int(friendship_point.str_value) db_session.add(got_user) result['added_friendship_point'] = int(friendship_point.str_value) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.NoData else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setOwnCostumebases(): result = {'type': ProtocolTypes.SetOwnCostumeBases} if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'own_costumebases'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: if got_data['own_costumebases'] == '': result['result'] = ResultCodes.InputParamError else: for got_costumebase_index in got_data['own_costumebases']: find_costumebase = OwnCostumebase.query.filter_by( user_id=got_user.id, costumebase_index=got_costumebase_index).first() if not find_costumebase: temp_costumebase = OwnCostumebase(got_user.id, got_costumebase_index) db_session.add(temp_costumebase) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def requestFriend(): result = dict( type=ProtocolTypes.RequestFriend, result=ResultCodes.Success) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'request_friend'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_friend = Friend.query.filter_by(user_id=got_user.id, friend_id=got_data['request_friend']).first() if not find_friend: friend_data = Friend(got_user.id, got_data['request_friend']) db_session.add(friend_data) write_mail = Mail(got_user.id, got_data['request_friend'], u"친구 신청") write_mail.request_friend = True db_session.add(write_mail) result['result'] = commitData() else: result['result'] = ResultCodes.DataExist else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setWornCostume(): result = dict( type=ProtocolTypes.SetWornCostume, result=ResultCodes.Success) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'costumes'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_worn_costume = WornCostume.query.filter_by(user_id=got_user.id).first() if find_worn_costume: find_worn_costume.costumes = json.dumps(got_data['costumes']) db_session.add(find_worn_costume) else: made_worn_costume = WornCostume(user_id=got_user.id, costumes=json.dumps(got_data['costumes'])) db_session.add(made_worn_costume) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def addOpenedPuzzle(): result = dict( type=ProtocolTypes.AddPuzzle, result=ResultCodes.Success ) # writeDirtyLog(request.form['data']) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = [ 'session_id', # 'puzzle_index', 'opened' ] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_puzzle = OpenedPuzzle.query.filter_by( user_id=got_user.id, puzzle_index=got_data['puzzle_index']).first() if find_puzzle: find_puzzle.opened = got_data['opened'] db_session.add(find_puzzle) else: made_puzzle = OpenedPuzzle( got_user.id, got_data['puzzle_index'], got_data['opened']) db_session.add(made_puzzle) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def openMail(): result = dict( type=ProtocolTypes.OpenMail, result=ResultCodes.Success ) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'mail_index'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: got_mail = Mail.query.filter_by(id=got_data['mail_index'], to_user_id=got_user.id).first() if got_mail: got_mail.opened = True db_session.add(got_mail) result['result'] = commitData() else: result['result'] = ResultCodes.NoData else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def deleteMails(): result = dict( type=ProtocolTypes.DeleteMails, result=ResultCodes.Success ) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'mail_indexes'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: if len(got_data['mail_indexes']) > 0: got_mails = Mail.query.filter( Mail.id.in_(got_data['mail_indexes'])).all() if got_mails: for got_mail in got_mails: db_session.delete(got_mail) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setButtonState(): result = dict( type=ProtocolTypes.SetButtonState, result=ResultCodes.Success) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'button'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_button = Button.query.filter_by( user_id=got_user.id).first() if find_button: find_button.state = got_data['button'] db_session.add(find_button) else: made_button = Button(got_user.id, got_data['button']) db_session.add(made_button) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setCash(): result = dict( type=ProtocolTypes.SetCash, result=ResultCodes.Success) # writeDirtyLog('setCash: ' + request.form['data']) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = [ 'session_id', 'cash' ] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: got_user.cash = got_data['cash'] or 0 db_session.add(got_user) result['result'] = commitData() else: result['result'] = ResultCodes.NoData else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def addDiary(): result = dict( type=ProtocolTypes.AddDiary, result=ResultCodes.Success ) # writeDirtyLog(request.form['data']) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = [ 'session_id', # 'diary_index' ] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_diary = Diary.query.filter_by( user_id=got_user.id, diary_index=got_data['diary_index']).first() if find_diary: result['result'] = ResultCodes.DataExist else: made_diary = Diary(got_user.id, got_data['diary_index']) db_session.add(made_diary) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setSlots(): result = dict( type=ProtocolTypes.SetSlots, result=ResultCodes.Success ) if request.method == 'POST' and request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'slots'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: got_user.slots = json.dumps(got_data['slots']) db_session.add(got_user) result['result'] = commitData() try: db_session.commit() except exc.SQLAlchemyError: result['result'] = ResultCodes.DBInputError else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setSavedCurrentZone(): result = dict( type=ProtocolTypes.SetSavedCurrentZone, result=ResultCodes.Success) def useFoundData(got_user_id, zone_index, episode, position, rotation): find_current_zone = SavedCurrentZone.query.filter_by( user_id=got_user_id).first() if find_current_zone: find_current_zone.zone_index = zone_index find_current_zone.episode = json.dumps(episode) find_current_zone.position = json.dumps(position) find_current_zone.rotation = json.dumps(rotation) find_current_zone.updated_date = datetime.datetime.now() return find_current_zone else: made_current_zone = SavedCurrentZone(got_user_id, zone_index) made_current_zone.episode = json.dumps(episode) made_current_zone.position = json.dumps(position) made_current_zone.rotation = json.dumps(rotation) made_current_zone.updated_date = datetime.datetime.now() return made_current_zone if request.form['data']: got_data = json.loads(request.form['data']) from_keys = [ 'session_id', 'zone_index', 'episode', 'position', 'rotation'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: db_session.add( useFoundData( got_user.id, got_data['zone_index'], got_data['episode'], got_data['position'], got_data['rotation'])) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setSavedStory(): result = dict( type=ProtocolTypes.SetSavedStory, result=ResultCodes.Success) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = [ 'session_id', 'zone_index', # 'episode_no', # 'wave_no', # 'position', # 'rotation', ] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_story = SavedStory.query.filter_by( user_id=got_user.id).first() if find_story: find_story.zone_index = got_data['zone_index'] find_story.episode_no = got_data['episode_no'] find_story.wave_no = got_data['wave_no'] find_story.position = json.dumps(got_data['position']) find_story.rotation = json.dumps(got_data['rotation']) find_story.updated_date = datetime.datetime.now() db_session.add(find_story) else: made_story = SavedStory( got_user.id, got_data['zone_index'], got_data['episode_no'], got_data['wave_no']) made_story.position = json.dumps(got_data['position']) made_story.rotation = json.dumps(got_data['rotation']) db_session.add(made_story) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def acceptFriend(): #writeDirtyLog('acceptFriend: ' + request.form['data']) result = dict( type=ProtocolTypes.AcceptFriend, result=ResultCodes.Success) if request.method == 'POST' and request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'accept_friend'] if checkContainKeys(from_keys, got_data): if (got_data['accept_friend'] is None) or (got_data['accept_friend'] == ''): result['result'] = ResultCodes.InputParamError else: result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: find_friend = Friend.query.filter_by( user_id=got_data['accept_friend'], friend_id=got_user.id, requested=True, accepted=False).first() if find_friend: find_friend.accepted = True db_session.add(find_friend) my_friend = Friend.query.filter_by( user_id=got_user.id, friend_id=got_data['accept_friend']).first() if my_friend: my_friend.request = True my_friend.accepted = True db_session.add(my_friend) else: regist_friend = Friend(got_user.id, got_data['accept_friend']) regist_friend.request = True regist_friend.accepted = True db_session.add(regist_friend) result['result'] = commitData() else: result['result'] = ResultCodes.NoData else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def gotCashFromCostumeBases(): result = dict( type=ProtocolTypes.GotCashFromCostumeBases, result=ResultCodes.Success) # writeDirtyLog('gotCashFromCostumeBases: ' + request.form['data']) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'costumebases'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: ok_bases = list() for costume_base in got_data['costumebases']: find_own_costumebase = OwnCostumebase.query.filter_by( user_id=got_user.id, costumebase_index=costume_base['index']).first() if find_own_costumebase: find_own_costumebase.lastdate_from_gotcash = datetime.datetime.now() db_session.add(find_own_costumebase) got_user.cash += costume_base['cash'] db_session.add(got_user) result['result'] = commitData() if result['result'] == ResultCodes.Success: ok_bases.append(find_own_costumebase.costumebase_index) else: result['result'] = ResultCodes.NoData if len(ok_bases) > 0: result['costumebase_indexes'] = ok_bases else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def createCharacter(): result = dict( type=ProtocolTypes.CreateCharacter, result=ResultCodes.Success) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'character'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: got_character = Character.query.filter_by(user_id=got_user.id).first() if got_character: result['result'] = ResultCodes.DataExist else: got_character = got_data['character'] got_user.name = got_character['name'] db_session.add(got_user) user_character = Character(got_user.id, got_character['name']) user_character.body_type = got_character['body_type'] user_character.cloak_type = got_character['cloak_type'] user_character.color_r = got_character['color_r'] user_character.color_g = got_character['color_g'] user_character.color_b = got_character['color_b'] user_character.face_type = got_character['face_type'] user_character.gender = got_character['gender'] user_character.hair_type = got_character['hair_type'] user_character.weapon_type = got_character['weapon_type'] db_session.add(user_character) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def writeMail(): result = dict( type=ProtocolTypes.WriteMail, result=ResultCodes.Success ) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'mail_to', 'content'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: got_friend = User.query.filter_by(id=got_data['mail_to']).first() if got_friend: got_mail = Mail( got_user.id, got_friend.id, got_data['content']) db_session.add(got_mail) result['result'] = commitData() else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def setInventories(): result = {'type': ProtocolTypes.SetInventories} if request.method == 'POST' and request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id', 'inventories'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: got_user.inventories = json.dumps(got_data['inventories']) db_session.add(got_user) result['result'] = commitData() # try: # db_session.commit() # except exc.SQLAlchemyError: # result['result'] = ResultCodes.DBInputError else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))
def register(): result = dict( type=ProtocolTypes.RegisterUser, result=ResultCodes.Success ) def checkValidNicknamePassword(nickname, password): import re check_all_letters = lambda given_value: re.match("^[A-Za-z0-9_-]*$", given_value) if ( not check_all_letters(nickname) or not check_all_letters(password) ): return False else: return True def checkReservedNickname(nickname): reserved_nickname = [ 'system', 'admin', 'administrator', 'root' ] if nickname in reserved_nickname: return False else: return True def checkFindNickname(nickname): finding_user = User.query.filter_by(nickname=nickname).first() if finding_user: return True else: return False if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['nickname', 'password'] if checkContainKeys(from_keys, got_data): got_nickname, got_password = got_data['nickname'], got_data['password'] if got_nickname == '' or got_password == '': result["result"] = ResultCodes.InputParamError else: if len(got_nickname) < 4: result['result'] = ResultCodes.ShortNickname elif len(got_password) < 4: result['result'] = ResultCodes.ShortPassword else: if (checkValidNicknamePassword( got_nickname, got_password ) and checkReservedNickname(got_nickname)): if checkFindNickname(got_nickname): result['result'] = ResultCodes.NicknameExist else: user_data = User( got_nickname, got_password) db_session.add(user_data) result['result'] = commitData() if result['result'] == ResultCodes.Success: view_friends.acceptFriendWithTara(got_nickname) else: result["result"] = ResultCodes.InputParamError else: result["result"] = ResultCodes.InputParamError else: result["result"] = ResultCodes.AccessError return str(json.dumps(result))
def useFriendShipPoint(): writeDirtyLog('useFriendShipPoint: ' + request.form['data']) result = dict( type=ProtocolTypes.UseFriendShipPoint, result=ResultCodes.Success ) if request.form['data']: got_data = json.loads(request.form['data']) from_keys = ['session_id'] if checkContainKeys(from_keys, got_data): result['result'], got_user = checkSessionId(got_data['session_id']) if got_user: max_friendship_point = Config.query.filter_by(str_key='max_friendship_point').first() if max_friendship_point: if got_user.friendship_point >= int(max_friendship_point.str_value): got_user.friendship_point -= int(max_friendship_point.str_value) import random got_cash_rates = [16] + [7] * 12 found_rate = random.random() * 100 rate_sum = 0 got_cash_amount = 0 for x in range(len(got_cash_rates)): rate_sum += got_cash_rates[x] if found_rate <= rate_sum: got_cash_amount = x + 1 break except_amount = range(1, 14) except_amount.remove(got_cash_amount) dummy_amount = list() for num in range(4): got_num = random.choice(except_amount) except_amount.remove(got_num) dummy_amount.append(got_num) got_user.cash += got_cash_amount db_session.add(got_user) result['result'] = commitData() result['cash_amount'] = got_cash_amount result['dummy_amount'] = dummy_amount else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.NoData else: result['result'] = ResultCodes.InputParamError else: result['result'] = ResultCodes.AccessError return str(json.dumps(result))