ccr-colorado / app.py
tstone87's picture
Update app.py
8a9ab6d verified
raw
history blame
8.97 kB
from flask import Flask, request
import random
app = Flask(__name__)
# In-memory storage for each player's game state.
game_states = {}
def limit_response(text, limit=400):
return text if len(text) <= limit else text[:limit-3] + "..."
def init_game(username, loadout):
bonus = 1.0
if loadout:
for w in loadout.lower().split(","):
if w.strip() in ["ak-47", "m4", "mp5", "ar-15"]:
bonus += 0.2
return {
"players": 150,
"kills": 0,
"bonus": bonus,
"round": 0,
"status": "active", # active, gulag, eliminated, won
"revives": 0,
"ammo": 100,
"armor": 100,
"inventory": [],
"equipment": [],
"plates": 3,
"max_plates": 12,
"in_gulag": False
}
@app.route("/")
def home():
return "Welcome to the BR game API! Use /start, /fight, or /gulag."
@app.route("/start", methods=["GET"])
def start():
user = request.args.get("user")
loadout = request.args.get("loadout", "")
if not user:
return limit_response("User required")
game_states[user] = init_game(user, loadout)
resp = (f"Game started for {user}! Players:150, Bonus:{game_states[user]['bonus']:.1f}, Plates:3. "
"Get ready for intense combat! Use !fight to engage.")
return limit_response(resp)
@app.route("/fight", methods=["GET"])
def fight():
user = request.args.get("user")
if not user or user not in game_states:
return limit_response("Start game first with !start")
state = game_states[user]
# If game already ended, show final result.
if state["status"] != "active":
msg = f"Game over. Final Kills: {state['kills']}. Please use !start to begin a new game."
if state["status"] == "gulag":
msg = "You are in Gulag waiting for an opponent. Use !gulag to fight."
return limit_response(msg)
# If no plates, reduce effective bonus.
effective_bonus = state["bonus"] if state["plates"] > 0 else state["bonus"] * 0.7
state["round"] += 1
r = state["round"]
final_round = state["players"] <= 3
event = random.choice(["fight", "loot", "buy", "ambush"])
resp = f"R{r}: "
if event == "fight":
roll = random.random() * effective_bonus
gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"])
if final_round or roll > 0.7:
kills = random.randint(1, 3)
state["kills"] += kills
state["players"] = max(state["players"] - kills, 1)
state["ammo"] = max(state["ammo"] - random.randint(5,15), 0)
resp += f"Engaged in a fierce gunfight and got {kills} kill(s)! Enemy downed with a {gun_used}. "
elif roll > 0.5:
if final_round:
kills = random.randint(1, 3)
state["kills"] += kills
state["players"] = max(state["players"] - kills, 1)
resp += f"Final round battle: secured {kills} kill(s) with your {gun_used}! "
else:
resp += f"Enemy retreated from your attack. Your aim is steady, but the battle rages on. "
else:
if random.random() < 0.3 and state["revives"] < 1:
state["revives"] += 1
if state["plates"] > 0:
lost_plate = 1
state["plates"] = max(state["plates"] - lost_plate, 0)
resp += f"Downed but you managed a self-revive (lost {lost_plate} plate). Your determination shines through. "
else:
resp += "Downed but self-revived despite having no plates! "
else:
lost_plate = random.randint(1, 2)
state["plates"] = max(state["plates"] - lost_plate, 0)
state["in_gulag"] = True
state["status"] = "gulag"
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."
return limit_response(resp)
elif event == "loot":
loot = random.choice(["weapon", "ammo", "armor", "plate"])
if loot == "weapon":
wpn = random.choice(["AK-47", "M4", "Shotgun", "Sniper"])
state["inventory"].append(wpn)
if wpn.lower() in ["ak-47", "m4", "mp5", "ar-15"]:
state["bonus"] += 0.2
resp += f"You scavenged a {wpn} from the wreckage. It might just turn the tide of battle. "
elif loot == "ammo":
add_ammo = random.randint(10, 30)
state["ammo"] += add_ammo
resp += f"Found {add_ammo} rounds of ammo on the ground. More firepower is always welcome. "
elif loot == "armor":
add_arm = random.randint(5, 20)
state["armor"] += add_arm
resp += f"Picked up {add_arm} armor points to bolster your defense. Stay protected! "
else: # plate
if state["plates"] < state["max_plates"]:
new_plates = random.randint(1, 3)
state["plates"] = min(state["plates"] + new_plates, state["max_plates"])
resp += f"Discovered {new_plates} extra plate(s)! Your protection increases. "
else:
resp += "Plates are already maxed out. "
elif event == "buy":
item = random.choice(["UAV", "Air Strike", "Cluster Strike", "Armor Box", "Plates"])
if item in ["UAV", "Air Strike"]:
state["equipment"].append(item)
state["bonus"] += 0.3
resp += f"Purchased {item}, giving you an edge on the battlefield. Your kill potential rises! "
elif item == "Armor Box":
state["armor"] += 10
resp += "Acquired an Armor Box to reinforce your defense. "
elif item == "Cluster Strike":
state["bonus"] += 0.15
resp += "Bought a Cluster Strike to thin out the opposition. "
elif item == "Plates":
if state["plates"] < state["max_plates"]:
added = random.randint(1, 3)
state["plates"] = min(state["plates"] + added, state["max_plates"])
resp += f"Reinforced your gear with {added} extra plate(s)! "
else:
resp += "Your plates are already maxed. "
elif event == "ambush":
resp += "Ambushed! "
amb = random.choice(["escaped", "lost ammo", "got a kill"])
gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"])
if amb == "escaped":
if final_round:
kills = random.randint(1, 2)
state["kills"] += kills
state["players"] = max(state["players"] - kills, 1)
resp += f"Ambush turned into a firefight and you secured {kills} kill(s) using a {gun_used}! "
else:
resp += "Managed to escape the ambush, but the threat still looms. "
elif amb == "lost ammo":
lost = random.randint(10, 20)
state["ammo"] = max(state["ammo"] - lost, 0)
resp += f"Got caught off guard and lost {lost} ammo during the ambush. "
else:
kills = random.randint(1, 2)
state["kills"] += kills
state["players"] = max(state["players"] - kills, 1)
resp += f"Overpowered the ambush and got {kills} kill(s) with your {gun_used}! "
nat = random.randint(5, 15)
state["players"] = max(state["players"] - nat, 1)
resp += f"Additionally, {nat} enemies were eliminated naturally. "
if state["players"] <= 1:
state["status"] = "won"
resp += f"Victory! Final Kills: {state['kills']}. Congratulations, warrior! Use !start to play again."
else:
resp += f"Remaining enemies: {state['players']}. Total Kills: {state['kills']}. Plates remaining: {state['plates']}. Stay alert!"
return limit_response(resp)
@app.route("/gulag", methods=["GET"])
def gulag():
user = request.args.get("user")
if not user or user not in game_states:
return limit_response("Start game first with !start")
state = game_states[user]
if not state.get("in_gulag"):
return limit_response("You're not in Gulag. If downed, use !gulag to fight your opponent.")
waiting_msg = "In Gulag: waiting for an opponent... "
if random.random() < (0.6 + state["bonus"] * 0.05):
state["status"] = "active"
state["in_gulag"] = False
state["players"] = max(state["players"] - 5, 1)
resp = waiting_msg + "Opponent engaged! You won the Gulag fight and are back in action. Use !fight to continue."
else:
state["status"] = "eliminated"
resp = waiting_msg + f"Opponent overpowered you. Game over. Final Kills: {state['kills']}. Use !start to begin a new game."
return limit_response(resp)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)