forked from Alan01252/Shoreditch-SampleAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
130 lines (107 loc) · 4.6 KB
/
logic.py
File metadata and controls
130 lines (107 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
###########
### AI Controller with HTTP abstracted away
###
### DB is a wrapper for whatever storage is backing the AI
### Use this for storage across games
###
### game contains a "storage" object which is a dict which will be
### persisted after returning
###
###########
from game import RESOURCES, GENERATOR_COST, GENERATOR_IMPROVEMENT_COST, PR_COST, MAX_RESOURCE_GENERATORS, MAX_IMPROVED_RESOURCE_GENERATORS
def start_game(db, game):
# A new game is starting
print "Starting a game"
def start_turn(db, game, actions):
# Start of a turn
# We have to end the turn with game.end_turn() when we're done
# alhough we only get 15 seconds to act before our turn is ended by force
# actions is a dict of things which have happened since my last turn,
# where the keys are player ids, and the values are lists of actions taken,
# each action is a dict which has an 'action' key (which can be 'purchase-pr', 'trade', etc.)
def trade_for(requirements):
# This just figures out how much I can give away without harming the minimum requirements
# then offers everything extra I have for everything I need.
# It's very dumb, you should replace it
request = {}
offer = {}
for resource in RESOURCES:
if resource in requirements and requirements[resource] > game.resources[resource]:
request[resource] = requirements[resource] - game.resources[resource]
else:
to_offer = game.resources[resource] - requirements.get(resource, 0)
if to_offer > 0:
offer[resource] = to_offer
#Don't offer something for nothing
if(sum(request.values()) > 0):
game.trade(offer, request)
return
#Trades anything we have surplus of requirements
#TODO refactor non dry code.
def trade_surplus(requirements):
request = {}
offer = {}
for resource in RESOURCES:
if resource in requirements:
if game.resources[resource] > requirements[resource]:
#TODO see if we can get one on one trades before giving everything away.
offer[resource] = game.resources[resource] - requirements[resource]
for resource in RESOURCES:
if resource not in offer and resource in requirements: #Make sure we're getting something we're not offering and something we want
request[resource] = 1
game.trade(offer,request)
request = {}
else: #try to offer all our other resources
if game.resources[resource] > 0 :
offer[resource] = game.resources[resource]
for resource in RESOURCES:
if resource not in offer and resource in requirements:
request[resource] = 1
game.trade(offer,request)
request = {}
return
##TODO If close to being able to improve a resource with available assets we should do that first for customers
### First try to trade for resources I need for exactly what we're missing.
if sum(game.generators.values()) < MAX_RESOURCE_GENERATORS:
# Can build generators - try to trade for them
trade_for(GENERATOR_COST)
trade_surplus(GENERATOR_COST)
if sum(game.improved_generators.values()) < MAX_IMPROVED_RESOURCE_GENERATORS:
# Can improve one of our existing ones
trade_for(GENERATOR_IMPROVEMENT_COST)
trade_surplus(GENERATOR_IMPROVEMENT_COST)
while game.can_purchase_generator() and game.turn:
generator_type = game.purchase_generator()
print "Purchased %s" % generator_type
while game.can_upgrade_generator() and game.turn:
generator_type = game.upgrade_generator()
print "Upgraded %s" % generator_type
#Make sure we have enough cash and ideas left for other things.
if game.resources['cash'] > GENERATOR_COST['cash'] and game.resources['idea'] > GENERATOR_COST['idea'] :
while game.can_purchase_pr() and game.turn:
#make sure we're still not going below our limit
if game.resources['cash'] > GENERATOR_COST['cash'] and game.resources['idea'] > GENERATOR_COST['idea'] :
game.purchase_pr()
print "Purchased PR"
else:
return
if game.turn:
game.end_turn()
def time_up(db, game):
# We have ran out of time for this turn, it has been forced to end
pass
def end_game(db, game, error=None):
if error:
print "Something went wrong! %s" % error
else:
print "Game over"
def incoming_trade(db, game, player, offering, requesting):
for resource in RESOURCES:
#If I need it buy it
#TODO more work needed here, want to make sure I'm not giving away something I need to increase productivity.
if (resource in offering and game.resources[resource] == 0) and (sum(offering.values()) == sum(requesting.values())):
return True
# As long as I'm gaining at least one resource more than I'm giving away, I'll accept
if sum(offering.values()) > sum(requesting.values()):
return True
return False