ccr-colorado / app.py
tstone87's picture
Update app.py
10e661f verified
raw
history blame
15 kB
from flask import Flask, request
import random
app = Flask(__name__)
# ---------------- Configuration ----------------
# Adjust these values to tune game difficulty:
AIR_STRIKE_CHANCE = 0.15 # Chance (15%) to call in an Air Strike if available
SELF_REVIVE_CHANCE = 0.10 # Chance (10%) to self-revive when downed
EXTRA_LOST_PLATE_CHANCE = 0.30 # Chance (30%) 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)
# ------------------------------------------------
# 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": [], # 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
}
@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. "
"Brace yourself 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 is over, display final result.
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)
# 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":
# Air Strike branch (if available)
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:
# Select one of 15 detailed gunfight scenarios.
scenario = random.randint(1,15)
gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"])
downed = False
kills = 0
if scenario == 1:
kills = 1
resp += f"Your precision shot with the {gun_used} downed {kills} foe. "
elif scenario == 2:
resp += f"Your {gun_used} jammed at a critical moment, and you were caught off guard—downed! "
downed = True
elif scenario == 3:
kills = 1
resp += f"You outmaneuvered an enemy with your {gun_used} and secured a kill. "
elif scenario == 4:
kills = 1
resp += f"A burst from your {gun_used} earned you {kills} kill, though you were rattled. "
elif scenario == 5:
resp += f"Your attempt to flank with the {gun_used} backfired—you were overwhelmed and downed! "
downed = True
elif scenario == 6:
kills = 1
resp += f"Your rapid reload with the {gun_used} allowed you to grab a kill. "
elif scenario == 7:
resp += f"Disaster! Your {gun_used} misfired and left you exposed—downed! "
downed = True
elif scenario == 8:
resp += f"A stray round from your {gun_used} grazed an enemy, but nothing decisive happened. "
kills = 0
elif scenario == 9:
kills = 1
resp += f"An aggressive assault with your {gun_used} got you {kills} kill. "
elif scenario == 10:
kills = 1
resp += f"A lucky headshot with your {gun_used} secured {kills} kill, but you lost a plate. "
state["plates"] = max(state["plates"] - 1, 0)
elif scenario == 11:
kills = 1
resp += f"Suppressive fire from your {gun_used} earned you {kills} kill. "
elif scenario == 12:
resp += f"In a moment of hesitation, you failed to act and were downed by enemy fire. "
downed = True
elif scenario == 13:
kills = 1
resp += f"Your {gun_used} shot knocked an enemy out for {kills} kill, but your cover was compromised."
if random.random() < 0.5:
downed = True
resp += " You were subsequently overwhelmed. "
elif scenario == 14:
kills = 1
resp += f"Your {gun_used} burst ricocheted, earning {kills} kill but costing you a plate. "
state["plates"] = max(state["plates"] - 1, 0)
elif scenario == 15:
kills = 0
resp += f"In the chaos, you managed no kills and were left vulnerable."
if random.random() < 0.5:
downed = True
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)
# Additional chance to lose a plate from enemy fire.
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, try self-revive; if unsuccessful, enter Gulag.
if downed:
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(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']}.")
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... "
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"
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)