#!/usr/bin/python

if False:
  # see: http://pychecker.sourceforge.net/
  import os
  os.environ['PYCHECKER'] = '--stdlib -100'
  import pychecker.checker


import sys

pathDMVC = "/usr/share/dmvc"
pathServer = "/usr/share/genteguada-server"
if not pathDMVC in sys.path:
  sys.path.append(pathDMVC)
if not pathServer in sys.path: 
  sys.path.append(pathServer)

import GG.model.ggsystem
import dMVC
import dMVC.remoteserver
from optparse import OptionParser
import traceback

__usage__ = "\n  %s -P puerto"%sys.argv[0]
parser = OptionParser( usage = __usage__ )
parser.add_option("-P",dest="port", help="puerto",default=8000, type = int)
(params, args) = parser.parse_args()

"""
def excepthook(type, value, tb):
  traceback.print_exception(type, value, tb)
"""

if __name__ == "__main__":
  def onLogin(rhandler, result, error):
    if result[0]:
      if result[0] == "Condiciones":
        rhandler.ggSession = None
      else:
        rhandler.ggSession = result[1]
    else:
      rhandler.ggSession = None
  
  def onDisconnection(rhandler):
    if hasattr(rhandler, 'ggSession'):
      if rhandler.ggSession:
        rhandler.ggSession.logout()
        server = dMVC.getRServer().getSocket()
        server.finish_request(rhandler.request, rhandler.client_address)

  VERSION = "0.17.1-1"
  #sys.excepthook = excepthook
  onExecution = [ [GG.model.ggsystem.GGSystem, 'login', onLogin] ]
  print "Iniciado el servidor del juego Gente Guada V "+VERSION
  dMVC.remoteserver.RServer(GG.model.ggsystem.GGSystem(), version = VERSION ,port = params.port, onDisconnection = onDisconnection, onExecution = onExecution)
