-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdungeon.py
89 lines (68 loc) · 2.1 KB
/
dungeon.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
import sqlite3, atexit, random
import schedule
from peewee import *
from datetime import datetime
from contextlib import closing
# configuration
DATABASE = 'sm_daily.db'
DEBUG = True
# Dungeon specifications
_TYPES = ["Other", "Audio", "Image", "Compressed", "Video", "Executable"]
_SEEDSIZE = 63
class Dungeon:
def __init__(self):
self.refresh()
def refresh(self):
import models
self.seed = random.getrandbits(_SEEDSIZE)
self.difficulty = random.randint(1, 5)
self.filetype = random.choice(_TYPES)
# remove expired dungeons
db.connect()
with db.transaction():
models.UserDungeon.delete().where(
models.UserDungeon.expiration_date < datetime.now()
).execute()
# clear table
models.DeadPlayer.delete().execute()
db.close()
def __iter__(self):
yield ('seed', self.seed)
yield ('type', self.filetype)
yield ('difficulty', self.difficulty)
db = SqliteDatabase(DATABASE, autocommit=True)
def create_tables():
import models
db.connect()
db.create_tables([models.DeadPlayer, models.UserDungeon], True)
db.close()
def main():
import server
import web
import tornado.ioloop
import tornado.web
# make sure db exists
create_tables()
# make sure dungeon exists
dungeon = Dungeon()
# create a new dungeon every day at midnight
schedule.every().day.at("00:00").do(dungeon.refresh)
# configuration
host = '0.0.0.0'
tcpport = 8081
httpport = 8080
# tcp server
server = server.StorymodeTCPServer()
server.listen(tcpport, host)
print("Listening on %s:%d..." % (host, tcpport))
# http server
http = tornado.web.Application([
(r'/daily', web.DailyDungeonRequestHandler, dict(dungeon=dungeon)),
(r'/community', web.UserUploadRequestHandler, dict(db=db)),
])
http.listen(httpport, host)
print("Listening on %s:%d..." % (host, httpport))
# infinite loop
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()