Example #1
0
class BreweryService:
    # ==========================================================
    # ---------------------- Service Name ----------------------
    # ==========================================================

    name = 'word_pack'

    # ==========================================================
    # ----------------------- Dependency -----------------------
    # ==========================================================

    database = dependencies.Database()

    # ==========================================================
    # ------------------------ Functions -----------------------
    # ==========================================================

    def __init__(self):
        print("Service Constructor")

    @rpc
    def get_all_wordPack(self):
        brewery = self.database.get_all_wordPack()
        self.database.close_connection()
        return schemas.BookSchema(many=True).dump(brewery)

    @rpc
    def get_wordpack_by_id(self, id):
        brewery = self.database.get_wordpack_by_id(id)
        self.database.close_connection()
        return schemas.BookSchema().dump(brewery)

    @rpc
    def search_wordpack(self, id, review):
        self.database.search_wordpack(id, review)

    def __del__(self):
        print("Service Destructor")
class WordService:
    name = 'word_service'
    database = dependencies.Database()

    def __init__(self):
        print("Service Constructor")

    ### WORDPACK ###
    @rpc
    def create_wordpack(self, data):
        result = {
            'err': 0,
            'msg': 'Wordpack Created'
        }
        self.database.create_wordpack(data)
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_wordpack(self, data):
        result = {
            'err': 0,
            'msg': 'Wordpack Updated'
        }
        if self.database.get_wordpack_by_id(data['id']):
            self.database.update_wordpack(data)
        else:
            result['err'] = 1
            result['msg'] = 'Wordpack Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_wordpack(self, data):
        result = {
            'err': 0,
            'msg': 'Wordpack Deleted'
        }
        if self.database.get_wordpack_by_id(data['id']):
            self.database.delete_wordpack(data)
        else:
            result['err'] = 1
            result['msg'] = 'Wordpack Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_wordpack(self):
        result = self.database.get_all_wordpack()
        return schemas.WordpackSchema(many=True).dump(result)

    @rpc
    def get_wordpack_by_id(self, data):
        result = self.database.get_wordpack_by_id(data['id'])
        return schemas.WordpackSchema().dump(result)

    @rpc
    def search_wordpack(self, data):
        result = self.database.search_wordpack(data['keyword'])
        return schemas.WordpackSchema(many=True).dump(result)

    ### WORDPAIR###
    @rpc
    def create_wordpair(self, data):
        result = {
            'err': 0,
            'msg': 'Wordpair Added'
        }
        if self.database.get_wordpack_by_id(data['id_word_pack']):
            self.database.create_wordpair(data)
        else:
            result['err'] = 1
            result['msg'] = 'Wordpack Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_wordpair(self, data):
        result = {
            'err': 0,
            'msg': 'Wordpair Updated'
        }
        if self.database.get_wordpair_by_id(data['id']):
            self.database.update_wordpair(data)
        else:
            result['err'] = 1
            result['msg'] = 'Wordpair Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_wordpair(self, data):
        result = {
            'err': 0,
            'msg': 'Wordpair Deleted'
        }
        if self.database.get_wordpair_by_id(data['id']):
            self.database.delete_wordpair(data)
        else:
            result['err'] = 1
            result['msg'] = 'Wordpair Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_wordpair(self):
        result = self.database.get_all_wordpair()
        return schemas.WordpairSchema(many=True).dump(result)

    @rpc
    def get_wordpair_by_id(self, data):
        result = self.database.get_wordpair_by_id(data['id'])
        return schemas.WordpairSchema().dump(result)

    @rpc
    def get_all_wordpair_by_wordpack_id(self, data):
        result = self.database.get_all_wordpair_by_wordpack_id(data['id'])
        return schemas.WordpairSchema(many=True).dump(result)
    
    @rpc
    def get_wordpair_by_wordpack_id(self, data):
        result = self.database.get_wordpair_by_wordpack_id(data['id'])
        return schemas.WordpairSchema().dump(result)
        
    def __del__(self):
        print("Service Destructor")
