ShowPurchasable.lua 19.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
-- TE4 - T-Engine 4
-- Copyright (C) 2009 - 2017 Nicolas Casalini
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Nicolas Casalini "DarkGod"
-- darkgod@te4.org

require "engine.class"
local Module = require "engine.Module"
local Downloader = require "engine.dialogs.Downloader"
local Entity = require "engine.Entity"
local Dialog = require "engine.ui.Dialog"
local Image = require "engine.ui.Image"
local Textzone = require "engine.ui.Textzone"
local TextzoneList = require "engine.ui.TextzoneList"
local ListColumns = require "engine.ui.ListColumns"
local Button = require "engine.ui.Button"
local WebView = require "engine.ui.WebView"

module(..., package.seeall, class.inherit(Dialog))

local bonus_vault_slots_text = "#{italic}##UMBER#Bonus vault slots from this order: #ROYAL_BLUE#%d#{normal}#"
local bonus_vault_slots_tooltip = "For every purchase of #{italic}##GREY#%s#LAST##{normal}# you gain a permanent additional vault slot.\n#GOLD##{italic}#Because why not!#{normal}#"

local coins_balance_text = "#{italic}##UMBER#Voratun Coins available from your donations: #ROYAL_BLUE#%d#{normal}#"
local coins_balance_tooltip = "For every donations you've ever made you have earned voratun coins. These can be spent purchasing expansions or options on the online store. This is the amount you have left, if your purchase total is below this number you'll instantly get your purchase validated, if not you'll need to donate some more first.\n#GOLD##{italic}#Thanks for your support, every little bit helps the game survive for years on!#{normal}#"

function _M:init(mode)
	if not mode then mode = core.steam and "steam" or "te4" end
	self.mode = mode

	self.ui = "microtxn"

	self.cart = {}

	self.base_title_text = game.__mod_info.long_name.." #GOLD#Online Store#LAST#"
	Dialog.init(self, self.base_title_text, game.w * 0.8, game.h * 0.8)

	self.categories_icons = {
		pay2die = Entity.new{image="/data/gfx/mtx/ui/category_pay2die.png"},
		community = Entity.new{image="/data/gfx/mtx/ui/category_community.png"},
		cosmetic = Entity.new{image="/data/gfx/mtx/ui/category_cosmetic.png"},
		misc = Entity.new{image="/data/gfx/mtx/ui/category_misc.png"},
	}
	local in_cart_icon = Entity.new{image="/data/gfx/mtx/ui/in_cart.png"}

	self.list = {}
	self.purchasables = {}
	self.recap = {}

	self.c_waiter = Textzone.new{auto_width=1, auto_height=1, text="#YELLOW#-- connecting to server... --"}
	self.c_list = ListColumns.new{width=self.iw - 350, height=self.ih, item_height=132, hide_columns=true, scrollbar=true, sortable=true, columns={
		{name="", width=100, display_prop="", direct_draw=function(item, x, y)
			item.img:toScreen(nil, x+2, y+2, 128, 128)
			item.category_img:toScreen(nil, x+2+64+32, y+2+64+32, 32, 32)
			if self.cart[item.id_purchasable] and item.nb_purchase > 0 then in_cart_icon:toScreen(nil, x+2, y+2, 128, 128) end
			item.txt:display(x+10+130, y+2 + (128 - item.txt.h) / 2, 0)
		end},
	}, list=self.list, all_clicks=true, fct=function(item, _, button) self:use(item, button) end, select=function(item, sel) self:onSelectItem(item) end}
	self.c_list.on_focus_change = function(_, v) if not v then game:tooltipHide() end end

	self.c_bonus_vault_slots = Textzone.new{has_box=true, width=340, auto_height=1, text=bonus_vault_slots_text:format(0), can_focus=true}
	self.c_bonus_vault_slots.on_focus_change = function(_, v)
		if v then
			local txt = self:getUIElement(self.c_bonus_vault_slots)
			game:tooltipDisplayAtMap(txt.x, txt.y, (bonus_vault_slots_tooltip):format(self:currencyDisplay(2)))
		else
			game:tooltipHide()
		end
	end

	self.c_coins_available = Textzone.new{has_box=true, width=340, auto_height=1, text=coins_balance_text:format(0), can_focus=true}
	self.c_coins_available.on_focus_change = function(_, v)
		if v then
			local txt = self:getUIElement(self.c_coins_available)
			game:tooltipDisplayAtMap(txt.x, txt.y, (coins_balance_tooltip):format())
		else
			game:tooltipHide()
		end
	end

	self.c_do_purchase = Button.new{text="Purchase", fct=function() self:doPurchase() end}

	self.c_recap = ListColumns.new{width=350, height=self.ih - self.c_do_purchase.h - math.max(self.c_bonus_vault_slots.h, self.c_coins_available.h), scrollbar=true, columns={
		{name="Name", width=50, display_prop="recap_name"},
		{name="Price", width=35, display_prop="recap_price"},
		{name="Qty", width=15, display_prop="recap_qty"},
	}, list=self.recap, all_clicks=true, fct=function(item, _, button)
		if item.total then return end
		if button == "left" then button = "right"
		elseif button == "right" then button = "left" end
		self:use(item.item, button)
	end, select=function(item, sel) end}

