Outfit impl

This commit is contained in:
Astrologies 2021-12-20 07:45:24 -05:00
parent 7cd4f5affc
commit 1d5919027a
5 changed files with 287 additions and 278 deletions

View File

@ -0,0 +1,254 @@
<?php
/*
Alphaland 2021
*/
namespace Alphaland\Users {
use Alphaland\Users\Render;
use Exception;
use PDO;
class Outfit
{
public static function UserOutfitCount(int $userid)
{
$outfits = $GLOBALS['pdo']->prepare('SELECT COUNT(*) FROM `user_outfits` WHERE `userid` = :uid');
$outfits->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfits->execute();
return $outfits->fetchColumn();
}
public static function UserOwnsOutfit(int $userid, int $outfitid)
{
$outfit = $GLOBALS['pdo']->prepare('SELECT COUNT(*) FROM `user_outfits` WHERE `userid` = :uid AND `id` = :id');
$outfit->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfit->bindParam(":id", $outfitid, PDO::PARAM_INT);
$outfit->execute();
if ($outfit->fetchColumn() > 0) {
return true;
}
return false;
}
public static function ThumbHashInOutfit(string $thumbhash)
{
$outfit = $GLOBALS['pdo']->prepare('SELECT COUNT(*) FROM `user_outfits` WHERE `ThumbHash` = :hash');
$outfit->bindParam(":hash", $thumbhash, PDO::PARAM_STR);
$outfit->execute();
if ($outfit->fetchColumn() > 0 || $thumbhash == $GLOBALS['defaultOutfitHash']) { //default outfit hash
return true;
}
return false;
}
public static function HeadshotThumbHashInOutfit(string $thumbhash)
{
$outfit = $GLOBALS['pdo']->prepare('SELECT COUNT(*) FROM user_outfits WHERE HeadshotThumbHash = :hash');
$outfit->bindParam(":hash", $thumbhash, PDO::PARAM_STR);
$outfit->execute();
if ($outfit->fetchColumn() > 0 || $thumbhash == $GLOBALS['defaultHeadshotHash']) { //default headshot hash
return true;
}
return false;
}
public static function CreateOutfit(string $name, int $userid)
{
$name = cleanInput($name);
if (strlen($name) <= 3) {
throw new Exception('Name too short, must be above 3 characters 2');
}
else if (strlen($name) >= 50) {
throw new Exception('Name too long, must be less than 50 characters 2');
}
else if (Outfit::UserOutfitCount($userid) >= 24) {
throw new Exception('Limit of 24 outfits');
}
else if (Render::PendingRendering($userid)) {
throw new Exception('Please wait for the current render');
} else {
//queries
$hash = $GLOBALS['pdo']->prepare("SELECT * FROM users WHERE id = " . $userid);
$hash->execute();
$hash = $hash->fetch(PDO::FETCH_OBJ);
$headshothash = $hash->HeadshotThumbHash;
$headshotAngelRight = $hash->headshotAngleRight;
$headshotAngleLeft = $hash->headshotAngleLeft;
$hash = $hash->ThumbHash;
$wearingcolors = $GLOBALS['pdo']->prepare('SELECT * FROM body_colours WHERE uid = ' . $userid);
$wearingcolors->execute();
$wearingcolors = $wearingcolors->fetch(PDO::FETCH_OBJ);
//users current body colors
$head = (int)$wearingcolors->h;
$torso = (int)$wearingcolors->t;
$leftarm = (int)$wearingcolors->la;
$rightarm = (int)$wearingcolors->ra;
$leftleg = (int)$wearingcolors->ll;
$rightleg = (int)$wearingcolors->rl;
//currently wearing items
$assets = wearingAssets($userid);
//add to db
$outfit = $GLOBALS['pdo']->prepare("INSERT INTO user_outfits(userid, assets, name, h, t, la, ra, ll, rl, headshotAngleRight, headshotAngleLeft, ThumbHash, HeadshotThumbHash, whenCreated) VALUES (:uid, :assets, :name, :h, :t, :la, :ra, :ll, :rl, :har, :hal, :th, :hth, UNIX_TIMESTAMP())");
$outfit->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfit->bindParam(":assets", $assets, PDO::PARAM_STR);
$outfit->bindParam(":name", $name, PDO::PARAM_STR);
$outfit->bindParam(":h", $head, PDO::PARAM_INT);
$outfit->bindParam(":t", $torso, PDO::PARAM_INT);
$outfit->bindParam(":la", $leftarm, PDO::PARAM_INT);
$outfit->bindParam(":ra", $rightarm, PDO::PARAM_INT);
$outfit->bindParam(":ll", $leftleg, PDO::PARAM_INT);
$outfit->bindParam(":rl", $rightleg, PDO::PARAM_INT);
$outfit->bindParam(":har", $headshotAngelRight, PDO::PARAM_INT);
$outfit->bindParam(":hal", $headshotAngleLeft, PDO::PARAM_INT);
$outfit->bindParam(":th", $hash, PDO::PARAM_STR);
$outfit->bindParam(":hth", $headshothash, PDO::PARAM_STR);
$outfit->execute();
return true;
}
}
public static function DeleteOutfit(int $userid, int $outfitid)
{
if (!Outfit::UserOwnsOutfit($userid, $outfitid)) {
throw new Exception('Error occurred');
} else {
$delete = $GLOBALS['pdo']->prepare("DELETE from user_outfits WHERE userid = :uid AND id = :id");
$delete->bindParam(":uid", $userid, PDO::PARAM_INT);
$delete->bindParam(":id", $outfitid, PDO::PARAM_INT);
$delete->execute();
if ($delete->rowCount() > 0) {
return true;
}
}
}
public static function UpdateOutfit(int $userid, int $outfitid, string $name)
{
$name = cleanInput($name);
if (strlen($name) <= 3) {
throw new Exception('Name too short, must be above 3 characters 1');
} else if (strlen($name) >= 50) {
throw new Exception('Name too long, must be less than 50 characters 1');
} else if (!Outfit::UserOwnsOutfit($userid, $outfitid)) {
throw new Exception('Error occurred');
} else if (Render::PendingRendering($userid)) {
throw new Exception('Please wait for the current render');
} else if (!Outfit::DeleteOutfit($userid, $outfitid)) {
throw new Exception('Failed to update outfit, contact an Administrator');
} else if (!Outfit::CreateOutfit($name, $userid)) {
throw new Exception('Failed to update outfit, contact an Administrator');
} else {
return true;
}
}
public static function ApplyOutfit(int $userid, int $outfitid)
{
if (!Outfit::UserOwnsOutfit($userid, $outfitid)) {
throw new Exception('Error occurred');
} else if (Render::PendingRendering($userid)) {
throw new Exception('Please wait for the current render');
} else {
$outfit = $GLOBALS['pdo']->prepare('SELECT * FROM user_outfits WHERE userid = :uid AND id = :id');
$outfit->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfit->bindParam(":id", $outfitid, PDO::PARAM_INT);
$outfit->execute();
if ($outfit->rowCount() == 0) {
throw new Exception('Error occurred');
} else {
//vars
$outfit = $outfit->fetch(PDO::FETCH_OBJ);
$outfitassets = explode(";", $outfit->assets);
//outfit body colors
$outfithead = (int)$outfit->h;
$outfittorso = (int)$outfit->t;
$outfitleftarm = (int)$outfit->la;
$outfitrightarm = (int)$outfit->ra;
$outfitleftleg = (int)$outfit->ll;
$outfitrightleg = (int)$outfit->rl;
//headshot settings
$headshotAngelRight = $outfit->headshotAngleRight;
$headshotAngleLeft = $outfit->headshotAngleLeft;
//apply outfit body colors
$bodycolor = $GLOBALS['pdo']->prepare("UPDATE body_colours SET h = :h, t = :t, la = :la, ra = :ra, ll = :ll, rl = :rl WHERE uid = :uid");
$bodycolor->bindParam(":h", $outfithead, PDO::PARAM_INT);
$bodycolor->bindParam(":t", $outfittorso, PDO::PARAM_INT);
$bodycolor->bindParam(":la", $outfitleftarm, PDO::PARAM_INT);
$bodycolor->bindParam(":ra", $outfitrightarm, PDO::PARAM_INT);
$bodycolor->bindParam(":ll", $outfitleftleg, PDO::PARAM_INT);
$bodycolor->bindParam(":rl", $outfitrightleg, PDO::PARAM_INT);
$bodycolor->bindParam(":uid", $userid, PDO::PARAM_INT);
$bodycolor->execute();
//delete all wearing items
$deequip = $GLOBALS['pdo']->prepare("DELETE from wearing_items WHERE uid = :u"); //delete all wearing
$deequip->bindParam(":u", $userid, PDO::PARAM_INT);
$deequip->execute();
//apply items in the outfit
foreach($outfitassets as $asset)
{
if ($asset != "") //hack for outfits with no wearing items
{
$equip = $GLOBALS['pdo']->prepare("INSERT INTO wearing_items(uid,aid,whenWorn) VALUES(:u,:a,UNIX_TIMESTAMP())");
$equip->bindParam(":u", $userid, PDO::PARAM_INT);
$equip->bindParam(":a", $asset, PDO::PARAM_INT);
$equip->execute();
}
}
//delete current render and headshot if its not part of an outfit
$prevhash = $GLOBALS['pdo']->prepare("SELECT * FROM users WHERE id = :i");
$prevhash->bindParam(":i", $userid, PDO::PARAM_INT);
$prevhash->execute();
$prevhash = $prevhash->fetch(PDO::FETCH_OBJ);
$oldhash = $prevhash->ThumbHash;
$oldheadshothash = $prevhash->HeadshotThumbHash;
if (!Outfit::ThumbHashInOutfit($oldhash)) {
unlink($GLOBALS['renderCDNPath'] . $oldhash);
} else if (!Outfit::HeadshotThumbHashInOutfit($oldheadshothash)) {
unlink($GLOBALS['renderCDNPath'] . $oldheadshothash);
}
//outfits hashes
$hash = $outfit->ThumbHash;
$headshothash = $outfit->HeadshotThumbHash;
if ($headshothash == NULL) //outfit was created before headshots release (probably?)
{
Render::RenderPlayerCloseup($userid);
$headshothash = userInfo($userid)->HeadshotThumbHash;
$update = $GLOBALS['pdo']->prepare('UPDATE user_outfits SET HeadshotThumbHash = :hhash WHERE id = :oid');
$update->bindParam(":hhash", $headshothash, PDO::PARAM_STR);
$update->bindParam(":oid", $outfitid, PDO::PARAM_INT);
$update->execute();
}
//apply the outfit (yay less render server load)
$user = $GLOBALS['pdo']->prepare('UPDATE users SET ThumbHash = :hash, HeadshotThumbHash = :hhash, headshotAngleRight = :har, headshotAngleLeft = :hal WHERE id = ' . $userid);
$user->bindParam(":hash", $hash, PDO::PARAM_STR);
$user->bindParam(":hhash", $headshothash, PDO::PARAM_STR);
$user->bindParam(":har", $headshotAngelRight, PDO::PARAM_INT);
$user->bindParam(":hal", $headshotAngleLeft, PDO::PARAM_INT);
$user->execute();
return true;
}
}
}
}
}

