-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmoerderspiel-cli.py
73 lines (67 loc) · 1.88 KB
/
moerderspiel-cli.py
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
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import os, glob
import sys
workdir = '.'
modpath = os.path.join(workdir,"lib")
sys.path.insert(0,modpath)
import pickle
import codecs
import os.path
import shutil
import moerderklassen
import filelock
import logging
import datetime
from moerdergraph import moerdergraph
from pprint import pformat
class G:
@staticmethod
def u8(s):
try:
return s.decode('utf8')
except UnicodeDecodeError:
try:
return s.decode('latin1')
except UnicodeDecodeError:
return None
G.workdir = workdir
G.modpath = modpath
G.cssdir = os.path.join(workdir, 'css')
G.templatedir = os.path.join(workdir, 'templates')
G.savegamedir = os.path.join(workdir, 'savegames')
logging.basicConfig(filename=os.path.join(G.savegamedir, 'moerderspiel-%s.log'%datetime.date.today()), level=logging.DEBUG)
def _savegame(game, checkifexists=False):
G.fname = G.savegamedir + '/' + game.id + '.pkl'
if checkifexists and os.path.isfile(G.fname):
#raise AssertionError
pass
output = open(G.fname + '.tmp', 'wb')
pickle.dump(game, output)
output.close()
os.rename(G.fname + '.tmp', G.fname)
G.lockfile.release()
del G.lockfile
return game.id
def _loadgame(gameid, lock=True):
G.fname = G.savegamedir + '/' + gameid + '.pkl'
G.lockfile = filelock.FileLock(G.fname + '.lock')
while G.lockfile.acquire():
pass
input = open(G.savegamedir + '/' + gameid + '.pkl', 'rd')
ret = pickle.load(input)
input.close()
if not lock:
G.lockfile.release()
del G.lockfile
return ret
def ldgame(gameid):
G.fname = G.savegamedir + '/' + gameid + '.pkl'
input = open(G.savegamedir + '/' + gameid + '.pkl', 'rd')
ret = pickle.load(input)
input.close()
return ret
def resetrounds(game):
for ro in game.rounds:
for participant in game.rounds[ro].participants:
participant.killedby = None
game.rounds[ro].participants = []