async def update_stocks(id_server=None): if id_server: exchange_data = EwDistrict(district=ewcfg.poi_id_stockexchange, id_server=id_server) resp_cont = EwResponseContainer(ewcfg.get_client(), id_server=id_server) for stock in ewcfg.stocks: s = EwStock(id_server, stock) # we don't update stocks when they were just added # or when shamblers have degraded it if s.timestamp != 0 and not exchange_data.is_degraded(): s.timestamp = int(time.time()) market_response = await stock_market_tick(s, id_server) resp_cont.add_channel_response(ewcfg.channel_stockexchange, market_response) await resp_cont.post()
async def withdraw(cmd): user_data = EwUser(member=cmd.message.author) if user_data.life_state == ewcfg.life_state_shambler: response = "You lack the higher brain functions required to {}.".format(cmd.tokens[0]) return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) time_now = round(time.time()) market_data = EwMarket(id_server=cmd.message.author.guild.id) if market_data.clock < 6 or market_data.clock >= 20: response = ewcfg.str_exchange_closed return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) if cmd.message.channel.name != ewcfg.channel_stockexchange: # or user_data.poi != ewcfg.poi_id_downtown: # Only allowed in the stock exchange. response = ewcfg.str_exchange_channelreq.format(currency="SlimeCoin", action="withdraw") return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) poi = poi_static.id_to_poi.get(user_data.poi) district_data = EwDistrict(district=poi.id_poi, id_server=user_data.id_server) if district_data.is_degraded(): response = "{} has been degraded by shamblers. You can't {} here anymore.".format(poi.str_name, cmd.tokens[0]) return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) if user_data.life_state == ewcfg.life_state_corpse: # Disallow withdraws from ghosts. response = "Your slimebroker can't confirm your identity while you're dead." return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) else: value = None stock = None if cmd.tokens_count > 1: value = ewutils.getIntToken(cmd.tokens[1:], allow_all=True) for token in cmd.tokens[1:]: if token.lower() in ewcfg.stocks: stock = token.lower() break if stock != None: stock = EwStock(id_server=cmd.guild.id, stock=stock) total_shares = market_utils.getUserTotalShares(id_server=user_data.id_server, stock=stock.id_stock, id_user=user_data.id_user) if value != None: if value < 0: value = total_shares if value <= 0: value = None if value != None: if value <= total_shares: exchange_rate = (stock.exchange_rate / 1000.0) shares = value slimecoin = round(value * exchange_rate) if user_data.time_lastinvest + ewcfg.cd_invest > time_now: # Limit frequency of withdrawals response = ewcfg.str_exchange_busy.format(action="withdraw") else: user_data.change_slimecoin(n=slimecoin, coinsource=ewcfg.coinsource_withdraw) total_shares -= shares user_data.time_lastinvest = time_now stock.total_shares -= shares response = "You exchange {shares:,} shares in {stock} for {coins:,} SlimeCoin.".format(coins=slimecoin, shares=shares, stock=ewcfg.stock_names.get(stock.id_stock)) user_data.persist() stock.timestamp = round(time.time()) stock.persist() market_utils.updateUserTotalShares(id_server=user_data.id_server, stock=stock.id_stock, id_user=user_data.id_user, shares=total_shares) else: response = "You don't have that many shares in {stock} to exchange.".format(stock=ewcfg.stock_names.get(stock.id_stock)) else: response = ewcfg.str_exchange_specify.format(currency="SlimeCoin", action="withdraw") else: response = "That's not a valid stock name, please use a proper one, you c**t: {}".format(ewutils.formatNiceList(names=ewcfg.stocks)) await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response))
async def invest(cmd): user_data = EwUser(member=cmd.message.author) if user_data.life_state == ewcfg.life_state_shambler: response = "You lack the higher brain functions required to {}.".format(cmd.tokens[0]) return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) time_now = round(time.time()) market_data = EwMarket(id_server=cmd.message.author.guild.id) if cmd.message.channel.name != ewcfg.channel_stockexchange: # or user_data.poi != ewcfg.poi_id_downtown: # Only allowed in the stock exchange. response = ewcfg.str_exchange_channelreq.format(currency="SlimeCoin", action="invest") return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) poi = poi_static.id_to_poi.get(user_data.poi) district_data = EwDistrict(district=poi.id_poi, id_server=user_data.id_server) if district_data.is_degraded(): response = "{} has been degraded by shamblers. You can't {} here anymore.".format(poi.str_name, cmd.tokens[0]) return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) if market_data.clock < 6 or market_data.clock >= 20: response = ewcfg.str_exchange_closed return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) if user_data.time_lastinvest + ewcfg.cd_invest > time_now: # Limit frequency of investments. response = ewcfg.str_exchange_busy.format(action="invest") return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) if user_data.life_state == ewcfg.life_state_corpse: # Disallow invests from ghosts. response = "Your slimebroker can't confirm your identity while you're dead." return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) if user_data.life_state == ewcfg.life_state_kingpin: # Disallow investments by RF and CK kingpins. response = "You need that money to buy more videogames." return await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response)) else: value = None stock = None if cmd.tokens_count > 1: value = ewutils.getIntToken(cmd.tokens, allow_all=True) for token in cmd.tokens[1:]: if token.lower() in ewcfg.stocks: stock = token.lower() break if value != None: if value < 0: value = user_data.slimecoin if value <= 0: value = None if value != None: if stock != None: stock = EwStock(id_server=cmd.guild.id, stock=stock) # basic exchange rate / 1000 = 1 share exchange_rate = (stock.exchange_rate / 1000.0) cost_total = round(value * 1.05) # gets the highest value possible where the player can still pay the fee if value == user_data.slimecoin: while cost_total > user_data.slimecoin: value -= cost_total - value cost_total = round(value * 1.05) # The user can only buy a whole number of shares, so adjust their cost based on the actual number of shares purchased. net_shares = round(value / exchange_rate) if user_data.slimecoin < cost_total: response = "You don't have enough SlimeCoin. ({:,}/{:,})".format(user_data.slimecoin, cost_total) elif value > user_data.slimecoin: response = "You don't have that much SlimeCoin to invest." elif net_shares == 0: response = "You don't have enough SlimeCoin to buy a share in {stock}".format(stock=ewcfg.stock_names.get(stock.id_stock)) else: user_data.change_slimecoin(n=-cost_total, coinsource=ewcfg.coinsource_invest) shares = market_utils.getUserTotalShares(id_server=user_data.id_server, stock=stock.id_stock, id_user=user_data.id_user) shares += net_shares market_utils.updateUserTotalShares(id_server=user_data.id_server, stock=stock.id_stock, id_user=user_data.id_user, shares=shares) user_data.time_lastinvest = time_now stock.total_shares += net_shares response = "You invest {coin:,} SlimeCoin and receive {shares:,} shares in {stock}. Your slimebroker takes his nominal fee of {fee:,} SlimeCoin.".format(coin=value, shares=net_shares, stock=ewcfg.stock_names.get(stock.id_stock), fee=(cost_total - value)) user_data.persist() stock.timestamp = round(time.time()) stock.persist() else: response = "That's not a valid stock name, please use a proper one, you c**t: {}".format(ewutils.formatNiceList(names=ewcfg.stocks)) else: response = ewcfg.str_exchange_specify.format(currency="SlimeCoin", action="invest") # Send the response to the player. await fe_utils.send_message(cmd.client, cmd.message.channel, fe_utils.formatMessage(cmd.message.author, response))