Exemplo n.º 1
0
 def test_deleteMatchFromUpcomingGames(self):
     self.test_insertMatchToUpcomingGames()
     db.deleteMatchFromUpcomingGames(date=today.strftime('%d-%m-%Y'),
                                     home_team_name="Fiorentina",
                                     away_team_name="Sampdoria")
     number_of_record = db.DBConnection.UpcomingGames.find({}).count()
     self.assertEqual(number_of_record, 0)
Exemplo n.º 2
0
 def test_getUpcomingGames(self):
     db.DBConnection.UpcomingGames.remove({})
     _match = upcoming_match(away_att=77,
                             away_def=67,
                             away_mid=69,
                             away_odds_n=0.27669516494195245,
                             away_odds_nn=3.2583333333333333,
                             away_team_name="Sampdoria",
                             away_team_rank=11,
                             away_team_received=2,
                             away_team_scored=1,
                             date=today,
                             draw_odds_n=0.31504895018143103,
                             draw_odds_nn=2.8616666666666664,
                             home_att=80,
                             home_def=71,
                             home_mid=74,
                             home_odds_n=0.4082558848766165,
                             home_odds_nn=2.208333333333333,
                             home_team_name="Fiorentina",
                             home_team_rank=18,
                             home_team_received=1,
                             home_team_scored=2,
                             league="Serie",
                             round=1)
     db.insertMatchToUpcomingGames(_match)
     dtos = db.getUpcomingGames(league='all')
     self.assertEqual(_match, dtos[0])
def setDoubleBet(receiptID, bet_value, bet_odd, match1_ID, match2_ID, result_1,
                 result_2):
    '''
    @param receiptID:string, receipt ID which Winner issue
    @param bet_value:float, amount of money that place on the bet
    @param bet_odd:float, the odds that the bets holds, given by Winner
    @param match1_ID:string, the ID of the first match that the bet place on
    @param match2_ID:string, the ID of the second match that the bet place on
    @param result_1:string, the outcome of the first match we expect ('1','X','2')
    @param result_2:string, the outcome of the second match we expect ('1','X','2')
    @return:None
    '''
    try:
        match1: Match = DBController.findMatch(match1_ID)
        match2: Match = DBController.findMatch(match2_ID)
        result1 = enumDICT[result_1]
        result2 = enumDICT[result_2]
        form = BetForm(receiptID=receiptID,
                       bet_value=bet_value,
                       bet_odd=bet_odd,
                       bets_list=[(match1, result1), (match2, result2)])
        match1.associateBetForm(receiptID)
        match2.associateBetForm(receiptID)
        DBController.saveBetForm(form.toDTO())
        return True
    except KeyError:
        print("Could not find the associate ENUM")
        return False
    except:
        print("An error has been occurred")
        return False
Exemplo n.º 4
0
 def test_insertMatchToUpcomingGames(self):
     db.DBConnection.UpcomingGames.remove({})
     _match = upcoming_match(away_att=77,
                             away_def=67,
                             away_mid=69,
                             away_odds_n=0.27669516494195245,
                             away_odds_nn=3.2583333333333333,
                             away_team_name="Sampdoria",
                             away_team_rank=11,
                             away_team_received=2,
                             away_team_scored=1,
                             date=today,
                             draw_odds_n=0.31504895018143103,
                             draw_odds_nn=2.8616666666666664,
                             home_att=80,
                             home_def=71,
                             home_mid=74,
                             home_odds_n=0.4082558848766165,
                             home_odds_nn=2.208333333333333,
                             home_team_name="Fiorentina",
                             home_team_rank=18,
                             home_team_received=1,
                             home_team_scored=2,
                             league="Serie",
                             round=1)
     db.insertMatchToUpcomingGames(_match)
     record = db.DBConnection.UpcomingGames.find_one(
         {}, projection={'_id': False})
     record = db.convertStrtoDate(record)
     found = upcoming_match.from_dict(record)
     self.assertEqual(_match, found)
