Skip to content

Commit

Permalink
Rewrite readme (#106)
Browse files Browse the repository at this point in the history
* Rename start script to omit space

* Use python on windows

* characters doesn't exist anymore, storage is probably correct

* Lock requirements.txt file with versions

Since there may be breaking changes between versions of
packages, it's generally recommended to use specific versions
This requirements.txt file was generated by installing the
existing requirements to a fresh virtual environment, then
running pip freeze.

* Upgrade Python requirement from 3.7 to 3.11

Python 3.7 stopped getting security updates four months ago.
See https://endoflife.date/python

* Rewrite README

Generally, prefer using virtual environments.
Reformulate and slim down some things.
Add new section "Pro Tips" for cool advice.
The readme does not generally need a whole debugging guide for docker.

The readme is not a blogpost. Refrain from 'hot takes', please.

* Update helper scripts to use venv

* No point in activating venv for one command

* Remove more cheeky comments
  • Loading branch information
OmniTroid authored Nov 14, 2023
1 parent 412212c commit 77819b0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- "3.7"
- "3.11"
stages:
- execute

Expand Down
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.7-alpine
FROM python:3.11-alpine

RUN apk --no-cache add git

Expand All @@ -10,7 +10,6 @@ RUN pip install -r requirements.txt

COPY server/ server/
COPY migrations/ migrations/
COPY characters/ characters/
COPY storage/ storage/

CMD python ./start_server.py

130 changes: 44 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,124 +3,82 @@

KFO-Server is the official Python-based server for Attorney Online, forked from tsuserver3.

## Commands
## Server setup

Documentation: ["Commands"](https://github.com/Crystalwarrior/KFO-Server/blob/master/docs/commands.md). You may also use the /help documentation on the server.

## Easy setup instructions

The server will not work out of the box. **You must follow these instructions.**
In order to set up the server, you must follow these instructions. This assumes you are familiar with using a terminal.

### Install Python

* Install the [latest version of Python](https://www.python.org/downloads/). **Python 2 will not work**, as KFO-Server depends on async/await, which can only be found on Python 3.7 and newer.
* Install the [latest version of Python](https://www.python.org/downloads/). You will need Python 3.11 or newer.
* If you run Windows, make sure to check the "Add Python to PATH" and install pip checkboxes in the installer
* If you run anything other than Windows, you should read "Advanced setup instructions" below.

### Download KFO-Server

We recommend [Git](https://git-scm.com/downloads/guis) - it makes it very easy to update tsuserver. But we are power users and therefore inherently biased. You can instead download the latest zip of KFO-Server with [this link](https://github.com/Crystalwarrior/KFO-Server/archive/refs/heads/master.zip). Extract it and put it wherever you want.

### Install dependencies

* Open Command Prompt or your terminal, and change to the directory where you downloaded KFO-Server to. You can do this in two ways:
* Go up one folder above the KFO-Server folder, Shift + right click the KFO-Server folder, and click `Open command window here`. This is the easiest method.
* Copy the path of the KFO-Server folder, open the terminal, and type in `cd "[paste here]"`, excluding the brackes, but including the quotation marks if the path contains spaces.
* To install PyYAML and dependencies, type in the following:

```bash
python -m pip install --user -r requirements.txt
```

If you are using Windows and have both Python 2 and 3 installed, you may do the following:

```batch
py -3 -m pip install --user -r requirements.txt
```

This operation should not require administrator privileges, unless you decide to remove the `--user` option.

### Configure tsuserver

* **Copy `config_sample` to `config`**.
* Edit the values in the `.yaml` files to your liking. If you downloaded tsuserver from a zip and try to edit it from Notepad, the line breaks will be missing. That's because Notepad is stupid. Use [Notepad++](https://notepad-plus-plus.org/) instead like the pro you are.
* Be sure to check your YAML file for syntax errors. Use this website: <http://www.yamllint.com/>
* *Use spaces only; do not use tabs.* That's another reason we recommend anything that isn't Notepad.
* You don't need to copy characters into the `characters` folder *unless* you specifically chose to disable iniswapping in an area (in `areas.yaml`). In this case, all tsuserver needs to know is the `char.ini` of each character. It doesn't need sprites.
* Don't forget to forward ports on your router. You will need to forward both the regular port and the webAO port.
### Download server software

### Run
We recommend [Git](https://git-scm.com/downloads/guis) for downloading the server software.
This makes it easier to update the server later. In order to use Git, just clone the respository.

* Run by either double-clicking `start_server.py` or typing in `py -3 start_server.py` if you use both Python 2 and 3. It is normal to not see any output once you start the server.
* To stop the server, press Ctrl+C multiple times.
If you don't want to use Git, you can download the latest zip of KFO-Server [here](https://github.com/Crystalwarrior/KFO-Server/archive/refs/heads/master.zip). Extract it and put it wherever you want.

## Advanced setup instructions
### Install dependencies

For servers that are intended to run for an extended period of time, Linux is recommended. Linux distros have a significantly more consistent and sane environment compared to Windows and macOS. These days, Microsoft is trying to cater the operating system it happened to prepackage with Candy Crush to power users by sticking a successful kernel inside its failed Win32 kernel, so you can try [that abomination](https://docs.microsoft.com/en-us/windows/wsl/install-win10) ([or its successor](https://docs.microsoft.com/en-us/windows/wsl/wsl2-install)) if you want. Or you can just use [a darn virtual machine](https://www.virtualbox.org/) instead.
In order to install dependencies, you will need to open a terminal.

I will just assume from now on that you have managed to produce an object that runs Linux.
On Windows, you can do this by pressing Win+R, typing in `cmd`, and pressing Enter.
On Linux, you can do this by pressing Ctrl+Alt+T.

### Using Docker
You should then navigate to the folder where the server is located.

The easiest way to set up and start tsuserver is with the Dockerfile. You just [install Docker](https://get.docker.com/) and [install Docker Compose](https://docs.docker.com/compose/install/).
Take note that depending on your operating system, the command for python may be python3 or python.
You should also verify the version by running `python --version` or `python3 --version`.

Once you have everything configured, do `sudo docker-compose up`. It will build the image and start tsuserver up for you. If you accidentally restart the server, the container will automatically start back up. If you're not understanding why it's starting, try starting it up manually:
First, we need to create the virtual environment. This can be done by running the following command:

```sh
sudo docker run -it -d -v `pwd`/storage:/app/storage -v `pwd`/logs:/app/logs -v `pwd`/config:/app/config -p 27018:27018 -p 27017:27017 --restart=unless-stopped tsuserver
```bash
python -m venv venv
```

Here is a breakdown of the command:

* `-it` puts an *interactive* *terminal* so that the output of the command line can be reaped later with `docker logs`. Without these options, the output is closed and printing things to the log will then break Python.
* `-d` starts the container in the background (`detached`).
* `-v SOURCE:TARGET` mounts the SOURCE directory on the host filesystem into the container's virtual filesystem at the TARGET location, so that we can modify server files without having to rebuild the entire image. However, SOURCE can't be a relative path, so we make an absolute path with `pwd`.
* `-p SOURCE:TARGET` exposes port SOURCE inside the container as port TARGET in the whole machine. This way, a port can be connected to by anyone, not just the machine which Docker is being run in.
* `--restart=unless-stopped` sets the restart policy to always restart, even after reboot, unless the container is explicitly stopped with `docker stop`.
* `tsuserver` is the name of the image to start a container from.
Then, we need to activate the virtual environment.
If you're on a unix system (bash or similar), you can run the following command:

Docker is a nifty tool, although I admit that setting it up and understanding how to use it can be tedious.

### Alternative: Using `venv`

`venv` is a tool built into Python to create a virtual Python environment to install packages in. It reduces the risk of mixing up Python versions and package versions. Sometimes, it's even necessary if `pip` installs require sudo privileges. (`sudo pip install` is highly frowned upon as it regularly causes conflicts with the system's own package manager. Don't do it, use `venv` instead!)

Suppose you installed tsuserver in `/home/me/tsuserver` (protip: this path is the same as `~me/tsuserver`).
```bash
./venv/bin/pip install -r requirements.txt
```

You can create a venv with the following command:
If you're on Windows (cmd), you may have to do this instead:

```sh
python3 -m venv /home/me/tsuserver/.venv
```batch
venv\Scripts\pip install -r requirements.txt
```

Then activate the venv:
### Configure tsuserver

```sh
source /home/me/tsuserver/.venv/bin/activate
```
* Copy `config_sample` to `config`
* Edit the values in the `.yaml` files to your liking.
* Be sure to check your YAML file for syntax errors. Use this website: <http://www.yamllint.com/>
* *Use spaces only; do not use tabs.*
* You don't need to copy characters into the `characters` folder *unless* you specifically chose to disable iniswapping in an area (in `areas.yaml`). In this case, all tsuserver needs to know is the `char.ini` of each character. It doesn't need sprites.

Install requirements:
### Run

```sh
cd /home/me/tsuserver
pip install -r requirements.txt
```
You can run the server using one of the helper scripts `start-unix.sh` or `start-windows.bat`.
They run the server using the local environment.

Start the server:
To stop the server, press Ctrl+C in the terminal.

```sh
python start_server.py
```
## Using Docker

You will always need to activate the venv before starting the server.
You can also use docker to run KFO-server. First you need to install [Docker](https://get.docker.com/) and [Docker Compose](https://docs.docker.com/compose/install/).

Refer to ["Creating Virtual Environments"](https://docs.python.org/3/library/venv.html#creating-virtual-environments) in the Python documentation for more information.
Once you have everything configured, do `docker-compose up`. It will build the image and start tsuserver up for you. If you accidentally restart the server, the container will automatically start back up. If you're not understanding why it's starting, try starting it up manually:

To keep the server running even after closing the terminal:
## Pro Tips

* Use a multiplexer, such as screen or tmux. These programs let you have multiple terminals open in a single window, and detach/reattach with them freely. They are extremely convenient.
* Create a systemd service. Not for the faint of heart.
* Use Docker instead.
* To keep the server running even if your login shell is closed, use a multiplexer, such as screen or tmux.
* For more info about available command, see [Commands](https://github.com/Crystalwarrior/KFO-Server/blob/master/docs/commands.md). You may also use the /help command on the server.
* For more info about Python virtual environments, refer to ["Creating Virtual Environments"](https://docs.python.org/3/library/venv.html#creating-virtual-environments)
* In order to join your server, it has to be accessible to the public internet. You might need to forward the ports in config.yaml to make this work.
* If you can't portforward, you may want to check out [ngrok](https://ngrok.com/). It's a service that allows you to expose your local server to the internet. It's free, but you can also pay for a subscription to get more features.

## License

Expand Down
32 changes: 25 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
oyaml
websockets
arrow
timeparse-plus
geoip2
discord
pystun3
aiohttp==3.8.6
aiosignal==1.3.1
arrow==1.3.0
async-timeout==4.0.3
attrs==23.1.0
certifi==2023.7.22
charset-normalizer==3.3.2
discord==2.3.2
discord.py==2.3.2
frozenlist==1.4.0
geoip2==4.7.0
idna==3.4
maxminddb==2.5.1
multidict==6.0.4
oyaml==1.0
pystun3==1.0.0
python-dateutil==2.8.2
PyYAML==6.0.1
requests==2.31.0
six==1.16.0
timeparse-plus==1.2.0
types-python-dateutil==2.8.19.14
urllib3==2.1.0
websockets==12.0
yarl==1.9.2
6 changes: 4 additions & 2 deletions Start - Linux.sh → start-unix.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/sh

set -euxo pipefail

while [ true ]
do
echo "Starting up server..."
python3 start_server.py
./venv/bin/python3 start_server.py
echo "Server has shut down. Will restart in 2 seconds (use CTRL-C to cancel)"
sleep 2
done

4 changes: 2 additions & 2 deletions Start - Windows.bat → start-windows.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@echo off
:while
echo Starting up server...
py start_server.py
venv\Scripts\python start_server.py
echo Server has shut down, restarting...
timeout /t 2 /nobreak
goto :while
goto :while

0 comments on commit 77819b0

Please sign in to comment.