<commit_before>from kivy.clock import Clock

import time

#from defines import *

import random as rand_m

import pentai.base.human_player as h_m
import pentai.base.rules as r_m
import pentai.base.game as g_m
import pentai.ai.ai_genome as aig_m
import pentai.db.ai_factory as f_m
import pentai.db.players_mgr as pm_m
from pentai.base.pente_exceptions import *

import audio as a_m

def play_speech(part_num):
    a_m.instance.demo(part_num)

def sim_press(button):
    button.sim_press()
    Clock.schedule_once(button.sim_release, 0.2)

class Demo():
    def __init__(self, app, size):
        self.app = app
        self.size = size
        self.interrupted = False
        self.speed_factor = 0.65
        self.sleep_remaining = 0

        # TODO Save settings
        # for settings demo

        self.script_list = [self.rules_script1, self.rules_script2,
                self.rules_script3, self.rules_script4, self.rules_script5]
        self.script = None

    def start(self):
        a_m.instance.start_demo()
        self.play()

    def play(self, dt=None):
        if self.interrupted:
            self.script = None
            self.interrupted = False
            self.sleep_remaining = 0

        if self.sleep_remaining > 0:
            self.sleep_remaining -= 0.05
            Clock.schedule_once(self.play, 0.05)
            return

        if self.script is None:
            if len(self.script_list) == 0:
                self.finish()
                return
            self.script = self.script_list[0]()
            del self.script_list[0]

        try:
            self.sleep_remaining = self.script.next() / self.speed_factor
            self.sleep_remaining = (.8 + .4 * rand_m.random()) * self.sleep_remaining
        except (StopIteration, IllegalMoveException):
            self.script = None
            self.sleep_remaining = 0

        Clock.schedule_once(self.play, 0)


    def interrupt(self):
        self.interrupted = True

    def finish(self):
        self.app.finish_demo()

    def clean_up(self):
        Clock.unschedule(self.play)

    def rules_script1(self):
        play_speech("intersections")

        # We're already on the menu screen
        app = self.app
        ms = app.menu_screen

        yield(1)
        sim_press(ms.ids.new_game_id)
        yield(.2)

        app.show_new_game_screen()
        yield(.1)

        ss = app.setup_screen
        # TODO: Select white player type
        ss.ids.black_type_id.val = "Human"
        ss.ids.bpl_id.val = "You"
        #ss.ids.white_type_id.val = "AI"
        ss.ids.white_type_id.val = "Computer"
        # TODO: Select computer player somehow?
        ss.ids.wpl_id.val = "Killer"
        yield(2)

        sim_press(ss.ids.start_game_id)

    def mm(self, x,y):
        self.game.make_move((x,y))
        self.app.pente_screen.perform(0)
        self.app.pente_screen.refresh_all()

    def rules_script2(s):
        # Use most recent human player name
        pmgr = pm_m.PlayersMgr()
        human = pmgr.get_recent_player_names("Human", 1)[0]
        p1 = h_m.HumanPlayer(human)

        aif = f_m.AIFactory()
        genome = aig_m.AIGenome("PentAI")
        genome.use_openings_book = False
        p2 = aif.create_player(genome)

        # Create a game
        r = r_m.Rules(13, "standard")
        s.game = g_m.Game(r, p1, p2)

        # And start it...

        # demo flag: Disable prompting of players
        s.app.start_game(s.game, demo=True)
        s.ps = s.app.pente_screen
        yield(1.2) # Compensating for .7 wait at start?!

        play_speech("centre_first")
        yield(1.5)
        s.mm(3,4)
        yield(1)

        s.mm(6,8)
        yield(.5)
        s.mm(4,4)
        yield(.5)

        s.mm(6,7)
        yield(.5)
        s.mm(5,8)
        yield(.5)
        play_speech("line_5_wins")

        s.mm(6,9)
        yield(.5)
        s.mm(8,3)
        yield(.5)

        s.mm(6,10)
        yield(3)

        # Diagonal white win
        s.app.pente_screen.set_review_mode(False)
        s.game.go_to_the_beginning()
        yield(1)

        s.mm(6, 6)<commit_msg>Remove "New Game" screen from demo for now<commit_after>from kivy.clock import Clock

import time

#from defines import *

import random as rand_m

