def update_state(state, move, outcome): """ Update (or create) outcome probabilities for a game state and move combo Arguments: state and move: standard notation game strings outcome: array of player scores [ player, bot ] """ from bot import Bot game = BotMoves.find_state(state, move) outcome = BotMoves.outcome(outcome) if game: if game[0].count() > 1: # Dup! # Use the most played move dups = [] for game in game[0]: dups.append(game) dups.sort(key=lambda g: g.played, reverse=True) for i in range(1, len(dups)): dups[i].delete() game = dups[0] else: game = game[0].one() game.played += 1 if outcome == 1: game.wins += 1 elif outcome == 0: game.draws += 1 else: game.losses += 1 game.winp = float(game.wins) / float(game.played) game.lossp = float(game.losses) / float(game.played) game.drawp = float(game.draws) / float(game.played) if game.played_in_sequence >= Bot.noise_factor(): game.played_in_sequence = 0 else: game.played_in_sequence += 1 elixir.session.commit() # Add mapped flag if all valid moves have been played from botmovesmapped import BotMovesMapped if not BotMovesMapped.has(game.state): from state import State state = State(game.state) if not Bot.get_missing_move(state): BotMovesMapped(state=game.state) elixir.session.commit() else: # Create new record w = d = l = 0 if outcome == 1: w = 1 elif outcome == 0: d = 1 else: l = 1 BotMoves(state=state, move=move, wins=w, draws=d, losses=l, winp=float(w), drawp=float(d), lossp=float(l)) elixir.session.commit()