View File

@ -236,271 +236,6 @@ function genTicket()
//TODO: Render Queue?
//outfit system
function playerOutfitsCount($userid)
{
$outfits = $GLOBALS['pdo']->prepare('SELECT * FROM user_outfits WHERE userid = :uid');
$outfits->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfits->execute();
return $outfits->rowCount();
}
function playerOwnsOutfit($userid, $outfitid)
{
$outfit = $GLOBALS['pdo']->prepare('SELECT * FROM user_outfits WHERE userid = :uid AND id = :id');
$outfit->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfit->bindParam(":id", $outfitid, PDO::PARAM_INT);
$outfit->execute();
if ($outfit->rowCount() > 0)
{
return true;
}
return false;
}
function isThumbHashInOutfit($thumbhash)
{
$outfit = $GLOBALS['pdo']->prepare('SELECT * FROM user_outfits WHERE ThumbHash = :hash');
$outfit->bindParam(":hash", $thumbhash, PDO::PARAM_STR);
$outfit->execute();
if ($outfit->rowCount() > 0 || $thumbhash == $GLOBALS['defaultOutfitHash']) //default outfit hash
{
return true;
}
return false;
}
function isHeadshotThumbHashInOutfit($thumbhash)
{
$outfit = $GLOBALS['pdo']->prepare('SELECT * FROM user_outfits WHERE HeadshotThumbHash = :hash');
$outfit->bindParam(":hash", $thumbhash, PDO::PARAM_STR);
$outfit->execute();
if ($outfit->rowCount() > 0 || $thumbhash == $GLOBALS['defaultHeadshotHash']) //default headshot hash
{
return true;
}
return false;
}
function createOutfit($name, $userid)
{
$name = cleanInput($name);
if (strlen($name) > 50)
{
return "Name too long";
}
else if (strlen($name) < 3)
{
return "Name too short";
}
else
{
if (playerOutfitsCount($userid) < 24)
{
if (!checkUserPendingRender($userid))
{
//queries
$hash = $GLOBALS['pdo']->prepare("SELECT * FROM users WHERE id = " . $userid);
$hash->execute();
$hash = $hash->fetch(PDO::FETCH_OBJ);
$headshothash = $hash->HeadshotThumbHash;
$headshotAngelRight = $hash->headshotAngleRight;
$headshotAngleLeft = $hash->headshotAngleLeft;
$hash = $hash->ThumbHash;
$wearingcolors = $GLOBALS['pdo']->prepare('SELECT * FROM body_colours WHERE uid = ' . $userid);
$wearingcolors->execute();
$wearingcolors = $wearingcolors->fetch(PDO::FETCH_OBJ);
//users current body colors
$head = (int)$wearingcolors->h;
$torso = (int)$wearingcolors->t;
$leftarm = (int)$wearingcolors->la;
$rightarm = (int)$wearingcolors->ra;
$leftleg = (int)$wearingcolors->ll;
$rightleg = (int)$wearingcolors->rl;
//currently wearing items
$assets = wearingAssets($userid);
//add to db
$outfit = $GLOBALS['pdo']->prepare("INSERT INTO user_outfits(userid, assets, name, h, t, la, ra, ll, rl, headshotAngleRight, headshotAngleLeft, ThumbHash, HeadshotThumbHash, whenCreated) VALUES (:uid, :assets, :name, :h, :t, :la, :ra, :ll, :rl, :har, :hal, :th, :hth, UNIX_TIMESTAMP())");
$outfit->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfit->bindParam(":assets", $assets, PDO::PARAM_STR);
$outfit->bindParam(":name", $name, PDO::PARAM_STR);
$outfit->bindParam(":h", $head, PDO::PARAM_INT);
$outfit->bindParam(":t", $torso, PDO::PARAM_INT);
$outfit->bindParam(":la", $leftarm, PDO::PARAM_INT);
$outfit->bindParam(":ra", $rightarm, PDO::PARAM_INT);
$outfit->bindParam(":ll", $leftleg, PDO::PARAM_INT);
$outfit->bindParam(":rl", $rightleg, PDO::PARAM_INT);
$outfit->bindParam(":har", $headshotAngelRight, PDO::PARAM_INT);
$outfit->bindParam(":hal", $headshotAngleLeft, PDO::PARAM_INT);
$outfit->bindParam(":th", $hash, PDO::PARAM_STR);
$outfit->bindParam(":hth", $headshothash, PDO::PARAM_STR);
$outfit->execute();
return true;
}
return "Please wait for the current render";
}
return "Limit of 24 outfits";
}
}
function deleteOutfit($userid, $outfitid)
{
if (playerOwnsOutfit($userid, $outfitid))
{
$delete = $GLOBALS['pdo']->prepare("DELETE from user_outfits WHERE userid = :uid AND id = :id");
$delete->bindParam(":uid", $userid, PDO::PARAM_INT);
$delete->bindParam(":id", $outfitid, PDO::PARAM_INT);
$delete->execute();
if ($delete->rowCount() > 0)
{
return true;
}
}
return "Error occurred";
}
function updateOutfit($userid, $outfitid, $name)
{
$name = cleanInput($name);
if (strlen($name) > 50)
{
return "Name too long";
}
else if (strlen($name) < 3)
{
return "Name too short";
}
else
{
if (playerOwnsOutfit($userid, $outfitid))
{
if (!checkUserPendingRender($userid))
{
if (deleteOutfit($userid, $outfitid) === TRUE);
{
createOutfit($name, $userid);
return true;
}
return "Failed to update outfit, contact an Administrator";
}
return "Please wait for the current render";
}
return "Error occurred";
}
}
function applyOutfit($userid, $outfitid)
{
if (playerOwnsOutfit($userid, $outfitid))
{
if (!checkUserPendingRender($userid))
{
$outfit = $GLOBALS['pdo']->prepare('SELECT * FROM user_outfits WHERE userid = :uid AND id = :id');
$outfit->bindParam(":uid", $userid, PDO::PARAM_INT);
$outfit->bindParam(":id", $outfitid, PDO::PARAM_INT);
$outfit->execute();
if ($outfit->rowCount() > 0)
{
//vars
$outfit = $outfit->fetch(PDO::FETCH_OBJ);
$outfitassets = explode(";", $outfit->assets);
//outfit body colors
$outfithead = (int)$outfit->h;
$outfittorso = (int)$outfit->t;
$outfitleftarm = (int)$outfit->la;
$outfitrightarm = (int)$outfit->ra;
$outfitleftleg = (int)$outfit->ll;
$outfitrightleg = (int)$outfit->rl;
//headshot settings
$headshotAngelRight = $outfit->headshotAngleRight;
$headshotAngleLeft = $outfit->headshotAngleLeft;
//apply outfit body colors
$bodycolor = $GLOBALS['pdo']->prepare("UPDATE body_colours SET h = :h, t = :t, la = :la, ra = :ra, ll = :ll, rl = :rl WHERE uid = :uid");
$bodycolor->bindParam(":h", $outfithead, PDO::PARAM_INT);
$bodycolor->bindParam(":t", $outfittorso, PDO::PARAM_INT);
$bodycolor->bindParam(":la", $outfitleftarm, PDO::PARAM_INT);
$bodycolor->bindParam(":ra", $outfitrightarm, PDO::PARAM_INT);
$bodycolor->bindParam(":ll", $outfitleftleg, PDO::PARAM_INT);
$bodycolor->bindParam(":rl", $outfitrightleg, PDO::PARAM_INT);
$bodycolor->bindParam(":uid", $userid, PDO::PARAM_INT);
$bodycolor->execute();
//delete all wearing items
$deequip = $GLOBALS['pdo']->prepare("DELETE from wearing_items WHERE uid = :u"); //delete all wearing
$deequip->bindParam(":u", $userid, PDO::PARAM_INT);
$deequip->execute();
//apply items in the outfit
foreach($outfitassets as $asset)
{
if ($asset != "") //hack for outfits with no wearing items
{
$equip = $GLOBALS['pdo']->prepare("INSERT INTO wearing_items(uid,aid,whenWorn) VALUES(:u,:a,UNIX_TIMESTAMP())");
$equip->bindParam(":u", $userid, PDO::PARAM_INT);
$equip->bindParam(":a", $asset, PDO::PARAM_INT);
$equip->execute();
}
}
//delete current render and headshot if its not part of an outfit
$prevhash = $GLOBALS['pdo']->prepare("SELECT * FROM users WHERE id = :i");
$prevhash->bindParam(":i", $userid, PDO::PARAM_INT);
$prevhash->execute();
$prevhash = $prevhash->fetch(PDO::FETCH_OBJ);
$oldhash = $prevhash->ThumbHash;
$oldheadshothash = $prevhash->HeadshotThumbHash;
if (!isThumbHashInOutfit($oldhash))
{
unlink($GLOBALS['renderCDNPath'] . $oldhash);
}
if (!isHeadshotThumbHashInOutfit($oldheadshothash))
{
unlink($GLOBALS['renderCDNPath'] . $oldheadshothash);
}
//outfits hashes
$hash = $outfit->ThumbHash;
$headshothash = $outfit->HeadshotThumbHash;
if ($headshothash == NULL) //outfit was created before headshots release (probably?)
{
UsersRender::RenderPlayerCloseup($userid);
$headshothash = userInfo($userid)->HeadshotThumbHash;
$update = $GLOBALS['pdo']->prepare('UPDATE user_outfits SET HeadshotThumbHash = :hhash WHERE id = :oid');
$update->bindParam(":hhash", $headshothash, PDO::PARAM_STR);
$update->bindParam(":oid", $outfitid, PDO::PARAM_INT);
$update->execute();
}
//apply the outfit (yay less render server load)
$user = $GLOBALS['pdo']->prepare('UPDATE users SET ThumbHash = :hash, HeadshotThumbHash = :hhash, headshotAngleRight = :har, headshotAngleLeft = :hal WHERE id = ' . $userid);
$user->bindParam(":hash", $hash, PDO::PARAM_STR);
$user->bindParam(":hhash", $headshothash, PDO::PARAM_STR);
$user->bindParam(":har", $headshotAngelRight, PDO::PARAM_INT);
$user->bindParam(":hal", $headshotAngleLeft, PDO::PARAM_INT);
$user->execute();
return true;
}
}
return "Please wait for the current render";
}
return "Error occurred";
}
//place launcher queue system
function removePlayerFromQueue($userid)