class TransactionService:
    # ==========================================================
    # ---------------------- Service Name ----------------------
    # ==========================================================

    name = 'transaction_service'

    # ==========================================================
    # ----------------------- Dependency -----------------------
    # ==========================================================

    database = dependencies.Database()
    word_rpc = RpcProxy('word_service')
    user_rpc = RpcProxy('user_service')
    transaction_rpc = RpcProxy('transaction_service')

    # ==========================================================
    # -----------------------   Proxy   ------------------------
    # ==========================================================

    # voucher_rpc = RpcProxy('voucher_service')
    # user_rpc = RpcProxy('user_service')
    # wordpack_rpc = RpcProxy('wordpack_service')

    # ==========================================================
    # ------------------------ Functions -----------------------
    # ==========================================================

    def __init__(self):
        print("Service Constructor")

    @rpc
    def get_all_transaction(self):
        transaction = self.database.get_all_transaction()
        self.database.close_connection()
        return schemas.TransactionSchema(many=True).dump(transaction)

    @rpc
    def get_transaction_by_id(self, id):
        transaction = self.database.get_transaction_by_id(id)
        self.database.close_connection()
        return schemas.TransactionSchema().dump(transaction)

    @rpc
    def create_transaction(self, data):
        result = {'err': 0, 'msg': 'Transaction Created'}
        user = self.user_rpc.get_user_by_id(data['id_user'])
        if user:
            wordpack = self.word_rpc.get_wordpack_by_id(data['id_word_pack'])
            if wordpack:
                new_balance = user['balance'] - wordpack['price']
                if new_balance >= 0:
                    data['amount'] = wordpack['price']
                    self.database.create_transaction(data)
                    self.user_rpc.create_user_wordpack({
                        'id_user':
                        data['id_user'],
                        'id_word_pack':
                        data['id_word_pack'],
                        'created_at':
                        None,
                        'updated_at':
                        None
                    })
                    self.user_rpc.update_user({
                        'id': user['id_user'],
                        'balance': new_balance
                    })
                else:
                    result['err'] = 1
                    result['msg'] = 'Balance Not Enough'
            else:
                result['err'] = 1
                result['msg'] = 'Wordpack Not Found'
        else:
            result['err'] = 1
            result['msg'] = 'User Not Found'
        self.database.close_connection()
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_transaction(self, data):
        result = {'err': 0, 'msg': 'Transaction Updated'}

        if self.database.get_transaction_by_id(data['id']):
            self.database.update_transaction(data)

            self.database.close_connection()

        else:
            result['err'] = 1
            result['msg'] = 'Transaction Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_transaction(self, id):
        result = {'err': 0, 'msg': 'Transaction Deleted'}

        if self.database.get_transaction_by_id(id):
            self.database.delete_transaction(id)

            self.database.close_connection()

        else:
            result['err'] = 1
            result['msg'] = 'Transaction Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_voucher(self):
        voucher = self.database.get_all_voucher()
        self.database.close_connection()
        return schemas.VoucherSchema(many=True).dump(voucher)

    @rpc
    def get_voucher_by_id(self, id):
        voucher = self.database.get_voucher_by_id(id)
        self.database.close_connection()
        return schemas.VoucherSchema().dump(voucher)

    @rpc
    def search_voucher(self, code):
        voucher = self.database.search_voucher(code)
        self.database.close_connection()
        return voucher['status'] == 1, schemas.VoucherSchema().dump(voucher)
        print(voucher['status'])

    @rpc
    def create_voucher(self, data):

        result = {'err': 0, 'msg': 'Voucher Created'}

        if self.database.search_voucher(data['code']):
            result['err'] = 1
            result['msg'] = 'Code Already Exists & Active'
        else:
            self.database.create_voucher(data)

            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_voucher(self, data):
        result = {'err': 0, 'msg': 'Voucher Updated'}

        if self.database.get_voucher_by_id(data['id']):
            self.database.update_voucher(data)

            self.database.close_connection()

        else:
            result['err'] = 1
            result['msg'] = 'Voucher Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_voucher(self, id):
        result = {'err': 0, 'msg': 'Voucher Deleted'}

        if self.database.get_voucher_by_id(id):
            self.database.delete_voucher(id)

            self.database.close_connection()

        else:
            result['err'] = 1
            result['msg'] = 'Voucher Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def redeem_voucher(self, data):

        result = {'err': 0, 'msg': 'Voucher Redeemed'}

        if self.database.search_voucher(data['code']):
            if self.transaction_rpc.search_voucher(data['code']):
                # self.database.redeem_voucher(data)
                # result['msg'] = 'Redeem Successfully'
                # self.database.close_connection()
                if self.user_rpc.get_user_by_id(
                        data['redeemed_by']):  #cek user ada atau ga
                    tmp = self.user_rpc.get_user_by_id(data['redeemed_by'])
                    tmp1 = self.transaction_rpc.search_voucher(data['code'])

                    uawal = int(tmp['balance'])
                    topup = tmp1[1]['amount']
                    uakhir = uawal + topup
                    statusvoc = tmp1[0]

                    if statusvoc:
                        self.user_rpc.update_user({
                            "id": data['redeemed_by'],
                            "balance": uakhir
                        })
                        self.database.redeem_voucher(data)
                        now = datetime.now()
                        dt_string = now.strftime("%Y-%m-%d %H:%M:%S")
                        print(dt_string)
                        result['err'] = 0
                        result[
                            'msg'] = 'Voucher Redeemed by' + " " + tmp['name']
                    else:
                        result['err'] = 0
                        result['msg'] = 'Voucher Cant be Redeemed'

                    self.database.close_connection()
                else:
                    result['err'] = 1
                    result['msg'] = 'User tidak ada'
                    self.database.close_connection()
            else:
                result['err'] = 1
                result['msg'] = 'Code Already Inactive'
                self.database.close_connection()
        else:
            result['err'] = 1
            result['msg'] = 'Code Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    def __del__(self):
        print("Service Destructor")
