summaryrefslogtreecommitdiff
path: root/qml/python/board.py
blob: 19f720a2f8da896e7fe57b1f7f4541abd8b2a7db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import os.path
try:
    import pyotherside
except:
    print("no pyotherside module loaded")

import sgfparser
from game import Game

counter = 0

path = ""
cursor = None

def setPath(qtPath):
    global path
    path = qtPath

def loadBoard(filename):
    global cursor

    if os.path.isfile(filename):
        sgfPath = filename
    else:
        sgfPath = os.path.join(path,"../content","sgf",filename);
    pyotherside.send('log', sgfPath)
    try:
        f = open(sgfPath)
        s = f.read()
        f.close()
    except IOError:
        pyotherside.send('log', "Cannot open %s" % filename)
        return

    try:
        cursor = sgfparser.Cursor(s)
    except sgfparser.SGFError:
        pyotherside.send('log', 'Error in SGF file!')
        return
    pyotherside.send('log', 'File %s loaded' % filename)
    pyotherside.send('log', 'Found %d problems' % cursor.root.numChildren)
    return cursor.root.numChildren

def getGame(n):
    global cursor

    cursor.game(n)

    game = Game(cursor)
    game.normalize()

    return {
        "tree": game.tree,
        "size": game.get_size(),
        "side": game.side,
        "current_player": game.current_player,
    }