Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.56 KB | None | 0 0
  1. from asyncore import *
  2. from socket import *
  3.  
  4. # Constants and stuff
  5. PLAYER_STATE_LOGIN = 0
  6. PLAYER_STATE_USERNAME = 1
  7. PLAYER_STATE_PASSWORD = 2
  8. PLAYER_STATE_INGAME = 3
  9. PLAYER_STATE_FIGHTING = 4
  10. PLAYER_STATE_SHOPPING = 5
  11. PLAYER_STATE_DEAD = 6
  12.  
  13. AI_STATE_IDLE = 0
  14. AI_STATE_MOVING = 1
  15. AI_STATE_FIGHTING = 2
  16.  
  17. # GlobalEvent Queue
  18. globalQueue = []
  19.  
  20. # rooms list?
  21. rooms = []
  22.  
  23. # some classes for the game... still thinking this out
  24.  
  25. # room Generator!
  26. class RoomFactory:
  27.     def generate(self, amount):
  28.         for x in range(amount):
  29.             rooms.append(Room())
  30.         rooms[0].set_name("start")
  31.         rooms[0].set_description("\r\nThe cold emptiness of the non-existant world around you baffles your senses\r\n")
  32.        
  33. # root class for all things in the game i guess
  34. class Obj:
  35.     def __init__(self, name, location):
  36.         self.name = name
  37.         self.location = location
  38. # a container, a chest, etc
  39. class Cont (Obj):
  40.     def __init__(self, name, location):
  41.         Obj(self, name, location)
  42.        
  43. # a room SHOULD be a container
  44. class Room (Cont):
  45.     """ more shitty code >.<"""
  46.     def __init__(self):
  47.         self.name = "void"
  48.         self.description = "You are in nothingness"
  49.    
  50.     def set_name(self, data):
  51.         self.name = data
  52.    
  53.     def get_name(self):
  54.         return self.name
  55.    
  56.     def set_description(self, data):
  57.         self.description = data
  58.    
  59.     def get_description(self):
  60.         return self.description
  61. # a creature living or dead, it is also container ( the inventory)
  62. class Mob (Cont):
  63.     def __init__(self, name, location):
  64.         Cont(self,name, location)
  65. # the player!... might remove... for better AI logic controll
  66. class Player (Mob):
  67.     def __init__(self):
  68.         pass
  69.     def __init__(self, name, location):
  70.         Obj.__init__(self, name, location)
  71. # NPC, monsters, etc
  72. class NPC (Mob):
  73.     pass
  74.    
  75. # the basic idea i have
  76. # using an  event handling system to handle input and output
  77. class MUDEvent:
  78.     """Basic Template for an Event"""
  79.     def __init__(self, data, caller):
  80.         self.caller = caller
  81.         self.contents = data
  82.         print "new event"
  83.  
  84.     def get_caller(self):
  85.         return self.caller
  86.    
  87.     def set_caller(self, obj):
  88.         self.caller = obj
  89.    
  90.     def get_content(self):
  91.         return self.contents
  92.    
  93.     def set_content(self, data):
  94.         self.contents = data
  95.    
  96. class GameLogic:
  97.     """Should be named EventHandler... not sure
  98.        Reads the globalQueue and processes events on it"""
  99.     def handle_events(self):
  100.         for event in globalQueue:
  101.             caller = event.get_caller()
  102.             callerState = caller.get_state()
  103.             if isinstance(caller, Konnection):
  104.                 if callerState == PLAYER_STATE_LOGIN:
  105.                     if event.get_content() == "LOGGING IN":
  106.                         self.greet(caller)
  107.                         caller.write("\r\nUser Name: ")
  108.                         caller.set_state(PLAYER_STATE_USERNAME)
  109.                        
  110.                 elif callerState == PLAYER_STATE_USERNAME:
  111.                     if len(event.get_content()) > 0:
  112.                         caller.assign_player(Player(event.get_content(),rooms[0]))
  113.                         caller.set_state(PLAYER_STATE_PASSWORD)
  114.                         globalQueue.append(MUDEvent("", caller))
  115.                        
  116.                 elif callerState == PLAYER_STATE_PASSWORD:
  117.                     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"
  118.                     caller.write(passmasg)
  119.                     caller.set_state(PLAYER_STATE_INGAME)
  120.                    
  121.                 elif callerState == PLAYER_STATE_INGAME:
  122.                     self.parse_cmd(caller, event.get_content())
  123.                    
  124.                 elif callerState:
  125.                     pass
  126.                    
  127.             elif isinstance(event.get_caller(), NPC):
  128.                 pass
  129.            
  130.             globalQueue.remove(event)
  131.                
  132.     def greet(self, caller):
  133.         greetmsg = """
  134.         Hello, and welcome to the test Server im working
  135.         on for my newest project!\r\n"""
  136.         caller.write(greetmsg)
  137.     def parse_cmd(self, caller, data):
  138.         if data == "look":
  139.             caller.write(caller.player.location.get_description())
  140.         pass
  141.        
  142. class Konnection (dispatcher, Mob):
  143.    
  144.     def __init__(self, address, sk, server):
  145.         dispatcher.__init__(self, sk)
  146.         self.conn = sk
  147.         self.ip = address[0]
  148.         self.sever = server
  149.         self.player = None
  150.         self.buffer = []
  151.         self.playState = PLAYER_STATE_LOGIN
  152.         self.write("\r\nConnecting to the Server...\r\n")
  153.         print "Login from " + self.ip
  154.         globalQueue.append(MUDEvent("LOGGING IN",self))
  155.    
  156.     def writable(self):
  157.         return self.buffer
  158.        
  159.     def end_connection(self):
  160.         self.buffer.append(None)
  161.    
  162.     def write(self, data):
  163.         self.buffer.append(data)
  164.    
  165.     def handle_read(self):
  166.         data = self.recv(1024)
  167.  
  168.         if not data == "\n" or  not data == "\r\n":
  169.             data = data.strip()
  170.        
  171.         globalQueue.append(MUDEvent(data, self))
  172.    
  173.     def handle_write(self):
  174.         if self.buffer[0] is None:
  175.             self.close()
  176.             return
  177.        
  178.         sent = self.send(self.buffer[0])
  179.         if sent >= len(self.buffer[0]):
  180.             self.buffer.pop(0)
  181.         else:
  182.             self.buffer[0] = self.buffer[0][send:]
  183.    
  184.     def handle_close(self):
  185.         self.server.connections.remove(self)
  186.        
  187.     def get_state(self):
  188.         return self.playState
  189.    
  190.     def set_state(self,STATE):
  191.         self.playState = STATE
  192.    
  193.     def assign_player(self, body):
  194.         self.player = body
  195.    
  196.     def get_player(self, body):
  197.         return self.player
  198.        
  199. class NetServer (dispatcher):
  200.  
  201.     def __init__(self, host, port):
  202.         dispatcher.__init__(self)
  203.         self.create_socket(AF_INET, SOCK_STREAM)
  204.         self.set_reuse_addr()
  205.         self.bind((host,port))
  206.         self.listen(5)
  207.         self.connections = []
  208.         print "Listening on port " +  `port` + "."
  209.  
  210.     def writable(self):
  211.         return 0
  212.     def handle_accept(self):
  213.         conn, addr= self.accept()
  214.         self.connections.append(Konnection(addr, conn, self))
  215.        
  216.     def end_server(self):
  217.             self.close()
  218.             for co in self.connections:
  219.                 co.write("\r\nSERVER IS SHUTTING DOWN\r\n")
  220.                 co.end_connection()
  221.        
  222.        
  223. # Main
  224.  
  225. server = NetServer("0.0.0.0",2002)
  226. game = GameLogic()
  227. rf = RoomFactory()
  228. rf.generate(100)
  229. try:
  230.     while 1:
  231.         game.handle_events()
  232.         poll() #loop(1.0)
  233.         #print "testing"
  234. except KeyboardInterrupt:
  235.     pass
  236.  
  237. server.end_server()
  238. loop(1.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement