tstone87 commited on
Commit
8a9ab6d
·
verified ·
1 Parent(s): 15faf61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -51
app.py CHANGED
@@ -6,7 +6,6 @@ app = Flask(__name__)
6
  # In-memory storage for each player's game state.
7
  game_states = {}
8
 
9
- # Helper to ensure responses never exceed 400 characters.
10
  def limit_response(text, limit=400):
11
  return text if len(text) <= limit else text[:limit-3] + "..."
12
 
@@ -21,12 +20,14 @@ def init_game(username, loadout):
21
  "kills": 0,
22
  "bonus": bonus,
23
  "round": 0,
24
- "status": "active", # active, gulag, eliminated, won
25
- "revives": 0, # self-revives used
26
  "ammo": 100,
27
  "armor": 100,
28
  "inventory": [],
29
- "equipment": [], # extra items like UAV, Air Strike
 
 
30
  "in_gulag": False
31
  }
32
 
@@ -39,135 +40,163 @@ def start():
39
  user = request.args.get("user")
40
  loadout = request.args.get("loadout", "")
41
  if not user:
42
- return limit_response("User required"), 400
43
  game_states[user] = init_game(user, loadout)
44
- resp = (f"Game started for {user}! Players:150, Bonus:{game_states[user]['bonus']:.1f}. "
45
- "Use !fight to engage.")
46
  return limit_response(resp)
47
 
48
  @app.route("/fight", methods=["GET"])
49
  def fight():
50
  user = request.args.get("user")
51
  if not user or user not in game_states:
52
- return limit_response("Start game first with !start"), 400
53
  state = game_states[user]
54
- if state["status"] not in ["active"]:
55
- return limit_response(f"Game over. Final Kills: {state['kills']}."), 400
 
 
 
 
 
 
 
 
56
  state["round"] += 1
57
  r = state["round"]
58
- final_round = state["players"] <= 3 # Final round condition
59
  event = random.choice(["fight", "loot", "buy", "ambush"])
60
  resp = f"R{r}: "
61
 
62
  if event == "fight":
63
- roll = random.random() * state["bonus"]
64
- # In final round, force kill outcome (no enemy fleeing)
65
  if final_round or roll > 0.7:
66
  kills = random.randint(1, 3)
67
  state["kills"] += kills
68
  state["players"] = max(state["players"] - kills, 1)
69
  state["ammo"] = max(state["ammo"] - random.randint(5,15), 0)
70
- resp += f"Fought & got {kills} kill(s)! "
71
  elif roll > 0.5:
72
- # In non-final rounds, enemy may flee—but warn if it's final round.
73
  if final_round:
74
  kills = random.randint(1, 3)
75
  state["kills"] += kills
76
  state["players"] = max(state["players"] - kills, 1)
77
- resp += f"Fought & got {kills} kill(s)! "
78
  else:
79
- resp += "Enemy fled! (Avoid fleeing in final rounds!) "
80
  else:
81
  if random.random() < 0.3 and state["revives"] < 1:
82
  state["revives"] += 1
83
- resp += "Downed but self-revived! "
 
 
 
 
 
84
  else:
 
 
85
  state["in_gulag"] = True
86
  state["status"] = "gulag"
87
- return limit_response(resp + "Downed! Enter Gulag. Use !gulag to fight your opponent.")
 
88
 
89
  elif event == "loot":
90
- loot = random.choice(["weapon", "ammo", "armor"])
91
  if loot == "weapon":
92
  wpn = random.choice(["AK-47", "M4", "Shotgun", "Sniper"])
93
  state["inventory"].append(wpn)
94
  if wpn.lower() in ["ak-47", "m4", "mp5", "ar-15"]:
95
  state["bonus"] += 0.2
96
- resp += f"Found {wpn}. "
97
  elif loot == "ammo":
98
- add_ammo = random.randint(10,30)
99
  state["ammo"] += add_ammo
100
- resp += f"Found {add_ammo} ammo. "
101
- else:
102
- add_arm = random.randint(5,20)
103
  state["armor"] += add_arm
104
- resp += f"Found {add_arm} armor. "
 
 
 
 
 
 
 
105
 
106
  elif event == "buy":
107
- item = random.choice(["UAV", "Air Strike", "Cluster Strike", "Armor Box"])
108
  if item in ["UAV", "Air Strike"]:
109
  state["equipment"].append(item)
110
- state["bonus"] += 0.3 # High-value equipment gives a bigger bonus
111
- resp += f"Bought {item} (boosts kills)! "
112
  elif item == "Armor Box":
113
  state["armor"] += 10
114
- resp += "Bought Armor Box. "
115
- else:
116
  state["bonus"] += 0.15
117
- resp += f"Bought {item}. "
 
 
 
 
 
 
 
118
 
119
  elif event == "ambush":
120
  resp += "Ambushed! "
121
  amb = random.choice(["escaped", "lost ammo", "got a kill"])
 
122
  if amb == "escaped":
123
- # In final rounds, don't allow escaping
124
  if final_round:
125
- kills = random.randint(1,2)
126
  state["kills"] += kills
127
  state["players"] = max(state["players"] - kills, 1)
128
- resp += f"Ambush turned to fight: got {kills} kill(s)! "
129
  else:
130
- resp += "Escaped an ambush. "
131
  elif amb == "lost ammo":
132
- lost = random.randint(10,20)
133
  state["ammo"] = max(state["ammo"] - lost, 0)
134
- resp += f"Lost {lost} ammo. "
135
  else:
136
- kills = random.randint(1,2)
137
  state["kills"] += kills
138
  state["players"] = max(state["players"] - kills, 1)
139
- resp += f"Got {kills} kill(s) in ambush! "
140
 
141
- # Natural eliminations happen regardless of event
142
- nat = random.randint(5,15)
143
  state["players"] = max(state["players"] - nat, 1)
144
- resp += f"Nat:{nat}. "
 
145
  if state["players"] <= 1:
146
  state["status"] = "won"
147
- resp += f"Victory! Final Kills: {state['kills']}."
148
  else:
149
- resp += f"Left:{state['players']}, Kills:{state['kills']}."
150
  return limit_response(resp)
151
 
152
  @app.route("/gulag", methods=["GET"])
153
  def gulag():
154
  user = request.args.get("user")
155
  if not user or user not in game_states:
156
- return limit_response("Start game first with !start"), 400
157
  state = game_states[user]
158
  if not state.get("in_gulag"):
159
- return limit_response("You're not currently in Gulag. If you've been downed, use !gulag to fight your opponent."), 400
160
 
161
- # Simulate the Gulag fight:
162
- waiting_message = "In Gulag: waiting for an opponent... "
163
  if random.random() < (0.6 + state["bonus"] * 0.05):
164
  state["status"] = "active"
165
  state["in_gulag"] = False
166
  state["players"] = max(state["players"] - 5, 1)
167
- resp = waiting_message + "Opponent engaged! You won the Gulag fight. Back in action. Use !fight."
168
  else:
169
  state["status"] = "eliminated"
170
- resp = waiting_message + f"Opponent overpowered you. Game over. Final Kills: {state['kills']}."
171
  return limit_response(resp)
172
 
173
  if __name__ == "__main__":
 
6
  # In-memory storage for each player's game state.
7
  game_states = {}
8
 
 
9
  def limit_response(text, limit=400):
10
  return text if len(text) <= limit else text[:limit-3] + "..."
11
 
 
20
  "kills": 0,
21
  "bonus": bonus,
22
  "round": 0,
23
+ "status": "active", # active, gulag, eliminated, won
24
+ "revives": 0,
25
  "ammo": 100,
26
  "armor": 100,
27
  "inventory": [],
28
+ "equipment": [],
29
+ "plates": 3,
30
+ "max_plates": 12,
31
  "in_gulag": False
32
  }
33
 
 
40
  user = request.args.get("user")
41
  loadout = request.args.get("loadout", "")
42
  if not user:
43
+ return limit_response("User required")
44
  game_states[user] = init_game(user, loadout)
45
+ resp = (f"Game started for {user}! Players:150, Bonus:{game_states[user]['bonus']:.1f}, Plates:3. "
46
+ "Get ready for intense combat! Use !fight to engage.")
47
  return limit_response(resp)
48
 
49
  @app.route("/fight", methods=["GET"])
50
  def fight():
51
  user = request.args.get("user")
52
  if not user or user not in game_states:
53
+ return limit_response("Start game first with !start")
54
  state = game_states[user]
55
+ # If game already ended, show final result.
56
+ if state["status"] != "active":
57
+ msg = f"Game over. Final Kills: {state['kills']}. Please use !start to begin a new game."
58
+ if state["status"] == "gulag":
59
+ msg = "You are in Gulag waiting for an opponent. Use !gulag to fight."
60
+ return limit_response(msg)
61
+
62
+ # If no plates, reduce effective bonus.
63
+ effective_bonus = state["bonus"] if state["plates"] > 0 else state["bonus"] * 0.7
64
+
65
  state["round"] += 1
66
  r = state["round"]
67
+ final_round = state["players"] <= 3
68
  event = random.choice(["fight", "loot", "buy", "ambush"])
69
  resp = f"R{r}: "
70
 
71
  if event == "fight":
72
+ roll = random.random() * effective_bonus
73
+ gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"])
74
  if final_round or roll > 0.7:
75
  kills = random.randint(1, 3)
76
  state["kills"] += kills
77
  state["players"] = max(state["players"] - kills, 1)
78
  state["ammo"] = max(state["ammo"] - random.randint(5,15), 0)
79
+ resp += f"Engaged in a fierce gunfight and got {kills} kill(s)! Enemy downed with a {gun_used}. "
80
  elif roll > 0.5:
 
81
  if final_round:
82
  kills = random.randint(1, 3)
83
  state["kills"] += kills
84
  state["players"] = max(state["players"] - kills, 1)
85
+ resp += f"Final round battle: secured {kills} kill(s) with your {gun_used}! "
86
  else:
87
+ resp += f"Enemy retreated from your attack. Your aim is steady, but the battle rages on. "
88
  else:
89
  if random.random() < 0.3 and state["revives"] < 1:
90
  state["revives"] += 1
91
+ if state["plates"] > 0:
92
+ lost_plate = 1
93
+ state["plates"] = max(state["plates"] - lost_plate, 0)
94
+ resp += f"Downed but you managed a self-revive (lost {lost_plate} plate). Your determination shines through. "
95
+ else:
96
+ resp += "Downed but self-revived despite having no plates! "
97
  else:
98
+ lost_plate = random.randint(1, 2)
99
+ state["plates"] = max(state["plates"] - lost_plate, 0)
100
  state["in_gulag"] = True
101
  state["status"] = "gulag"
102
+ resp += f"Downed by enemy's {gun_used} shot and lost {lost_plate} plate(s)! You're now in Gulag. Use !gulag to fight your opponent."
103
+ return limit_response(resp)
104
 
105
  elif event == "loot":
106
+ loot = random.choice(["weapon", "ammo", "armor", "plate"])
107
  if loot == "weapon":
108
  wpn = random.choice(["AK-47", "M4", "Shotgun", "Sniper"])
109
  state["inventory"].append(wpn)
110
  if wpn.lower() in ["ak-47", "m4", "mp5", "ar-15"]:
111
  state["bonus"] += 0.2
112
+ resp += f"You scavenged a {wpn} from the wreckage. It might just turn the tide of battle. "
113
  elif loot == "ammo":
114
+ add_ammo = random.randint(10, 30)
115
  state["ammo"] += add_ammo
116
+ resp += f"Found {add_ammo} rounds of ammo on the ground. More firepower is always welcome. "
117
+ elif loot == "armor":
118
+ add_arm = random.randint(5, 20)
119
  state["armor"] += add_arm
