Module: prepare_demo_session.py

"""
Module for the demonstration. It prepares the negotiation position for one player
from configuration files found in that player's directory.

Invoke module with one parameter: the letter of the player whose position is being
prepared.

We have arranged this so that Players A and B have made opposite assumptions about price
trends so that we can assure an agreement for demonstration purposes.


"""

import sys

basepath = "/Users/pbaker/Documents/Office Projects/Software Dev/demo1"


def print_asks(f, letter, asks):
    if len(asks) == 0:
        f.write("# There are no ask statements for party%s.\n" % letter)
    else:
        f.write("# Ask statements for party%s.\n\n" % letter)
        for h in asks:
            f.write("ask:\n    have: %s\n    want: %s\n" % (h["swap_stock"], h["for_stock"]))
            f.write("    shares: %d\n" % h["number"] )
            f.write("    cash_per_share: [%10.4f, %10.4f]\n" % (h["cash_per_share"][0],
                                                                h["cash_per_share"][1]))
            f.write("    interval: %d\n" % h["interval"] )
            f.write("    ref_id: %d\n" % h["ref_id"] )
            f.write("end\n\n")

def write_asks_py(f, letter, asks):
    for ask in asks:
        f.write("%s\n" % str( ask))
                    

def build_offerer_position(letter):
    print "build negotiation positions for player%s" % letter

    sys.path[-1] = "%s/player%s" % (basepath, letter)
    import experience
    import current_asks as asks
    import market_estimate as estimate
    import compute_offers as compute
    
    e = estimate.MarketEstimate(1.0)
    
    # write a report on experience
    g = open( "%s/player%s/experience.txt" % (basepath, letter),"w")
    experience.print_experience_factors(g)
    g.close()

    # write any ask statements in a text report
    h = open( "%s/player%s/current_asks.txt" % (basepath, letter),"w")
    print_asks(h, letter, asks.asks )
    h.close()

    for b in broker.players:
        if letter == b:
            continue
        print "Build offer from %s to %s" % (letter, b)
        # open position files
        f = open("%s/player%s/positions/player%s_position.py" % (basepath, letter, b), "w" )
        g = open("%s/player%s/positions/player%s_position.txt" % (basepath, letter, b), "w" )
        write_asks_py(f, letter, asks.asks)
        print_asks(g, letter, asks.asks)
        compute.create_offers(letter, b, e, g, f)

        f.close()
        g.close()
        
                 


if __name__ == '__main__':
    sys.path.append( "%s/broker" % basepath)
    sys.path.append( "%s/playerX" % basepath)
    import broker_model as broker

    letter = sys.argv[1]
    if letter not in broker.players:
        print "The letter specified, %s, is not a valid player" % letter
    else:
        build_offerer_position(letter)