ccr-colorado / app.py
tstone87's picture
Create app.py
15ba985 verified
raw
history blame
5.12 kB
from flask import Flask, request
import random
app = Flask(__name__)
# In-memory storage for each player's game state.
game_states = {}
# Helper to ensure responses never exceed 400 characters.
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, # self-revives used
"ammo": 100,
"armor": 100,
"inventory": [],
"in_gulag": False
}
@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"), 400
game_states[user] = init_game(user, loadout)
resp = (f"Game started for {user}! Players:150, Bonus:{game_states[user]['bonus']:.1f}. "
"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"), 400
state = game_states[user]
if state["status"] != "active":
return limit_response(f"Game over. Final Kills: {state['kills']}."), 400
state["round"] += 1
r = state["round"]
event = random.choice(["fight", "loot", "buy", "ambush"])
resp = f"R{r}: "
if event == "fight":
roll = random.random() * state["bonus"]
if 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"Fought & got {kills} kill(s)! "
elif roll > 0.5:
resp += "Enemy fled! "
else:
if random.random() < 0.3 and state["revives"] < 1:
state["revives"] += 1
resp += "Downed but self-revived! "
else:
state["in_gulag"] = True
state["status"] = "gulag"
return limit_response(resp + "Downed! Now in Gulag. Use !gulag.")
elif event == "loot":
loot = random.choice(["weapon", "ammo", "armor"])
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"Found {wpn}. "
elif loot == "ammo":
add_ammo = random.randint(10,30)
state["ammo"] += add_ammo
resp += f"Found {add_ammo} ammo. "
else:
add_arm = random.randint(5,20)
state["armor"] += add_arm
resp += f"Found {add_arm} armor. "
elif event == "buy":
item = random.choice(["UAV", "Air Strike", "Cluster Strike", "Armor Box"])
if item == "Armor Box":
state["armor"] += 10
else:
state["bonus"] += 0.15
resp += f"Bought {item}. "
elif event == "ambush":
resp += "Ambushed! "
amb = random.choice(["escaped", "lost ammo", "got a kill"])
if amb == "escaped":
resp += "Escaped. "
elif amb == "lost ammo":
lost = random.randint(10,20)
state["ammo"] = max(state["ammo"] - lost, 0)
resp += f"Lost {lost} ammo. "
else:
kills = random.randint(1,2)
state["kills"] += kills
state["players"] = max(state["players"] - kills, 1)
resp += f"Got {kills} kill(s)! "
# Natural eliminations
nat = random.randint(5,15)
state["players"] = max(state["players"] - nat, 1)
resp += f"Nat:{nat}. "
if state["players"] <= 1:
state["status"] = "won"
resp += f"Victory! Final Kills: {state['kills']}."
else:
resp += f"Left:{state['players']}, Kills:{state['kills']}."
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"), 400
state = game_states[user]
if not state.get("in_gulag"):
return limit_response("Not in Gulag."), 400
if random.random() < (0.6 + state["bonus"] * 0.05):
state["status"] = "active"
state["in_gulag"] = False
state["players"] = max(state["players"] - 5, 1)
resp = "Won Gulag fight! Back in action. Use !fight next."
else:
state["status"] = "eliminated"
resp = f"Lost Gulag fight. Game over. Final Kills: {state['kills']}."
return limit_response(resp)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)