From 8d526066dbd6bba763a32c4576f90469d01d5739 Mon Sep 17 00:00:00 2001
From: DarkGod <darkgod@net-core.org>
Date: Thu, 9 Mar 2017 15:58:25 +0100
Subject: [PATCH] checkbox

---
 game/engines/default/engine/PlayerProfile.lua         | 11 ++++++-----
 game/engines/default/modules/boot/class/Game.lua      |  2 +-
 .../default/modules/boot/dialogs/ProfileLogin.lua     |  7 ++++++-
 .../modules/boot/dialogs/ProfileSteamRegister.lua     |  7 ++++++-
 game/profile-thread/Client.lua                        |  7 ++++++-
 5 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/game/engines/default/engine/PlayerProfile.lua b/game/engines/default/engine/PlayerProfile.lua
index 26ca09a867..6278f98e54 100644
--- a/game/engines/default/engine/PlayerProfile.lua
+++ b/game/engines/default/engine/PlayerProfile.lua
@@ -386,10 +386,11 @@ function _M:performlogin(login, pass)
 	end
 end
 
-function _M:performloginSteam(token, name, email)
+function _M:performloginSteam(token, name, email, news)
 	self.steam_token = token
 	self.steam_token_name = name
 	if email then self.steam_token_email = email end
+	if news ~= nil then self.steam_token_news = news end
 	print("[ONLINE PROFILE] attempting log in steam", token)
 	self.auth_tried = nil
 	self:tryAuth()
@@ -579,7 +580,7 @@ function _M:tryAuth()
 	print("[ONLINE PROFILE] auth")
 	self.auth_last_error = nil
 	if self.steam_token then
-		core.profile.pushOrder(table.serialize{o="SteamLogin", token=self.steam_token, name=self.steam_token_name, email=self.steam_token_email})
+		core.profile.pushOrder(table.serialize{o="SteamLogin", token=self.steam_token, name=self.steam_token_name, email=self.steam_token_email, news=self.steam_token_news})
 	else
 		core.profile.pushOrder(table.serialize{o="Login", l=self.login, p=self.pass})
 	end
@@ -861,10 +862,10 @@ function _M:currentCharacter(module, title, uuid)
 	print("[ONLINE PROFILE] current character ", title)
 end
 
-function _M:newProfile(Login, Name, Password, Email)
-	print("[ONLINE PROFILE] profile options ", Login, Email, Name)
+function _M:newProfile(Login, Name, Password, Email, Newsletter)
+	print("[ONLINE PROFILE] profile options ", Login, Email, Name, Newsletter)
 
-	core.profile.pushOrder(table.serialize{o="NewProfile2", login=Login, email=Email, name=Name, pass=Password})
+	core.profile.pushOrder(table.serialize{o="NewProfile2", login=Login, email=Email, name=Name, pass=Password, newsletter=Newsletter and 'yes' or 'no'})
 	local id = nil
 	local reason = nil
 	self:waitEvent("NewProfile2", function(e) id = e.uid reason = e.reason end)
diff --git a/game/engines/default/modules/boot/class/Game.lua b/game/engines/default/modules/boot/class/Game.lua
index 040ed75e03..a966ddd87d 100644
--- a/game/engines/default/modules/boot/class/Game.lua
+++ b/game/engines/default/modules/boot/class/Game.lua
@@ -594,7 +594,7 @@ function _M:createProfile(loginItem)
 	else
 		self.auth_tried = nil
 		local d = Dialog:simpleWaiter("Registering...", "Registering on http://te4.org/, please wait...") core.display.forceRedraw()
-		local ok, err = profile:newProfile(loginItem.login, loginItem.name, loginItem.pass, loginItem.email)
+		local ok, err = profile:newProfile(loginItem.login, loginItem.name, loginItem.pass, loginItem.email, loginItem.news)
 		profile:waitFirstAuth()
 		d:done()
 		if profile.auth then
diff --git a/game/engines/default/modules/boot/dialogs/ProfileLogin.lua b/game/engines/default/modules/boot/dialogs/ProfileLogin.lua
index ad4ed5f7b0..68d09d7217 100644
--- a/game/engines/default/modules/boot/dialogs/ProfileLogin.lua
+++ b/game/engines/default/modules/boot/dialogs/ProfileLogin.lua
@@ -20,6 +20,7 @@
 require "engine.class"
 local Dialog = require "engine.ui.Dialog"
 local Button = require "engine.ui.Button"
+local Checkbox = require "engine.ui.Checkbox"
 local Textbox = require "engine.ui.Textbox"
 local Textzone = require "engine.ui.Textzone"
 
@@ -64,6 +65,8 @@ function _M:init(dialogdef, profile_help_text)
 		self.c_pass = Textbox.new{title="Password: ", size_title=pwa, text="", chars=30, max_len=20, hide=true, filter=pass_filter, fct=function(text) self:okclick() end}
 		self.c_pass2 = Textbox.new{title=pwa, text="", size_title=pwa, chars=30, max_len=20, hide=true, filter=pass_filter, fct=function(text) self:okclick() end}
 		self.c_email = Textbox.new{title="Email: ", size_title=pwa, text="", chars=30, max_len=60, filter=pass_filter, fct=function(text) self:okclick() end}