def addMatch(league, date, home_team, away_team, result=None):
    '''
    @param league:string , the league that the match took place.
    @param date:datetime object, the date that the match took place.
    @param home_team:string, name of the home team.
    @param away_team:string, name of the way team.
    @param result (OPTIONAL):string, the result of the match ('1','X','2')
    @return: Match object that represent the match
    '''
    try:
        matchID = generateMatchID()
        if result != None:
            _result = enumDICT[result]
        newMatch = Match(matchID=matchID,
                         league=league,
                         date=date,
                         home_team=home_team,
                         away_team=away_team,
                         result=_result)
        DBController.saveMatch(newMatch.toDTO())
        return newMatch
    except KeyError:
        print("Could not find the associate ENUM")
        return False
    except:
        print("An error has been occurred")
        return False
def predict(league):
    """
    @param league: string that represent the league (can be 'all')
    @return: list of predictions DTOs
    """
    predictions = []
    #region Data
    all_data = DBController.getAllData(as_dataframe=True)
    upcoming_games = DBController.getUpcomingGames(league, as_dataframe=True)
    x, y = data_preprocessor.train_preprocess(all_data)
    to_predict = data_preprocessor.prediction_preprocess(upcoming_games)
    #endregion
    #region ANN
    for i in range(0, AVG):
        ann = NeuralNet(x.shape[1])
        ann.train(x, y, EPOC)
        predictions.append(ann.predict(to_predict))
    #endregion
    #region Calculate avg of predictions
    lines = predictions[0].shape[0]
    columns = predictions[0].shape[1]
    avgPrediction = np.zeros((lines, columns))
    for line in range(lines):
        for cell in range(columns):
            sum = 0
            for prediction in predictions:
                sum = sum + prediction[line, cell]
            avgPrediction[line, cell] = sum / AVG
    #endregion
    #region Converting avgPrediction to pandas DataFrame
    y_pred, indexes = apply_indexes(avgPrediction, upcoming_games)
    details = upcoming_games.iloc[indexes]
    details = details[[
        'league', 'date', 'home_team_name', 'away_team_name', 'home_odds_nn',
        'draw_odds_nn', 'away_odds_nn'
    ]]
    final = pd.concat([details, y_pred], axis=1, sort=False)

    prediction_dtos = []
    for row in final.shape[0]:
        prediction_dtos.append(
            Prediction(
                final.loc[row, 'leauge'], final.loc[row, 'date'],
                final.loc[row, 'home_team_name'], final.loc[row,
                                                            'away_team_name'],
                final.loc[row, 'home_odds_nn'], final.loc[row, 'draw_odds_nn'],
                final.loc[row, 'away_odds_nn'], final.loc[row, 'pred_1'],
                final.loc[row, 'pred_2'], final.loc[row, 'pred_x'],
                calc_exp(predictions=[
                    final.loc[row, 'pred_1'], final.loc[row, 'pred_x'],
                    final.loc[row, 'pred_2']
                ],
                         odds=[
                             final.loc[row, 'home_odds_nn'],
                             final.loc[row, 'draw_odds_nn'],
                             final.loc[row, 'away_odds_nn']
                         ]), final.loc[row, 'result']))
    return prediction_dtos
Exemplo n.º 7
0
 def test_saveMatch(self):
     db.DBConnection.Matches.remove({})
     newMatch = Match(matchID='{}_TeamA_TeamB'.format(today),
                      date=today,
                      league='league',
                      home_team='TeamA',
                      away_team='TeamB',
                      result=Result.Home)
     db.saveMatch(newMatch.toDTO())
     found = db.findMatch(matchID='{}_TeamA_TeamB'.format(today))
     self.assertEqual(newMatch, found)
Exemplo n.º 8
0
def updateFund(name, amount):
    '''
    @permission private function to generalize the changes in the funds and the connection with the DB
    @param name:string, by who the action has been made
    @param amount:float
    @return:
    '''
    currentStatus = DBController.getLastFundStatus()
    DBController.updateFundStatus(currentStatus + amount)
    DBController.saveTransaction(name, amount)
    return
Exemplo n.º 9
0
 def test_saveBetForm(self):
     db.DBConnection.BetForms.remove({})
     form = betForm(receiptID='ID',
                    date=today,
                    bet_value=10.3131,
                    bet_odd=11.33,
                    isWin=False,
                    profitExpectation=10.3131 * 11.33,
                    bets=[('{}_TeamA_TeamB'.format(today), '1')])
     db.saveBetForm(form)
     found = betForm.from_dict(
         db.convertStrtoDate(
             db.DBConnection.BetForms.find_one({"receiptID": 'ID'},
                                               projection={'_id': False})))
     self.assertEqual(form, found)