--	local wv = WebView.new{width=500,height=500, url='https://i.giphy.com/media/uk9A7Mz8Jv4GI/giphy-downsized.gif'}

	local uis = {
		{vcenter=0, hcenter=0, ui=self.c_waiter},
		{left=0, top=0, ui=self.c_list},
		{right=0, top=0, ui=self.c_recap},
		{right=0, bottom=0, ui=self.c_do_purchase},
--		{left=0, top=0, ui=wv},
	}
	-- Only show those for steam as te4.org purchases require already having a donation up
	if mode == "steam" then
		uis[#uis+1] = {right=0, bottom=self.c_do_purchase, ui=self.c_bonus_vault_slots}
	elseif mode == "te4" then
		uis[#uis+1] = {right=0, bottom=self.c_do_purchase, ui=self.c_coins_available}
	end

	self:loadUI(uis)

	self:setupUI(false, false)
	self:toggleDisplay(self.c_list, false)

	self.key:addBinds{
		ACCEPT = "EXIT",
		EXIT = function()
			game:unregisterDialog(self)
			if on_exit then on_exit() end
		end,
	}

	self:checks()

	self:generateList()
end

function _M:checks() game:onTickEnd(function()
	if not profile.auth then
		game:unregisterDialog(self)
		Dialog:simplePopup("Online Store", "You need to be logged in before using the store. Please go back to the main menu and login.")
		return
	end

	if self.mode == "steam" then
		if not profile.auth.steamid then
			game:unregisterDialog(self)
			Dialog:yesnoPopup("Online Store", "Steam users need to link their profiles to their steam account. This is very easy in just a few clicks. Once this is done, simply restart the game.", function(ret) if ret then
				util.browserOpenUrl("https://te4.org/user/"..profile.auth.drupid.."/steam", {is_external=true})
			end end, "Let's do it! (Opens in your browser)", "Not now")
		end
	elseif self.mode == "te4" then
		-- Handle me more smoothly
		if profile.auth.donated < 6 then
			game:unregisterDialog(self)
			Dialog:yesnoPopup("Online Store", "The Online Store (and expansions) are only purchasable by players that bought the game. Plaese go have a look at the donation page for more explanations.", function(ret) if ret then
				util.browserOpenUrl("https://te4.org/donate", {is_external=true})
			end end, "Let's go! (Opens in your browser)", "Not now")
		end
	end
end) end

function _M:onSelectItem(item)
	if self.in_paying_ui then game:tooltipHide() return end
	if not item then return game:tooltipHide() end

	if self.cur_sel_item then self.cur_sel_item.txt.pingpong = nil self.cur_sel_item.txt.scrollbar.pos = 0 end
	item.txt.pingpong = 0

	if item.last_display_x then
		game:tooltipDisplayAtMap(item.last_display_x + self.c_list.w, item.last_display_y, item.tooltip)
	else
		game:tooltipHide()
	end
	self.cur_sel_item = item
end

function _M:use(item, button)
	if self.in_paying_ui then return end
	if not item then return end
	if button == "right" then
		item.nb_purchase = math.max(0, item.nb_purchase - 1)
		if item.nb_purchase <= 0 then self.cart[item.id] = nil end
	elseif button == "left" then
		if item.can_multiple then
			item.nb_purchase = item.nb_purchase + 1
		else
			item.nb_purchase = math.min(1, item.nb_purchase + 1)
		end
		self.cart[item.id] = true
	end

	self:updateCart()
end

function _M:currencyDisplay(v)
	if self.user_currency then
		return ("%0.2f %s"):format(v, self.user_currency)
	else
		return ("%d coins"):format(v)
	end
end

function _M:updateCart()
	local nb_items, total_sum, total_core_sum = 0, 0, 0
	table.empty(self.recap)

	for id, ok in pairs(self.cart) do if ok then
		local item = self.purchasables[id]
		nb_items = nb_items + item.nb_purchase
		total_sum = total_sum + item.nb_purchase * item.price
		total_core_sum = total_core_sum + item.nb_purchase * item.core_price

		self.recap[#self.recap+1] = {
			sort_name = item.name,
			recap_name = item.img:getDisplayString()..item.name,
			recap_price = self:currencyDisplay(item.price * item.nb_purchase),
			recap_qty = item.nb_purchase,
			item = item,
		}
	end end
	table.sort(self.recap, "sort_name")
	self.recap[#self.recap+1] = {
		recap_name = "#{bold}#TOTAL#{normal}#",
		recap_price = self:currencyDisplay(total_sum),
		recap_qty = nb_items,
		total = true,
	}

	self.c_recap:setList(self.recap, true)
	self:updateTitle(self.base_title_text..("  (%d items in cart, %s)"):format(nb_items, self:currencyDisplay(total_sum)))

	self:toggleDisplay(self.c_do_purchase, nb_items > 0)

	self.c_bonus_vault_slots.text = bonus_vault_slots_text:format(total_core_sum / 20 + (profile.auth.donated % 2) / 2)
	self.c_bonus_vault_slots:generate()
end

function _M:doPurchase()
	self.in_paying_ui = true
	if core.steam then self:doPurchaseSteam()
	else self:doPurchaseTE4()
	end
end

function _M:installShimmer(item)
	if not core.webview then
		Dialog:simpleLongPopup(item.name, "In-game browser is inoperant or disabled, impossible to auto-install shimmer pack.\nPlease go to https://te4.org/ to download it manually.", 600)
		return
	end

	-- When download is finished, we will try to load the addon dynamically and add it to the current character. We can do taht because cosmetic addons dont require much setup
	local when_done = function()
		local found = false
		local addons = Module:listAddons(game.__mod_info, true)
		for _, add in ipairs(addons) do if add.short_name == "cosmetic-"..item.effect then
			found = true

			local hooks_list = {}
			Module:loadAddon(game.__mod_info, add, {}, hooks_list)

			dofile("/data/gfx/mtx-shimmers/"..item.effect..".lua")

			Dialog:simplePopup(item.name, [[Shimmer pack installed!]])
			break
		end end

		if not found then
			Dialog:simpleLongPopup(item.name, [[Could not dynamically link addon to current character, maybe the installation weng wrong.
You can fix that by manually downloading the shimmer addon from https://te4.org/ and placing it in game/addons/ folder.]], 600)
		end
	end

	local co co = coroutine.create(function()
		local filename = ("/addons/%s-cosmetic-%s.teaa"):format(game.__mod_info.short_name, item.effect)
		print("==> downloading", "https://te4.org/download-mtx/"..item.id_purchasable, filename)
		local d = Downloader.new{title="Downloading cosmetic pack: #LIGHT_GREEN#"..item.name, co=co, dest=filename..".tmp", url="https://te4.org/download-mtx/"..item.id_purchasable, allow_downloads={addons=true}}
		local ok = d:start()
		if ok then
			local wdir = fs.getWritePath()
			local _, _, dir, name = filename:find("(.+/)([^/]+)$")
			if dir then
				fs.setWritePath(fs.getRealPath(dir))
				fs.delete(name)
				fs.rename(name..".tmp", name)
				fs.setWritePath(wdir)

				when_done()
			end
		end
	end)
	print(coroutine.resume(co))
end

function _M:paymentSuccess()
	self.in_paying_ui = false

	local list = {}
	for id, ok in pairs(self.cart) do if ok then
		local item = self.purchasables[id]
		if item.is_shimmer then
			self:installShimmer(item)
			list[#list+1] = ("- #{bold}##ROYAL_BLUE#%s #SLATE#x%d#WHITE##{normal}#: The pack should be downloading or even finished by now."):format(item.name, item.nb_purchase)
		elseif item.self_event or item.community_event then
			list[#list+1] = ("- #{bold}##ROYAL_BLUE#%s #SLATE#x%d#WHITE##{normal}#: You can now trigger it whenever you are ready."):format(item.name, item.nb_purchase)
		elseif item.effect == "vaultspace" then
			list[#list+1] = ("- #{bold}##ROYAL_BLUE#%s #SLATE#x%d#WHITE##{normal}#: Your available vault space has increased."):format(item.name, item.nb_purchase)
		end
	end end

	game:unregisterDialog(self)
	Dialog:simpleLongPopup("Payment", "Payment accepted.\n"..table.concat(list, "\n"), 700)
end

function _M:paymentFailure()
	self.in_paying_ui = false
end

function _M:paymentCancel()
	self.in_paying_ui = false
end

function _M:doPurchaseSteam()
	local popup = Dialog:simplePopup("Connecting to Steam", "Steam Overlay should appear, if it does not please make sure it you have not disabled it.", nil, true)

	local cart = {}
	for id, ok in pairs(self.cart) do if ok then
		local item = self.purchasables[id]
		cart[#cart+1] = {
			id_purchasable = id,
			nb_purchase = item.nb_purchase,
		}
	end end

	local function onMTXResult(id_cart, ok)
		local finalpopup = Dialog:simplePopup("Connecting to Steam", "Finalizing transaction with Steam servers...", nil, true)
		profile:registerTemporaryEventHandler("MicroTxnSteamFinalizeCartResult", function(e)
			game:unregisterDialog(finalpopup)
			if e.success then
				if e.new_donated then profile.auth.donated = e.new_donated end
				self:paymentSuccess()
			else
				Dialog:simplePopup("Payment", "Payment refused, you have not been billed.")
				self:paymentFailure()
			end
		end)
		core.profile.pushOrder(string.format("o='MicroTxn' suborder='steam_finalize_cart' module=%q store=%q id_cart=%q", game.__mod_info.short_name, "steam", id_cart))
	end

	profile:registerTemporaryEventHandler("MicroTxnListCartResult", function(e)
		game:unregisterDialog(popup)
		if e.success then
			core.steam.waitMTXResult(onMTXResult)
		else
			Dialog:simplePopup("Payment", "Payment refused, you have not been billed.")
			self:paymentFailure()
		end
	end)
	core.profile.pushOrder(string.format("o='MicroTxn' suborder='create_cart' module=%q store=%q cart=%q", game.__mod_info.short_name, "steam", table.serialize(cart)))
end

function _M:doPurchaseTE4()
	local popup = Dialog:simplePopup("Connecting to server", "Please wait...", nil, true)

	local cart = {}
	for id, ok in pairs(self.cart) do if ok then
		local item = self.purchasables[id]
		cart[#cart+1] = {
			id_purchasable = id,
			nb_purchase = item.nb_purchase,
		}
	end end

	local function finalizePurchase(id_cart)
		local finalpopup = Dialog:simplePopup("Connecting to server", "Please wait...", nil, true)
		profile:registerTemporaryEventHandler("MicroTxnTE4FinalizeCartResult", function(e)
			game:unregisterDialog(finalpopup)
			if e.success then
				if e.new_donated then profile.auth.donated = e.new_donated end
				self:paymentSuccess()
			else
				Dialog:simplePopup("Payment", "Payment refused, you have not been billed.")
				self:paymentFailure()
			end
		end)
		core.profile.pushOrder(string.format("o='MicroTxn' suborder='te4_finalize_cart' module=%q store=%q id_cart=%q", game.__mod_info.short_name, "te4", id_cart))
	end

	profile:registerTemporaryEventHandler("MicroTxnListCartResult", function(e)
		game:unregisterDialog(popup)
		if e.success and e.info then
			if e.info:prefix("instant_buy:") then
				local id_cart = tonumber(e.info:sub(13))
				Dialog:yesnoPopup("Online Store", "You have enough coins to instantly purchase those options. Confirm?", function(ret) if ret then
					finalizePurchase(id_cart)
				end end, "Purchase", "Cancel")
			elseif e.info:prefix("requires:") then
				local more = tonumber(e.info:sub(10))
				Dialog:yesnoPopup("Online Store", "You need "..more.." more coins to purchase those options. Do you want to go to the donation page now?", function(ret) if ret then
					util.browserOpenUrl("https://te4.org/donate", {is_external=true})
				end end, "Let's go! (Opens in your browser)", "Not now")
				self:paymentCancel()
			end
		else
			Dialog:simplePopup("Payment", "Payment refused, you have not been billed.")
			self:paymentFailure()
		end
	end)
	core.profile.pushOrder(string.format("o='MicroTxn' suborder='create_cart' module=%q store=%q cart=%q", game.__mod_info.short_name, "te4", table.serialize(cart)))
end

function _M:buildTooltip(item)
	local text = {}
	if item.community_event then
		text[#text+1] = [[#{bold}##GOLD#Community Online Event#WHITE##{normal}#: Once you have purchased a community event you will be able to trigger it at any later date, on whichever character you choose.
Community events once triggered will activate for #{bold}#every player currently logged on#{normal}# including yourself. Every player receiving it will know you sent it and thus that you are to thank for it.
To activate it you will need to have your online events option set to "all" (which is the default value).]]
	end
	if item.self_event then
		text[#text+1] = [[#{bold}##GOLD#Event#WHITE##{normal}#: Once you have purchased an event you will be able to trigger it at any later date, on whichever character you choose.
To activate it you will need to have your online events option set to "all" (which is the default value).]]
	end
	if item.non_immediate then
		text[#text+1] = [[#{bold}##GOLD#Non Immediate#WHITE##{normal}#: This events adds new content that you have to find by exploration. If you die before finding it, there can be no refunds.]]
	end
	if item.once_per_character then
		text[#text+1] = [[#{bold}##GOLD#Once per Character#WHITE##{normal}#: This event can only be received #{bold}#once per character#{normal}#. Usualy because it adds a new zone or effect to the game that would not make sense to duplicate.]]
	end
	if item.is_shimmer then
		text[#text+1] = [[#{bold}##GOLD#Shimmer Pack#WHITE##{normal}#: Once purchased the game will automatically install the shimmer pack to your game and enable it for your current character too (you will still need to use the Mirror of Reflection to switch them on).
#LIGHT_GREEN#Bonus perk:#LAST# purchasing any shimmer pack will also give your characters a portable Mirror of Reflection to be able to change your appearance anywhere, anytime!]]
	end
	if item.effect == "vaultspace" then
		text[#text+1] = [[#{bold}##GOLD#Vault Space#WHITE##{normal}#: Once purchased your vault space is permanently increased.]]
	end
	return table.concat(text, '\n')
end

function _M:generateList()
	profile:registerTemporaryEventHandler("MicroTxnListPurchasables", function(e)
		if e.error then
			Dialog:simplePopup("Online Store", e.error:capitalize())
			game:unregisterDialog(self)
			return
		end

		if not e.data then return end
		e.data = e.data:unserialize()

		if e.data.infos.steam then
			self.user_country = e.data.infos.steam.country
			self.user_currency = e.data.infos.steam.currency
		end

		local list = {}
		for _, res in ipairs(e.data.list) do
			res.id_purchasable = res.id
			res.nb_purchase = 0
			res.img = Entity.new{image=res.image}
			res.category_img = self.categories_icons[res.category or "misc"] or self.categories_icons.misc
			res.txt = TextzoneList.new{width=self.iw - 10 - 132 - 350, height=128, pingpong=20, scrollbar=true}
			res.txt:switchItem(true, ("%s (%s)\n#SLATE##{italic}#%s#{normal}#"):format(res.name, self:currencyDisplay(res.price), res.desc))
			res.txt.pingpong = nil
			res.tooltip = self:buildTooltip(res)
			list[#list+1] = res
			self.purchasables[res.id] = res
		end
		self.list = list
		self.c_list:setList(list)
		self:toggleDisplay(self.c_list, true)
		self:toggleDisplay(self.c_waiter, false)
		self:setFocus(self.c_list)

		self.c_coins_available.text = coins_balance_text:format((e.data.infos.balance or 0) * 10)
		self.c_coins_available:generate()
		self.cur_coins_left = e.data.infos.balance or 0
	end)
	core.profile.pushOrder(string.format("o='MicroTxn' suborder='list_purchasables' module=%q store=%q", game.__mod_info.short_name, core.steam and "steam" or "te4"))
end