class GameService:
    name = 'game_service'
    database = dependencies.Database()
    user_rpc = RpcProxy('user_service')
    word_rpc = RpcProxy('word_service')

    def __init__(self):
        print("Service Constructor")

    ### GAME ###
    @rpc
    def create_game(self, data):
        result = {
            'err': 0,
            'msg': 'Game Created'
        }
        self.database.create_game(data)
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_game(self, data):
        result = {
            'err': 0,
            'msg': 'Game Updated'
        }
        if self.database.get_game_by_id(data['id']):
            self.database.update_game(data)
        else:
            result['err'] = 1
            result['msg'] = 'Game Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_game(self, data):
        result = {
            'err': 0,
            'msg': 'Game Deleted'
        }
        if self.database.get_game_by_id(data['id']):
            self.database.delete_game(data)
        else:
            result['err'] = 1
            result['msg'] = 'Game Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_game(self):
        result = self.database.get_all_game()
        return schemas.GameSchema(many=True).dump(result)

    @rpc
    def get_game_by_id(self, data):
        result = self.database.get_game_by_id(data['id'])
        return schemas.GameSchema().dump(result)

    @rpc
    def get_game_by_schedule_id(self, data):
        result = self.database.get_game_by_schedule_id(data['id'])
        return schemas.GameSchema().dump(result)

    ### GAME MEMBER ###
    @rpc
    def add_game_member(self, data):
        result = {
            'err': 0,
            'msg': 'Game Member Added'
        }
        if self.user_rpc.get_user_by_id(data['id_member']):
            self.database.add_game_member(data)
            self.database.close_connection()
        else:
            result['err'] = 1
            result['msg'] = 'User Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_game_member(self, data):
        result = {
            'err': 0,
            'msg': 'Game Member Updated'
        }
        if self.database.get_game_member_by_id(data['id']):
            self.database.update_game_member(data)
        else:
            result['err'] = 1
            result['msg'] = 'Game Member Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_game_member(self, data):
        result = {
            'err': 0,
            'msg': 'Game Member Deleted'
        }
        if self.database.get_game_member_by_id(data['id']):
            self.database.delete_game_member(data)
        else:
            result['err'] = 1
            result['msg'] = 'Game Member Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_game_member(self):
        result = self.database.get_all_game_member()
        return schemas.GameMemberSchema(many=True).dump(result)

    @rpc
    def get_game_member_by_id(self, data):
        result = self.database.get_game_by_id(data['id'])
        return schemas.GameMemberSchema().dump(result)

    @rpc
    def get_game_member_by_game_id(self, data):
        result = self.database.get_game_member_by_game_id(data['id'])
        return schemas.GameMemberSchema(many=True).dump(result)

    ### GAME WORD PACK ###
    @rpc
    def create_game_wordpack(self, data):
        result = {
            'err': 0,
            'msg': 'Game Wordpack Added'
        }
        self.database.create_game_wordpack(data)
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_game_wordpack(self, data):
        result = {
            'err': 0,
            'msg': 'Game Wordpack Updated'
        }
        if self.database.get_game_wordpack_by_id(data['id']):
            self.database.update_game_wordpack(data)
        else:
            result['err'] = 1
            result['msg'] = 'Game Wordpack Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_game_wordpack(self, data):
        result = {
            'err': 0,
            'msg': 'Game Wordpack Deleted'
        }
        if self.database.get_game_wordpack_by_id(data['id']):
            self.database.delete_game_wordpack(data)
        else:
            result['err'] = 1
            result['msg'] = 'Game Wordpack Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_game_wordpack(self):
        result = self.database.get_all_game_wordpack()
        return schemas.GameWordpackSchema(many=True).dump(result)

    @rpc
    def get_game_wordpack_by_id(self, data):
        result = self.database.get_game_wordpack_by_id(data['id'])
        return schemas.GameWordpackSchema().dump(result)

    @rpc
    def get_game_wordpack_by_game_id(self, data):
        result = self.database.get_game_wordpack_by_game_id(data['id'])
        return schemas.GameWordpackSchema(many=True).dump(result)
        
    def __del__(self):
        print("Service Destructor")
