← Back to team overview

brigas-team team mailing list archive

[Branch ~brigas-team/brigas/trunk] Rev 9: game-server/src/services/game_manager.py:

 

------------------------------------------------------------
revno: 9
committer: Diogo Dutra <dda@optimus>
branch nick: trunk
timestamp: Fri 2009-06-05 16:12:53 -0300
message:
  game-server/src/services/game_manager.py:
  Terminados os métodos:
  soap_GetUUID
  soap_GetAvailableGames
  soap_GetAvailableRooms
  soap_CreateRoom
  soap_EnterRoom
  Para se ter um GameManager funcional falta somente avisar aos control points que a partida 
  começou.
  
  game-server/src/games/base_game.py:
  Criei a classe base onde todos os jogos devem herdar.
  Também criei a classe BaseRoom de onde todas as rooms de todos os jogos dever herdar.
  
  game-server/src/games/truco/implementation.py:
  Comecei a implementação do futuro serviço de truco, ainda é fake, mas futuramente só vai 
  extendê-lo.
  Tem as classes TrucoRoom e Truco.
  
  game-server/src/games/truco/truco-scpd.xml:
  Arquivo contendo a especificação do serviço de truco, é fake, não tem nada verdadeiro dentro 
  dele.
  
  game-server/src/game_server_impl.py:
  Agora a instância do jogo é criada aqui, e não no __init__.py do jogo, melhor encapsulado.
added:
  game-server/src/games/base_game.py
  game-server/src/games/truco/implementation.py
  game-server/src/games/truco/truco-scpd.xml
modified:
  game-server/bin/simple_control_point
  game-server/src/game_server_impl.py
  game-server/src/games/__init__.py
  game-server/src/games/truco/__init__.py
  game-server/src/services/game_manager.py
  game-server/src/services/xmls/game-manager-scpd.xml

=== modified file 'game-server/bin/simple_control_point'
--- game-server/bin/simple_control_point	2009-06-04 22:44:22 +0000
+++ game-server/bin/simple_control_point	2009-06-05 19:12:53 +0000
@@ -111,12 +111,13 @@
         elif input == 'createroom':
             device = devices[0]
             k, service = device.services.items()[1]
-            print service.CreateRoom(Game='Truco', UUID=uuid)
+            print service.CreateRoom(Game='Truco', UUID=uuid, NumPlayers=2)
 
-        elif input == 'enterroom':
+        elif input.startswith('enterroom'):
             device = devices[0]
             k, service = device.services.items()[1]
-            print service.EnterRoom(Game='Truco', RoomID=0, UUID=uuid)
+            args = input.split(' ')
+            print service.EnterRoom(Game='Truco', RoomID=args[1], UUID=uuid)
 
         elif input == 'help':
             print 'Commands available: list, exit, ' \

=== modified file 'game-server/src/game_server_impl.py'
--- game-server/src/game_server_impl.py	2009-06-04 22:44:22 +0000
+++ game-server/src/game_server_impl.py	2009-06-05 19:12:53 +0000
@@ -13,10 +13,6 @@
 import brisa_game_server
 from brisa_game_server.services import GameManager
 
-class Game(object):
-  def __init__(self, serv):
-    self.service = serv
-    self.rooms = {}
 
 class GameServerDevice(object):
 
@@ -51,9 +47,9 @@
       try:
         module_path = "%s.%s" % (self.games_module_path, dir)
         __import__(module_path)
-        service = eval("%s.service" % module_path)
-        self._games[service.id] = Game(service)
-        self.device.add_service(service)
+        game = eval("%s.%s()" % (module_path, eval(module_path).service_name))
+        self._games[game.id] = game
+        self.device.add_service(game)
       except Exception, e:
         msg = 'Error while importing %s game. The module '\
               'path used to load was: %s. Error: %s' % \

=== modified file 'game-server/src/games/__init__.py'
--- game-server/src/games/__init__.py	2009-06-03 19:57:05 +0000
+++ game-server/src/games/__init__.py	2009-06-05 19:12:53 +0000
@@ -0,0 +1,3 @@
+
+from brisa_game_server.games.base_game import BaseGame
+from brisa_game_server.games.base_game import BaseRoom

