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);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|