Skip to content

Commit

Permalink
magic level training similar to ultima online
Browse files Browse the repository at this point in the history
  • Loading branch information
hclivess committed Sep 17, 2024
1 parent 2c58a86 commit 5e6ec1c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 38 deletions.
2 changes: 1 addition & 1 deletion combat_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_weapon_damage(attacker: Dict) -> Dict:
return {"damage": final_damage, "base_damage": damage, "martial_bonus": martial_bonus, "message": message}

def get_spell_damage(spell_damage: int, caster: Dict) -> Dict:
magic_bonus = caster.get("magic", 0)
magic_bonus = caster.get("sorcery", 0)
final_damage = spell_damage + magic_bonus
return {"damage": final_damage, "base_damage": spell_damage, "magic_bonus": magic_bonus}

Expand Down
17 changes: 17 additions & 0 deletions enclaves.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from revive import revive
from learn import learn_spell
from log import log_user_action, log_turn_engine_event
from train_sorcery import train_sorcery

MAX_SIZE = 1000000
DISTANCE = 15
Expand Down Expand Up @@ -737,6 +738,21 @@ def _perform_fight(self, user, user_data, target, target_name, league):
mapdb[league])


class TrainSorceryHandler(BaseHandler):
def get(self):
user = tornado.escape.xhtml_escape(self.current_user)
league = self.get_current_league()

user_data = get_user_data(user, usersdb[league])
success, message = train_sorcery(user, user_data, usersdb[league], mapdb[league])

log_user_action(user, "train_sorcery", f"Success: {success}, Message: {message}")

response = json.dumps({"success": success, "message": message})

self.set_header("Content-Type", "application/json")
self.write(response)

class ConquerHandler(UserActionHandler):
def get(self, *args, **kwargs):
user = tornado.escape.xhtml_escape(self.current_user)
Expand Down Expand Up @@ -1129,6 +1145,7 @@ def make_app():
(r"/learn", LearnHandler),
(r"/chat", ChatHandler),
(r"/ws/chat", ChatWebSocketHandler),
(r"/train_sorcery", TrainSorceryHandler),
(r"/assets/(.*)", tornado.web.StaticFileHandler, {"path": "assets"}),
(r"/img/(.*)", tornado.web.StaticFileHandler, {"path": "img"}),
(r"/css/(.*)", tornado.web.StaticFileHandler, {"path": "css"}),
Expand Down
Binary file added img/assets/dojo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions player.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(self, username: str, x_pos: int, y_pos: int, profile_pic: str = "",
self.units = []
self.spells = []
self.skills = []
self.magic = 1
self.martial = 1
self.sorcery = 0
self.fitness = 0
self.research = 0
self.base_hp = 100 # Base HP value
self.base_mana = 100 # Base mana value
Expand Down
97 changes: 62 additions & 35 deletions templates/temple.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="/img/favicon.ico">
<link href="css/cursor.css" rel="stylesheet">
<title>Temple - Learn Spells</title>
<title>Temple - Learn Spells & Train Sorcery</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="/css/temple.css">
<style>

/* Add any additional styles here if needed */
</style>
</head>


<body class="bg-light">
<div class="container mt-4">
<div class="row justify-content-center">
Expand All @@ -31,7 +30,21 @@

<div class="container my-4">
<div id="infopanel" class="alert alert-info mb-4">
Learn new spells to enhance your magical abilities!
Learn new spells and train your sorcery to enhance your magical abilities!
</div>

<!-- New Train Sorcery section -->
<div class="card mb-4">
<div class="card-header bg-dark text-warning">
<h4 class="mb-0">Train Sorcery</h4>
</div>
<div class="card-body">
<p>Enhance your magical prowess by training your sorcery skills.</p>
<p>Cost: 5000 EXP</p>
<p>Success Rate: 50%</p>
<p>Magic Power Gain on Success: +50</p>
<button class="btn btn-primary" onclick="trainSorcery()">Train Sorcery</button>
</div>
</div>

<div class="card">
Expand Down Expand Up @@ -66,37 +79,51 @@ <h5 class="spell-name">{{ spell.DISPLAY_NAME }}</h5>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function learnSpell(spellType) {
fetch(`/learn`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `spell=${spellType}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayMessage(data.message, 'success');
location.reload(); // Reload the page to update the spell status
} else {
displayMessage(data.message, 'error');
}
})
.catch(error => {
console.error('Error:', error);
displayMessage('An error occurred while learning the spell.', 'error');
});
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function learnSpell(spellType) {
fetch(`/learn`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `spell=${spellType}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayMessage(data.message, 'success');
location.reload(); // Reload the page to update the spell status
} else {
displayMessage(data.message, 'error');
}
})
.catch(error => {
console.error('Error:', error);
displayMessage('An error occurred while learning the spell.', 'error');
});
}

function trainSorcery() {
fetch(`/train_sorcery`, {
method: 'GET',
})
.then(response => response.json())
.then(data => {
displayMessage(data.message, data.message.includes("successful") ? 'success' : 'error');
})
.catch(error => {
console.error('Error:', error);
displayMessage('An error occurred while training sorcery.', 'error');
});
}

function displayMessage(message, type) {
const infopanel = document.getElementById('infopanel');
infopanel.textContent = message;
infopanel.classList.remove('text-success', 'text-danger');
infopanel.classList.add(`text-${type}`);
}
</script>
function displayMessage(message, type) {
const infopanel = document.getElementById('infopanel');
infopanel.textContent = message;
infopanel.classList.remove('text-success', 'text-danger');
infopanel.classList.add(`text-${type}`);
}
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions templates/user_panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ <h5 class="mb-1">{{user}}</h5>
<p class="mb-1">Army: {{ file.get("army_free", "N/A") }} / {{ file.get("army_deployed", "N/A") }}</p>
<p class="mb-1">Action points: {{ file.get("action_points", "N/A") }}</p>
<p class="mb-1">Score: {{ file.get("score", "N/A") }}</p>
<p class="mb-1">Sorcery: {{ file.get("sorcery", "N/A") }}</p>
<p class="mb-1">Fitness: {{ file.get("fitness", "N/A") }}</p>
<p class="mb-1">Position: ({{ file.get("x_pos", "N/A") }}, {{ file.get("y_pos", "N/A") }})</p>
</div>
</div>
Expand Down
20 changes: 20 additions & 0 deletions train_sorcery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import random


def train_sorcery(user, user_data, usersdb, mapdb):
exp_cost = 5000
magic_gain = 50
failure_chance = 0.5

if user_data['exp'] < exp_cost:
return False, f"You need {exp_cost} exp to train sorcery. You only have {user_data['exp']} exp."

if random.random() < failure_chance:
user_data['exp'] -= exp_cost
return False, f"Your sorcery training failed. You lost {exp_cost} exp."

user_data['exp'] -= exp_cost
user_data['sorcery'] = user_data.get('sorcery', 0) + magic_gain

usersdb[user].update(user_data)
return True, f"Your sorcery training was successful! You gained {magic_gain} magic power."

0 comments on commit 5e6ec1c

Please sign in to comment.