Tuesday 1 January 2019

making a calculation with the elements of an elasticsearch json object, of a contract bridge score, using Python

This is stressing me out. I am trying to build a contract bridge match point scoring system with an elasticsearch cluster and a python flask app. This is long and complicated, sorry.

The results of a given board are:

board_number,nsp, ewp, contract, by, tricks, nsscore, ewscore 
1,1,8,3NT,N,10,430,0
1,2,9,3NT,S,10,430,0
1,3,10,4S,N,10,420,0
1,4,11,3NT,N,11,460,0
1,5,12,3NT,S,10,400,0
1,6,13,4S,S,11,480,0
1,7,14,3NT,S,8,0,50

Sorry not very well formatted above, I did not know how to insert a range from a spreadsheet. The north/south pair, column "nsscore" get 2 points for each score they beat and 1 point for each score they tie. So, nsp 6 get 12 points as they have the best score (480) and ewp 14 get 12 points for their score of 50 as they were the only east/west pair who had a plus score.

I want to loop through the results and score each n/s pair and each e/w pair by comparing their score with the other pair in the same direction (all n/s compared against each other and all e/w pairs against each other).

After multiple efforts the closest I can get is as follows:

@application.route('/go', methods=['GET', 'POST'])
def go():
    resp = es.search(index="mitchell-index", body={"query": {"match_all": {}}})
    pair_list = []
    score_list = {}
    ns_mp_score = 0
    ew_mp_score = 0
    for row in resp["hits"]["hits"]:
        pair_list.append(row['_source']['nsp'])
        for index in range(len(pair_list)):
            nsp = pair_list[index]
            query = {
                "query": { 
            "bool": { 
            "must": [
            { "match": { "nsp":  nsp        }}, 
            { "match": {"board_number":  "1" }}
            ]
            }
            }
        }
            query = json.dumps(query)
            scores =  es.search(index="match", body=query)
            for row in scores["hits"]["hits"]:
                nsscore = int(row['_source']['nsscore'])
                if int(row['_source']['nsscore']) < nsscore:
                    ns_mp_score = ns_mp_score 
                    #ew_mp_score = ew_mp_score + 2
                elif int(row['_source']['nsscore']) > nsscore:
                    ns_mp_score = ns_mp_score + 2
                    #ew_mp_score = ew_mp_score  
                else:
                    ns_mp_score = ns_mp_score + 1
                    #ew_mp_score = ew_mp_score + 1
                score_list.update({ 'board': row['_source']['board_number'], 'nsp': row['_source']['nsp'], 'ewp': row['_source']['ewp'], 'ns_mp_score':  ns_mp_score, 'ew_mp_score': ew_mp_score})

which produces:

{'board': '1', 'nsp': '1', 'ewp': '8', 'ns_mp_score': 1, 'ew_mp_score': 0}
{'board': '1', 'nsp': '4', 'ewp': '11', 'ns_mp_score': 3, 'ew_mp_score': 0}
{'board': '1', 'nsp': '5', 'ewp': '12', 'ns_mp_score': 6, 'ew_mp_score': 0}
{'board': '1', 'nsp': '7', 'ewp': '14', 'ns_mp_score': 10, 'ew_mp_score': 0}
{'board': '1', 'nsp': '2', 'ewp': '9', 'ns_mp_score': 15, 'ew_mp_score': 0}
{'board': '1', 'nsp': '3', 'ewp': '10', 'ns_mp_score': 21, 'ew_mp_score': 0}
{'board': '1', 'nsp': '6', 'ewp': '13', 'ns_mp_score': 28, 'ew_mp_score': 0}

This matches the pairs together correctly but gets the score calculation wrong. For example in the last row nsp 6 against ewp 13 the mp scores should be 12 and 0 respectively.

Happy Holidays and help gratefully achieved to assist me in solving this problem!



from making a calculation with the elements of an elasticsearch json object, of a contract bridge score, using Python

No comments:

Post a Comment