-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.py
148 lines (130 loc) · 8.06 KB
/
profile.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
import sys
import time
import logging
import itertools
import requests
from threading import Lock
from hsespionage import privatedata
from hsespionage import app
from hsespionage import pgInstance
from hsespionage import oapi
from hsespionage import VICTIMS_PER_USER
from flask import request
killLock = Lock()
logging.basicConfig(filename='/var/log/hsk/game.log',level=logging.DEBUG)
def tryKuhn(v, used, matched, edges):
if v in used:
return False
used.add(v)
for x in edges[v]:
if x not in matched or tryKuhn(matched[x], used, matched, edges):
matched[x] = v
return True
return False
@app.route("/engine/profile", methods=["GET","POST"])
def profile():
member = oapi.authOpenAPIMember()
if member:
if member["player"]:
userinfo = pgInstance().one("SELECT * FROM players WHERE vk_id=%(id)s", {'id': member["id"]}, back_as=dict)
victims = userinfo["victims_ids"]
victimId = request.form.get("victim_id")
if request.form.get("death_word") and victimId and 0 <= int(victimId) < VICTIMS_PER_USER:
if time.time() - userinfo['last_query_time'] < 60:
return '{"result": "time"}'
pgInstance().run("UPDATE players SET last_query_time=%(tm)s WHERE vk_id=%(vid)s", {'tm': time.time(), 'vid': member["id"]})
tobekilledId = str(victims[int(victimId)])
if tobekilledId == '-1':
return '{"result": "success"}'
toBeKilled = pgInstance().one("SELECT name, death_word, vk_id, victims_ids, killed_count, anon_id FROM players WHERE vk_id=%(vid)s", {'vid': tobekilledId}, back_as=dict)
if request.form.get("death_word").lower().strip() == toBeKilled["death_word"]:
status = pgInstance().one("SELECT value FROM vars WHERE name='status'")
if status != "running":
return '{"result": "Game not running"}'
if not userinfo['alive']:
return '{"result": "Not in the game"}'
logging.info('{0} ({1}) killed {2} ({3})'.format(userinfo['anon_id'], userinfo['name'], toBeKilled['anon_id'], toBeKilled['name']))
with killLock:
killed_list = userinfo["killed_list"]
killed_list.append(toBeKilled["vk_id"])
userinfo["killed_list"] = json.dumps(killed_list)
userinfo["score"] += 10 #TODO: score+=10 -> score += x
userinfo["killed_count"] += 1
currKillTime = time.time()
pgInstance().run("UPDATE players SET score=%(score)s, killed_count=%(kc)s, killed_list=%(kl)s, last_kill_time=%(time)s WHERE vk_id=%(vid)s", {'score': userinfo["score"], 'kc': userinfo["killed_count"], 'kl': userinfo["killed_list"], 'time': currKillTime, 'vid': member["id"]})
pgInstance().run("UPDATE players SET alive=false WHERE vk_id=%(vid)s", {'vid': toBeKilled["vk_id"]})
victims = {} # victims of the victim
for victimid in toBeKilled["victims_ids"]:
vicInfo = pgInstance().one("SELECT vk_id, name, dep, secret_word FROM players WHERE vk_id=%(vid)s", {'vid': victimid}, back_as=dict)
victims[vicInfo['vk_id']] = vicInfo
killers = {}
for killerInfo in pgInstance().all("SELECT vk_id, victims_showed, victims_ids, alive FROM players WHERE (victims_ids ? %(vid)s) AND alive=true", {'vid': toBeKilled["vk_id"]}, back_as=dict):
killers[killerInfo['vk_id']] = killerInfo
for vkid, killer in killers.items():
iof = killer['victims_ids'].index(toBeKilled["vk_id"])
del killer['victims_ids'][iof]
del killer['victims_showed'][iof]
# match killers & victims. Kuhn algo
absentEdges = {}
for killerid, killer in killers.items():
for victimid, victim in victims.items():
if victimid not in killer['victims_ids']:
if killerid not in absentEdges:
absentEdges[killerid] = []
absentEdges[killerid].append(victimid) # killer id -> victim id
matched = {} # victim id to killer id
for killerid in killers:
used = set()
tryKuhn(killerid, used, matched, absentEdges)
if len(matched) != VICTIMS_PER_USER:
logging.warning('Edges problem!')
# add an extra edge
vc = None
for victimid in victims:
if victimid not in matched:
vc = victimid
break
kl = None
for killerid in killers:
if killerid not in matched.values():
kl = killerid
break
matched[vc] = kl
logging.warning('Rematch: ' + json.dumps(matched))
for victimid, killerid in matched.items():
killers[killerid]['victims_ids'].append(victimid)
killers[killerid]['victims_showed'].append({"showing_dep": victims[victimid]['dep'],"showing_secret_word": victims[victimid]['secret_word'],"showing_name": victims[victimid]['name']})
for killer in killers.values():
pgInstance().run("UPDATE players SET victims_showed=%(vshow)s, victims_ids=%(vids)s WHERE vk_id=%(vid)s", {'vshow': json.dumps(killer['victims_showed'], ensure_ascii=False), 'vids': json.dumps(killer['victims_ids']), 'vid': killer["vk_id"]})
#check gamefinish
if len(pgInstance().all("SELECT vk_id FROM players WHERE alive=true")) <= (VICTIMS_PER_USER + 1):
logging.info('Game finished')
pgInstance().run("UPDATE vars SET value='finished' WHERE name='status'")
#achievements
if 1 not in userinfo['achievements']: # hat
if len(pgInstance().all("SELECT vk_id FROM players WHERE alive=false")) == 1: # TODO: to COUNT()
logging.info('New ac: 1')
userinfo['achievements'].append(1)
if 2 not in userinfo['achievements']: # double
if currKillTime - userinfo['last_kill_time'] < 86400: # 24h
logging.info('New ac: 2')
userinfo['achievements'].append(2)
if 3 not in userinfo['achievements']: # rock
if toBeKilled['killed_count'] > userinfo['killed_count'] - 1:
logging.info('New ac: 3')
userinfo['achievements'].append(3)
pgInstance().run("UPDATE players SET achievements=%(achs)s WHERE vk_id=%(vid)s", {'achs': json.dumps(userinfo['achievements']), 'vid': member["id"]})
return '{"result": "success"}'
else:
return '{"result": "wrong secret word"}'
else:
userinfoToReturn = userinfo
userinfoToReturn["result"] = "success"
del userinfoToReturn["victims_ids"]
del userinfoToReturn["killed_list"]
return json.dumps(userinfoToReturn)
else:
return '{"result": "not a player"}'
else:
return '{"result": "no auth"}'