comparison orpg/networking/mplay_server_gui.py @ 56:c7f04d3c76f5 traipse_dev

Major update to Server GUI. Basically makes it functional.
author sirebral
date Fri, 07 Aug 2009 21:53:14 -0500
parents a571772a45c7
children 9014d7861bb3
comparison
equal deleted inserted replaced
55:0b79d5dbbe9e 56:c7f04d3c76f5
5 # 5 #
6 6
7 __appname__=' OpenRPG GUI Server v0.7 ' 7 __appname__=' OpenRPG GUI Server v0.7 '
8 __version__='$Revision: 1.26 $'[11:-2] 8 __version__='$Revision: 1.26 $'[11:-2]
9 __cvsinfo__='$Id: mplay_server_gui.py,v 1.26 2007/11/06 00:32:39 digitalxero Exp $'[5:-2] 9 __cvsinfo__='$Id: mplay_server_gui.py,v 1.26 2007/11/06 00:32:39 digitalxero Exp $'[5:-2]
10 __doc__=""" 10 __doc__="""OpenRPG Server Graphical Interface"""
11 OpenRPG Server Graphical Interface
12 """
13 11
14 import os 12 import os
15 import sys 13 import sys
16 import time 14 import time
17 import types 15 import types
20 from orpg.orpg_wx import * 18 from orpg.orpg_wx import *
21 import webbrowser 19 import webbrowser
22 from threading import Thread 20 from threading import Thread
23 from meta_server_lib import post_server_data, remove_server 21 from meta_server_lib import post_server_data, remove_server
24 from mplay_server import mplay_server 22 from mplay_server import mplay_server
23 from xml.dom import minidom
25 24
26 # Constants ###################################### 25 # Constants ######################################
27 SERVER_RUNNING = 1 26 SERVER_RUNNING = 1
28 SERVER_STOPPED = 0 27 SERVER_STOPPED = 0
29 MENU_START_SERVER = wx.NewId() 28 MENU_START_SERVER = wx.NewId()
86 class ServerConfig: 85 class ServerConfig:
87 """ This class contains configuration 86 """ This class contains configuration
88 setting used to control the server. 87 setting used to control the server.
89 """ 88 """
90 89
91 def __init__(self, owner ): 90 def __init__(self, owner ):
92 """ Loads default configuration settings. 91 """ Loads default configuration settings."""
93 """ 92 userPath = orpg.dirpath.dir_struct["user"]
94 OPENRPG_PORT = 9557 93 validate = orpg.tools.validate.Validate(userPath)
94 validate.config_file( "server_ini.xml", "default_server_ini.xml" )
95 configDom = minidom.parse(userPath + 'server_ini.xml')
96 port = configDom.childNodes[0].childNodes[1].getAttribute('port')
97 OPENRPG_PORT = 6774 if port == '' else int(port) #Pretty ugly, but I couldn't find the tag any other way.
95 self.owner = owner 98 self.owner = owner
96 99
97 def load_xml(self, xml): 100 def load_xml(self, xml):
98 """ Load configuration from XML data. 101 """ Load configuration from XML data.
99 xml (xml) -- xml string to parse 102 xml (xml) -- xml string to parse
143 # Main = GUI 146 # Main = GUI
144 class Connections(wx.ListCtrl): 147 class Connections(wx.ListCtrl):
145 def __init__( self, parent, main ): 148 def __init__( self, parent, main ):
146 wx.ListCtrl.__init__( self, parent, -1, wx.DefaultPosition, wx.DefaultSize, wx.LC_REPORT|wx.SUNKEN_BORDER|wx.EXPAND|wx.LC_HRULES ) 149 wx.ListCtrl.__init__( self, parent, -1, wx.DefaultPosition, wx.DefaultSize, wx.LC_REPORT|wx.SUNKEN_BORDER|wx.EXPAND|wx.LC_HRULES )
147 self.main = main 150 self.main = main
148 self.roomList = { "0" : "Lobby" } 151 self.roomList = { 0 : "Lobby" }
149 self._imageList = wx.ImageList( 16, 16, False ) 152 self._imageList = wx.ImageList( 16, 16, False )
150 img = wx.Image(orpg.dirpath.dir_struct["icon"]+"player.gif", wx.BITMAP_TYPE_GIF).ConvertToBitmap() 153 img = wx.Image(orpg.dirpath.dir_struct["icon"]+"player.gif", wx.BITMAP_TYPE_GIF).ConvertToBitmap()
151 self._imageList.Add( img ) 154 self._imageList.Add( img )
152 img = wx.Image(orpg.dirpath.dir_struct["icon"]+"player-whisper.gif", wx.BITMAP_TYPE_GIF).ConvertToBitmap() 155 img = wx.Image(orpg.dirpath.dir_struct["icon"]+"player-whisper.gif", wx.BITMAP_TYPE_GIF).ConvertToBitmap()
153 self._imageList.Add( img ) 156 self._imageList.Add( img )
169 # Build our pop up menu to do cool things with the players in the list 172 # Build our pop up menu to do cool things with the players in the list
170 self.menu = wx.Menu() 173 self.menu = wx.Menu()
171 self.menu.SetTitle( "Player Menu" ) 174 self.menu.SetTitle( "Player Menu" )
172 self.menu.Append( MENU_PLAYER_BOOT, "Boot Player" ) 175 self.menu.Append( MENU_PLAYER_BOOT, "Boot Player" )
173 self.menu.AppendSeparator() 176 self.menu.AppendSeparator()
174 self.menu.Append( MENU_PLAYER_SEND_MESSAGE, "Send Message" ) 177 self.menu.Append( MENU_PLAYER_SEND_MESSAGE, "Send Player Message" )
178 self.menu.Append( MENU_PLAYER_SEND_ROOM_MESSAGE, "Send Room Message" )
175 self.menu.Append( MENU_PLAYER_SEND_SERVER_MESSAGE, "Broadcast Server Message" ) 179 self.menu.Append( MENU_PLAYER_SEND_SERVER_MESSAGE, "Broadcast Server Message" )
176 180
177 # Associate our events 181 # Associate our events
178 self.Bind(wx.EVT_RIGHT_DOWN, self.OnPopupMenu) 182 self.Bind(wx.EVT_RIGHT_DOWN, self.OnPopupMenu)
179 self.Bind(wx.EVT_MENU, self.OnPopupMenuItem, id=MENU_PLAYER_BOOT) 183 self.Bind(wx.EVT_MENU, self.OnPopupMenuItem, id=MENU_PLAYER_BOOT)
182 self.Bind(wx.EVT_MENU, self.OnPopupMenuItem, id=MENU_PLAYER_SEND_SERVER_MESSAGE) 186 self.Bind(wx.EVT_MENU, self.OnPopupMenuItem, id=MENU_PLAYER_SEND_SERVER_MESSAGE)
183 187
184 def add(self, player): 188 def add(self, player):
185 i = self.InsertImageStringItem( 0, player["id"], 0 ) 189 i = self.InsertImageStringItem( 0, player["id"], 0 )
186 self.SetStringItem( i, 1, self.stripHtml( player["name"] ) ) 190 self.SetStringItem( i, 1, self.stripHtml( player["name"] ) )
187 self.SetStringItem( i, 2, self.stripHtml( player["status"] ) ) 191 self.SetStringItem( i, 2, "new" )
188 self.SetStringItem( i, 3, "ROOM" ) 192 self.SetStringItem( i, 3, self.roomList[0] )
189 self.SetStringItem( i, 4, self.stripHtml( player["version"] ) ) 193 self.SetStringItem( i, 4, self.stripHtml( player["version"] ) )
190 self.SetStringItem( i, 5, self.stripHtml( player["role"] ) ) 194 self.SetStringItem( i, 5, self.stripHtml( player["role"] ) )
191 self.SetStringItem( i, 6, self.stripHtml( player["ip"] ) ) 195 self.SetStringItem( i, 6, self.stripHtml( player["ip"] ) )
192 self.SetStringItem (i, 7, "PING" ) 196 self.SetStringItem (i, 7, "PING" )
193 self.SetItemData( i, int(player["id"]) ) 197 self.SetItemData( i, int(player["id"]) )
210 self.Refresh() 214 self.Refresh()
211 215
212 def update(self, player): 216 def update(self, player):
213 i = self.FindItemData( -1, int(player["id"]) ) 217 i = self.FindItemData( -1, int(player["id"]) )
214 if i > -1: 218 if i > -1:
215 self.SetStringItem(i, 1, player["name"]) 219 self.SetStringItem(i, 1, self.stripHtml(player["name"]))
216 self.SetStringItem(i, 2, self.stripHtml( player["status"] ) ) 220 self.SetStringItem(i, 2, self.stripHtml(player["status"]))
217 self.AutoAjust() 221 self.AutoAjust()
218 else: self.add(player) 222 else: self.add(player)
219 223
220 def updateRoom( self, data ): 224 def updateRoom( self, data ):
221 (from_id, id) = data 225 (room, room_id, player) = data
222 i = self.FindItemData( -1, int(from_id) ) 226 i = self.FindItemData( -1, int(player) )
223 self.SetStringItem( i, 3, self.roomList[id] ) 227 self.SetStringItem( i, 3, room )
224 self.SetStringItem( i, 5, "Lurker") 228 self.AutoAjust()
225 self.Refresh()
226 229
227 def setPlayerRole( self, id, role ): 230 def setPlayerRole( self, id, role ):
228 i = self.FindItemData( -1, int(id) ) 231 i = self.FindItemData( -1, int(id) )
229 self.SetStringItem( i, 5, role ) 232 self.SetStringItem( i, 5, role )
230 233
252 def OnPopupMenuItem( self, event ): 255 def OnPopupMenuItem( self, event ):
253 # if we are not running, the player list is empty anyways 256 # if we are not running, the player list is empty anyways
254 if self.main.STATUS == SERVER_RUNNING: 257 if self.main.STATUS == SERVER_RUNNING:
255 menuItem = event.GetId() 258 menuItem = event.GetId()
256 playerID = str( self.GetItemData( self.selectedItem ) ) 259 playerID = str( self.GetItemData( self.selectedItem ) )
260 room = str(self.GetItem((int(playerID)-1), 3).GetText())
257 idList = self.main.server.server.groups 261 idList = self.main.server.server.groups
258 groupID = 0 262 for r in self.roomList:
263 if room == self.roomList[r]: groupID = r
264 else: groupID = 0
265 print self.roomList[groupID]
266 #groupID = self.roomList.get("'"+room+"'"); print groupID
259 if menuItem == MENU_PLAYER_BOOT: 267 if menuItem == MENU_PLAYER_BOOT:
260 print "booting player: ", playerID 268 print "booting player: ", playerID
261 self.main.server.server.del_player( playerID, groupID ) 269 self.main.server.server.del_player( playerID, groupID )
262 self.main.server.server.check_group( playerID, groupID ) 270 self.main.server.server.check_group( playerID, groupID )
263 self.remove( playerID ) 271 self.remove( playerID )
264 elif menuItem == MENU_PLAYER_SEND_MESSAGE: 272 elif menuItem == MENU_PLAYER_SEND_MESSAGE:
265 print "send a message..." 273 print "send a message..."
266 msg = self.GetMessageInput( "Send a message to player" ) 274 msg = self.GetMessageInput( "Send a message to player" )
267 if len(msg): self.main.server.server.send( msg, playerID, str(groupID) ) 275 if len(msg): self.main.server.server.send( msg, playerID, str(groupID) )
268 #Leave this in for now. 276 #Leave this in for now.
269 #elif menuItem == MENU_PLAYER_SEND_TO_ROOM: 277 elif menuItem == MENU_PLAYER_SEND_ROOM_MESSAGE:
270 # print "Send message to room..." 278 print "Send message to room..."
271 # msg = self.GetMessageInput( "Send message to room of this player") 279 msg = self.GetMessageInput( "Send message to room of this player")
272 # if len(msg): 280 if len(msg):
273 # self.main.server.server.send_to_group( 0, GroupID, msg ) 281 self.main.server.server.send_to_group("0", str(groupID), msg )
274 282
275 elif menuItem == MENU_PLAYER_SEND_SERVER_MESSAGE: 283 elif menuItem == MENU_PLAYER_SEND_SERVER_MESSAGE:
276 print "broadcast a message..." 284 print "broadcast a message..."
277 msg = self.GetMessageInput( "Broadcast Server Message" ) 285 msg = self.GetMessageInput( "Broadcast Server Message" )
278 # If we got a message back, send it 286 # If we got a message back, send it
308 self.build_status() 316 self.build_status()
309 317
310 # Server Callbacks 318 # Server Callbacks
311 cb = {} 319 cb = {}
312 cb["log"] = self.Log 320 cb["log"] = self.Log
313 cb["connect"] = self.OnConnect 321 cb["connect"] = self.OnConnect ##Fixed!!
314 cb["disconnect"] = self.OnDisconnect 322 cb["disconnect"] = self.OnDisconnect
315 cb["update"] = self.OnUpdatePlayer 323 cb["update"] = self.OnUpdatePlayer
316 cb["data_recv"] = self.OnDataRecv 324 cb["data_recv"] = self.OnDataRecv
317 cb["data_sent"] = self.OnDataSent 325 cb["data_sent"] = self.OnDataSent
318 cb["create_group"] = self.OnCreateGroup 326 cb["create_group"] = self.OnCreateGroup
430 438
431 ### Server Callbacks ##################################### 439 ### Server Callbacks #####################################
432 def Log(self, log): 440 def Log(self, log):
433 wx.LogMessage(str(log)) 441 wx.LogMessage(str(log))
434 442
435 def OnConnect(player, self, data): 443 def OnConnect(self, player):
436 self.conns.add(player) 444 self.conns.add(player)
437 445
438 def OnDisconnect(self, id): 446 def OnDisconnect(self, id):
439 self.conns.remove(id) 447 self.conns.remove(id)
440 448
450 self.total_messages_received += 1 458 self.total_messages_received += 1
451 self.total_data_received += bytes 459 self.total_data_received += bytes
452 self.sb.SetStatusText("Recv: %s (%d)" % (format_bytes(self.total_data_received), self.total_messages_received), 2) 460 self.sb.SetStatusText("Recv: %s (%d)" % (format_bytes(self.total_data_received), self.total_messages_received), 2)
453 461
454 def OnCreateGroup( self, data ): 462 def OnCreateGroup( self, data ):
455 print "room list: ", self.conns.roomList 463 room_id = data[1]
456 self.conns.roomList[id] = name 464 name = data[0]
457 (id, name) = data 465 self.conns.roomList[room_id] = name
458 print "room list: ", self.conns.roomList 466 (room, room_id, player) = data
467 self.conns.updateRoom(data)
459 468
460 def OnDeleteGroup( self, data ): 469 def OnDeleteGroup( self, data ):
461 (from_id, id) = data 470 (room_id, player) = data
462 #del self.conns.roomList[id] 471 del self.conns.roomList[room_id]
463 print "OnDeleteGroup room list: ", self.conns.roomList, id
464 472
465 def OnJoinGroup( self, data ): 473 def OnJoinGroup( self, data ):
466 self.conns.updateRoom( data ) 474 self.conns.updateRoom( data )
467 475
468 def OnSetRole( self, data ): 476 def OnSetRole( self, data ):
471 479
472 ### Misc. ################################################ 480 ### Misc. ################################################
473 def OnStart(self, event = None): 481 def OnStart(self, event = None):
474 """ Start server. """ 482 """ Start server. """
475 if self.STATUS == SERVER_STOPPED: 483 if self.STATUS == SERVER_STOPPED:
476 serverNameEntry = wx.TextEntryDialog( self, "Please Enter The Server Name You Wish To Use:", 484 # see if we already have name specified
485 try:
486 userPath = orpg.dirpath.dir_struct["user"]
487 validate = orpg.tools.validate.Validate(userPath)
488 validate.config_file( "server_ini.xml", "default_server_ini.xml" )
489 configDom = minidom.parse(userPath + 'server_ini.xml')
490 configDom.normalize()
491 configDoc = configDom.documentElement
492 if configDoc.hasAttribute("name"): self.serverName = configDoc.getAttribute("name")
493 except: pass
494 if self.serverName == '':
495 serverNameEntry = wx.TextEntryDialog( self, "Please Enter The Server Name You Wish To Use:",
477 "Server's Name", self.serverName, wx.OK|wx.CANCEL|wx.CENTRE ) 496 "Server's Name", self.serverName, wx.OK|wx.CANCEL|wx.CENTRE )
478 if serverNameEntry.ShowModal() == wx.ID_OK: self.serverName = serverNameEntry.GetValue() 497 if serverNameEntry.ShowModal() == wx.ID_OK: self.serverName = serverNameEntry.GetValue()
479 serverPasswordEntry = wx.TextEntryDialog(self, "Please Enter The Server Admin Password:", 498 # see if we already have password specified
480 "Server's Password", self.bootPwd, wx.OK|wx.CANCEL|wx.CENTRE) 499 try:
481 if serverPasswordEntry.ShowModal() == wx.ID_OK: self.bootPwd = serverPasswordEntry.GetValue() 500 userPath = orpg.dirpath.dir_struct["user"]
501 validate = orpg.tools.validate.Validate(userPath)
502 validate.config_file( "server_ini.xml", "default_server_ini.xml" )
503 configDom = minidom.parse(userPath + 'server_ini.xml')
504 configDom.normalize()
505 configDoc = configDom.documentElement
506 if configDoc.hasAttribute("admin"): self.bootPwd = configDoc.getAttribute("admin")
507 elif configDoc.hasAttribute("boot"): self.bootPwd = configDoc.getAttribute("boot")
508 except: pass
509 if self.bootPwd == '':
510 serverPasswordEntry = wx.TextEntryDialog(self, "Please Enter The Server Admin Password:", "Server's Password", self.bootPwd, wx.OK|wx.CANCEL|wx.CENTRE)
511 if serverPasswordEntry.ShowModal() == wx.ID_OK: self.bootPwd = serverPasswordEntry.GetValue()
512
482 if len(self.serverName): 513 if len(self.serverName):
483 wx.BeginBusyCursor() 514 wx.BeginBusyCursor()
484 self.server = ServerMonitor(self.callbacks, self.conf, self.serverName, self.bootPwd) 515 self.server = ServerMonitor(self.callbacks, self.conf, self.serverName, self.bootPwd)
485 self.server.start() 516 self.server.start()
486 self.STATUS = SERVER_RUNNING 517 self.STATUS = SERVER_RUNNING