Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from asyncore import *
- from socket import *
- # Constants and stuff
- PLAYER_STATE_LOGIN = 0
- PLAYER_STATE_USERNAME = 1
- PLAYER_STATE_PASSWORD = 2
- PLAYER_STATE_INGAME = 3
- PLAYER_STATE_FIGHTING = 4
- PLAYER_STATE_SHOPPING = 5
- PLAYER_STATE_DEAD = 6
- AI_STATE_IDLE = 0
- AI_STATE_MOVING = 1
- AI_STATE_FIGHTING = 2
- # GlobalEvent Queue
- globalQueue = []
- # rooms list?
- rooms = []
- # some classes for the game... still thinking this out
- # room Generator!
- class RoomFactory:
- def generate(self, amount):
- for x in range(amount):
- rooms.append(Room())
- rooms[0].set_name("start")
- rooms[0].set_description("\r\nThe cold emptiness of the non-existant world around you baffles your senses\r\n")
- # root class for all things in the game i guess
- class Obj:
- def __init__(self, name, location):
- self.name = name
- self.location = location
- # a container, a chest, etc
- class Cont (Obj):
- def __init__(self, name, location):
- Obj(self, name, location)
- # a room SHOULD be a container
- class Room (Cont):
- """ more shitty code >.<"""
- def __init__(self):
- self.name = "void"
- self.description = "You are in nothingness"
- def set_name(self, data):
- self.name = data
- def get_name(self):
- return self.name
- def set_description(self, data):
- self.description = data
- def get_description(self):
- return self.description
- # a creature living or dead, it is also container ( the inventory)
- class Mob (Cont):
- def __init__(self, name, location):
- Cont(self,name, location)
- # the player!... might remove... for better AI logic controll
- class Player (Mob):
- def __init__(self):
- pass
- def __init__(self, name, location):
- Obj.__init__(self, name, location)
- # NPC, monsters, etc
- class NPC (Mob):
- pass
- # the basic idea i have
- # using an event handling system to handle input and output
- class MUDEvent:
- """Basic Template for an Event"""
- def __init__(self, data, caller):
- self.caller = caller
- self.contents = data
- print "new event"
- def get_caller(self):
- return self.caller
- def set_caller(self, obj):
- self.caller = obj
- def get_content(self):
- return self.contents
- def set_content(self, data):
- self.contents = data
- class GameLogic:
- """Should be named EventHandler... not sure
- Reads the globalQueue and processes events on it"""
- def handle_events(self):
- for event in globalQueue:
- caller = event.get_caller()
- callerState = caller.get_state()
- if isinstance(caller, Konnection):
- if callerState == PLAYER_STATE_LOGIN:
- if event.get_content() == "LOGGING IN":
- self.greet(caller)
- caller.write("\r\nUser Name: ")
- caller.set_state(PLAYER_STATE_USERNAME)
- elif callerState == PLAYER_STATE_USERNAME:
- if len(event.get_content()) > 0:
- caller.assign_player(Player(event.get_content(),rooms[0]))
- caller.set_state(PLAYER_STATE_PASSWORD)
- globalQueue.append(MUDEvent("", caller))
- elif callerState == PLAYER_STATE_PASSWORD:
- passmasg = "\r\nHi!, " + caller.player.name + ". Use the Look command. Since this is just for testing" + "\r\nYou will be signed in without an account\r\n"
- caller.write(passmasg)
- caller.set_state(PLAYER_STATE_INGAME)
- elif callerState == PLAYER_STATE_INGAME:
- self.parse_cmd(caller, event.get_content())
- elif callerState:
- pass
- elif isinstance(event.get_caller(), NPC):
- pass
- globalQueue.remove(event)
- def greet(self, caller):
- greetmsg = """
- Hello, and welcome to the test Server im working
- on for my newest project!\r\n"""
- caller.write(greetmsg)
- def parse_cmd(self, caller, data):
- if data == "look":
- caller.write(caller.player.location.get_description())
- pass
- class Konnection (dispatcher, Mob):
- def __init__(self, address, sk, server):
- dispatcher.__init__(self, sk)
- self.conn = sk
- self.ip = address[0]
- self.sever = server
- self.player = None
- self.buffer = []
- self.playState = PLAYER_STATE_LOGIN
- self.write("\r\nConnecting to the Server...\r\n")
- print "Login from " + self.ip
- globalQueue.append(MUDEvent("LOGGING IN",self))
- def writable(self):
- return self.buffer
- def end_connection(self):
- self.buffer.append(None)
- def write(self, data):
- self.buffer.append(data)
- def handle_read(self):
- data = self.recv(1024)
- if not data == "\n" or not data == "\r\n":
- data = data.strip()
- globalQueue.append(MUDEvent(data, self))
- def handle_write(self):
- if self.buffer[0] is None:
- self.close()
- return
- sent = self.send(self.buffer[0])
- if sent >= len(self.buffer[0]):
- self.buffer.pop(0)
- else:
- self.buffer[0] = self.buffer[0][send:]
- def handle_close(self):
- self.server.connections.remove(self)
- def get_state(self):
- return self.playState
- def set_state(self,STATE):
- self.playState = STATE
- def assign_player(self, body):
- self.player = body
- def get_player(self, body):
- return self.player
- class NetServer (dispatcher):
- def __init__(self, host, port):
- dispatcher.__init__(self)
- self.create_socket(AF_INET, SOCK_STREAM)
- self.set_reuse_addr()
- self.bind((host,port))
- self.listen(5)
- self.connections = []
- print "Listening on port " + `port` + "."
- def writable(self):
- return 0
- def handle_accept(self):
- conn, addr= self.accept()
- self.connections.append(Konnection(addr, conn, self))
- def end_server(self):
- self.close()
- for co in self.connections:
- co.write("\r\nSERVER IS SHUTTING DOWN\r\n")
- co.end_connection()
- # Main
- server = NetServer("0.0.0.0",2002)
- game = GameLogic()
- rf = RoomFactory()
- rf.generate(100)
- try:
- while 1:
- game.handle_events()
- poll() #loop(1.0)
- #print "testing"
- except KeyboardInterrupt:
- pass
- server.end_server()
- loop(1.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement