# LasTres

## Contributing

Do you know Perl, Typescript, SCSS or React and want
to get the best of this program?

You can contribute and make your dreams for
this game real.

[Contributing guide](CONTRIBUTING.md).

## Installation instructions. (Prod)

This is an example, you can configure the server
as you please, use this guide as a reference.

This guide uses Debian stable 12, this server
requires at least a system compatible with
Redis such as Linux, Windows is not compatible
with Redis, but you can use it virtualized or
with WSL2.

We recommend Linux or at least something Unix-like
to deploy both in server and in development this
project.

That said features that improve OS compatibility
will always be appreciated.

### Postgresql notes.

We are going to use the peer authentication that
comes preconfigured in Debian, if you are using
other OS you should care of `pg_hba.conf` to
be configured as peer for empty address.

This is how it looks on the Debian I used.

You can also configure it with password if the database
server is not the same host that the webserver, it
is supported by the software.

```
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256
```

### Creating user

```shell
sudo useradd -m las_tres -d /var/lib/las_tres
```

### Installing dependencies.

```shell
sudo apt update && \
sudo apt install git postgresql redis nginx pwgen liblocal-lib-perl libpq-dev
```

### Starting an enable dependency services

```shell
sudo systemctl daemon-reload && \
sudo systemctl enable redis-server && \
sudo systemctl start redis-server && \
sudo systemctl enable postgresql && \
sudo systemctl start postgresql && \
sudo systemctl enable nginx && \
sudo systemctl start nginx
```

### Configuring the database

```shell
( cat << 'EOF'
create user "las_tres";
create database "las_tres";
grant all privileges on database "las_tres" to "las_tres";
alter database "las_tres" owner to "las_tres";
EOF
) | sudo -u postgres psql
```

### Cloning project.

```shell
sudo -u las_tres bash -c "$(cat <<'EOF'
cd && \
git clone https://git.owlcode.tech/sergiotarxz/LasTres
EOF
)"
```

### If you are going to change the JS. (Only once.)

```shell
sudo apt update && sudo apt install nodejs npm && \
sudo -u las_tres bash -c "$(cat <<'EOF'
cd ~/LasTres && \
npm install
EOF
)"
```

### Deploying your js changes

```shell
sudo -u las_tres bash -c "$(cat <<'EOF'
cd ~/LasTres && \
npx webpack
EOF
)"
```

### If you are going to change the css (Only once.)

```shell
sudo apt update && sudo apt install sassc
```

### Deploying the css changes

```shell
sudo -u las_tres bash -c "$(cat <<'EOF'
cd ~/LasTres && \
bash build_styles.sh
EOF
)"
```


### Configuring NGINX. 

#### (TLS is out of the scope of this tutorial, but you must do it if you want to deploy this server to the public world.)

```shell
( cat << 'EOF'
map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}
upstream backend_las_tres {
    server 127.0.0.1:3000 fail_timeout=0;
}

server {
  listen 80;
  listen [::]:80;
  server_name las_tres.example.com;
  location /.well-known/acme-challenge/ { allow all; }

  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 80m;

  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/x-icon;

  location / {
        add_header Strict-Transport-Security "max-age=31536000" always;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Proxy "";
        proxy_pass_header Server;

        proxy_pass http://backend_las_tres;
        proxy_buffering on;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        tcp_nodelay on;
  }

}
EOF
) | sudo tee /etc/nginx/sites-enabled/las_tres.conf && \
sudo systemctl reload nginx
```

### Configuring the app

For non standard configurations you should peek the
config in `las_tres.example.yml` copy to `las_tres.yml` 
and modify the copy until it suits your needs, patches
and issues are welcome if you need more options than 
example allows you to do.

```shell
sudo -u las_tres bash -c "$( cat << 'EOF'
cd ~/LasTres && \
cat << EOF1 > las_tres.yml
secrets:
  - $(pwgen -s 512 1)
database:
  dbname: las_tres
hypnotoad:
  listen:
# Here we have changed it to only listen localhost.
    - http://127.0.0.1:3000
EOF1
EOF
)"
```


### Installing Perl deps

```shell
sudo -u las_tres bash -c "$( cat << EOF
if ! grep PERL5LIB ~/.profile; then
	perl -Mlocal::lib 2>/dev/null >> ~/.profile ;
fi && \
source ~/.profile && \
rm -fr ~/.cpan && \
( echo y && echo reload cpan && echo o conf commit ) | perl -MCPAN -Mlocal::lib -e shell && \
cd ~/LasTres && \
perl Build.PL && \
./Build installdeps
EOF
)"
```

### Creating Systemd service

```shell
( cat << 'EOF'
[Unit]
Description=LasTres the web text game
After=network.target postgresql.service redis.service

[Service]
User=las_tres
Group=las_tres
WorkingDirectory=/var/lib/las_tres/LasTres/
ExecStart=/bin/bash -c "source /var/lib/las_tres/.profile && hypnotoad -f script/las_tres"
User=las_tres
Group=las_tres
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=las_tres.service
EOF
) | sudo tee /etc/systemd/system/las_tres.service
```

### Deploying the database

#### If you are installing for the first time.

```shell
sudo -u las_tres bash -c "$( cat << EOF
cd ~/LasTres && \
source ~/.profile && \
perl script/install.pl
EOF
)"
```

#### If you are migrating from a older version

```shell
sudo -u las_tres bash -c "$( cat << EOF
cd ~/LasTres && \
source ~/.profile && \
perl script/upgrade.pl
EOF
)"
```

### Enabling and starting service

```shell
sudo systemctl daemon-reload && \
sudo systemctl restart las_tres && \
sudo systemctl enable las_tres
```
