<commit_before>#!/usr/bin/env python

#  Copyright 2010 <NAME> <<EMAIL>>

# This file is part of Webridge.
# Webridge is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Webridge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with Webridge.  If not, see <http://www.gnu.org/licenses/>.

import logging

from google.appengine.ext import db
from google.appengine.api import users, channel
from django.utils  import simplejson as json

import bridge
from utils import m

class Deal(db.Model) :
    N = db.ListProperty(int, required=True)
    S = db.ListProperty(int, required=True)
    E = db.ListProperty(int, required=True)
    W = db.ListProperty(int, required=True)
    vulnerability = db.IntegerProperty(choices = bridge.VULN_OPTIONS, required=True)
    dealer = db.IntegerProperty(choices = bridge.DEALERS, required=True)
    createDate = db.DateTimeProperty(auto_now_add=True)
    @staticmethod
    def create(deal, vuln, dealer) :
        d = Deal(vulnerability=vuln, dealer=dealer, **deal)
        d.put()
        return d

    def hand_by_side(self, side) :
        return self.__getattribute__(side)

    def todeck(self) :
        return {'N': self.N, 'E': self.E, 'S': self.S, 'W': self.W}
    

class Protocol(db.Model) :
    N = db.UserProperty(required=True)
    S = db.UserProperty(required=True)
    E = db.UserProperty(required=True)
    W = db.UserProperty(required=True)
    bidding = db.StringListProperty()
    moves = db.ListProperty(int)
    deal = db.ReferenceProperty(Deal, required=True)
    contract = db.StringProperty() # <level><suit>[d|r]<decl> eg. 3SdN, 2DS 
    tricks = db.IntegerProperty() # 0 = just made, +1 = one overtrick, -1 = one down
    result = db.IntegerProperty() # +100, -800, etc.
    playStarted = db.DateTimeProperty(auto_now_add=True)
    
    @staticmethod
    def create(dealmodel, N, E, S, W) :
        return Protocol(deal = dealmodel, N = N, E = E, S = S, W = W).put()
    
    @staticmethod
    def get_by_deal(deal) :
        return Protocol.all().filter('deal =', deal).order('playStarted')

    @staticmethod
    def get_unused_deal(playerlist) :
        used = set()
        for player in playerlist :
            for p : Protocol.all().filter('N = ', player) :
               used.add(p.deal) 
            for p : Protocol.all().filter('S = ', player) :
               used.add(p.deal) 
            for p : Protocol.all().filter('E = ', player) :
               used.add(p.deal) 
            for p : Protocol.all().filter('W = ', player) :
               used.add(p.deal) 
        for d in Deal.all() :
            if d not in used :
                return d
        return None

    def round_ended(self) :
        return len(self.moves) % 4 == 0

    def finished(self) :
        return len(self.moves) == 52

    def dummy(self) :
        return bridge.SIDES[(bridge.SIDES.index(self.contract[-1]) + 2) % 4]

class Table(db.Model) :
    protocol = db.ReferenceProperty(Protocol)
    whosmove = db.StringProperty(choices = bridge.SIDES)
    claim = db.StringProperty()

    def nextmove(self) :
        self.whosmove = bridge.SIDES[(bridge.SIDES.index(self.whosmove) + 1) % 4]
    
    def user_by_side(self, side) :
        res = TablePlace.get1(table=self, side=side)
        if res is None :
            return None
        else :<commit_msg>#12 implement unused deal search in more effective way (hopefully)<commit_after>#!/usr/bin/env python

#  Copyright 2010 <NAME> <<EMAIL>>

# This file is part of Webridge.
# Webridge is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Webridge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with Webridge.  If not, see <http://www.gnu.org/licenses/>.

import logging

from google.appengine.ext import db
from google.appengine.api import users, channel
from django.utils  import simplejson as json

import bridge
from utils import m

class Deal(db.Model) :
    N = db.ListProperty(int, required=True)
    S = db.ListProperty(int, required=True)
    E = db.ListProperty(int, required=True)
    W = db.ListProperty(int, required=True)
    vulnerability = db.IntegerProperty(choices = bridge.VULN_OPTIONS, required=True)
    dealer = db.IntegerProperty(choices = bridge.DEALERS, required=True)
    createDate = db.DateTimeProperty(auto_now_add=True)
    @staticmethod
    def create(deal, vuln, dealer) :
        d = Deal(vulnerability=vuln, dealer=dealer, **deal)
        d.put()
        return d

    def hand_by_side(self, side) :
        return self.__getattribute__(side)

    def todeck(self) :
        return {'N': self.N, 'E': self.E, 'S': self.S, 'W': self.W}
    

class Protocol(db.Model) :
    N = db.UserProperty(required=True)
    S = db.UserProperty(required=True)
    E = db.UserProperty(required=True)
    W = db.UserProperty(required=True)
    bidding = db.StringListProperty()
    moves = db.ListProperty(int)
    deal = db.ReferenceProperty(Deal, required=True)
    contract = db.StringProperty() # <level><suit>[d|r]<decl> eg. 3SdN, 2DS 
    tricks = db.IntegerProperty() # 0 = just made, +1 = one overtrick, -1 = one down
    result = db.IntegerProperty() # +100, -800, etc.
    playStarted = db.DateTimeProperty(auto_now_add=True)
    
    @staticmethod
    def create(dealmodel, N, E, S, W) :
        return Protocol(deal = dealmodel, N = N, E = E, S = S, W = W).put()
    
    @staticmethod
    def get_by_deal(deal) :
        return Protocol.all().filter('deal =', deal).order('playStarted')

    @staticmethod
    def get_deals(protoq, deals) :
        proceed = True
        while proceed :
            ps = protoq.fetch(100)
            deals.update([p.deal for p in ps])
            protoq.with_cursor(protoq.cursor())
            proceed = ps.size() > 0
            
    @staticmethod
    def get_unused_deal(playerlist) :
        used = set()
        for player in playerlist :
            Protocol.get_deals(Protocol.all().filter('N = ', player), used)
            Protocol.get_deals(Protocol.all().filter('S = ', player), used)
            Protocol.get_deals(Protocol.all().filter('E = ', player), used)
            Protocol.get_deals(Protocol.all().filter('W = ', player), used)
        for d in GqlQuery("select __key__ from Deal") :
            if d not in used :
                return db.get(d)
        return None

    def round_ended(self) :
        return len(self.moves) % 4 == 0

    def finished(self) :
        return len(self.moves) == 52

    def dummy(self) :
        return bridge.SIDES[(bridge.SIDES.index(self.contract[-1]) + 2) % 4]

class Table(db.Model) :
    protocol = db.ReferenceProperty(Protocol)
    whosmove = db.StringProperty(choices = bridge.SIDES)
    claim = db.StringProperty()

    def nextmove(self) :
        self.whosmove = bridge.SIDES[(bridge.SIDES.index(self.whosmove) + 1) % 4]
    
    def user_by_side(self, side) :
        res = TablePlace.get1(table=self, side=side)
        if res is None :
            return None
        else :
{'fraction_non_alphanumeric': 0.11475619096247128, 'fraction_numerical': 0.007403625223385244, 'mean_word_length': 2.8200877620672844, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1592788', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2421, 'n_words': 699}