79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
from PodSixNet.Channel import Channel
|
|
from PodSixNet.Server import Server
|
|
import time
|
|
import sys
|
|
import threading
|
|
chatters = {}
|
|
queue = []
|
|
banned = []
|
|
|
|
def tostring(target):
|
|
temp = ""
|
|
for i in range(len(target)):
|
|
index = i
|
|
i = target[i]
|
|
if index != len(target)-1:
|
|
temp = temp + i + " "
|
|
else:
|
|
temp = temp + i
|
|
return temp
|
|
|
|
class clientchannel(Channel):
|
|
def __init__(self, conn=None, addr=..., server=None, map=None):
|
|
super().__init__(conn, addr, server, map)
|
|
self.name = "Unknown"
|
|
|
|
def Network_send(self, data):
|
|
global queue
|
|
queue.append("{}:{}".format(self.name, data["message"]))
|
|
|
|
def Network_setname(self, data):
|
|
global queue
|
|
global banned
|
|
if data["name"] in banned:
|
|
queue.append("{} is banned, disconnecting".format(data["name"]))
|
|
self.close()
|
|
queue.append("{} joined!".format(data["name"]))
|
|
global chatters
|
|
chatters[data["name"]] = self
|
|
self.name = data["name"]
|
|
|
|
class server(Server):
|
|
channelClass = clientchannel
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
Server.__init__(self, *args, **kwargs)
|
|
|
|
def Connected(self, channel, addr):
|
|
global chatters
|
|
chatters[channel.name] = channel
|
|
|
|
gameserver = server(localaddr=("localhost", 2003))
|
|
def chatloop():
|
|
global queue
|
|
global chatters
|
|
while True:
|
|
cmd = input()
|
|
cmd2 = cmd.split()
|
|
if cmd2[0] == "kick":
|
|
cmd2.remove(cmd2[0])
|
|
cmd = tostring(cmd2)
|
|
queue.append("{} has been kicked".format(cmd))
|
|
chatters[cmd].close()
|
|
chatters.pop(cmd)
|
|
if cmd2[0] == "ban":
|
|
cmd2.remove(cmd2[0])
|
|
cmd = tostring(cmd2)
|
|
queue.append("{} has been banned".format(cmd))
|
|
chatters[cmd].close()
|
|
banned.append(cmd)
|
|
chatters.pop(cmd)
|
|
threading.Thread(target=chatloop).start()
|
|
while True:
|
|
for i in chatters:
|
|
chatters[i].Send({"action": "queuesync", "queue": queue})
|
|
for i in queue:
|
|
print(i)
|
|
queue.remove(i)
|
|
gameserver.Pump()
|
|
time.sleep(0.001) |