forked from gregstoll/cluesolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clue.py
executable file
·101 lines (96 loc) · 4.67 KB
/
clue.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python3
import xml.etree.ElementTree as ET
import cgi, sys, json
import clueengine
from functools import reduce
def error(s):
print("Content-type: application/json\n")
print('{"errorStatus": 1, "errorText": "%s"}' % s)
sys.exit(0)
def success(s):
print("Content-type: application/json\n")
print('{"errorStatus": 0, %s}' % s)
sys.exit(0)
def getClauseInfo(engine):
info = {}
for i in range(engine.numPlayers):
curInfo = []
for clause in engine.players[i].possibleCards:
curInfo.append(list(clause))
if len(curInfo) > 0:
info[repr(i)] = curInfo
return info
def getInfoFromChangedCards(engine, changedCards):
info = []
for card in changedCards:
possibleOwners = list(engine.whoHasCard(card))
if len(possibleOwners) == 1:
owner = possibleOwners[0]
if (owner == engine.numPlayers):
status = 2
else:
status = 1
info.append({"card": card, "status": status, "owner":possibleOwners})
else:
if engine.numPlayers in possibleOwners:
status = 0
else:
status = 1
info.append({"card": card, "status": status, "owner":possibleOwners})
return info
form = cgi.FieldStorage()
action = None
if ('action' in form):
action = form.getfirst('action')
else:
error("Internal error - No action specified!")
# Valid actions are 'new', 'whoOwns', 'suggestion', 'fullInfo', 'simulate' ('accusation' in the future?)
if (action != 'new' and action != 'whoOwns' and action != 'suggestion' and action != 'fullInfo' and action != 'simulate'):
error("Internal error - invalid action '%s'!" % action)
if (action != 'new' and ('sess' not in form)):
error("Internal error - missing sess!")
if (action != 'new'):
(engine, s) = clueengine.ClueEngine.loadFromString(form.getfirst('sess'))
if (s != ''):
error("Internal error - invalid session string '%s'!" % form.getfirst('sess'))
else:
if ('players' not in form):
error("Internal error - action new without players!")
numPlayers = int(form.getfirst('players'))
engine = clueengine.ClueEngine(numPlayers)
for i in range(numPlayers):
if ('numCards%d' % i not in form):
error("Internal error - action new missing key numCards%d!" % i)
numP = int(form.getfirst('numCards%d' % i))
engine.players[i].numCards = numP
if (action == 'new'):
# This is all we have to do.
success('"session": "%s"' % engine.writeToString())
if (action == 'whoOwns'):
# See who owns what.
if ('owner' not in form or 'card' not in form):
error("Internal error: action=whoOwns, missing owner or card!")
owner = int(form.getfirst('owner'))
card = form.getfirst('card')
changedCards = engine.infoOnCard(owner, card, True)
success('"newInfo": %s, "clauseInfo": %s, "session": "%s", "isConsistent": %s' % (json.dumps(getInfoFromChangedCards(engine, changedCards)), json.dumps(getClauseInfo(engine)), engine.writeToString(), json.dumps(engine.isConsistent())))
if (action == 'suggestion'):
# See what the suggestion is
if ('suggestingPlayer' not in form or 'card1' not in form or 'card2' not in form or 'card3' not in form or 'refutingPlayer' not in form or 'refutingCard' not in form):
error("Internal error: action=whoOwns, missing suggestingPlayer, card1, card2, card3, refutingPlayer, or refutingCard!")
suggestingPlayer = int(form.getfirst('suggestingPlayer'))
card1 = form.getfirst('card1')
card2 = form.getfirst('card2')
card3 = form.getfirst('card3')
refutingPlayer = int(form.getfirst('refutingPlayer'))
refutingCard = form.getfirst('refutingCard')
if (refutingPlayer == -1):
refutingPlayer = None
if (refutingCard == "None"):
refutingCard = None
changedCards = engine.suggest(suggestingPlayer, card1, card2, card3, refutingPlayer, refutingCard)
success('"newInfo": %s, "clauseInfo": %s, "session": "%s", "isConsistent": %s' % (json.dumps(getInfoFromChangedCards(engine, changedCards)), json.dumps(getClauseInfo(engine)), engine.writeToString(), json.dumps(engine.isConsistent())))
if (action == 'fullInfo'):
success('"newInfo": %s, "clauseInfo": %s, "session": "%s", "numPlayers": %d, "numCards": %s, "isConsistent": %s' % (json.dumps(getInfoFromChangedCards(engine, reduce(lambda x, y: x+y, [engine.cards[x] for x in engine.cards]))), json.dumps(getClauseInfo(engine)), engine.writeToString(), engine.numPlayers, json.dumps([x.numCards for x in engine.players[:-1]]), json.dumps(engine.isConsistent())))
if (action == 'simulate'):
success('"simData": %s' % json.dumps(engine.getSimulationData()))