class GroupService:
    # ==========================================================
    # ---------------------- Service Name ----------------------
    # ==========================================================
    
    name = 'group_service'

    # ==========================================================
    # ----------------------- Dependency -----------------------
    # ==========================================================

    database = dependencies.Database()

    # ==========================================================
    # -------------------------- Proxy -------------------------
    # ==========================================================

    game_rpc = RpcProxy('game_service')
    user_rpc = RpcProxy('user_service')

    # ==========================================================
    # ------------------------ Functions -----------------------
    # ==========================================================

    def __init__(self):
        print("Service Constructor")

    @rpc
    def create_group(self, name):
        result = {
            'status': 0,
            'msg': 'Group Created'
        }

        self.database.create_group(name)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def edit_group(self, id, name):
        result = {
            'status': 0,
            'msg': 'Group Updated'
        }

        self.database.edit_group(id, name)
        return schemas.ResultSchema().dumps(result)
    
    @rpc
    def delete_group(self, id):
        result = {
            'status': 0,
            'msg': 'Group Deleted'
        }

        self.database.delete_group(id)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def get_all_group(self):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_all_group()
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def get_group_by_id(self,id):
        result = {
            'status': 0,
            'msg': ''           
        }

        result['data'] = self.database.get_group_by_id(id)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def get_group_by_name(self,name):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_group_by_name(name)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def get_all_schedule(self):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_all_schedule()
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)
    
    @rpc
    def get_schedule_by_id(self, id):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_schedule_by_id(id)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def get_schedule_by_id_user(self, id):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_all_schedule_by_id_user(id)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)
    
    @rpc
    def get_schedule_by_id_group(self, id):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_all_schedule_by_id_group(id)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def add_schedule(self, id_group, id_user, date, start_time, end_time):
        result = {
            'status': 0,
            'msg': 'Schedule Created'
        }

        self.database.add_schedule(id_group, id_user, date, start_time)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def edit_schedule_date(self, id, date):
        result = {
            'status': 0,
            'msg': 'Schedule Updated'
        }

        self.database.edit_schedule_date(id,date)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def edit_schedule_start(self, id, start_time):
        result = {
            'status': 0,
            'msg': 'Schedule Updated'
        }

        self.database.edit_schedule_start(id, start_time)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def edit_schedule_end(self, id, end_time):
        result = {
            'status': 0,
            'msg': 'Schedule Updated'
        }

        self.database.edit_schedule_end(id, end_time)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def delete_schedule(self, id):
        result = {
            'status': 0,
            'msg': 'Schedule Deleted'
        }

        self.database.delete_schedule(id)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def add_group_member(self, id_group, id_user):
        result = {
            'status': 0,
            'msg': 'Group Member Added'
        }

        self.database.add_group_member(id_group, id_user)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def remove_group_member(self, id):
        result = {
            'status': 0,
            'msg': 'Group Member Removed'
        }

        self.database.remove_group_member(id)
        return schemas.ResultSchema().dumps(result)

    @rpc
    def search_group_member(self, id_group):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.search_group_member(id_group)
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def get_all_group_member(self):
        result = {
            'status': 0,
            'msg': ''
        }

        result['data'] = self.database.get_all_group_member()
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    @rpc
    def check_schedule(self):
        result = {
            'status': 0,
            'msg': '',
            'data': []
        }

        current_hour = datetime.datetime.now().hour
        current_min = datetime.datetime.now().minute
        menitNow = (int(current_hour) * 60) + (int(current_min))
        hasil = self.database.check_schedule()
        count = -1
        if (len(hasil) != 0):
            for i in hasil:
                tanggal = str(i['date']) + " " + str(i['start_time'])
                waktu = datetime.datetime.strptime(tanggal, '%Y-%m-%d %H:%M:%S')
                menit = (int(waktu.hour) * 60) + (int(waktu.minute))
                # print('now : ' + str(menitNow))
                # print(menit)
                if (menitNow == menit):
                    result['msg'] = 'Room Created'
                    count = count + 1
                    result['data'].append({
                        'id_schedule': i['id'],
                        'id_gamemaster' : i['id_user']
                    })
                    self.game_rpc.create_game(result['data'][count])

        self.database.close_connection()
        if(count == -1):
            result['msg'] = 'No Room Created'
        return schemas.ResultSchema().dumps(result)

    @rpc
    def close_schedule_game(self):
        result = {
            'status': 0,
            'msg': 'No Room Deleted'
        }

        hasilSchedule = []
        count = -1

        schedule = self.database.get_all_schedule()
        for i in schedule:
            hasilSchedule.append({
                'id': i['id']
            })
            count = count + 1
            id_game = self.game_rpc.get_game_by_schedule_id(hasilSchedule[count])
            
            if(self.database.check_close_schedule_game(i['end_time'], i['date']) == True):
                
                if('id' in id_game):
                    kirimanGameId = []
                    kirimanGameId.append({
                        'id': id_game['id']
                    })
                    
                    game_member = self.game_rpc.get_game_member_by_game_id(kirimanGameId[0])
                    
                    if (self.database.close_schedule_game(game_member) == True):
                        self.game_rpc.delete_game(kirimanGameId[0])
                        result['msg'] = 'Room Deleted'
        
        self.database.close_connection()
        return schemas.ResultSchema().dumps(result)

    def __del__(self):
        print("Service Destructor")
