コード例 #1
0
 def handler_message(self, msg, FileNo):
     "Stub for derived classes"
     if "DUMP" in msg:
         self.Dump(msg)
         return
     if msg == "?STATE":
         self.SendState(FileNo)
         return
     if msg == "?PLAYERS":
         response = [str(x is not None) for x in self.PlayerList]
         response = "PLAYERS," + ",".join(response)
         self.sendmessage(FileNo, response)
         return
     if msg == "Join-Observer":
         self.ObserverList.append(FileNo)
         print self.ObserverList
         return
     if "JOIN" in msg:
         self.AddPlayer(msg, FileNo)
         return
     if msg.startswith("OBSERVE_AS"):
         ID = int(msg[11:]) % self.NumPlayers
         self.ObserveAs[FileNo] = ID
         return
     if self.State == 'Started':
         if msg == "FINISHED":
             try:
                 p_id = self.PlayerLookup[FileNo]
             except:
                 return
             if self.Game.PlayerTurnFinished(p_id):
                 print "Turn finished; broadcast!"
             return
         if msg == "?PLANETS":
             if FileNo in self.ObserveAs:
                 ID = self.ObserveAs[FileNo]
             else:
                 try:
                     ID = self.PlayerLookup[FileNo]
                 except:
                     return
             response = self.Game.GetPlanets(ID)
             self.sendmessage(FileNo, response)
             return
         if msg == "?FLEETS":
             #print "Received ?FLEETS"
             if FileNo in self.ObserveAs:
                 ID = self.ObserveAs[FileNo]
             else:
                 try:
                     ID = self.PlayerLookup[FileNo]
                 except:
                     return
             response = self.Game.GetFleets(ID)
             self.sendmessage(FileNo, response)
             return
         if msg.startswith('MOVE'):
             try:
                 ID = self.PlayerLookup[FileNo]
             except:
                 return
             msg = msg.split(";")
             try:
                 fleetID = int(msg[1])
                 planetID = int(msg[2])
             except:
                 self.sendmessage(FileNo, "ERROR;Move Syntax")
                 return
             status = self.Game.OrderMoveFleet(ID, fleetID, planetID)
             if status is not None:
                 self.sendmessage(FileNo, "ERROR;" + status)
             return
         if msg.startswith('SPLIT'):
             try:
                 ID = self.PlayerLookup[FileNo]
             except KeyError:
                 return
             msg = msg.split(";")
             try:
                 fleetID = int(msg[1])
                 planetID = int(msg[2])
                 ships = int(msg[3])
             except Exception:
                 self.sendmessage(FileNo, "ERROR;Split Syntax")
                 return
             status = self.Game.OrderSplitFleet(ID, fleetID, planetID,
                                                ships)
             if status is not None:
                 self.sendmessage(FileNo, "ERROR;" + status)
             return
         if msg.startswith("SETNAME"):
             try:
                 ID = self.PlayerLookup[FileNo]
             except KeyError:
                 return
             try:
                 info = utils.ParseCommString(msg)
             except ValueError:
                 self.sendmessage(FileNo, "Error: SETNAME Syntax: " + msg)
             try:
                 info = info[0]
                 self.Game.PlayerList[ID].Name = info["Name"]
                 self.Game.PlayerList[ID].Race = info["Race"]
             except KeyError:
                 self.sendmessage(FileNo,
                                  "Error: Missing SETNAME info: " + msg)
             return
     print "Unhandled message:", msg, FileNo
コード例 #2
0
 def test_ParseCommString_Invalid(self):
     self.assertEqual(
         utils.ParseCommString("PLANET|x=2.;ID=3;name=Snert|x=1;Skip!"),
         [dict(x='2.', ID='3', name='Snert'),
          dict(x='1')])
コード例 #3
0
 def test_ParseCommString_singleline(self):
     self.assertEqual(utils.ParseCommString("x=2.;ID=4;name=Snert"),
                      [dict(x='2.', ID='4', name='Snert')])
コード例 #4
0
 def test_ParseCommString_singlelineWithLeader(self):
     self.assertEqual(utils.ParseCommString("PLANET|x=2.;ID=3;name=Snert"),
                      [dict(x='2.', ID='3', name='Snert')])