=== added file 'game-server/src/games/base_game.py'
--- game-server/src/games/base_game.py	1970-01-01 00:00:00 +0000
+++ game-server/src/games/base_game.py	2009-06-05 19:12:53 +0000
@@ -0,0 +1,25 @@
+
+class BaseRoom(object):
+  def __init__(self, game, id, num_players):
+    self.id = id
+    self.num_players = num_players
+    self.players = []
+    self._game = game
+    self.match_started = False
+
+  def add_player(self, player_uuid):
+    self.players.append(player_uuid)
+    if len(self.players) == self.num_players:
+      self.match_started = True
+      self._game.start_new_match(self.id)
+
+class BaseGame(object):
+  def __init__(self):
+    self.rooms = {}
+    self._room_counter = 0
+
+  def start_new_match(self, room):
+    pass
+
+  def add_room(self):
+    pass

=== modified file 'game-server/src/games/truco/__init__.py'
--- game-server/src/games/truco/__init__.py	2009-06-04 00:23:02 +0000
+++ game-server/src/games/truco/__init__.py	2009-06-05 19:12:53 +0000
@@ -1,13 +1,4 @@
 
-import os
-
-from brisa.upnp.device.service import Service
-
-from brisa_game_server.services.xmls import xml_path
-
-service_name = 'Truco'
-service_type = 'urn:schemas-upnp-org:service:Truco:1'
-scpd_xml_path = os.path.join(xml_path, 'game-manager-scpd.xml')
-
-
-service = Service(service_name, service_type, '', scpd_xml_path)
+from brisa_game_server.games.truco.implementation import Truco
+from brisa_game_server.games.truco.implementation import service_name
+

=== added file 'game-server/src/games/truco/implementation.py'
--- game-server/src/games/truco/implementation.py	1970-01-01 00:00:00 +0000
+++ game-server/src/games/truco/implementation.py	2009-06-05 19:12:53 +0000
@@ -0,0 +1,30 @@
+
+
+import os
+from uuid import uuid4
+    
+from brisa.upnp.device.service import Service
+
+from brisa_game_server.games import BaseGame
+from brisa_game_server.games import BaseRoom
+    
+service_name = 'Truco'
+service_type = 'urn:schemas-upnp-org:service:Truco:1'
+scpd_xml_path = os.path.join(os.path.dirname(__file__), 'truco-scpd.xml')
+        
+class TrucoRoom(BaseRoom):
+  def __init__(self, truco, id, num_players=4):
+    BaseRoom.__init__(self, truco, id, num_players)
+
+class Truco(BaseGame, Service):
+  def __init__(self):
+    Service.__init__(self, service_name, service_type, '', scpd_xml_path)
+    BaseGame.__init__(self)
+
+  def add_room(self, uuid, num_players=4):
+    self.rooms[self._room_counter] = \
+      TrucoRoom(self, self._room_counter, num_players)
+    self.rooms[self._room_counter].add_player(uuid)
+    self._room_counter += 1
+    return self._room_counter - 1
+

=== added file 'game-server/src/games/truco/truco-scpd.xml'
--- game-server/src/games/truco/truco-scpd.xml	1970-01-01 00:00:00 +0000
+++ game-server/src/games/truco/truco-scpd.xml	2009-06-05 19:12:53 +0000
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<scpd xmlns="urn:schemas-upnp-org:service-1-0">
+    <specVersion>
+        <major>1</major>
+        <minor>0</minor>
+    </specVersion>
+    <actionList>
+        <action>
+            <name>GetUUID</name>
+            <argumentList>
+                <argument>
+                    <name>UUID</name>
+                    <direction>out</direction>
+                    <relatedStateVariable>A_ARG_TYPE_UUID</relatedStateVariable>
+                </argument>
+            </argumentList>
+        </action>
+    </actionList>
+    <serviceStateTable>
+        <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_UUID</name>
+            <dataType>uuid</dataType>
+        </stateVariable>
+    </serviceStateTable>
+</scpd>

=== modified file 'game-server/src/services/game_manager.py'
--- game-server/src/services/game_manager.py	2009-06-04 22:44:22 +0000
+++ game-server/src/services/game_manager.py	2009-06-05 19:12:53 +0000
@@ -22,7 +22,7 @@
     if uuid in self._players:
       for game in self._games.values():
         for room in game.rooms.values():
-          if uuid in room:
+          if uuid in room.players:
             return False
       return True
     else:
