Skip to content
Open

News #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions action/news.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
If you aren't already in Discord, join us @ https://discord.aq2world.com/ ! <<<
Don't forget to visit our forums @ https://forums.aq2world.com/ ! <<<
Congrats to matic for his Map Jam-winning map, torg! <<<
Watch for new events in Discord, such as our regular CTF matches! <<<
2 changes: 1 addition & 1 deletion plugins/cvarbans.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function q2a_load(config)
return 0
end

gi.dprintf("cvarbans.lua q2a_load(): Checking/Downloading new cvarbans... ")
gi.dprintf("cvarbans.lua q2a_load(): Checking/Downloading new cvarbans...\n")
cvarbans_update = root_dir..'plugins/cvarbans_update.sh "'..url..'" "'..game..'"'
cvarbans_os_exec(cvarbans_update) -- check for updated cvarbanlist on load
end
60 changes: 60 additions & 0 deletions plugins/news.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local version = "1.0"
gi.AddCommandString("set q2a_news "..version.."\n")
local game = gi.cvar("game", "").string
local broadcasts = {}

function wait (millisecond)
end

function load_news()
for line in io.lines('action/news.txt') do
table.insert(broadcasts, line)
end
end

function news_os_exec(script)
os.execute(script)
wait(1000) -- wait 1s to make sure download is complete and went smooth
local last_update = os.date()
gi.AddCommandString('sets news_update '..last_update..'\n') -- write update time to cvar
load_news()
end

function q2a_load(config)
gi.dprintf("news.lua q2a_load(): "..version.." for Action Quake 2 loaded.\n")

root_dir = config.root_dir
url = config.url
if root_dir == nil then
gi.dprintf("news.lua q2a_load(): 'root_dir' not defined in the config.lua file... aborting\n")
return 0
end
if url == nil then
gi.dprintf("news.lua q2a_load(): 'url' not defined in the config.lua file... aborting\n")
return 0
end

gi.dprintf("news.lua q2a_load(): Checking/Downloading news...\n")
news_update = root_dir..'plugins/news_update.sh "'..url..'" "'..game..'"'
news_os_exec(news_update) -- check for updated news on load
end


local current_index = 1
local last_check = os.time()

function RunFrame()
local now = os.time()

if now - last_check >= 600 then -- 600 seconds = 10 minutes
local bcast = broadcasts[current_index]
gi.bprintf(PRINT_CHAT, '*** '..bcast..'\n')

current_index = current_index + 1
if current_index > #broadcasts then
current_index = 1
end

last_check = now
end
end
27 changes: 27 additions & 0 deletions plugins/news_update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/bash
# a script that checks and downloads of the news master file

# download URL
url="$1"
gamedir="$2"
# bypass modsecurity
usaragent="-d --user-agent=news-update-script"
# parse the filename from the download path
filename=$(echo $url | sed -n 's/^\(.*\/\)*\(.*\)/\2/p')

# script to download latest news.txt from github
cd "${0%/*}" # cd to current dir
cd ../$gamedir/ # cd back and to game dir

#### script
# download the file if updated since last time, and output wheter it updated or not to console

if [[ "$url" == *"githubusercontent"* ]]; then
# not using -N switch for wget since github does not support "last update" parameter, the file will be downloaded on every check, wich is not optimal
wget -O $filename $url 2>&1 | tee | grep -E 'Omitting download|"news.txt" saved|ERROR 404: Not Found'
else
# using -N switch for wget since the file os not hosted on github, best solution, since file will only be downloaded if changed
wget $usaragent -N $url 2>&1 | tee | grep -E 'Omitting download|"news.txt" saved|ERROR 404: Not Found'
fi

exit