Stub code – v0.0

# --------------------------------------------
# def stub( t, w, b, r="?", result="*" ):
# --------------------------------------------

def stub( t, w, b, rnd="?", result="*", comment="" ):
   '''
   stub( tid, w_pid, b_pid, rnd="?", result="*", comment="" ) -- generate stub game

        Note: w, b are input as pid's, maybe someday we can allow names too.
   '''
   tag = { "Event"     : "?", 
           "Site"      : "?", 
           "Date"      : "?", 
           "Round"     : "?",
           "White"     : "?",
           "Black"     : "?",
           "EventDate" : "?",
           "Result"    : "*",
           "PlyCount"  : "0",
           "CG_id"     : "0.0.0.0" }
   
   G = TG[t]    # Global data - TG[t] is list of games in tournaments.
    
   # Normalize the stub:
   
   for k in [ "Event", "Site", "EventDate" ]:
      tag[ k ] = G[0].__dict__[ k ]
      
   # Some input tags get special handling for convenience.
   
   if type( rnd    ) != types.StringType: rnd    = str( rnd    )
   if type( result ) != types.StringType: result = str( result )
   
   rnd    = rnd.strip()
   result = result.strip()
   
   if result in ( "?" ) : result = "*"
   if result in ( "+", "1.0", "1" ) : result = "1-0"
   if result in ( "-", "0.0", "0" ) : result = "0-1"
   if result in ( "=", "0.5",     ) : result = "1/2-1/2"
   
   # Set some other tags.
   tag[ "CG_id"  ] = "%d.0.%d.%d" % ( t, w, b )
   tag[ "Result" ] = result
   tag[ "Round"  ] = rnd
   
   #
   # Let's get the player's names, and hopefully the round date.
   
   D = {}    # Look up name by id.
   R = {}    # Look up round dates.
   
   for g in G: 
      if g.White not in D: D[ g.id[2] ] = g.White
      if g.Black not in D: D[ g.id[3] ] = g.Black
      
      if g.Round not in R: R[ g.Round ] = {}
      
      if g.Date not in R[ g.Round ]: R[ g.Round ][ g.Date ] = 0
      
      R[ g.Round ][ g.Date ] += 1
      
   tag[ "White" ] = D[ w ]
   tag[ "Black" ] = D[ b ]
      
   if rnd in R:
      tag[ "Date" ] = [ 
         k for k in R[ rnd ] if 
              R[ rnd ][k] == max( [ R[rnd][j] for j in R[rnd] ] ) ][0]
   
   p = PGN.PGN( tag, "" )
   
   p.id = ( t, 0, w, b )
   
   if comment:
      p.pgn = "{ %%stub - %s (R%s) %s:  %s }\n\n" \
              % ( p.CG_id, p.Round, p.Result, comment )
   else:
      p.pgn = "{ %%stub - %s (R%s) %s }\n\n" \
              % ( p.CG_id, p.Round, p.Result }
   
   return p
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s