+		self.c_news = Checkbox.new{title="Accept to receive #{bold}#very infrequent#{normal}# (a few per year) mails", default=true, fct=function() self:okclick() end}
+		self.c_news2 = Textzone.new{text="about important game events from us.", width=self.iw - 20, auto_height=true}
 		local ok = require("engine.ui.Button").new{text="Create", fct=function() self:okclick() end}
 		local cancel = require("engine.ui.Button").new{text="Cancel", fct=function() self:cancelclick() end}
 
@@ -73,6 +76,8 @@ function _M:init(dialogdef, profile_help_text)
 			{left=0, top=self.c_desc.h+self.c_login.h+5, ui=self.c_pass},
 			{left=0, top=self.c_desc.h+self.c_login.h+self.c_pass.h+5, ui=self.c_pass2},
 			{left=0, top=self.c_desc.h+self.c_login.h+self.c_pass.h+self.c_pass2.h+10, ui=self.c_email},
+			{left=0, top=self.c_desc.h+self.c_login.h+self.c_pass.h+self.c_pass2.h+self.c_email.h+10, ui=self.c_news},
+			{left=0, top=self.c_desc.h+self.c_login.h+self.c_pass.h+self.c_pass2.h+self.c_email.h+self.c_news2.h+10, ui=self.c_news2},
 			{left=0, bottom=0, ui=ok},
 			{right=0, bottom=0, ui=cancel},
 		}
@@ -105,7 +110,7 @@ function _M:okclick()
 	end
 
 	game:unregisterDialog(self)
-	game:createProfile({create=self.c_email and true or false, login=self.c_login.text, pass=self.c_pass.text, email=self.c_email and self.c_email.text})
+	game:createProfile({create=self.c_email and true or false, login=self.c_login.text, pass=self.c_pass.text, email=self.c_email and self.c_email.text, news=self.c_news.checked})
 end
 
 function _M:cancelclick()
diff --git a/game/engines/default/modules/boot/dialogs/ProfileSteamRegister.lua b/game/engines/default/modules/boot/dialogs/ProfileSteamRegister.lua
index 90c562617d..6694017dce 100644
--- a/game/engines/default/modules/boot/dialogs/ProfileSteamRegister.lua
+++ b/game/engines/default/modules/boot/dialogs/ProfileSteamRegister.lua
@@ -20,6 +20,7 @@
 require "engine.class"
 local Dialog = require "engine.ui.Dialog"
 local Button = require "engine.ui.Button"
+local Checkbox = require "engine.ui.Checkbox"
 local Textbox = require "engine.ui.Textbox"
 local Textzone = require "engine.ui.Textzone"
 
@@ -42,12 +43,16 @@ Luckily this is very easy to do: you only require a profile name and optionally
 
 	self.c_login = Textbox.new{title="Username: ", text="", chars=30, max_len=20, fct=function(text) self:okclick() end}
 	self.c_email = Textbox.new{title="Email: ", size_title=self.c_login.title, text="", chars=30, max_len=60, fct=function(text) self:okclick() end}
+	self.c_news = Checkbox.new{title="Accept to receive #{bold}#very infrequent#{normal}# (a few per year) mails", default=true, fct=function() self:okclick() end}
+	self.c_news2 = Textzone.new{text="about important game events from us.", width=self.iw - 20, auto_height=true}
 	local ok = require("engine.ui.Button").new{text="Register", fct=function() self:okclick() end}
 	local cancel = require("engine.ui.Button").new{text="Cancel", fct=function() self:cancelclick() end}
 	self:loadUI{
 		{left=0, top=0, ui=self.c_desc},
 		{left=0, top=self.c_desc.h, ui=self.c_login},
 		{left=0, top=self.c_desc.h+self.c_login.h+5, ui=self.c_email},
+		{left=0, top=self.c_desc.h+self.c_login.h+self.c_email.h+5, ui=self.c_news},
+		{left=0, top=self.c_desc.h+self.c_login.h+self.c_email.h+self.c_news.h+5, ui=self.c_news2},
 		{left=0, bottom=0, ui=ok},
 		{right=0, bottom=0, ui=cancel},
 	}
@@ -78,7 +83,7 @@ function _M:okclick()
 			return
 		end
 
-		profile:performloginSteam(ticket:toHex(), self.c_login.text, self.c_email.text ~= "" and self.c_email.text)
+		profile:performloginSteam(ticket:toHex(), self.c_login.text, self.c_email.text ~= "" and self.c_email.text, self.c_news.checked)
 		profile:waitFirstAuth()
 		d:done()
 		if not profile.auth and profile.auth_last_error then
diff --git a/game/profile-thread/Client.lua b/game/profile-thread/Client.lua
index a15569b786..dea4d4ac1f 100644
--- a/game/profile-thread/Client.lua
+++ b/game/profile-thread/Client.lua
@@ -162,6 +162,8 @@ function _M:login()
 		if self.steam_token_email then
 			self:command("STM_ EMAIL", self.steam_token_email)
 			self:read("200")
+			self:command("STM_ NEWS", self.steam_token_news and 'yes' or 'no')
+			self:read("200")
 		end
 		self:command("STM_ AUTH", self.steam_token)
 		if not self:read("200") then
@@ -303,7 +305,10 @@ end
 function _M:orderSteamLogin(o)
 	self.steam_token = o.token
 	self.steam_token_name = o.name
-	if o.email and #o.email > 1 then self.steam_token_email = o.email end
+	if o.email and #o.email > 1 then
+		self.steam_token_email = o.email
+		self.steam_token_news = o.news
+	end
 
 	if not self.sock then cprofile.pushEvent("e='Disconnected'") return end
 
-- 
GitLab