-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
236 lines (197 loc) · 7.23 KB
/
game.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import random
import story
from geopy import distance
import mysql.connector
conn = mysql.connector.connect(
host='localhost',
port=3306,
database='some_database_name',
user='some_user_name',
password='some_password',
autocommit=True
)
# FUNCTIONS
# select 30 airports for the game
def get_airports():
sql = """SELECT iso_country, ident, name, type, latitude_deg, longitude_deg
FROM airport
WHERE continent = 'EU'
AND type='large_airport'
ORDER by RAND()
LIMIT 30;"""
cursor = conn.cursor(dictionary=True)
cursor.execute(sql)
result = cursor.fetchall()
return result
# get all goals
def get_goals():
sql = "SELECT * FROM goal;"
cursor = conn.cursor(dictionary=True)
cursor.execute(sql)
result = cursor.fetchall()
return result
# create new game
def create_game(start_money, p_range, cur_airport, p_name, a_ports):
sql = "INSERT INTO game (money, player_range, location, screen_name) VALUES (%s, %s, %s, %s);"
cursor = conn.cursor(dictionary=True)
cursor.execute(sql, (start_money, p_range, cur_airport, p_name))
g_id = cursor.lastrowid
# add goals / loot boxes
goals = get_goals()
goal_list = []
for goal in goals:
for i in range(0, goal['probability'], 1):
goal_list.append(goal['id'])
# exclude starting airport
g_ports = a_ports[1:].copy()
random.shuffle(g_ports)
for i, goal_id in enumerate(goal_list):
sql = "INSERT INTO ports (game, airport, goal) VALUES (%s, %s, %s);"
cursor = conn.cursor(dictionary=True)
cursor.execute(sql, (g_id, g_ports[i]['ident'], goal_id))
return g_id
# get airport info
def get_airport_info(icao):
sql = f'''SELECT iso_country, ident, name, latitude_deg, longitude_deg
FROM airport
WHERE ident = %s'''
cursor = conn.cursor(dictionary=True)
cursor.execute(sql, (icao,))
result = cursor.fetchone()
return result
# check if airport has a goal
def check_goal(g_id, cur_airport):
sql = f'''SELECT ports.id, goal, goal.id as goal_id, name, money
FROM ports
JOIN goal ON goal.id = ports.goal
WHERE game = %s
AND airport = %s'''
cursor = conn.cursor(dictionary=True)
cursor.execute(sql, (g_id, cur_airport))
result = cursor.fetchone()
if result is None:
return False
return result
# calculate distance between two airports
def calculate_distance(current, target):
start = get_airport_info(current)
end = get_airport_info(target)
return distance.distance((start['latitude_deg'], start['longitude_deg']),
(end['latitude_deg'], end['longitude_deg'])).km
# get airports in range
def airports_in_range(icao, a_ports, p_range):
in_range = []
for a_port in a_ports:
dist = calculate_distance(icao, a_port['ident'])
if dist <= p_range and not dist == 0:
in_range.append(a_port)
return in_range
# set loot box opened
# update location
def update_location(icao, p_range, u_money, g_id):
sql = f'''UPDATE game SET location = %s, player_range = %s, money = %s WHERE id = %s'''
cursor = conn.cursor(dictionary=True)
cursor.execute(sql, (icao, p_range, u_money, g_id))
# game starts
# ask to show the story
storyDialog = input('Do you want to read the background story? (Y/N): ')
if storyDialog == 'Y':
# print wrapped string line by line
for line in story.getStory():
print(line)
# GAME SETTINGS
print('When you are ready to start, ')
player = input('type player name: ')
# boolean for game over and win
game_over = False
win = False
# start money = 1000
money = 1000
# start range in km = 2000
player_range = 2000
# score = 0
score = 0
# boolean for diamond found
diamond_found = False
# all airports
all_airports = get_airports()
# start_airport ident
start_airport = all_airports[0]['ident']
# current airport
current_airport = start_airport
# game id
game_id = create_game(money, player_range, start_airport, player, all_airports)
# GAME LOOP
while not game_over:
# get current airport info
airport = get_airport_info(current_airport)
# show game status
print(f'''You are at {airport['name']}.''')
print(f'''You have {money:.0f}$ and {player_range:.0f}km of range.''')
# pause
input('\033[32mPress Enter to continue...\033[0m')
# if airport has goal ask if player wants to open it
# check goal type and add/subtract money accordingly
goal = check_goal(game_id, current_airport)
if goal:
question = input(
f'''Do you want to open lootbox for {"100$ or " if money > 100 else ""}{"50km range" if player_range > 50 else ""}? M = money, R = range, enter to skip: ''')
if not question == '':
if question == 'M':
money -= 100
elif question == 'R':
player_range -= 50
if goal['money'] > 0:
money += goal['money']
print(f'''Congratulations! You found {goal['name']}. That is worth {goal['money']}$.''')
print(f'''You have now {money:.0f}$''')
elif goal['money'] == 0:
win = True
print(f'''Congratulations! You found the diamond. Now go to start.''')
else:
money = 0
print(f'''Oh no! You have been robbed. You lost all your money''')
# pause
input("\033[32mPress Enter to continue...\033[0m")
# ask to buy fuel/range
if money > 0:
question2 = input('Do you want to by fuel? 1$ = 2km of range. Enter amount or press enter. ')
if not question2 == '':
question2 = float(question2)
if question2 > money:
print(f'''You don't have enough money.''')
else:
player_range += question2 * 2
money -= question2
print(f'''You have now {money:.0f}$ and {player_range:.0f}km of range''')
# pause
input("\033[32mPress Enter to continue...\033[0m")
# if no range, game over
# show airports in range. if none, game over
airports = airports_in_range(current_airport, all_airports, player_range)
print(f'''\033[34mThere are {len(airports)} airports in range: \033[0m''')
if len(airports) == 0:
print('You are out of range.')
game_over = True
else:
print(f'''Airports: ''')
for airport in airports:
ap_distance = calculate_distance(current_airport, airport['ident'])
print(f'''{airport['name']}, icao: {airport['ident']}, distance: {ap_distance:.0f}km''')
# ask for destination
dest = input('Enter destination icao: ')
selected_distance = calculate_distance(current_airport, dest)
player_range -= selected_distance
update_location(dest, player_range, money, game_id)
current_airport = dest
if player_range < 0:
game_over = True
# if diamond is found and player is at start, game is won
if win and current_airport == start_airport:
print(f'''You won! You have {money}$ and {player_range}km of range left.''')
game_over = True
# if game is over loop stops
# show game result
print(f'''{'You won!' if win else 'You lost!'}''')
print(f'''You have {money:.0f}$''')
print(f'''Your range is {player_range:.0f}km''')