Exemplo n.º 10
0
 def getUpcomingGames(self, request, context):
     print("request: getUpcomingGames")
     matchlist = match_pb2.MatchList()
     upcoming = DBController.getUpcomingGames(league=request.league)
     for game in upcoming:
         _match = matchlist.list.add()
         _match.league = game.league
         _match.date = game.date.isoformat()
         _match.round = game.round
         _match.home_team_name = game.home_team_name
         _match.away_team_name = game.away_team_name
         _match.home_team_rank = game.home_team_rank
         _match.away_team_rank = game.away_team_rank
         _match.home_team_scored = game.home_team_scored
         _match.away_team_scored = game.away_team_scored
         _match.home_team_received = game.home_team_received
         _match.away_team_received = game.away_team_received
         _match.home_att = game.home_att
         _match.away_att = game.away_att
         _match.home_def = game.home_def
         _match.away_def = game.away_def
         _match.home_mid = game.home_mid
         _match.away_mid = game.away_mid
         _match.home_odds_n = game.home_odds_n
         _match.draw_odds_n = game.draw_odds_n
         _match.away_odds_n = game.away_odds_n
         _match.home_odds_nn = game.home_odds_nn
         _match.draw_odds_nn = game.draw_odds_nn
         _match.away_odds_nn = game.away_odds_nn
     return matchlist
Exemplo n.º 11
0
 def getMatchInLastSeasons(self, request, context):
     print("request: getMatchInLastSeasons")
     matchlist = match_pb2.MatchList()
     for game in DBController.getAllData(as_dataframe=False):
         _match = matchlist.list.add()
         _match.league = game.league
         _match.date = game.date.isoformat()
         _match.round = game.round
         _match.home_team_name = game.home_team_name
         _match.away_team_name = game.away_team_name
         _match.home_team_rank = game.home_team_rank
         _match.away_team_rank = game.away_team_rank
         _match.home_team_scored = game.home_team_scored
         _match.away_team_scored = game.away_team_scored
         _match.home_team_received = game.home_team_received
         _match.away_team_received = game.away_team_received
         _match.home_att = game.home_att
         _match.away_att = game.away_att
         _match.home_def = game.home_def
         _match.away_def = game.away_def
         _match.home_mid = game.home_mid
         _match.away_mid = game.away_mid
         _match.home_odds_n = game.home_odds_n
         _match.draw_odds_n = game.draw_odds_n
         _match.away_odds_n = game.away_odds_n
         _match.result = game.result
         _match.home_odds_nn = game.home_odds_nn
         _match.draw_odds_nn = game.draw_odds_nn
         _match.away_odds_nn = game.away_odds_nn
         break
     return matchlist
Exemplo n.º 12
0
 def test_findBetForm(self):
     from Server.BetsFinancial.BetForm import BetForm
     db.DBConnection.BetForms.remove({})
     db.DBConnection.Matches.remove({})
     match = Match(matchID='{}_TeamA_TeamB'.format(today),
                   date=today,
                   league='league',
                   home_team='TeamA',
                   away_team='TeamB',
                   result=Result.Home)
     form = BetForm(receiptID='ID',
                    date=today,
                    bet_value=10.3131,
                    bet_odd=11.33,
                    bets_list=[(match, Result.Home)])
     db.saveMatch(match.toDTO())
     db.saveBetForm(form.toDTO())
     found = db.findBetForm(receiptID='ID')
     self.assertEqual(form, found)
Exemplo n.º 13
0
def setMatchResult(matchID, result):
    '''
    @param matchID:string, the ID of the match
    @param result:string, the result of the match ('1','X','2')
    @return: Match object that represent the match
    '''
    try:
        match = DBController.findMatch(matchID)
        result = enumDICT[result]
        match.setResult(result)
        DBController.updateMatch(match.toDTO())
        for receiptID in match._associateBets:
            DBController.findBetForm(receiptID).checkWin()
        return match
    except KeyError:
        print("Could not find the associate ENUM")
        return False
    except:
        print("An error has been occurred")
        return False
Exemplo n.º 14
0
 def test_updateFundStatus(self):
     db.updateFundStatus(18)
     #list=db.DBConnection.FundStatus.find().sort([('time',-1)]).limit(1)
     record = db.getLastFundStatus()
     self.assertEqual(record, 18)