Spaces:
Sleeping
Sleeping
from flask import Flask, request | |
import random | |
app = Flask(__name__) | |
# ---------------- Configuration ---------------- | |
AIR_STRIKE_CHANCE = 0.35 # Chance to call in an Air Strike if available | |
SELF_REVIVE_CHANCE = 0.10 # Chance to self-revive when downed | |
EXTRA_LOST_PLATE_CHANCE = 1.50 # Chance to lose an extra plate from enemy fire | |
GULAG_WIN_THRESHOLD = 0.35 # In Gulag: outcome <35% = win (return to battle) | |
GULAG_ELIMINATED_THRESHOLD = 0.65 # In Gulag: outcome >=35% and <65% = eliminated; >=65% = stalemate (lose a plate and try again) | |
NAT_MIN_DEATHS = 13 # Minimum natural enemy deaths per round | |
NAT_MAX_DEATHS = 27 # Maximum natural enemy deaths per round | |
# ------------------------------------------------ | |
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": [], # e.g. "UAV", "Air Strike" | |
"plates": 3, | |
"max_plates": 12, | |
"in_gulag": False, | |
"gulag_count": 0, # number of times downed | |
"gulag_tokens": 0 # only obtainable AFTER first gulag | |
} | |
def home(): | |
return "Welcome to the BR game API! Use /start, /fight, or /gulag." | |
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. " | |
"Brace yourself for intense combat! Use !fight to engage.") | |
return limit_response(resp) | |
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 state["status"] != "active": | |
msg = f"Game over. Final Kills: {state['kills']}. " | |
if state["status"] == "won": | |
msg += "You emerged victorious! Use !start to play again." | |
elif state["status"] == "eliminated": | |
msg += "You were eliminated. Use !start to try again." | |
elif state["status"] == "gulag": | |
msg += "You're in Gulag! Use !gulag to fight your opponent." | |
return limit_response(msg) | |
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": | |
if "Air Strike" in state["equipment"] and random.random() < AIR_STRIKE_CHANCE: | |
kills = random.randint(2, 3) | |
state["kills"] += kills | |
state["players"] = max(state["players"] - kills, 1) | |
state["ammo"] = max(state["ammo"] - random.randint(5,10), 0) | |
resp += f"You called in an Air Strike! It obliterated {kills} enemies. " | |
state["equipment"].remove("Air Strike") | |
else: | |
scenario = random.randint(1,20) | |
gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"]) | |
downed = False | |
kills = 0 | |
if scenario <= 4: | |
kills = random.randint(1,2) | |
resp += f"Your precise shot with the {gun_used} downed {kills} foe(s). " | |
elif scenario <= 12: | |
kills = random.randint(0,1) | |
if kills == 0: | |
resp += f"Your {gun_used} malfunctioned, leaving you exposed—you were downed! " | |
else: | |
resp += f"Your {gun_used} fired erratically, securing {kills} kill(s) but leaving you vulnerable—you were downed! " | |
downed = True | |
else: | |
kills = random.randint(1,2) | |
resp += f"In a desperate counterattack with your {gun_used}, you secured {kills} kill(s). " | |
if final_round and not downed and kills == 0: | |
kills = 1 | |
resp += f"Under final round pressure, you managed a desperate 1 kill with your {gun_used}. " | |
state["kills"] += kills | |
if kills > 0: | |
state["players"] = max(state["players"] - kills, 1) | |
state["ammo"] = max(state["ammo"] - random.randint(5,10), 0) | |
if not downed and random.random() < EXTRA_LOST_PLATE_CHANCE: | |
lost = 1 | |
state["plates"] = max(state["plates"] - lost, 0) | |
resp += f"Amid the firefight, you lost {lost} plate from enemy fire. " | |
if downed: | |
# If players left are 30 or fewer, do NOT allow Gulag—auto-eliminate. | |
if state["players"] <= 30: | |
if random.random() < SELF_REVIVE_CHANCE: | |
state["revives"] += 1 | |
if state["plates"] > 0: | |
lost_plate = 1 | |
state["plates"] = max(state["plates"] - lost_plate, 0) | |
resp += f"Miraculously, you self-revived (lost {lost_plate} plate). " | |
else: | |
resp += "Miraculously, you self-revived despite having no plates. " | |
else: | |
lost_plate = random.randint(1,2) | |
state["plates"] = max(state["plates"] - lost_plate, 0) | |
state["status"] = "eliminated" | |
resp += (f"Downed by enemy fire (lost {lost_plate} plate(s))! With only {state['players']} players remaining, " | |
"you were eliminated. Use !start to play again.") | |
return limit_response(resp) | |
else: | |
if random.random() < SELF_REVIVE_CHANCE: | |
state["revives"] += 1 | |
if state["plates"] > 0: | |
lost_plate = 1 | |
state["plates"] = max(state["plates"] - lost_plate, 0) | |
resp += f"Miraculously, you self-revived (lost {lost_plate} plate). " | |
else: | |
resp += "Miraculously, you self-revived despite having no plates. " | |
else: | |
lost_plate = random.randint(1,2) | |
state["plates"] = max(state["plates"] - lost_plate, 0) | |
if state["gulag_count"] == 0: | |
state["gulag_count"] += 1 | |
state["in_gulag"] = True | |
state["status"] = "gulag" | |
resp += f"Downed by enemy fire (lost {lost_plate} plate(s))! You've earned a free Gulag chance. Use !gulag." | |
return limit_response(resp) | |
else: | |
if state["gulag_tokens"] > 0: | |
state["gulag_tokens"] -= 1 | |
state["gulag_count"] += 1 | |
state["in_gulag"] = True | |
state["status"] = "gulag" | |
resp += f"Downed again (lost {lost_plate} plate(s))! You used a Gulag Token for another chance. Use !gulag." | |
return limit_response(resp) | |
else: | |
state["status"] = "eliminated" | |
resp += f"Downed by enemy fire (lost {lost_plate} plate(s))! No Gulag Tokens available. Game over. Final Kills: {state['kills']}. Use !start to play again." | |
return limit_response(resp) | |
elif event == "loot": | |
loot = random.choice(["weapon", "ammo", "armor", "plate", "gulag token"]) | |
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. " | |
elif loot == "ammo": | |
add_ammo = random.randint(10, 30) | |
state["ammo"] += add_ammo | |
resp += f"Found {add_ammo} rounds of ammo. " | |
elif loot == "armor": | |
add_arm = random.randint(5, 20) | |
state["armor"] += add_arm | |
resp += f"Picked up {add_arm} armor points. " | |
elif loot == "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)! " | |
else: | |
resp += "Plates are already maxed out. " | |
elif loot == "gulag token": | |
if state["gulag_count"] > 0 and state["gulag_tokens"] == 0: | |
state["gulag_tokens"] = 1 | |
resp += "Found a Gulag Token on the battlefield! " | |
else: | |
resp += "No useful token found. " | |
elif event == "buy": | |
item = random.choice(["UAV", "Air Strike", "Cluster Strike", "Armor Box", "Plates", "Gulag Token"]) | |
if item in ["UAV", "Air Strike"]: | |
state["equipment"].append(item) | |
state["bonus"] += 0.3 | |
resp += f"Purchased {item}, boosting your firepower! " | |
elif item == "Armor Box": | |
state["armor"] += 10 | |
resp += "Acquired an Armor Box to reinforce your defenses. " | |
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 at maximum. " | |
elif item == "Gulag Token": | |
if state["gulag_count"] > 0 and state["gulag_tokens"] == 0: | |
state["gulag_tokens"] = 1 | |
resp += "Purchased a Gulag Token for a second chance! " | |
else: | |
resp += "No need for a token right now. " | |
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 escalated into a firefight and you secured {kills} kill(s) with your {gun_used}! " | |
else: | |
resp += "Managed to evade the ambush, but remain on high alert. " | |
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. " | |
else: | |
kills = random.randint(1, 2) | |
state["kills"] += kills | |
state["players"] = max(state["players"] - kills, 1) | |
resp += f"Overpowered the ambushers and secured {kills} kill(s) with your {gun_used}! " | |
nat = random.randint(NAT_MIN_DEATHS, NAT_MAX_DEATHS) | |
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']}. You emerged triumphant, warrior! Use !start to play again." | |
else: | |
resp += (f"Remaining enemies: {state['players']}. Total Kills: {state['kills']}. " | |
f"Plates remaining: {state['plates']}.") | |
return limit_response(resp) | |
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 state["status"] in ["eliminated", "won"]: | |
msg = f"Game over. Final Kills: {state['kills']}. Use !start to play again." | |
return limit_response(msg) | |
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... " | |
outcome = random.random() | |
if outcome < GULAG_WIN_THRESHOLD: | |
state["status"] = "active" | |
state["in_gulag"] = False | |
state["players"] = max(state["players"] - random.randint(2,5), 1) | |
resp = waiting_msg + "After a fierce duel, you won the Gulag fight and return to battle. Use !fight to continue." | |
elif outcome < GULAG_ELIMINATED_THRESHOLD: | |
state["status"] = "eliminated" | |
state["in_gulag"] = False | |
resp = waiting_msg + f"Your opponent proved too strong. You were eliminated in Gulag. Final Kills: {state['kills']}. Use !start to play again." | |
else: | |
if state["plates"] > 0: | |
state["plates"] = max(state["plates"] - 1, 0) | |
resp = waiting_msg + "The Gulag duel ended in a stalemate—you lost a plate and remain in Gulag. Use !gulag to try again." | |
return limit_response(resp) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=7860) | |