Version 0.95 BETA

This commit is contained in:
OTCv8
2019-10-02 03:38:52 +02:00
parent 9219c78f15
commit 5220a3bdd2
501 changed files with 38097 additions and 2 deletions

View File

@@ -0,0 +1,119 @@
-- private variables
local news
local newsPanel
local updateNewsEvent = nil
local ongoingNewsUpdate = false
local lastNewsUpdate = 0
local newsUpdateInterval = 30 -- seconds
-- public functions
function init()
news = g_ui.displayUI('news')
newsPanel = news:recursiveGetChildById('newsPanel')
connect(rootWidget, { onGeometryChange = updateSize })
connect(g_game, { onGameStart = hide, onGameEnd = show })
if g_game.isOnline() then
hide()
else
show()
end
end
function terminate()
disconnect(rootWidget, { onGeometryChange = updateSize })
disconnect(g_game, { onGameStart = hide, onGameEnd = show })
removeEvent(updateNewsEvent)
clearNews()
news:destroy()
news = nil
end
function hide()
news:hide()
end
function show()
news:show()
updateSize()
updateNews()
end
function updateSize()
if Services.news == nil or Services.news:len() < 4 or g_game.isOnline() then
return
end
if rootWidget:getWidth() < 790 and news:isVisible() then
hide()
elseif news:isHidden() then
show()
end
news:setWidth(math.min(math.max(250, rootWidget:getWidth() / 4), 300))
end
function updateNews()
if Services.news == nil or Services.news:len() < 4 then
hide()
return
end
if ongoingNewsUpdate or os.time() < lastNewsUpdate + newsUpdateInterval then
return
end
HTTP.getJSON(Services.news .. "?lang=" .. modules.client_locales.getCurrentLocale().name, onGotNews)
ongoingNewsUpdate = true
lastNewsUpdate = os.time()
end
function clearNews()
while newsPanel:getChildCount() > 0 do
local child = newsPanel:getLastChild()
newsPanel:destroyChildren(child)
end
end
function onGotNews(data, err)
ongoingNewsUpdate = false
if err then
return gotNewsError("Error:\n" .. err)
end
clearNews()
for i, news in pairs(data) do
local title = news["title"]
local text = news["text"]
local image = news["image"]
if title ~= nil then
newsLabel = g_ui.createWidget('NewsLabel', newsPanel)
newsLabel:setText(title)
end
if text ~= nil then
newsText = g_ui.createWidget('NewsText', newsPanel)
newsText:setText(text)
end
if image ~= nil then
newsImage = g_ui.createWidget('NewsImage', newsPanel)
newsImage:setId(imageName)
newsImage:setImageSourceBase64(image)
newsImage:setImageFixedRatio(true)
newsImage:setImageAutoResize(false)
newsImage:setHeight(200)
end
end
end
function gotNewsError(err)
updateNewsEvent = scheduleEvent(function()
updateNews()
end, 3000)
clearNews()
errorLabel = g_ui.createWidget('NewsLabel', newsPanel)
errorLabel:setText(tr("Error"))
errorInfo = g_ui.createWidget('NewsText', newsPanel)
errorInfo:setText(err)
ongoingNewsUpdate = true
end

View File

@@ -0,0 +1,10 @@
Module
name: client_news
description: News
author: otclient.ovh
website: http://otclient.ovh
sandboxed: true
scripts: [ news ]
dependencies: [ client_topmenu ]
@onLoad: init()
@onUnload: terminate()

View File

@@ -0,0 +1,47 @@
NewsLabel < Label
text-wrap: false
text-auto-resize: true
text-align: center
font: terminus-14px-bold
NewsText < Label
text-wrap: true
text-auto-resize: true
text-align: left
margin-bottom: 10
NewsImage < Label
text-wrap: true
margin-bottom: 5
text-align: center
StaticWindow
anchors.left: parent.left
anchors.top: topMenu.bottom
anchors.bottom: parent.bottom
margin-top: 10
margin-left: 20
margin-bottom: 10
id: newsPanelHolder
width: 300
!text: tr('News')
ScrollablePanel
id: newsPanel
layout:
type: verticalBox
vertical-scrollbar: newsScroll
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
padding-right: 10
margin-right: 10
VerticalScrollBar
id: newsScroll
anchors.top: newsPanel.top
anchors.bottom: newsPanel.bottom
anchors.left: newsPanel.right
step: 14
pixels-scroll: true