@@ -44,21 +44,27 @@
   def soap_GetAvailableRooms(self, *args, **kwargs):
     try:
       game = self._games[kwargs['Game']]
-      roomsID = ':'.join([str(room) for room in game.rooms.keys()])
-      roomsLen = ':'.join([str(len(room_len)) for room_len in game.rooms.values()])
-      return {'RoomsID' : roomsID, 'RoomsLen' : roomsLen}
-    except:
-      return {'RoomsID' : '-1', 'RoomsLen' : '-1'}
+      rooms = []
+      for room in game.rooms.values():
+        if not room.match_started:
+          rooms.append(room)
+      rooms_id = ':'.join([str(room.id) for room in rooms])
+      num_players = ':'.join([str(len(room.players)) for room in rooms])
+      return {'RoomsID' : rooms_id, 'NumPlayers' : num_players}
+    except Exception, e:
+      print e.message
+      return {'RoomsID' : '-1', 'NumPlayers' : '-1'}
 
   def soap_CreateRoom(self, *args, **kwargs):
     uuid = kwargs['UUID']
     if self._check_uuid(uuid):
       try:
         game = self._games[kwargs['Game']]
-        game.rooms[self._rooms_counter] = [uuid]
-        self._rooms_counter += 1
-        return {'RoomID' : (self._rooms_counter - 1)}
-      except:
+        num_players = int(kwargs['NumPlayers'])
+        room_id = game.add_room(uuid, num_players)
+        return {'RoomID' : room_id}
+      except Exception, e:
+        print e.message
         return {'RoomID' : -2}
     else:
       return {'RoomID' : -3}
@@ -70,16 +76,14 @@
       uuid = kwargs['UUID']
       if self._check_uuid(uuid):
         try:
-          if len(game.rooms[rid]) == 3:
-            full = True
-          game.rooms[rid].append(uuid)
-          if full:
-            games.rooms.pop(rid)
+          game.rooms[rid].add_player(uuid)
           return {'Return' : 1}
-        except:
-           return {'Return' : 0}
+        except Exception, e:
+          print e.message
+          return {'Return' : 0}
       else:
         return {'Return' : -1}
-    except:
+    except Exception, e:
+      print e.message
       return {'Return' : -2}
 

=== modified file 'game-server/src/services/xmls/game-manager-scpd.xml'
--- game-server/src/services/xmls/game-manager-scpd.xml	2009-06-04 22:44:22 +0000
+++ game-server/src/services/xmls/game-manager-scpd.xml	2009-06-05 19:12:53 +0000
@@ -39,9 +39,9 @@
                     <relatedStateVariable>A_ARG_TYPE_RoomsID</relatedStateVariable>
                 </argument>
                 <argument>
-                    <name>RoomsLen</name>
+                    <name>NumPlayers</name>
                     <direction>out</direction>
-                    <relatedStateVariable>A_ARG_TYPE_RoomsLen</relatedStateVariable>
+                    <relatedStateVariable>A_ARG_TYPE_NumPlayers</relatedStateVariable>
                 </argument>
             </argumentList>
         </action>
@@ -59,6 +59,11 @@
                     <relatedStateVariable>A_ARG_TYPE_Game</relatedStateVariable>
                 </argument>
                 <argument>
+                    <name>NumPlayers</name>
+                    <direction>in</direction>
+                    <relatedStateVariable>A_ARG_TYPE_NumPlayers</relatedStateVariable>
+                </argument>
+                <argument>
                     <name>RoomID</name>
                     <direction>out</direction>
                     <relatedStateVariable>A_ARG_TYPE_RoomID</relatedStateVariable>
@@ -105,6 +110,10 @@
             <dataType>string</dataType>
         </stateVariable>
         <stateVariable sendEvents="no">
+            <name>A_ARG_TYPE_NumPlayers</name>
+            <dataType>i1</dataType>
+        </stateVariable>
+        <stateVariable sendEvents="no">
             <name>A_ARG_TYPE_RoomsID</name>
             <dataType>string</dataType>
         </stateVariable>



--
lp:brigas
https://code.launchpad.net/~brigas-team/brigas/trunk

Your team Brisa Game Server Team is subscribed to branch lp:brigas.
To unsubscribe from this branch go to https://code.launchpad.net/~brigas-team/brigas/trunk/+edit-subscription.