-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_client.py
More file actions
177 lines (142 loc) · 5.57 KB
/
game_client.py
File metadata and controls
177 lines (142 loc) · 5.57 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from copy import deepcopy
from Resources.classes import Player, Phase, Host
import globals
class Game_client:
"""
The game class for the client (player) and host. Keeps track of key game aspects
"""
def __init__(self, role):
self.role = role
self.year = 1
self.years = globals.default_years
self.bidRounds = globals.default_rounds
self.storePlants = []
self.phase = Phase()
self.ip = ""
self.phase = "Strategy phase"
self.bidRound = 1
self.strategyTime = "--:--"
self.bidTime = "--:--"
self.endTime = ""
self.weather_effect = dict(PV=1, wind=1) # no weather effect initially
self.initialMoney = globals.default_money*1000000
self.co2_tax = 0
self.gas_cost_fixed = 0
self.gas_cost_variable = 0
self.coal_cost_fixed = 0
self.coal_cost_variable = 0
self.expected_demand_fixed = 0
self.expected_demand_variable = 0
self.simple_players = [] # list of Simple_Players objects used to show the players who else is in the game.
self.host_status = "" # Used for the countdown. Sttus from the host is stored here, so that the player can act on these messages from it's own thread
if role == "player":
self.player = Player()
elif role == "host":
self.host = Host()
else:
print("Error creating game object. Role is not recognized")
def getBidRounds(self):
return self.bidRounds
#def setInitialMoney(self, money):
# self.initialMoney = money
def setYear(self, year):
self.year = year
def setYears(self, years):
self.years = years
def setBidRounds(self, bidRounds):
self.bidRounds = bidRounds
def setStartPlant(self, startPlant):
"""
Input is string, not a plant object
"""
self.startPlant = startPlant
def newYear(self):
self.year = self.year +1
def getStorePlants(self):
return self.storePlants
def getStorePlant(self, identifier):
for plant in self.storePlants:
if plant.identifier == identifier:
return plant
def clearStorePlants(self):
self.storePlants.clear()
def getYear(self):
return self.year
def getYears(self):
return self.years
def getPlantPrice(self, n):
return self.storePlants[n].getInvestmentCost()
def removePlant(self, n):
self.storePlants.pop(n)
def addPlant(self, plant):
newPlant = deepcopy(plant)
self.storePlants.append(newPlant)
def addStorePlant(self, plant):
self.storePlants.append(plant)
def getIp(self):
return self.ip
def setIp(self, ip):
self.ip = ip
def transition(self):
"""
When called it transitions into the correct phase ie. new bidding phase, new strategy phase, endGame
phases: "strategy phase", "Bidding phase", "End game"
"""
try:
# Strategy phase always initiates a bidding phase
if self.phase == "Strategy phase":
self.phase = "Bidding phase"
self.bidRound = 1
# A bidding phase can initiate strategy phase, bid phase and endGame
elif self.phase == "Bidding phase":
self.bidRound += 1
if self.bidRound > self.bidRounds:
self.year += 1
if self.year > self.years:
self.phase = "End game"
else: # still more years left in game transition to strategy phase
self.phase = "Strategy phase"
if self.role == "host":
self.host.setAllPlayersNotReady()
elif self.role == "player":
self.player.status == "Not ready"
except Exception as e:
print(e)
def getPhase(self):
return self.phase
def incrementBidRound(self):
self.bidRound = self.bidRound + 1
def getBidRound(self):
return self.bidRound
def setStrategyTime(self, sec):
self.strategyTime = sec
def getStrategyTime(self):
return self.strategyTime
def setBidTime(self, sec):
self.bidTime = sec
def getBidTime(self):
return self.bidTime
def setEndTime(self, endTime):
self.endTime = endTime
def getEndTime(self):
return self.endTime
def calculate_demand_slope(self):
"""
Calculates the demand slope based on the total installed capacity
"""
# TODO: method needs adjusting. It should also be moved to host??
self.host.demand[1] = self.host.demand[0]/((0.6+0.05*(self.year-1))*self.host.total_capacity)
if globals.DEBUGGING:
print("Debbuging in calculate_demand_slope:")
print(self.host.total_capacity)
print(self.host.demand[0])
print(self.host.demand[1])
print("Debugging completed")
def removeSimplePlayer(self, playerNumber):
for index, player in enumerate(self.simple_players):
if player.playerNumber == playerNumber:
if globals.DEBUGGING:
print("Removing player {}".format(player.firm_name))
self.simple_players.pop(index)
if globals.DEBUGGING:
print("Simple players remaining {}".format(self.simple_players))