class RoundService:
    name = 'round_service'
    database = dependencies.Database()
    user_rpc = RpcProxy('user_service')
    game_rpc = RpcProxy('game_service')

    def __init__(self):
        print("Service Constructor")

    ## UTILITY ##

    # ID Role 0 = Mr. White
    # ID Role 1 = Civilian
    # ID Role 2 = Undercover

    @rpc
    def check_win(self, data):
        if("mrwhite_word" in data):
            win = self.database.mrwhite_guess(data)
            self.database.close_connection()
            if win:
                return "Mr. White wins!"
            else:
                return "Try again next time!"
        elif("civilians" in data):
            temp = self.database.get_all_alive_player(data)
            self.database.close_connection()
            win = 1
            for i in temp:
                if i['id_role'] == 0:
                    win = 0
                elif i['id_role'] == 2:
                    win = 0
            if win:
                return "Civilian wins!"
        elif("one_civilian" in data):
            temp = self.database.get_all_alive_player(data)
            self.database.close_connection()
            civil_count = 0
            for i in temp:
                if i['id_role'] == 1:
                    civil_count += 1
            if civil_count == 1:
                return "Mr. White and Undercover wins!"

    ## GAME ROUND ##
    @rpc
    def create_round(self, data):
        result = {
            'err': 0,
            'msg': 'Round Created'
        }
        self.database.create_round(data)
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_round(self, data):
        result = {
            'err': 0,
            'msg': 'Round Updated'
        }
        if self.database.get_round_by_id(data['id']):
            self.database.update_round(data)
        else:
            result['err'] = 1
            result['msg'] = 'Round Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_round(self, data):
        result = {
            'err': 0,
            'msg': 'Round Deleted'
        }
        if self.database.get_round_by_id(data['id']):
            self.database.delete_round(data['id'])
        else:
            result['err'] = 1
            result['msg'] = 'Round Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_round(self):
        result = self.database.get_all_round()
        return schemas.RoundSchema(many=True).dump(result)

    @rpc
    def get_round_by_id(self, data):
        result = self.database.get_round_by_id(data['id'])
        return schemas.RoundSchema().dump(result)

    ## ROUND DETAIL ##
    @rpc
    def create_round_detail(self, data):
        result = {
            'err': 0,
            'msg': 'Round Detail Created'
        }
        if not self.game_rpc.get_round_by_id(data['id_round']):
            result['err'] = 1
            result['msg'] = 'Round Not Found'
        elif not self.user_rpc.get_user_by_id(data['id_user']):
            result['err'] = 1
            result['msg'] = 'User Not Found'
        else:
            self.database.create_round_detail(data)
            self.database.close_connection()
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_round_detail(self, data):
        result = {
            'err': 0,
            'msg': 'Round Detail Updated'
        }
        if not self.database.get_round_detail_by_id(data['id']):
            result['err'] = 1
            result['msg'] = 'Round Detail Not Found'
        elif not self.user_rpc.get_user_by_id(data['id_user']):
            result['err'] = 1
            result['msg'] = 'User Not Found'
        else:
            self.database.update_round_detail(data)
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_round_detail(self, data):
        result = {
            'err': 0,
            'msg': 'Round Detail Deleted'
        }
        if self.database.get_round_detail_by_id(data['id']):
            self.database.delete_round_detail(data['id'])
        else:
            result['err'] = 1
            result['msg'] = 'Round Detail Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_round_detail(self):
        result = self.database.get_all_round_detail()
        return schemas.RoundDetailSchema(many=True).dump(result)

    @rpc
    def get_round_detail_by_id(self, data):
        result = self.database.get_round_detail_by_id(data['id'])
        return schemas.RoundDetailSchema().dump(result)

    ## TURN DETAIL ##
    @rpc
    def create_turn_detail(self, data):
        result = {
            'err': 0,
            'msg': 'Turn Detail Created'
        }
        self.database.create_turn_detail(data)
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def update_turn_detail(self, data):
        result = {
            'err': 0,
            'msg': 'Turn Detail Updated'
        }
        if self.database.get_turn_detail_by_id(data['id']):
            self.database.update_turn_detail(data['id'])
        else:
            result['err'] = 1
            result['msg'] = 'Turn Detail Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def delete_turn_detail(self, data):
        result = {
            'err': 0,
            'msg': 'Turn Detail Deleted'
        }
        if self.database.get_turn_detail_by_id(data['id']):
            self.database.delete_turn_detail(data['id'])
        else:
            result['err'] = 1
            result['msg'] = 'Turn Detail Not Found'
        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_turn_detail(self):
        result = self.database.get_all_turn_detail()
        return schemas.TurnDetailSchema(many=True).dump(result)

    @rpc
    def get_turn_detail_by_id(self, data):
        result = self.database.get_turn_detail_by_id(data['id'])
        return schemas.TurnDetailSchema().dump(result)

    def __del__(self):
        print("Service Destructor")
