summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2017-01-04 00:26:05 +0100
committeremkael <emkael@tlen.pl>2017-01-04 20:56:19 +0100
commit9e475e3cc99849c3238995aebac9a1dd361bfec3 (patch)
treeb87f2263e13b3c53775c0287c8375f48dd8ed562
parent41007b36132e8594c17f4d11f2d1834340c75456 (diff)
Compiling opponents previous scores for each segment butler
-rw-r--r--ausbutler/butler.py11
-rw-r--r--ausbutler/interface.py18
-rw-r--r--butler.py1
3 files changed, 29 insertions, 1 deletions
diff --git a/ausbutler/butler.py b/ausbutler/butler.py
index 424e385..fb78fdb 100644
--- a/ausbutler/butler.py
+++ b/ausbutler/butler.py
@@ -14,6 +14,17 @@ def cutoff(score):
score += config['cutoff_point']
return score * sign
+def get_opponents(butler, player):
+ table = butler.table
+ if player in [table.openE, table.openW]:
+ return [table.openN, table.openS]
+ if player in [table.openN, table.openS]:
+ return [table.openE, table.openW]
+ if player in [table.closeE, table.closeW]:
+ return [table.closeN, table.closeS]
+ if player in [table.closeN, table.closeS]:
+ return [table.closeE, table.closeW]
+
def get_room(butler, player):
table = butler.table
if player in [table.openE, table.openW, table.openN, table.openS]:
diff --git a/ausbutler/interface.py b/ausbutler/interface.py
index 50bd8b3..37af0e3 100644
--- a/ausbutler/interface.py
+++ b/ausbutler/interface.py
@@ -1,6 +1,6 @@
from .db import Session
from .model import AusButler, Butler
-from .butler import cutoff, get_room
+from .butler import cutoff, get_opponents, get_room
import re
@@ -34,3 +34,19 @@ class Interface:
self.session.add(aus_b)
self.session.commit()
+ def opp_scores(self):
+ butlers = self.session.query(AusButler).all()
+ for butler in butlers:
+ opps = get_opponents(butler, butler.id)
+ averages = {opps[0]: {'sum': 0.0, 'count': 0},
+ opps[1]: {'sum': 0.0, 'count': 0}}
+ for opp_butler in butlers:
+ if opp_butler.id in opps and (opp_butler.match < butler.match or (opp_butler.match == butler.match and opp_butler.segment < butler.segment)):
+ averages[opp_butler.id]['sum'] += opp_butler.score
+ averages[opp_butler.id]['count'] += opp_butler.board_count
+ butler.opp_score = sum(
+ [opp['sum'] / opp['count'] if opp['count'] > 0 else 0.0
+ for opp in averages.values()]
+ ) / 2
+ self.session.commit()
+
diff --git a/butler.py b/butler.py
index ed5cc11..a15d6be 100644
--- a/butler.py
+++ b/butler.py
@@ -3,3 +3,4 @@ from ausbutler.interface import Interface
i = Interface()
i.init_db()
i.populate_db()
+i.opp_scores()