ccr-colorado / app.py
tstone87's picture
Update app.py
4c554c4 verified
raw
history blame
12.2 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": [], # e.g. "UAV", "Air Strike"
"plates": 3,
"max_plates": 12,
"in_gulag": False,
"gulag_count": 0, # number of times you've been downed
"gulag_tokens": 0 # only obtainable AFTER your 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. "
"Prepare for 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, return 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 begin a new game."
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)
# If no plates, effective bonus is reduced.
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}: "
# -- FIGHT EVENT --
if event == "fight":
# Check for Air Strike usage if available.
if "Air Strike" in state["equipment"] and random.random() < 0.3:
# Special air strike outcome.
kills = random.randint(2, 4)
state["kills"] += kills
state["players"] = max(state["players"] - kills, 1)
state["ammo"] = max(state["ammo"] - random.randint(5,15), 0)
resp += f"You called in an Air Strike! It obliterated {kills} enemies. "
state["equipment"].remove("Air Strike")
else:
# Choose one of 10 detailed gunfight scenarios.
scenario = random.randint(1,10)
gun_used = random.choice(["M4", "AK-47", "Shotgun", "Sniper", "SMG"])
if scenario == 1:
kills = random.randint(1,2)
resp += f"Your precision shot with the {gun_used} downed {kills} foe(s). "
elif scenario == 2:
kills = 1
resp += f"You outmaneuvered an enemy and secured a kill with your {gun_used}. "
elif scenario == 3:
resp += f"Your shot missed badly with the {gun_used}! The enemy counterattacked and you lost a plate. "
state["plates"] = max(state["plates"] - 1, 0)
kills = 0
elif scenario == 4:
kills = random.randint(2,3)
resp += f"A burst from your {gun_used} decimated multiple foes, earning you {kills} kill(s). "
elif scenario == 5:
kills = random.randint(1,3)
resp += f"Overwhelming firepower from your {gun_used} got {kills} kill(s), but you overexerted and lost a plate. "
state["plates"] = max(state["plates"] - 1, 0)
elif scenario == 6:
kills = 1
resp += f"An enemy flanked you, but you quickly retaliated with your {gun_used} for 1 kill, losing a plate in the scramble. "
state["plates"] = max(state["plates"] - 1, 0)
elif scenario == 7:
resp += f"Your {gun_used} jammed at a critical moment! You dodged a deadly shot but lost a plate. "
state["plates"] = max(state["plates"] - 1, 0)
kills = 0
elif scenario == 8:
kills = 1
resp += f"Your rapid reload with the {gun_used} turned the tide, securing 1 kill at a small ammo cost. "
elif scenario == 9:
kills = 1
resp += f"A lucky shot from your {gun_used} knocked an enemy out; however, you lost a plate in the exchange. "
state["plates"] = max(state["plates"] - 1, 0)
elif scenario == 10:
kills = 0
resp += f"Your evasive maneuvers with the {gun_used} avoided danger but yielded no kills. "
# In final round, force at least one kill if outcome would be 0.
if final_round and kills == 0:
kills = 1
resp += f"Final round pressure: you managed to secure 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,15), 0)
# Chance to get additional damage from enemy fire even if you killed enemies.
if random.random() < 0.2 and state["plates"] > 0:
lost = 1
state["plates"] = max(state["plates"] - lost, 0)
resp += f"In the exchange, you also lost {lost} plate. "
# -- LOOT EVENT --
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":
# Only allow picking up a token if you've been in Gulag already and don't already have one.
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. "
# -- BUY EVENT --
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":
# Only allow buying a token if you've been to gulag and don't already have one.
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. "
# -- AMBUSH EVENT --
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 cautious. "
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 in the chaos. "
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}! "
# Natural eliminations occur regardless.
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()
# Multiple Gulag outcomes:
if outcome < 0.4:
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 < 0.7:
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:
# Stalemate: you lose a plate and must fight again in Gulag.
if state["plates"] > 0:
state["plates"] = max(state["plates"] - 1, 0)
resp = waiting_msg + "The Gulag duel is inconclusive—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)