2022-09-02 23:58:55 +00:00
from files . __main__ import app
from files . classes . hats import *
from files . helpers . alerts import *
from files . helpers . wrappers import *
from files . helpers . const import *
2022-09-03 00:52:59 +00:00
from files . helpers . actions import *
2022-09-02 23:58:55 +00:00
from flask import g
@app.get ( " /hats " )
@auth_required
2022-10-11 07:29:24 +00:00
@feature_required ( ' HATS ' )
2022-09-02 23:58:55 +00:00
def hats ( v ) :
2022-09-03 19:11:02 +00:00
owned_hat_ids = [ x . hat_id for x in v . owned_hats ]
2022-09-02 23:58:55 +00:00
2022-09-05 08:27:37 +00:00
if request . values . get ( " sort " ) == ' author_asc ' :
2022-09-10 05:42:24 +00:00
hats = g . db . query ( HatDef , User ) . join ( HatDef . author ) . filter ( HatDef . submitter_id == None ) . order_by ( User . username ) . all ( )
2022-09-05 08:27:37 +00:00
elif request . values . get ( " sort " ) == ' author_desc ' :
2022-09-10 05:42:24 +00:00
hats = g . db . query ( HatDef , User ) . join ( HatDef . author ) . filter ( HatDef . submitter_id == None ) . order_by ( User . username . desc ( ) ) . all ( )
2022-09-03 19:11:02 +00:00
else :
2022-09-05 08:27:37 +00:00
if v . equipped_hat_ids :
2022-09-10 05:42:24 +00:00
equipped = g . db . query ( HatDef , User ) . join ( HatDef . author ) . filter ( HatDef . submitter_id == None , HatDef . id . in_ ( owned_hat_ids ) , HatDef . id . in_ ( v . equipped_hat_ids ) ) . order_by ( HatDef . price , HatDef . name ) . all ( )
not_equipped = g . db . query ( HatDef , User ) . join ( HatDef . author ) . filter ( HatDef . submitter_id == None , HatDef . id . in_ ( owned_hat_ids ) , HatDef . id . notin_ ( v . equipped_hat_ids ) ) . order_by ( HatDef . price , HatDef . name ) . all ( )
2022-09-05 08:27:37 +00:00
owned = equipped + not_equipped
else :
2022-09-10 05:42:24 +00:00
owned = g . db . query ( HatDef , User ) . join ( HatDef . author ) . filter ( HatDef . submitter_id == None , HatDef . id . in_ ( owned_hat_ids ) ) . order_by ( HatDef . price , HatDef . name ) . all ( )
2022-09-05 08:27:37 +00:00
2022-09-10 05:42:24 +00:00
not_owned = g . db . query ( HatDef , User ) . join ( HatDef . author ) . filter ( HatDef . submitter_id == None , HatDef . id . notin_ ( owned_hat_ids ) ) . order_by ( HatDef . price , HatDef . name ) . all ( )
2022-09-05 08:27:37 +00:00
hats = owned + not_owned
2022-09-03 19:11:02 +00:00
2022-09-03 22:32:46 +00:00
sales = g . db . query ( func . sum ( User . coins_spent_on_hats ) ) . scalar ( )
2022-09-10 05:42:24 +00:00
num_of_hats = g . db . query ( HatDef ) . filter ( HatDef . submitter_id == None ) . count ( )
2022-09-08 18:36:43 +00:00
return render_template ( " hats.html " , owned_hat_ids = owned_hat_ids , hats = hats , v = v , sales = sales , num_of_hats = num_of_hats )
2022-09-02 23:58:55 +00:00
@app.post ( " /buy_hat/<hat_id> " )
@auth_required
2022-10-11 07:29:24 +00:00
@feature_required ( ' HATS ' )
2022-09-02 23:58:55 +00:00
def buy_hat ( v , hat_id ) :
try : hat_id = int ( hat_id )
2022-10-11 13:01:39 +00:00
except : abort ( 404 , " Hat not found! " )
2022-09-02 23:58:55 +00:00
2022-09-10 05:42:24 +00:00
hat = g . db . query ( HatDef ) . filter_by ( submitter_id = None , id = hat_id ) . one_or_none ( )
2022-10-11 13:01:39 +00:00
if not hat : abort ( 404 , " Hat not found! " )
2022-09-02 23:58:55 +00:00
2022-09-03 03:58:43 +00:00
existing = g . db . query ( Hat ) . filter_by ( user_id = v . id , hat_id = hat . id ) . one_or_none ( )
2022-10-11 13:01:39 +00:00
if existing : abort ( 400 , " You already own this hat! " )
2022-09-03 03:58:43 +00:00
2022-09-02 23:58:55 +00:00
if request . values . get ( " mb " ) :
2022-09-16 08:58:12 +00:00
charged = v . charge_account ( ' procoins ' , hat . price )
2022-10-11 13:01:39 +00:00
if not charged : abort ( 400 , " Not enough marseybux. " )
2022-09-16 08:58:12 +00:00
2022-09-03 19:53:38 +00:00
hat . author . procoins + = hat . price * 0.1
2022-09-02 23:58:55 +00:00
currency = " marseybux "
else :
2022-09-16 08:59:15 +00:00
charged = v . charge_account ( ' coins ' , hat . price )
2022-10-11 13:01:39 +00:00
if not charged : abort ( 400 , " Not enough coins. " )
2022-09-16 08:58:12 +00:00
2022-09-02 23:58:55 +00:00
v . coins_spent_on_hats + = hat . price
2022-09-03 19:53:38 +00:00
hat . author . coins + = hat . price * 0.1
2022-09-02 23:58:55 +00:00
currency = " coins "
new_hat = Hat ( user_id = v . id , hat_id = hat . id )
g . db . add ( new_hat )
g . db . add ( v )
g . db . add ( hat . author )
2022-09-05 07:59:24 +00:00
send_repeatable_notification (
hat . author . id ,
f " :marseycapitalistmanlet: @ { v . username } has just bought ` { hat . name } `, you have received your 10% cut ( { int ( hat . price * 0.1 ) } { currency } ) :!marseycapitalistmanlet: "
)
2022-09-02 23:58:55 +00:00
2022-09-16 21:57:56 +00:00
if v . num_of_owned_hats > = 250 :
2022-09-03 00:54:17 +00:00
badge_grant ( user = v , badge_id = 154 )
2022-09-16 21:57:56 +00:00
elif v . num_of_owned_hats > = 100 :
2022-09-03 00:54:17 +00:00
badge_grant ( user = v , badge_id = 153 )
2022-09-16 21:57:56 +00:00
elif v . num_of_owned_hats > = 25 :
2022-09-03 00:54:17 +00:00
badge_grant ( user = v , badge_id = 152 )
2022-09-03 00:18:10 +00:00
2022-09-11 14:32:00 +00:00
return { " message " : f " ' { hat . name } ' bought! " }
2022-09-02 23:58:55 +00:00
@app.post ( " /equip_hat/<hat_id> " )
@auth_required
2022-10-11 07:29:24 +00:00
@feature_required ( ' HATS ' )
2022-09-02 23:58:55 +00:00
def equip_hat ( v , hat_id ) :
try : hat_id = int ( hat_id )
2022-10-11 13:01:39 +00:00
except : abort ( 404 , " Hat not found! " )
2022-09-02 23:58:55 +00:00
hat = g . db . query ( Hat ) . filter_by ( hat_id = hat_id , user_id = v . id ) . one_or_none ( )
2022-10-11 13:01:39 +00:00
if not hat : abort ( 403 , " You don ' t own this hat! " )
2022-09-02 23:58:55 +00:00
2022-09-05 03:44:24 +00:00
hat . equipped = True
g . db . add ( hat )
2022-09-02 23:58:55 +00:00
2022-09-11 14:32:00 +00:00
return { " message " : f " ' { hat . name } ' equipped! " }
2022-09-02 23:58:55 +00:00
2022-09-05 03:44:24 +00:00
@app.post ( " /unequip_hat/<hat_id> " )
2022-09-02 23:58:55 +00:00
@auth_required
2022-10-11 07:29:24 +00:00
@feature_required ( ' HATS ' )
2022-09-05 03:44:24 +00:00
def unequip_hat ( v , hat_id ) :
try : hat_id = int ( hat_id )
2022-10-11 13:01:39 +00:00
except : abort ( 404 , " Hat not found! " )
2022-09-05 03:44:24 +00:00
hat = g . db . query ( Hat ) . filter_by ( hat_id = hat_id , user_id = v . id ) . one_or_none ( )
2022-10-11 13:01:39 +00:00
if not hat : abort ( 403 , " You don ' t own this hat! " )
2022-09-05 03:44:24 +00:00
hat . equipped = False
g . db . add ( hat )
2022-09-02 23:58:55 +00:00
2022-09-11 14:32:00 +00:00
return { " message " : f " ' { hat . name } ' unequipped! " }
2022-09-03 19:36:50 +00:00
@app.get ( " /hat_owners/<hat_id> " )
@auth_required
def hat_owners ( v , hat_id ) :
try : hat_id = int ( hat_id )
except : abort ( 400 )
try : page = int ( request . values . get ( " page " , 1 ) )
except : page = 1
users = [ x [ 1 ] for x in g . db . query ( Hat , User ) . join ( Hat . owners ) . filter ( Hat . hat_id == hat_id ) . offset ( 25 * ( page - 1 ) ) . limit ( 26 ) . all ( ) ]
next_exists = ( len ( users ) > 25 )
users = users [ : 25 ]
2022-09-05 20:23:35 +00:00
return render_template ( " user_cards.html " ,
2022-09-04 23:15:37 +00:00
v = v ,
users = users ,
next_exists = next_exists ,
page = page ,
2022-09-29 05:43:29 +00:00
)