Skip to content

Commit 0e91ec8

Browse files
author
yorickvanpelt
committed
- Codechange: make webserver a plugin
1 parent b5bec02 commit 0e91ec8

File tree

5 files changed

+308
-285
lines changed

5 files changed

+308
-285
lines changed

generate-doc.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
c:\Python26\Scripts\epydoc.py -v --config web/api/epydoc.cfg
1+
c:\Python26\Scripts\epydoc.py -v --config plugins/web/api/epydoc.cfg
22
@pause

newgrfs.grflist

358 Bytes
Binary file not shown.

ottd-client.py

-85
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,6 @@ def processCommand(self, event):
233233
elif command == 'unloadirc' and not self.irc is None:
234234
self.stopIRC()
235235
Broadcast("unloaded IRC", parentclient=self)
236-
elif command == 'startwebserver':
237-
self.startWebserver()
238-
elif command == 'stopwebserver':
239-
self.stopWebserver()
240236
elif command == 'reconnect':
241237
payload = packExt('z', "%s (reconnecting)" % config.get("openttd", "quitmessage"))
242238
Broadcast("Reconnecting to server", parentclient=self, parent=event)
@@ -272,16 +268,6 @@ def processCommand(self, event):
272268
if commandstring in self.commands:
273269
self.commands[commandstring](event, command)
274270

275-
def startWebserver(self):
276-
if not config.getboolean("webserver", "enable") or not self.webserver is None:
277-
return
278-
from webserver import myWebServer
279-
LOG.debug("starting webserver ...")
280-
port = config.getint("webserver", "port")
281-
self.webserver = myWebServer(self, port)
282-
self.webserver.start()
283-
Broadcast("webserver started on port %d"%port, parentclient=self)
284-
285271
def startIRC(self):
286272
from irc_lib import IRCBotThread
287273
self.irc = IRCBotThread(self.irc_channel, config.get("irc", "nickname"), self.irc_server, self, self.irc_server_port)
@@ -301,13 +287,6 @@ def quit(self):
301287
self.reconnectCond = False
302288
self.sendMsg_TCP(const.PACKET_CLIENT_QUIT, payload)
303289

304-
def stopWebserver(self):
305-
if self.webserver:
306-
LOG.debug("stopping webserver ...")
307-
self.webserver.stop()
308-
self.webserver = None
309-
Broadcast("webserver stopped", parentclient=self)
310-
311290
def on_irc_pubmsg(self, c, e):
312291
if not e.source() is None and e.source().find('!') != -1:
313292
IRCPublicChat(e.arguments()[0], e.source().split('!')[0], parentclient=self, parentircevent=e)
@@ -399,57 +378,6 @@ def handlePacket(self, command, content):
399378
self.doCallback("on_server_newmap")
400379
self.runCond = False
401380

402-
def updateStats(self):
403-
available = True
404-
try:
405-
import pickle
406-
except ImportError:
407-
available=False
408-
if not available:
409-
LOG.error("error while loading pickle module, stats saving disabled!")
410-
return
411-
412-
LOG.debug("updating stats...")
413-
tstart = time.time()
414-
415-
fn = config.get("stats", "cachefilename")
416-
obj=[]
417-
firstSave=False
418-
try:
419-
f = open(fn, 'rb')
420-
obj = pickle.load(f)
421-
f.close()
422-
except IOError:
423-
firstSave=True
424-
425-
value = [
426-
self.getGameInfo(encode_grfs=True, short=not firstSave),
427-
self.getCompanyInfo().companies,
428-
tstart
429-
]
430-
431-
obj.append(value)
432-
433-
try:
434-
f = open(fn, 'wb')
435-
#if you use python < 2.3 use this line:
436-
#pickle.dump(obj, f)
437-
pickle.dump(obj, f, 1)
438-
f.close()
439-
except IOError:
440-
LOG.error("error while saving stats cache file!")
441-
442-
tdiff = time.time() - tstart
443-
fs = float(os.path.getsize(fn)) / float(1024)
444-
LOG.debug("stats updated in %0.5f seconds. File is %.2fKB big (%d lines)"%(tdiff, fs, len(obj)))
445-
446-
def clearStats(self):
447-
fn = config.get("stats", "cachefilename")
448-
try:
449-
os.remove(fn)
450-
LOG.debug("stats cleared")
451-
except:
452-
pass
453381
def findPlayerByNick(self, nick):
454382
for client in self.playerlist:
455383
if self.playerlist[client]['name'] == nick:
@@ -581,17 +509,8 @@ def joinGame(self):
581509
# auto start IRC
582510
if config.getboolean("irc", "autojoin"):
583511
self.startIRC()
584-
if config.getboolean("webserver", "autostart"):
585-
self.startWebserver()
586512

587513
ignoremsgs = []
588-
companyrefresh_interval = 120 #every two minutes
589-
companyrefresh_last = 0
590-
591-
592-
doStats = config.getboolean("stats", "enable")
593-
if doStats:
594-
self.clearStats()
595514

596515
while self.runCond:
597516
size, command, content = self.receiveMsg_TCP()
@@ -611,10 +530,6 @@ def joinGame(self):
611530
self.sendMsg_TCP(const.PACKET_CLIENT_ACK, payload)
612531
frameCounter=0
613532

614-
if doStats and time.time() - companyrefresh_last > companyrefresh_interval:
615-
self.updateStats()
616-
companyrefresh_last = time.time()
617-
618533
if command == const.PACKET_SERVER_COMMAND:
619534
[player, command2, p1, p2, tile, text, callback, frame, my_cmd], size = unpackFromExt('BIIIIzBIB', content)
620535

ottd_config.py

-10
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@
2323
"channel":"#openttd-python",
2424
"quitmessage":"bot shutting down..."
2525
},
26-
"webserver":{
27-
"enable":"On",
28-
"autostart":"Off",
29-
"port":"8080"
30-
},
3126
"irccommands":{},
32-
"serverstats":{
33-
"verbose":"Off",
34-
"savenewgrfs":"On",
35-
"savehistory":"On"
36-
}
3727
}
3828
def LoadConfig():
3929
config.read('config.cfg')

0 commit comments

Comments
 (0)