<commit_before>

def save_game_state(game_id, game_state):
    pass

def save_game_history(game_type, player_ids, game_states):
    pass

def load_game_history(game_id):
    pass
<commit_msg>implemented persistence based on couchdb<commit_after>from uuid import uuid4
from couchdb.client import Server
import json

def get_server():
    s = Server()
    return s

def get_db(name=None,server=None):
    if name is None:
        name="game_states"
    if server is None:
        server=get_server()
    if name in server:
        db = server[name]
    else:
        db = server.create(name)
    return db

def get_new_id():
    doc_id = uuid4().hex
    return doc_id

def save_game_state(game_id, sequence,game_type, player_ids, game_state,db=None):
    if db is None:
        db = get_db()
    state_id = get_new_id()
    doc = {"sequence":sequence,
           "player_ids":json.dumps(player_ids),
           "game_state":json.dumps(game_state),
           "game_id":game_id,
           "game_type":game_type,
           "_id":state_id}

    db.save(doc)
    return state_id


def save_game_history(game_type, player_ids, game_states,db=None):
    game_id = get_new_id()
    if db is None:
        db = get_db()

    for i,state in enumerate(game_states):
        save_game_state(game_id=game_id,
                        sequence=i,
                        game_type=game_type,
                        player_ids=player_ids,
                        game_state=state,
                        db=db)
    return game_id


def load_game_history(game_id,db=None):
    map_fun = '''function(doc) {emit([doc.game_id], doc)}'''
    if db is None:
        db = get_db()
    result = db.query(map_fun)
    data =  {r.id:r.value for r in result[[game_id]]}
    for key,value in data.items():
        game_state=data[key]["game_state"]
        data[key]["game_state"] = json.loads(game_state)
    return data

{'fraction_non_alphanumeric': 0.10490247759620454, 'fraction_numerical': 0.0010542962572482868, 'mean_word_length': 2.5018450184501844, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '1993611', 'n_tokens_mistral': 643, 'n_tokens_neox': 635, 'n_words': 138}