Spaces:
Sleeping
Sleeping
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, | |
"gulag_count": 0, | |
"gulag_tokens": 0 | |
} | |
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. " | |
"Prepare for battle! 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 the game is over, just display final results. | |
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 are in Gulag! Use !gulag to fight your opponent." | |
return limit_response(msg) | |
# Adjust effective bonus if no plates remain. | |
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.8: | |
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 brutal gunfight and secured {kills} kill(s)! Enemy downed with a {gun_used}. " | |
elif roll > 0.6: | |
if final_round: | |
kills = random.randint(1, 3) | |
state["kills"] += kills | |
state["players"] = max(state["players"] - kills, 1) | |
resp += f"Final showdown: achieved {kills} kill(s) with your {gun_used}! " | |
else: | |
resp += f"Enemy retreated from your precise fire with your {gun_used}. Your focus remains sharp. " | |
else: | |
# Attempt self-revive | |
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 self-revived (lost {lost_plate} plate). Your resolve is unbroken. " | |
else: | |
resp += "Downed but managed a desperate self-revive despite having no plates! " | |
else: | |
lost_plate = random.randint(1, 2) | |
state["plates"] = max(state["plates"] - lost_plate, 0) | |
# Check if this is the first Gulag chance | |
if state["gulag_count"] == 0: | |
state["gulag_count"] += 1 | |
state["in_gulag"] = True | |
state["status"] = "gulag" | |
resp += f"Downed by enemy's {gun_used} shot and lost {lost_plate} plate(s)! You've earned a free Gulag chance. Use !gulag." | |
return limit_response(resp) | |
else: | |
# Subsequent downing requires a Gulag Token. | |
if state["gulag_tokens"] > 0: | |
state["gulag_tokens"] -= 1 | |
state["gulag_count"] += 1 | |
state["in_gulag"] = True | |
state["status"] = "gulag" | |
resp += f"Downed again by enemy's {gun_used} shot and 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's {gun_used} shot and 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 battlefield. It might change the tide of war. " | |
elif loot == "ammo": | |
add_ammo = random.randint(10, 30) | |
state["ammo"] += add_ammo | |
resp += f"Found {add_ammo} rounds of ammo. More firepower at your disposal. " | |
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. " | |
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)! Your protection increases. " | |
else: | |
resp += "Plates are already maxed out. " | |
elif loot == "gulag token": | |
state["gulag_tokens"] += 1 | |
resp += "Stumbled upon a Gulag Token on the field! This token can grant you an extra chance in Gulag. " | |
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 battlefield prowess! " | |
elif item == "Armor Box": | |
state["armor"] += 10 | |
resp += "Acquired an Armor Box to fortify 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": | |
state["gulag_tokens"] += 1 | |
resp += "Purchased a Gulag Token for a second chance in battle! " | |
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 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']}. 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']}. Gulag Tokens: {state['gulag_tokens']}. Stay vigilant!") | |
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 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) | |