From 29be2dbb3e29c96b200e64ebc2a29b3f2a180931 Mon Sep 17 00:00:00 2001
From: DarkGod <darkgod@net-core.org>
Date: Sun, 14 Jan 2018 18:42:10 +0100
Subject: [PATCH] plop

---
 game/engines/default/engine/PlayerProfile.lua | 12 ++++++++-
 .../dialogs/microtxn/ShowPurchasable.lua      | 26 +++++++++++++------
 game/modules/tome/data/microtxn/poosh.lua     |  2 +-
 game/profile-thread/Client.lua                | 21 ++++++++++++++-
 4 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/game/engines/default/engine/PlayerProfile.lua b/game/engines/default/engine/PlayerProfile.lua
index 7829743979..4d17ca1038 100644
--- a/game/engines/default/engine/PlayerProfile.lua
+++ b/game/engines/default/engine/PlayerProfile.lua
@@ -76,6 +76,7 @@ function _M:init()
 	self.chat = UserChat.new()
 	self.dlc_files = {classes={}, files={}}
 	self.saved_events = {}
+	self.temporary_event_handlers = {}
 	self.generic = {}
 	self.modules = {}
 	self.evt_cbs = {}
@@ -628,11 +629,20 @@ function _M:eventFunFacts(e)
 	end
 end
 
+function _M:registerTemporaryEventHandler(name, fct)
+	self.temporary_event_handlers[name] = self.temporary_event_handlers[name] or {}
+	table.insert(self.temporary_event_handlers[name], fct)
+end
+
 --- Got an event from the profile thread
 function _M:handleEvent(e)
 	if type(e) == "string" then e = e:unserialize() end
 	if not e then return end
-	if self["event"..e.e] then self["event"..e.e](self, e) end
+	if self["event"..e.e] then self["event"..e.e](self, e)
+	elseif self.temporary_event_handlers[e.e] then
+		for _, fct in ipairs(self.temporary_event_handlers[e.e]) do print("[PROFILE] temporary_event_handlers", e.e, pcall(fct, e)) end
+		self.temporary_event_handlers[e.e] = nil
+	end
 	return e
 end
 
diff --git a/game/engines/default/engine/dialogs/microtxn/ShowPurchasable.lua b/game/engines/default/engine/dialogs/microtxn/ShowPurchasable.lua
index 6a576451a5..20fb610319 100644
--- a/game/engines/default/engine/dialogs/microtxn/ShowPurchasable.lua
+++ b/game/engines/default/engine/dialogs/microtxn/ShowPurchasable.lua
@@ -34,6 +34,7 @@ function _M:init(mode)
 
 	self:generateList()
 
+	self.c_waiter = Textzone.new{auto_width=1, auto_height=1, text="#YELLOW#-- connecting to server... --"}
 	self.c_list = ListColumns.new{width=self.iw, 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)
@@ -43,9 +44,11 @@ function _M:init(mode)
 
 
 	self:loadUI{
+		{vcenter=0, hcenter=0, ui=self.c_waiter},
 		{left=0, top=0, ui=self.c_list},
 	}
 	self:setupUI(false, false)
+	self:toggleDisplay(self.c_list, false)
 
 	self.key:addBinds{
 		ACCEPT = "EXIT",
@@ -57,16 +60,23 @@ function _M:init(mode)
 end
 
 function _M:generateList()
-	local list = {}
-	for file in fs.iterate("/data/microtxn/", "%.lua$") do
-		local f, err = loadfile("/data/microtxn/"..file)
-		setfenv(f, {mode=self.mode})
-		local ok, res = pcall(f)
-		if ok and res then
+	self.list = {}
+
+	profile:registerTemporaryEventHandler("MicroTxnListPurchasables", function(e)
+		if not e.data then return end
+		e.data = e.data:unserialize()
+		local list = {}
+		for _, res in ipairs(e.data.list) do
 			res.img = Entity.new{image=res.image}
 			res.txt = Textzone.new{width=self.iw - 10 - 132, auto_height=true, text=("%s\n#SLATE##{italic}#%s#{normal}#"):format(res.name, res.desc)}
 			list[#list+1] = res
 		end
-	end
-	self.list = list
+		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)
+		game.log("===balance: %s", tostring(e.data.balance))
+	end)
+	core.profile.pushOrder(string.format("o='MicroTxn' suborder='list_purchasables' module=%q", game.__mod_info.short_name))
 end
diff --git a/game/modules/tome/data/microtxn/poosh.lua b/game/modules/tome/data/microtxn/poosh.lua
index 6649b34e89..fe31f5a00a 100644
--- a/game/modules/tome/data/microtxn/poosh.lua
+++ b/game/modules/tome/data/microtxn/poosh.lua
@@ -22,7 +22,7 @@ return {
 	name = "Lost Land of Poosh (server-wide)",
 	image = "/data/gfx/achievements/land_poosh.png",
 	desc = [[The Bearscape is a level 15+ zone that spawns on the worldmap.
-Poosh has gone missing! But what is push? Who stole it? And most importantly, what deliciously terrible deaths will it give your character?
+Poosh has gone missing! But what is poosh? Who stole it? And most importantly, what deliciously terrible deaths will it give your character?
 Will you survive and claim your rewards? Or will you die forever lost in a lost land?
 #GREEN#Server-wide:#LAST# This event will trigger for your character and every other player currently playing.]],
 
diff --git a/game/profile-thread/Client.lua b/game/profile-thread/Client.lua
index c3ab20f1b1..a5e8d7015c 100644
--- a/game/profile-thread/Client.lua
+++ b/game/profile-thread/Client.lua
@@ -23,7 +23,7 @@ local UserChat = require "profile-thread.UserChat"
 
 module(..., package.seeall, class.make)
 
-local debug = false
+local debug = true
 
 local metaport = 2240
 local profilehost = "profiles.te4.org"
@@ -744,6 +744,25 @@ function _M:orderAddonAuthoring(o)
 		end
 	end
 end
+
+function _M:orderMicroTxn(o)
+	if o.suborder == "list_purchasables" then
+		self:command("MTXN LIST_PURCHASABLE", o.module)
+		if self:read("200") then
+			local _, _, size = self.last_line:find("^([0-9]+)")
+			size = tonumber(size)
+			local body = {}
+			if size and size > 1 then
+				body = self:receive(size)
+				if body then body = zlib.decompress(body) end
+			end
+
+			cprofile.pushEvent(("e='MicroTxnListPurchasables' data=%q"):format(body))
+			return
+		end
+	end
+end
+
 --------------------------------------------------------------------
 -- Pushes comming from the push socket
 --------------------------------------------------------------------
-- 
GitLab