class UserService:
    name = 'user_service'
    database = dependencies.Database()

    # voucher_rpc = RpcProxy('voucher_service')
    # user_rpc = RpcProxy('user_service')
    # transaction_rpc = RpcProxy('transaction_service')

    def __init__(self):
        print("Service Constructor")

    @rpc
    def create_user(self, data):
        self.database.create_user(data)
        self.database.close_connection()
        return "New User Created"

    @rpc
    def login(self, data):
        user = self.database.login(data)
        self.database.close_connection()
        if user:
            return "User berhasil login"
        else:
            return "Email / password salah atau tidak ada"

    @rpc
    def update_user(self, data):

        result = {'err': 0, 'msg': 'User Updated'}

        if self.database.get_user_by_id(data['id']):
            self.database.update_user(data)

            self.database.close_connection()

        else:
            result['err'] = 1
            result['msg'] = 'User Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_user(self):
        user = self.database.get_all_user()
        self.database.close_connection()
        return schemas.UsersSchema(many=True).dump(user)

    @rpc
    def get_user_by_id(self, id):
        user = self.database.get_user_by_id(id)
        self.database.close_connection()
        return schemas.UsersSchema().dump(user)

    @rpc
    def set_user_active(self, id):
        self.database.set_user_active(id)
        return "Set Active Successfully"

    @rpc
    def create_user_wordpack(self, data):
        self.database.create_user_wordpack(data)
        self.database.close_connection()
        return "New User Wordpack Created"

    @rpc
    def update_user_wordpack(self, data):

        result = {'err': 0, 'msg': 'User Wordpack Updated'}

        if self.database.get_user_wordpack_by_id(data['id']):
            self.database.update_user_wordpack(data)

            self.database.close_connection()

        else:
            result['err'] = 1
            result['msg'] = 'User Wordpack Not Found'
            self.database.close_connection()

        return schemas.CommandResultSchema().dumps(result)

    @rpc
    def get_all_user_wordpack(self):
        wordpack = self.database.get_all_user_wordpack()
        self.database.close_connection()
        return schemas.UserWordPacksSchema(many=True).dump(wordpack)

    @rpc
    def get_user_wordpack_by_id(self, id):
        wordpack = self.database.get_user_wordpack_by_id(id)
        self.database.close_connection()
        return schemas.UserWordPacksSchema().dump(wordpack)

    @rpc
    def get_all_user_wordpack_by_user_id(self, id):
        wordpack = self.database.get_all_user_wordpack_by_user_id(id)
        self.database.close_connection()
        return schemas.UserWordPacksSchema(many=True).dump(wordpack)

    def __del__(self):
        print("Service Destructor")