import pentai.base.human_player as h_m
import pentai.base.rules as r_m
import pentai.base.game as g_m
import pentai.ai.ai_genome as aig_m
import pentai.db.ai_factory as f_m
import pentai.db.players_mgr as pm_m
from pentai.base.pente_exceptions import *

import audio as a_m

def play_speech(part_num):
    a_m.instance.demo(part_num)

def sim_press(button):
    button.sim_press()
    Clock.schedule_once(button.sim_release, 0.2)

class Demo():
    def __init__(self, app, size):
        self.app = app
        self.size = size
        self.interrupted = False
        self.speed_factor = 0.65
        self.sleep_remaining = 0

        # TODO Save settings
        # for settings demo

        self.script_list = [self.rules_script2,
                self.rules_script3, self.rules_script4, self.rules_script5]
        self.script = None

    def start(self):
        a_m.instance.start_demo()
        self.play()

    def play(self, dt=None):
        if self.interrupted:
            self.script = None
            self.interrupted = False
            self.sleep_remaining = 0

        if self.sleep_remaining > 0:
            self.sleep_remaining -= 0.05
            Clock.schedule_once(self.play, 0.05)
            return

        if self.script is None:
            if len(self.script_list) == 0:
                self.finish()
                return
            self.script = self.script_list[0]()
            del self.script_list[0]

        try:
            self.sleep_remaining = self.script.next() / self.speed_factor
            self.sleep_remaining = (.8 + .4 * rand_m.random()) * self.sleep_remaining
        except (StopIteration, IllegalMoveException):
            self.script = None
            self.sleep_remaining = 0

        Clock.schedule_once(self.play, 0)


    def interrupt(self):
        self.interrupted = True

    def finish(self):
        self.app.finish_demo()

    def clean_up(self):
        Clock.unschedule(self.play)

    def rules_script1(self):
        # I'll leave this dead code as an example for now
        '''
        play_speech("intersections")

        # We're already on the menu screen
        app = self.app
        ms = app.menu_screen

        yield(1)
        sim_press(ms.ids.new_game_id)
        yield(.2)

        app.show_new_game_screen()
        yield(.1)

        ss = app.setup_screen
        # TODO: Select white player type
        ss.ids.black_type_id.val = "Human"
        ss.ids.bpl_id.val = "You"
        #ss.ids.white_type_id.val = "AI"
        ss.ids.white_type_id.val = "Computer"
        # TODO: Select computer player somehow?
        ss.ids.wpl_id.val = "Killer"
        yield(2)

        sim_press(ss.ids.start_game_id)
        '''

    def mm(self, x,y):
        self.game.make_move((x,y))
        self.app.pente_screen.perform(0)
        self.app.pente_screen.refresh_all()

    def rules_script2(s):
        play_speech("intersections")

        # Use most recent human player name
        pmgr = pm_m.PlayersMgr()
        human = pmgr.get_recent_player_names("Human", 1)[0]
        p1 = h_m.HumanPlayer(human)

        aif = f_m.AIFactory()
        genome = aig_m.AIGenome("PentAI")
        genome.use_openings_book = False
        p2 = aif.create_player(genome)

        # Create a game
        r = r_m.Rules(13, "standard")
        s.game = g_m.Game(r, p1, p2)

        # And start it...

        # demo flag: Disable prompting of players
        s.app.start_game(s.game, demo=True)
        s.ps = s.app.pente_screen

        yield(1.2) # Compensating for .7 wait at start?!

        # 
        yield(1.5)

        play_speech("centre_first")
        yield(1.5)
        s.mm(3,4)
        yield(1)

        s.mm(6,8)
        yield(.5)
        s.mm(4,4)
        yield(.5)

        s.mm(6,7)
        yield(.5)
        s.mm(5,8)
        yield(.5)
        play_speech("line_5_wins")

        s.mm(6,9)
        yield(.5)
        s.mm(8,3)
        yield(.5)

        s.mm(6,10)
        yield(3)

        # Diagonal white win
        s.app.pente_screen.set_review_mode(False)
        s.game.go_to_the_beginning()
        yield(1)

        s.mm(6, 6)
{'fraction_non_alphanumeric': 0.1065838212450711, 'fraction_numerical': 0.018042776914804635, 'mean_word_length': 1.867420349434738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2532412', 'n_tokens_mistral': 2961, 'n_tokens_neox': 2795, 'n_words': 565}