PDA

View Full Version : [Share] Char online status and login logs


Charis
13.11.2006, 00:02
*Sorry for pure English - it's not my native :)*

1.
As I see, all statistics sites for L2 servers uses comparision login > logout time to deside is char online or offline.
But when server crashes, some chars stays "online" even if they are not. I think I've resolved that problem. All you need is to update procedure in DB.
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

/********************************************
lin_DeleteNotOwnedItem

INPUT
OUTPUT

return
made by
carrot
date
2003-10-12
********************************************/
ALTER PROCEDURE [DBO].[lin_DeleteNotOwnedItem]
AS
SET NOCOUNT ON

-- Charis start
update lin2world.dbo.user_data
set logout = getdate()
where login > logout
-- Charis end

DELETE user_item WHERE char_id = 0 OR item_type = 0

DECLARE @ToDeleteCharacter CURSOR
DECLARE @char_id INT
SET @ToDeleteCharacter = CURSOR FAST_FORWARD FOR
SELECT char_id
FROM user_data
WHERE account_id > 0 AND temp_delete_date IS NOT NULL AND DATEDIFF ( dd , temp_delete_date , GETDATE()) >= 7
OPEN @ToDeleteCharacter
FETCH FROM @ToDeleteCharacter INTO @char_id

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC lin_deleteChar @char_id
FETCH NEXT FROM @ToDeleteCharacter INTO @char_id
END

CLOSE @ToDeleteCharacter
DEALLOCATE @ToDeleteCharacter

exec dbo.lin_CleanUpGhostData

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



2.
I think it may be usefull for users to see when and from what IP they have logged in on account.
It is nided to do such changes in DB.
Add Table for logs of logins in lin2db database:
CREATE TABLE [login_list] (
[uid] [int] NOT NULL ,
[last_login] [datetime] NOT NULL ,
[last_logout] [datetime] NULL ,
[last_ip] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [PK_login_list] PRIMARY KEY CLUSTERED
(
[uid],
[last_login]
) WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
GO
Modify stored procedure for recording logs in lin2db database:

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

ALTER PROCEDURE [dbo].[ap_SLog]
@uid int, @lastlogin datetime, @lastlogout datetime, @LastGame int, @LastWorld tinyint, @LastIP varchar(15)
AS
--Charis start
declare @last_list int
declare @old_last_login datetime
declare @old_last_logout datetime
declare @old_last_ip nvarchar(50)
declare @msg nvarchar(214)

SET @last_list = 0

if @lastlogin is null or isdate(@lastlogin)!=1 return

select @old_last_login = last_login,
@old_last_logout = last_logout,
@old_last_ip = last_ip
from user_account
where uid = @uid

if @old_last_login is not null and isdate(@old_last_login)=1
begin
SELECT @last_list = 1
FROM login_list
WHERE uid = @uid and last_login = @old_last_login

if @last_list = 0
insert into login_list ([uid], [last_login], [last_logout], [last_ip])
VALUES (@uid, @old_last_login, @old_last_logout, @old_last_ip)
SET @last_list = 0
end


SELECT @last_list = 1
FROM login_list
WHERE uid = @uid and last_login = @lastlogin

if (@last_list <> 1)
insert into login_list ([uid], [last_login], [last_logout], [last_ip])
VALUES (@uid, @lastlogin, @lastlogout, @lastip)
else
update login_list
set last_logout = @lastlogout,
last_ip = @lastip
where uid = @uid and last_login = @lastlogin
--Charis end

UPDATE user_account
SET last_login = @lastlogin, last_logout=@lastlogout, last_world=@lastWorld, last_game=@lastGame, last_ip=@lastIP
WHERE uid=@uid

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

That's all. Enjoy :)

m4rk
13.11.2006, 00:30
I'll check it for sure! Thanks, will edit this post once I try it!

Works like a charm, really useful to keep track of all people who connected and their ips

Phantom2005
13.11.2006, 07:55
It's cool i especially like the log system.. can be very useful :)

Tank6585
13.11.2006, 14:43
Been looking for something like this, thanks!

rodier
13.11.2006, 15:08
too complicated ;-) better is this new C4 function

SELECT TOP 1 world_user as l2players FROM user_count WHERE server_id < 2 ORDER BY record_time DESC

Look:
http://gameparkl2.no-ip.org/stats/online.php
Grab this php code ;-) (make .ini file - in that file will be stored numbers temporary.. to be defended before dos attacks..)


<?php

header("Expires: ". gmdate("D, d M Y H:i:s")." GMT");

$online["cache"] = @file("./temp/online.ini");
@list($online["last"]["check"], $online["last"]["online"]) = explode(";", $online["cache"][0]);
if($online["last"]["check"] == "" || $online["last"]["check"] < (time() - 60)) {
include("config.php");
mssql_select_db("lin2db");
$mssql["entry"] = mssql_fetch_array(mssql_query("SELECT TOP 1 world_user as pocet FROM user_count WHERE server_id < 2 ORDER BY record_time DESC;"));
$online["online"] = $mssql["entry"]["pocet"];
echo "<body bgcolor=\"#272727\">";
echo "<font face=\"verdana\" size=\"2\" color=\"#dd6900\"><center><b>Online: ".$online["online"]."<br><font size=-2>(record: 800+)</b></center></font></body>";
chdir("c:/www/stats/");
$f = fopen("./temp/online.ini", "w");
fputs($f, time().";".$online["online"]);
fclose($f);
} else {
echo "<body background=\"http://lineage2.ratafak.net/images/bg-mini.gif\">";
echo "<body bgcolor=\"#272727\">";
echo "<font face=\"verdana\" size=\"2\" color=\"#dd6900\"><center><b>Online: ".$online["last"]["online"]."<br><font size=-2>(record: 800+)</b></center></font></body>";
}
?>

rodier
13.11.2006, 15:10
config.php

<?php

header("Expires: ". gmdate("D, d M Y H:i:s")." GMT");

$mssql["server"] = "hostname_here";
$mssql["user"] = "";
$mssql["password"] = "";
$mssql["database"] = "lin2db";
$mssql["die"] = "Error when connecting to db...";
mssql_connect($mssql["server"], $mssql["user"], $mssql["password"]); // or die($mssql["die"])
mssql_select_db($mssql["database"]);
?>

Charis
15.11.2006, 16:59
May be I don't understand what do you meen,
But I guess that we are talking about different things.

BTW there is not only fix for online status issue ;)

m4rk
15.11.2006, 19:08
I correct your query Fidow (has some writing error :))

SELECT TOP 1 world_user
FROM user_count
WHERE record_time = (SELECT MAX(record_time) FROM user_count)
AND server_id = 1;

Kenjiro
17.11.2006, 12:32
I correct your query Fidow (has some writing error :))

SELECT TOP 1 world_user
FROM user_count
WHERE record_time = (SELECT MAX(record_time) FROM user_count)
AND server_id = 1;

SELECT TOP 1 world_user
FROM user_count
WHERE server_id = 1
ORDER BY record_time DESC

is way faster, than double select. Technically speaking query ends as soon as sorting is finished within desired 'where' limit. Look at 'estimated execution plan' - CTRL+L in Management Studio.

khelben
05.03.2007, 06:59
Small fix to your SQL.
Hardly noticable, but it may have been a character's first login, in that event logout would be null.


-- Charis start
update lin2world.dbo.user_data
set logout = getdate()
where login > logout
-- Charis end


Becomes


update lin2world.dbo.user_data
set logout = getdate()
where login > isnull(logout, 0)