Forum

> > CS2D > Scripts > usgn id > name table
Forums overviewCS2D overview Scripts overviewLog in to reply

English usgn id > name table

25 replies
Page
To the start Previous 1 2 Next To the start

old Re: usgn id > name table

Apache uwu
User Off Offline

Quote
I don't like the [[]] brackets because I can't selective comment...

And no I think he's trying to say is to get 1 usgn ID it would just be easier to grab it from the userid=***** link.

It wouldn't because that would cause the application to grab thousands of html requests which would kill bw...

old Re: usgn id > name table

Flacko
User Off Offline

Quote
user Apache uwu has written
I don't like the [[]] brackets because I can't selective comment...

And no I think he's trying to say is to get 1 usgn ID it would just be easier to grab it from the userid=***** link.

It wouldn't because that would cause the application to grab thousands of html requests which would kill bw...


Not to mention that your server will freeze until the data has arrived, which can take several seconds. (unless you use coroutines)

Anyways, this requiers some library binding, which also
requires you to do some C/C++

If I recall correctly, I used the Wininet API, still, this is a horrible idea.

old Re: usgn id > name table

Banaan
User Off Offline

Quote
Since coroutines don't run parallel (only one at a time), the server would still freeze, right?

old Re: usgn id > name table

Lee
Moderator Off Offline

Quote
actually, that still lags CS2D since the sockets have to be blocking for TCP (as in it blocks the entirety of the process owning its descriptor, which is the instance of CS2D running it). The only way to emulate nonblocking TCP sockets is to use operating system specific calls. (either a multithreaded lua environment of a multiprocess environment)

On the other hand, if you just download a huge file at startup every other day, it wouldn't really affect the server.

For example, here's a simple implementation that checks once every 24 hours:

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
package.cpath = package.cpath .. ";libs/?.dll"
package.path = package.path .. ";libs/lua/?.lua"

require "socket"

get_latest = socket.protect(function(time)
	if os.time() - time < 86400 then return end
	local c = socket.try(socket.connect("usgn.uphero.com", 80))
	local try = socket.newtry(function() c:close() end)
	try(c:send("GET / HTTP/1.1\r\nHost: usgn.uphero.com\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0\r\n\r\n"))
	local answer = try(c:receive("*a"))

	start = answer:find("-- last updated on ")
	answer = answer:sub(start)
	e = answer:find(string.char(0x0a,0x0d,0x0a,0x30))
	answer = answer:sub(1, e) -- last character is \n or 0x0a
	c:close()
	return answer, os.time()
end)

-- Example usage: autoupdate on startup if last checked is over a day ago
function autoupdate()
	local f = io.open("latest_usgnname", "r")

	local time

	if f then
		time = tonumber(f:read("*a"))
	else
		time = 0
	end

	newest, time = get_latest(time)
	if newest then
		f = io.open("latest_usgnname", "w")
		f:write(time)
		f:close()

		f = io.open("sys/lua/usgn_names.lua", "w")
		f:write(newest)
		f:close()
	end

	return dofile "sys/lua/usgn_names.lua"
end
autoupdate()

-- Example usage: overloading the player function
-- player(id, "username") now returns a player's unrealsoftware username
local __old_player_ = player
function player(id, t)
	if t == "username" then
		local usgn = __old_player_(id, "usgn")
		if not usgn or not usgn_name[usgn] then return end
		return usgn_name[usgn]
	end
	return __old_player_(id, t)
end

-- you can now use the usgn_name table
--[[--
addhook("join", "checkusgn")
function checkusgn(id)
	local usgn = player(id, "usgn")
	if not usgn or not usgn_name[usgn] then return end

	msg("Welcome " .. usgn_name[usgn])
end
--]]--
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview