def create_random_game(user_ids, game_name): initial_recipes = [ json.dumps(r) for r in [{"action":"Produce","name": "Breed", "input": {"B":1}, "output": {"P": 1}}, {"action":"Produce", "name": "Goldminer", "input": {"P":1, "M":2}, "output": {"G": 1}}, {"action":"Trade", "name": "Trade - w for s", "input": {"P":1, "C":2}, "output": {"T": 1}}, ] ] g = Game(name=game_name) g.save() gid= g.id if user_ids: players = [User.objects.get(pk=user_id) for user_id in user_ids] else: players = User.objects.all() systems = [] print "Game! %s Players! %s" % (str(g), str( players )) num_players = len( players ) for p in players: s = System(controller=p, name="%s" % str(p), game=g) s.set_production( {'B': random.randint(0,10), 'M': random.randint(0,5), 'C': random.randint(0,5), 'P': random.randint(5,10) }) s.set_resources( {'B': random.randint(0,100), 'M': random.randint(0,50), 'C': random.randint(0,10), 'P': random.randint(10,100) }) s.save() systems.append(s) for init_recipe in initial_recipes: j = Job(controller=p, pop=random.randint(1,10), from_node=s, to_node=random.choice(systems),recipe=init_recipe) j.save() for i in range( num_players * 2): from_sys = random.choice(systems) to_sys=random.choice(systems) e = Edge(from_node=from_sys, to_node=to_sys) print "Creating an edge from %s to %s" % (str(from_sys), str(to_sys)) e.save() turn_results = {'nodes':{}, 'edges':{} } turn_state, turn_json = game_state_json( g.id, raw_return=True ) new_turn_content = {'state': turn_state, 'events': turn_results} gg = get_game_graph(g.id) graph_json = build_graph(gg, turn_results) new_turn = Turn(state_json=json.dumps(new_turn_content), graph_json=graph_json, game=g) new_turn.save() return gid
def process_cluster(game): cluster_jobs = [] cluster_prod = {} turn_results = {'nodes':{}, 'edges':{} } # for each system, sample from jobs according to last_turn_job_dist + gold_commands # for s in game.system_set.all(): turn_results['nodes'][str(s)] = [] cluster_jobs += get_system_jobs(game, s) for s in turn_results['nodes'].keys(): turn_results['edges'][s] = {} for t in turn_results['nodes'].keys(): turn_results['edges'][s][t] = [] random.shuffle(cluster_jobs) # current_pop = s.get_resources()['P'] # print "Cluster jobs: %s" % str(cluster_jobs) for j in cluster_jobs: r = j.get_transform() #print "Transform: %s" % str(r) if r['action'] == "Produce": # example P->B food to Bio resource work_sys = j.from_node work_sys.remove_resources( r['input'] ) work_sys.add_resources( r['output'] ) # cluster_prod[work_sys.id] = cluster_prod.get(work_sys.id, {}).update(r['output']) print "Updated %s with %s" % (str(work_sys), str(r['output'])) #do_production(cluster_jobs) turn_results['nodes'][str(work_sys)].append(r) elif r['action']=="Trade": pool = get_eligible_jobs(j, cluster_jobs) if not pool: print "No partner available..." continue partner = random.choice(pool) work_sys.remove_resources( r['input'] ) work_sys.add_resources( r['output'] ) remote_sys.input_resources( r['input'] ) remote_sys.remove_resources( r['output'] ) turn_results['edges'][str(work_sys)][str(remote_sys)].append(r) # if work_sys != remote_sys: print "Trade (%s, %s): %s -> %s" \ % (str(work_sys), str(remote_sys), str( r['input']), str(r['output'])) # do_trade(j, r, work_sys, partner) del(cluster_jobs[ partner ]) elif r['action']=="Research": print "Uh, researching?" pass # now take fresh population, and distribute across the jobs available for next turn # using this turn's pop dist'n as a guide, modified by gold. #for s in game.system_set.all(): #new_pop = s.get_pop() # new_jobs = # for i in range(new_pop): turn_state, turn_json = game_state_json( game.id, raw_return=True ) gg = get_game_graph(game.id) graph_json = build_graph(gg, turn_results) #print "Got graphjson: %s" % graph_json new_turn_content = {'state': turn_state, 'events': turn_results} #pprint.PrettyPrinter().pprint((new_turn_content)) new_turn = Turn(state_json=json.dumps(new_turn_content), graph_json=graph_json, index=game.current_turn + 1, game=game) new_turn.save() game.current_turn += 1 game.save()