120
+ resp += f"Picked up {add_arm} armor points to bolster your defense. Stay protected! "
121
+ else: # plate
122
+ if state["plates"] < state["max_plates"]:
123
+ new_plates = random.randint(1, 3)
124
+ state["plates"] = min(state["plates"] + new_plates, state["max_plates"])
125
+ resp += f"Discovered {new_plates} extra plate(s)! Your protection increases. "
126
+ else:
127
+ resp += "Plates are already maxed out. "
128
 
129
  elif event == "buy":
130
+ item = random.choice(["UAV", "Air Strike", "Cluster Strike", "Armor Box", "Plates"])
131
  if item in ["UAV", "Air Strike"]:
132
  state["equipment"].append(item)
133
+ state["bonus"] += 0.3
134
+ resp += f"Purchased {item}, giving you an edge on the battlefield. Your kill potential rises! "
135
  elif item == "Armor Box":
136
  state["armor"] += 10
137
+ resp += "Acquired an Armor Box to reinforce your defense. "
138
+ elif item == "Cluster Strike":
139
  state["bonus"] += 0.15
140
+ resp += "Bought a Cluster Strike to thin out the opposition. "
141
+ elif item == "Plates":
142
+ if state["plates"] < state["max_plates"]:
143
+ added = random.randint(1, 3)
144
+ state["plates"] = min(state["plates"] + added, state["max_plates"])
145
+ resp += f"Reinforced your gear with {added} extra plate(s)! "
146
+ else:
147
+ resp += "Your plates are already maxed. "
148
 
149
  elif event == "ambush":
150
  resp += "Ambushed! "
151
  amb = random.choice(["escaped", "lost ammo", "got a kill"])
152
+ gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"])
153
  if amb == "escaped":
 
154
  if final_round:
155
+ kills = random.randint(1, 2)
156
  state["kills"] += kills
157
  state["players"] = max(state["players"] - kills, 1)
158
+ resp += f"Ambush turned into a firefight and you secured {kills} kill(s) using a {gun_used}! "
159
  else:
160
+ resp += "Managed to escape the ambush, but the threat still looms. "
161
  elif amb == "lost ammo":
162
+ lost = random.randint(10, 20)
163
  state["ammo"] = max(state["ammo"] - lost, 0)
164
+ resp += f"Got caught off guard and lost {lost} ammo during the ambush. "
165
  else:
166
+ kills = random.randint(1, 2)
167
  state["kills"] += kills
168
  state["players"] = max(state["players"] - kills, 1)
169
+ resp += f"Overpowered the ambush and got {kills} kill(s) with your {gun_used}! "
170
 
171
+ nat = random.randint(5, 15)
 
172
  state["players"] = max(state["players"] - nat, 1)
173
+ resp += f"Additionally, {nat} enemies were eliminated naturally. "
174
+
175
  if state["players"] <= 1:
176
  state["status"] = "won"
177
+ resp += f"Victory! Final Kills: {state['kills']}. Congratulations, warrior! Use !start to play again."
178
  else:
179
+ resp += f"Remaining enemies: {state['players']}. Total Kills: {state['kills']}. Plates remaining: {state['plates']}. Stay alert!"
180
  return limit_response(resp)
181
 
182
  @app.route("/gulag", methods=["GET"])
183
  def gulag():
184
  user = request.args.get("user")
185
  if not user or user not in game_states:
186
+ return limit_response("Start game first with !start")
187
  state = game_states[user]
188
  if not state.get("in_gulag"):
189
+ return limit_response("You're not in Gulag. If downed, use !gulag to fight your opponent.")
190
 
191
+ waiting_msg = "In Gulag: waiting for an opponent... "
 
192
  if random.random() < (0.6 + state["bonus"] * 0.05):
193
  state["status"] = "active"
194
  state["in_gulag"] = False
195
  state["players"] = max(state["players"] - 5, 1)
196
+ resp = waiting_msg + "Opponent engaged! You won the Gulag fight and are back in action. Use !fight to continue."
197
  else:
198
  state["status"] = "eliminated"
199
+ resp = waiting_msg + f"Opponent overpowered you. Game over. Final Kills: {state['kills']}. Use !start to begin a new game."
200
  return limit_response(resp)
201
 
202
  if __name__ == "__main__":