giants-stupid-server/server.py
2019-01-22 15:11:47 +01:00

64 lines
1.8 KiB
Python

from giants.map import Map
from giants.player import Player
from dpnet.netserver import Netserver
from dpnet.session import Session
from dpnet.DFrame import DFrame
import socket
class Server:
def __init__(self, **kwargs):
self.listen_ip = kwargs.get("ip", "0.0.0.0")
self.listen_port = kwargs.get("port", 19711)
fake_session = Session(socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
fake_session.ip = "127.0.0.1"
fake_session.port = 3333
self.players = [Player("Amazed4", fake_session)]
self.currentmap = kwargs.get("map", Map("wow rly"))
self.maxplayers = kwargs.get("maxplayers", 20)
self.name = kwargs.get("name", "Default Server Name")
self.accept_new_players = True
self.register_with_ms = False
# events
self._on_new_player = []
self._on_new_map = []
def new_player(self, player):
self.players.append(player)
for func in self._on_new_player:
func(player)
def change_map(self, mapname):
for func in self._on_new_map:
func(mapname)
def on_new_player(self, func):
self._on_new_player.append(func)
return func
def broadcast_message(self, text):
for player in self.players:
player.session.send_gamedata(b'\x35\x80\x81'+text.encode("ascii")+b'\x00\x00')
def on_new_map(self, func):
self._on_new_map.append(func)
return func
def run(self):
return Netserver(self).run()
if __name__ == '__main__':
server = Server(name="giantsd", map=Map("expect crashes"), maxplayers=10)
@server.on_new_player
def new_player(player):
#print("A NEW PLAYER HAS JOINED: %s" % player)
pass
@server.on_new_map
def new_map(mapname):
pass
server.run() # blocking