summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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()