2024-07-13 23:15:25 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Player
|
|
|
|
{
|
|
|
|
public static function create(array $data): int
|
|
|
|
{
|
2024-07-15 14:24:13 -05:00
|
|
|
// get the player's class
|
|
|
|
$class = Classes::get($data['class_id'] ?? 1);
|
|
|
|
if ($class == false) die('Player::create: Invalid class selected. ' . print_r($data, true));
|
|
|
|
|
|
|
|
// get player level
|
|
|
|
$l = $data['level'] ?? 1;
|
|
|
|
|
|
|
|
// calculate player stats
|
|
|
|
$data['hp'] = $data['max_hp'] = $data['max_hp'] ?? ($class['start_hp'] + ($class['growth_hp'] * ($l - 1)));
|
|
|
|
$data['mp'] = $data['max_mp'] = $data['max_mp'] ?? ($class['start_mp'] + ($class['growth_mp'] * ($l - 1)));
|
|
|
|
$data['tp'] = $data['max_tp'] = $data['max_tp'] ?? (5 + (App::$s['tp_growth'] * ($l - 1)));
|
|
|
|
$data['str'] = $data['str'] ?? ($class['start_str'] + ($class['growth_str'] * ($l - 1)));
|
|
|
|
$data['atk'] = $data['atk'] ?? ($class['start_atk'] + ($class['growth_atk'] * ($l - 1)));
|
|
|
|
$data['def'] = $data['def'] ?? ($class['start_def'] + ($class['growth_def'] * ($l - 1)));
|
|
|
|
$data['dex'] = $data['dex'] ?? ($class['start_dex'] + ($class['growth_dex'] * ($l - 1)));
|
|
|
|
$data['stat_points'] = $data['stat_points'] ?? (App::$s['stat_point_gain'] * ($l - 1));
|
|
|
|
$data['exp2l'] = $data['exp2l'] ?? expToLevel($l + 1);
|
|
|
|
|
2024-07-15 16:24:52 -05:00
|
|
|
// award spells
|
|
|
|
if (!isset($data['spells']) || empty($data['spells'])) {
|
|
|
|
$spells = Classes::spellsAtLevel($class['id'], $l);
|
|
|
|
$data['spells'] = implode(',', $spells);
|
|
|
|
}
|
|
|
|
|
2024-07-15 14:24:13 -05:00
|
|
|
// compress data and generate placeholers
|
2024-07-13 23:15:25 -05:00
|
|
|
$keys = implode(', ', array_keys($data));
|
|
|
|
$placeholders = implode(', ', array_fill(0, count($data), '?'));
|
|
|
|
|
2024-07-15 14:24:13 -05:00
|
|
|
// insert into db
|
2024-07-13 23:15:25 -05:00
|
|
|
App::$db->do("INSERT INTO 'players' ($keys) VALUES ($placeholders);", array_values($data));
|
|
|
|
return App::$db->lastInsertID();
|
|
|
|
}
|
2024-07-15 18:05:35 -05:00
|
|
|
|
|
|
|
public static function validateCredentials(string $identifier, string $password, bool $fetch = false): int|false
|
|
|
|
{
|
|
|
|
// get the player from their username or email
|
2024-07-15 19:54:50 -05:00
|
|
|
$player = App::$db->do("SELECT " . ($fetch ? '*' : 'id, password') . " FROM players WHERE username = :i OR email = :i LIMIT 1;", ['i' => $identifier]);
|
2024-07-15 18:05:35 -05:00
|
|
|
if ($player == false) return false;
|
|
|
|
$player = $player->fetch();
|
|
|
|
|
|
|
|
// check password, return the player data if good
|
|
|
|
if (password_verify($password, $player['password'])) {
|
|
|
|
unset($player['password']);
|
|
|
|
return $fetch ? $player : $player['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-07-13 23:15:25 -05:00
|
|
|
}
|