View File

@ -6,6 +6,9 @@ Alphaland 2021
*/
//headers
use Alphaland\Users\Outfit;
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
header("access-control-allow-credentials: true");
@ -18,10 +21,12 @@ if (!$data)
http_response_code(400);
}
else
{
$applyoutfit = applyOutfit($user->id, $id);
if ($applyoutfit === true) {
$applyoutfit = "Outfit Applied";
{ try {
if (Outfit::ApplyOutfit($user->id, $id)) {
$applyoutfit = "Outfit Applied";
}
} catch (Exception $e) {
$applyoutfit = $e->getMessage();
}
header('Content-Type: application/json');

View File

@ -6,6 +6,9 @@ Alphaland 2021
*/
//headers
use Alphaland\Users\Outfit;
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
header("access-control-allow-credentials: true");
@ -19,9 +22,12 @@ if (!$data)
}
else
{
$createoutfit = createOutfit($name, $user->id);
if ($createoutfit === true) {
$createoutfit = "Outfit Created";
try {
if (Outfit::CreateOutfit($name, $user->id)) {
$createoutfit = "Outfit Created";
}
} catch (Exception $e) {
$createoutfit = $e->getMessage();
}
header('Content-Type: application/json');

View File

@ -6,6 +6,9 @@ Alphaland 2021
*/
//headers
use Alphaland\Users\Outfit;
header("Access-Control-Allow-Origin: https://www.alphaland.cc");
header("access-control-allow-credentials: true");
@ -26,18 +29,24 @@ else
$outfitid = $data->id;
$name = $data->name;
$outfitchange = updateOutfit($user->id, $outfitid, $name);
if ($outfitchange === true) {
$outfitchange = "Outfit Updated";
try {
if (Outfit::UpdateOutfit($user->id, $id, $name)) {
$outfitchange = "Outfit Updated";
}
} catch (Exception $e) {
$outfitchange = $e->getMessage();
}
}
else if ($deleteoutfit)
{
$outfitid = $data->id;
$outfitchange = deleteOutfit($user->id, $outfitid);
if ($outfitchange === true) {
$outfitchange = "Outfit Deleted";
try {
if (Outfit::DeleteOutfit($user->id, $outfitid)) {
$outfitchange = "Outfit Deleted";
}
} catch (Exception $e) {
$outfitchange = $e->getMessage();
}
}