Jump to content

Recommended Posts

Posted (edited)

So I was bored again...

 

L2jRest is a RESTful API for L2j

 

It is created for latest aCis but you should be able to adapt it is you wanna use it for other projects.

 

L2jRest is an open source project licensed under MIT. You can find the source here: https://github.com/Elfocrash/L2jRest

 

Why use it?

  • You can use the data of your server in your website
  • You can create an account control panel with it
  • You can expose data to your community to allow them to make third party apps

 

Technical stuff

  • It is written in Kotlin.
  • It is using the Ktor framework. Ktor is using coroutines to achieve asynchronous request handling. It is very efficient and very fast.
  • It is using Netty as the underlying server.
  • It is written in a CQRS manner with query handlers for get endpoints and command handlers for post, put, patch, delete etc.
  • It is using Koin as the IoC framework.

 

How to setup

  • Download and install Intellij IDEA
  • Git clone the project and open it with Intellij
  • Run the build Gradle task. It is configured to create a fat jar with all the dependencies included
  • Paste the jar in your project, add it in your classpath and add the following line at the bottom of your Gameserver.java: L2jRestApi.INSTANCE.start();
  • Running the gameserver will also run the api. It is running under port 6969

 

How to extend it

  • All you need to do if you wanna add more endpoints is to add a new handler and then add the Get, Post etc annotation depending on what endpoint you want this to be.

 

Current endpoints:

http://localhost:6969/api/players

http://localhost:6969/api/players/{id}

 

A couple of endpoint examples

 

Endpoints: http://localhost:6969/api/players

Response:

{
    players: [
		{
			"id": 268480927,
            "name": "Test",
            "level": 1,
            "isOnline": false,
            "pvpKills": 0,
            "pkKills": 0,
            "isNobless": false
		},
		{
			"id": 268480924,
            "name": "Test2",
            "level": 1,
            "isOnline": true,
            "pvpKills": 0,
            "pkKills": 0,
            "isNobless": false
		}
	]
}

 

Endpoint: http://localhost:6969/api/players/268480927

Response:

{
    "id": 268480927,
    "name": "Test",
    "level": 1,
    "isOnline": false,
    "pvpKills": 0,
    "pkKills": 0,
    "isNobless": false
}

 

Currently it just supports two endpoints and no authentication. I am planning to add more endpoints and ApiKey based auth tomorrow.

 

If you can't be arsed to build the project yourself but you wanna take a look anyway you can download the jar here

 

Edited by Elfocrash
  • Like 3
  • Upvote 2
Posted (edited)

Thank you a lot for that. That's what I was searching. I have one question

 

L2DatabaseFactory.getInstance().connection.use { con ->
                val statement = con.prepareStatement(SqlQueries.GetAllPlayers)
                val resultSet = statement.executeQuery()

                var player: PlayerResponse? = null
                if (resultSet.next())
                {
                    val id = resultSet.getInt("obj_Id")
                    val name = resultSet.getString("char_name")
                    val level = resultSet.getInt("level")
                    val isOnline = resultSet.getInt("online") == 1
                    val pvpKills = resultSet.getInt("pvpkills")
                    val pkKills = resultSet.getInt("pkkills")
                    val isNobless = resultSet.getInt("nobless") == 1
                    player = PlayerResponse(id, name, level, isOnline, pvpKills, pkKills, isNobless)

 

 

Wouldn't be better to take these infos from the player object itself for example?(if player is online) So it would be real time , because database values are not always real time on L2J due to delay of updating. This specific feature may isn't the best example to what I mean actually but propably you got the point.

 

Also practically could we extend this to create token authentication and the ACP(or whatever) to just be  a cool SPA with front end framework(react,vue,etc)? 

Edited by Lioy
Posted (edited)
52 minutes ago, Lioy said:

Thank you a lot for that. That's what I was searching. I have one question

 


L2DatabaseFactory.getInstance().connection.use { con ->
                val statement = con.prepareStatement(SqlQueries.GetAllPlayers)
                val resultSet = statement.executeQuery()

                var player: PlayerResponse? = null
                if (resultSet.next())
                {
                    val id = resultSet.getInt("obj_Id")
                    val name = resultSet.getString("char_name")
                    val level = resultSet.getInt("level")
                    val isOnline = resultSet.getInt("online") == 1
                    val pvpKills = resultSet.getInt("pvpkills")
                    val pkKills = resultSet.getInt("pkkills")
                    val isNobless = resultSet.getInt("nobless") == 1
                    player = PlayerResponse(id, name, level, isOnline, pvpKills, pkKills, isNobless)

 

 

Wouldn't be better to take these infos from the player object itself for example?(if player is online) So it would be real time , because database values are not always real time on L2J due to delay of updating. This specific feature may isn't the best example to what I mean actually but propably you got the point.

 

Also practically could we extend this to create token authentication and the ACP(or whatever) to be just a cool SPA with front end framework(react,vue,etc)? 

The problem is that the offline players are not loaded in memory so the dB call is inevitable. Everything that is loaded in the in memory cache will be served by the cache when possible. If the endpoint was get online players only then yeah it would be coming from the World class.

 

Auth is very simple to add. It’s basically a simple middleware.

 

originally when I started this it was to actually make a new ACP but it’s too much work and I don’t really wanna do front end anymore so I just created the api. If people wanna make their front end that’s fine by me. 

 

EDIT: Oh I now see what you mean, I read it wrong the first time. Yes it would make sense to get that info from the cache if the player is online. I will add that.

Edited by Elfocrash
Posted (edited)

PlayerInfoTable retains some informations (cause player names are needed for stuff like friendlist), you probably can add missing ones, but it means you have to update it in same time otherwise you will refer to wrong values.

 

That whole class probably can be enhanced or used differently (PlayerInfo being maybe part of Player, once player is loaded ? Like loading an id card, which avoid duplicates variables or even caring about refreshing 2 places).

 

If such stuff is edited, that would simplify the developement of a lot of customs.

Edited by Tryskell
Posted
1 minute ago, Tryskell said:

PlayerInfoTable retains some informations (cause player names are needed for stuff like friendlist), you probably can add missing ones, but it means you have to update it in same time otherwise you will refer to wrong values.

 

That whole class probably can be enhanced or used differently (maybe part of Player, once player is loaded ? Like loading an id card, which avoid duplicates variables).

The problem with having multiple points of truth is that you don't have a single one you can fully trust, that's why i trust nothing but the database and the World class. I also don't wanna touch the core at all so if I do any caching it will happen in the API level.

Posted (edited)
3 minutes ago, Elfocrash said:

The problem with having multiple points of truth is that you don't have a single one you can fully trust, that's why i trust nothing but the database and the World class. I also don't wanna touch the core at all so if I do any caching it will happen in the API level.

 

I edited my answer :), and the idea is to only have one point of entry, which would be PlayerInfo linked to a Player, like they are actually linked to an AccessLevel, a Appearance, etc. SO drop inner variables of Player, and use only PlayerInfo to refresh those values.

 

Np.

Edited by Tryskell
Posted

Changed the way handlers are registered. Now they will be scanned automatically during server startup and they will be registered based on their annotation. This means that all you have to do to add a new endpoint is to create a request handler and add the Get, Post etc annotation.

Posted

Added api key based authentication as requested. Requests will only be served when the x-api-key header is set with the configured value.

Posted
On 7/24/2019 at 1:25 AM, Elfocrash said:

Added api key based authentication as requested. Requests will only be served when the x-api-key header is set with the configured value.

 

Hey Elfo , what's the logic on this authentication? If credentials are correct then send the API key to the client? And all authorization is possible via this simple unique API key?

Just asking, because never worked with that kind of auth. (I know JWT for example , maybe I confused things somewhere)

Posted
2 minutes ago, Lioy said:

 

Hey Elfo , what's the logic on this authentication? If credentials are correct then send the API key to the client? And all authorization is possible via this simple unique API key?

Just asking, because never worked with that kind of auth. (I know JWT for example , maybe I confused things somewhere)

It's using ApiKey auth. It's not client facing auth where you get credentials and return a key. You can do that with Basic auth but you have to enable it.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • Hello Guys. Lf l2 pride pack + source. Pref Gracia Final.
    • His posts clearly show how unprofessional he is. He acts as though there are no stable and well-established projects that people actually enjoy. PlayInera, for example, is one of those projects where the staff works hard to deliver a solid, stable server experience.
    • You think you have good “l2j” files until you try running a low-rate server.   Saying “there’s not a single L2 server out there worth mentioning” just shows you probably only know the first 10 servers on voting sites, the same voting sites that owned by them. You call the forum dead, yet you’re here discussing your next projects… From my perspective, you don’t seem ready to run any L2 server in 2025. Around 70% of players are here for RMT or ask for payment just to bring their clan, and you really think the community cares about Premium or donations or files quality? The other 20% spend their time downloading and deleting servers all day, playing for one day, then quitting for whatever random reason. And finally, the last 10% are the only ones who actually play because they genuinely like your features, your server files, and your overall project. Good luck 🙂
    • I genuinely admire your bravery - in an age where AI can whip up something better in under a minute, you still stubbornly try to sell these "projects" of yours on a forum that’s been clinically dead for years. That’s no longer determination, that’s digital archaeology. I just can’t tell whether you’re actually trying to make money, or simply testing how much we can endure before we ask an AI to generate you some actual talent.   And ofc AI will make it for free, $220 saved.
    • I’m glad I’m not the only one who appreciates Maxthor’s involvement in group gay orgies, he can’t be bothered to reply to messages, but covering the entire forum in gay lights is absolutely no issue for him. As for the project - the forum is packed with feedback from the testers, the lads are spending every spare moment fixing even the tiniest typo in an NPC’s text. I’ll share the links as soon as I get the green light. Edit: I forgot to add that the GM recruitment will begin once the links are released. Three people will be accepted, and they’ll work in a three-shift rotation so that there’s always a GM available online.
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock