mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-07 16:06:12 +02:00
Merge pull request #248 from the-programmers-hangout/content-clean-up-markdown
content(resources): clean up markdown files, headings & codeblocks
This commit is contained in:
@@ -11,50 +11,50 @@ external_resources:
|
|||||||
---
|
---
|
||||||
|
|
||||||
Over my time in TPH, I have noticed that a common woe aspiring bot developers have is that they are unable to host
|
Over my time in TPH, I have noticed that a common woe aspiring bot developers have is that they are unable to host
|
||||||
their Discord bot online as they may not have access to a credit card.
|
their Discord bot online as they may not have access to a credit card.
|
||||||
|
|
||||||
## Introducing Heroku!
|
## Introducing Heroku!
|
||||||
|
|
||||||
While the official Discord bots used in TPH - like HotBot - is hosted via paid platforms, there are free alternatives
|
While the official Discord bots used in TPH - like HotBot - is hosted via paid platforms, there are free alternatives
|
||||||
to deploying your bot online. This is where Heroku comes into the picture!
|
to deploying your bot online. This is where Heroku comes into the picture!
|
||||||
|
|
||||||
> Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps — we're the fastest way to
|
> Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps — we're the fastest way to
|
||||||
> go from idea to URL, bypassing all those infrastructure headaches.
|
> go from idea to URL, bypassing all those infrastructure headaches.
|
||||||
|
|
||||||
Heroku's free tier does not require any credit card information and has sufficient uptime for your basic bot
|
Heroku's free tier does not require any credit card information and has sufficient uptime for your basic bot
|
||||||
development needs and it is a great starting place to understand hosting.
|
development needs and it is a great starting place to understand hosting.
|
||||||
|
|
||||||
## How does Heroku work?
|
## How does Heroku work?
|
||||||
|
|
||||||
Before diving into setting up a Discord bot on Heroku, it is best to explain how Heroku is used. Heroku relies on the
|
Before diving into setting up a Discord bot on Heroku, it is best to explain how Heroku is used. Heroku relies on the
|
||||||
Git version control system (VCS) to manage an application. This means that it integrates well with any existing projects
|
Git version control system (VCS) to manage an application. This means that it integrates well with any existing projects
|
||||||
that already use Git. Do not fret, even if your application does not use Git, the configuration and setup for Heroku is
|
that already use Git. Do not fret, even if your application does not use Git, the configuration and setup for Heroku is
|
||||||
still simple.
|
still simple.
|
||||||
|
|
||||||
By using Git, Heroku receives the project files directly and it is responsible for building the project. This is unlike
|
By using Git, Heroku receives the project files directly and it is responsible for building the project. This is unlike
|
||||||
other hosting platforms where you would often only supply the final executable - a `.jar` file in our case - to the
|
other hosting platforms where you would often only supply the final executable - a `.jar` file in our case - to the
|
||||||
hosting platform to run.
|
hosting platform to run.
|
||||||
|
|
||||||
In order for Heroku to understand how it will build and deploy your application, you must provide a `Procfile`.
|
In order for Heroku to understand how it will build and deploy your application, you must provide a `Procfile`.
|
||||||
|
|
||||||
The `Procfile` is comprised of two key components - the dyno to run the application on and the commands to run your
|
The `Procfile` is comprised of two key components - the dyno to run the application on and the commands to run your
|
||||||
application.
|
application.
|
||||||
|
|
||||||
According to the [Heroku documentation on dynos](https://www.heroku.com/dynos), dynos are containers that are used to
|
According to the [Heroku documentation on dynos](https://www.heroku.com/dynos), dynos are containers that are used to
|
||||||
run and scale all Heroku applications. Rather than worrying about configuring your build environment or OS, you can
|
run and scale all Heroku applications. Rather than worrying about configuring your build environment or OS, you can
|
||||||
focus on building your applications and allowing Heroku to take over the build and deployment process. For all
|
focus on building your applications and allowing Heroku to take over the build and deployment process. For all
|
||||||
Discord bots, we will use a **worker** dyno.
|
Discord bots, we will use a **worker** dyno.
|
||||||
|
|
||||||
The build commands we supply correspond to the build commands we use to run our bots locally.
|
The build commands we supply correspond to the build commands we use to run our bots locally.
|
||||||
|
|
||||||
As Heroku uses the project files to determine the type of tools we are using, we do not need to specify the
|
As Heroku uses the project files to determine the type of tools we are using, we do not need to specify the
|
||||||
instructions to create the executable. In our case, since we are using Maven, it can intelligently detect the
|
instructions to create the executable. In our case, since we are using Maven, it can intelligently detect the
|
||||||
`pom.xml` file and create the `.jar` accordingly. This leaves us with only the run commands to include in our `Procfile`.
|
`pom.xml` file and create the `.jar` accordingly. This leaves us with only the run commands to include in our `Procfile`.
|
||||||
|
|
||||||
Finally, to tighten security, we will store all bot tokens in Heroku's
|
Finally, to tighten security, we will store all bot tokens in Heroku's
|
||||||
[config vars.](https://devcenter.heroku.com/articles/config-vars) From a code perspective, these config vars are simply
|
[config vars.](https://devcenter.heroku.com/articles/config-vars) From a code perspective, these config vars are simply
|
||||||
environment variables available to our applications. This allows us to load our bot token during runtime and prevent
|
environment variables available to our applications. This allows us to load our bot token during runtime and prevent
|
||||||
the bot token from being leaked.
|
the bot token from being leaked.
|
||||||
|
|
||||||
Thus, we can define our deployment plan as such:
|
Thus, we can define our deployment plan as such:
|
||||||
|
|
||||||
@@ -64,13 +64,13 @@ Thus, we can define our deployment plan as such:
|
|||||||
4. Store the bot token as a config var to be used by your bot
|
4. Store the bot token as a config var to be used by your bot
|
||||||
|
|
||||||
What I have just presented is a general overview of Heroku as a hosting platform. I will be diving into the implementation
|
What I have just presented is a general overview of Heroku as a hosting platform. I will be diving into the implementation
|
||||||
in the following sections.
|
in the following sections.
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
For this article, I will be using a very simple Discord bot written in Kotlin. I have chosen to use JDA as the focus of
|
For this article, I will be using a very simple Discord bot written in Kotlin. I have chosen to use JDA as the focus of
|
||||||
this guide is to understand Heroku. The code repository can be found
|
this guide is to understand Heroku. The code repository can be found
|
||||||
[here.](https://github.com/woojiahao/discord-heroku-deployment-demo)
|
[here.](https://github.com/woojiahao/discord-heroku-deployment-demo)
|
||||||
|
|
||||||
If you wish to follow along, you can get the repository via
|
If you wish to follow along, you can get the repository via
|
||||||
|
|
||||||
@@ -83,23 +83,23 @@ Aside from that, basic understanding of the following is good to have to underst
|
|||||||
|
|
||||||
1. [Git](https://git-scm.com/book/en/v2) - version control system that integrates with Heroku to enable easy deployments
|
1. [Git](https://git-scm.com/book/en/v2) - version control system that integrates with Heroku to enable easy deployments
|
||||||
2. [Maven](http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html) - build tool for Kotlin to
|
2. [Maven](http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html) - build tool for Kotlin to
|
||||||
manage application dependencies
|
manage application dependencies
|
||||||
|
|
||||||
In Kotlin/Java, we are looking to create a `.jar` file. This `.jar` file can be thought of like a `.exe` file.
|
In Kotlin/Java, we are looking to create a `.jar` file. This `.jar` file can be thought of like a `.exe` file.
|
||||||
Essentially, it bundles the application and allows us to run our bot without having to fire up an IDE.
|
Essentially, it bundles the application and allows us to run our bot without having to fire up an IDE.
|
||||||
|
|
||||||
To create this `.jar` file, we will use Maven. For more information about using Maven to create `.jar` files, refer to
|
To create this `.jar` file, we will use Maven. For more information about using Maven to create `.jar` files, refer to
|
||||||
[this](http://tutorials.jenkov.com/maven/maven-build-fat-jar.html) guide.
|
[this](http://tutorials.jenkov.com/maven/maven-build-fat-jar.html) guide.
|
||||||
|
|
||||||
With the formalities out of the way, let's get down to deploying our bot.
|
With the formalities out of the way, let's get down to deploying our bot.
|
||||||
|
|
||||||
## Installing Heroku
|
## Installing Heroku
|
||||||
|
|
||||||
You will have to install Heroku onto your machine to execute the following commands in the command line. You can find
|
You will have to install Heroku onto your machine to execute the following commands in the command line. You can find
|
||||||
the installation instructions for Heroku [here](https://devcenter.heroku.com/articles/heroku-cli).
|
the installation instructions for Heroku [here](https://devcenter.heroku.com/articles/heroku-cli).
|
||||||
|
|
||||||
To ensure that you have installed Heroku successfully, run `heroku --version`. My version of Heroku is
|
To ensure that you have installed Heroku successfully, run `heroku --version`. My version of Heroku is
|
||||||
`heroku/7.39.2 linux-x64 node-v13.12.0`
|
`heroku/7.39.2 linux-x64 node-v13.12.0`
|
||||||
|
|
||||||
## Setup a Git repository
|
## Setup a Git repository
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ for deploying your applicaiton to Heroku.
|
|||||||
If you are using the sample bot, it is already a Git repository.
|
If you are using the sample bot, it is already a Git repository.
|
||||||
|
|
||||||
If you are deploying your own bot, initialise a repository by using the following command inside the root folder of your
|
If you are deploying your own bot, initialise a repository by using the following command inside the root folder of your
|
||||||
codebase.
|
codebase.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ git init
|
$ git init
|
||||||
@@ -126,13 +126,13 @@ $ heroku create [project name]
|
|||||||
```
|
```
|
||||||
|
|
||||||
The project name is optional and will be automatically generated if not provided. It is recommended that you give a name
|
The project name is optional and will be automatically generated if not provided. It is recommended that you give a name
|
||||||
to be organised.
|
to be organised.
|
||||||
|
|
||||||
To ensure that the Heroku application has been created, run the `git remote -v` command to list the remotes of your
|
To ensure that the Heroku application has been created, run the `git remote -v` command to list the remotes of your
|
||||||
repository. Should your application have been created successfully, you will see a new remote added linking to a Heroku
|
repository. Should your application have been created successfully, you will see a new remote added linking to a Heroku
|
||||||
Git remote.
|
Git remote.
|
||||||
|
|
||||||
```
|
```bash
|
||||||
$ git remote -v
|
$ git remote -v
|
||||||
heroku https://git.heroku.com/chill-ping-bot.git (fetch)
|
heroku https://git.heroku.com/chill-ping-bot.git (fetch)
|
||||||
heroku https://git.heroku.com/chill-ping-bot.git (push)
|
heroku https://git.heroku.com/chill-ping-bot.git (push)
|
||||||
@@ -143,32 +143,32 @@ With the Heroku application created, we can begin configuring our repository to
|
|||||||
## Creating a Procfile
|
## Creating a Procfile
|
||||||
|
|
||||||
As explained earlier, the `Procfile` acts as a build instruction manual for our application. It instructs Heroku how we
|
As explained earlier, the `Procfile` acts as a build instruction manual for our application. It instructs Heroku how we
|
||||||
want to run our application. Heroku takes over the rest and helps with managing our build environment.
|
want to run our application. Heroku takes over the rest and helps with managing our build environment.
|
||||||
|
|
||||||
For my sample bot, the `Procfile` looks like this:
|
For my sample bot, the `Procfile` looks like this:
|
||||||
|
|
||||||
```
|
```yaml
|
||||||
worker: java -jar target/Bot.jar
|
worker: java -jar target/Bot.jar
|
||||||
```
|
```
|
||||||
|
|
||||||
Let's breakdown this file. We first declare the dyno type as `worker`. Then, we specify the command to run our `.jar`
|
Let's breakdown this file. We first declare the dyno type as `worker`. Then, we specify the command to run our `.jar`
|
||||||
file.
|
file.
|
||||||
|
|
||||||
Heroku is able to intelligently detect that our Kotlin application uses Maven as a build tool and runs the
|
Heroku is able to intelligently detect that our Kotlin application uses Maven as a build tool and runs the
|
||||||
`mvn clean install` command to create our `Bot.jar` file. Then, it will use the commands in the `Procfile` to run the
|
`mvn clean install` command to create our `Bot.jar` file. Then, it will use the commands in the `Procfile` to run the
|
||||||
application.
|
application.
|
||||||
|
|
||||||
## Securing Discord bot tokens
|
## Securing Discord bot tokens
|
||||||
|
|
||||||
A Discord bot requires a token to run.
|
A Discord bot requires a token to run.
|
||||||
|
|
||||||
You can obtain this bot token when you make a new Discord bot from the Discord
|
You can obtain this bot token when you make a new Discord bot from the Discord
|
||||||
[developer dashboard](https://discordpy.readthedocs.io/en/latest/discord.html).
|
[developer dashboard](https://discordpy.readthedocs.io/en/latest/discord.html).
|
||||||
However, you do not want to expose this token in your repository as this would mean that others could launch and
|
However, you do not want to expose this token in your repository as this would mean that others could launch and
|
||||||
access your bot.
|
access your bot.
|
||||||
|
|
||||||
As mentioned earlier, we will make use of Heroku's [config vars](https://devcenter.heroku.com/articles/config-vars) to
|
As mentioned earlier, we will make use of Heroku's [config vars](https://devcenter.heroku.com/articles/config-vars) to
|
||||||
safely store and access this token.
|
safely store and access this token.
|
||||||
|
|
||||||
We will add our bot's token as an environment variable and use `System.getenv()` method to retrieve this value.
|
We will add our bot's token as an environment variable and use `System.getenv()` method to retrieve this value.
|
||||||
|
|
||||||
@@ -184,11 +184,11 @@ val token = System.getenv("BOT_TOKEN")
|
|||||||
```
|
```
|
||||||
|
|
||||||
This will retrieve the corresponding environment variable that we have stored in Heroku. If there is no environment
|
This will retrieve the corresponding environment variable that we have stored in Heroku. If there is no environment
|
||||||
variable present, we will stop the bot from launching and display an error.
|
variable present, we will stop the bot from launching and display an error.
|
||||||
|
|
||||||
An additional benefit of storing our bot tokens as an environment variable is that we are able to store the bot token
|
An additional benefit of storing our bot tokens as an environment variable is that we are able to store the bot token
|
||||||
locally as an environment variable which streamlines our development process as we could have a separate token used
|
locally as an environment variable which streamlines our development process as we could have a separate token used
|
||||||
for a development/testing bot.
|
for a development/testing bot.
|
||||||
|
|
||||||
## Launching the bot
|
## Launching the bot
|
||||||
|
|
||||||
@@ -201,40 +201,40 @@ $ git push heroku master
|
|||||||
```
|
```
|
||||||
|
|
||||||
If you encounter a problem with pushing to the `heroku` remote, use the command `heroku logs --tail` and find the
|
If you encounter a problem with pushing to the `heroku` remote, use the command `heroku logs --tail` and find the
|
||||||
latest error messages to debug any errors.
|
latest error messages to debug any errors.
|
||||||
|
|
||||||
After pushing the changes, Heroku will build your application. However, it is not online yet as you have to scale
|
After pushing the changes, Heroku will build your application. However, it is not online yet as you have to scale
|
||||||
your application. This tells Heroku how many instances of your application you wish to run. For our case, we can go
|
your application. This tells Heroku how many instances of your application you wish to run. For our case, we can go
|
||||||
with one worker dyno.
|
with one worker dyno.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ heroku ps:scale worker=1
|
$ heroku ps:scale worker=1
|
||||||
```
|
```
|
||||||
|
|
||||||
You can now invite your bot to a server and test it out. If you're using the sample PingBot, you can use `!ping` and
|
You can now invite your bot to a server and test it out. If you're using the sample PingBot, you can use `!ping` and
|
||||||
expect the bot to respond with `Pong!`.
|
expect the bot to respond with `Pong!`.
|
||||||
|
|
||||||
## Now what?
|
## Now what?
|
||||||
|
|
||||||
Congratulations! You have just deployed a Discord bot onto Heroku! When you make changes to the bot, you are free to
|
Congratulations! You have just deployed a Discord bot onto Heroku! When you make changes to the bot, you are free to
|
||||||
commit and push those changes to the `heroku` remote to update the bot that is online.
|
commit and push those changes to the `heroku` remote to update the bot that is online.
|
||||||
|
|
||||||
Here are some tips for developing with Heroku.
|
Here are some tips for developing with Heroku.
|
||||||
|
|
||||||
1. While working on your development copy of the bot, it is recommended that you obtain a seprate bot token and
|
1. While working on your development copy of the bot, it is recommended that you obtain a seprate bot token and
|
||||||
attach it as an environment variable to your local development environment. Doing so allows you to maintain your
|
attach it as an environment variable to your local development environment. Doing so allows you to maintain your
|
||||||
bot's uptime while making changes.
|
bot's uptime while making changes.
|
||||||
2. If you encounter any errors or your bot is not responding, use the `heroku logs --tail` command to view the logs
|
2. If you encounter any errors or your bot is not responding, use the `heroku logs --tail` command to view the logs
|
||||||
of your application. Doing so allows you to check if there were any errors while running your project.
|
of your application. Doing so allows you to check if there were any errors while running your project.
|
||||||
3. If you require persistent storage, Heroku comes with a free tier plugin for
|
3. If you require persistent storage, Heroku comes with a free tier plugin for
|
||||||
[PostgreSQL.](https://www.heroku.com/postgres) Heroku - by default - has ephemeral storage, meaning it does not
|
[PostgreSQL.](https://www.heroku.com/postgres) Heroku - by default - has ephemeral storage, meaning it does not
|
||||||
maintain new files after each build.
|
maintain new files after each build.
|
||||||
|
|
||||||
## Conclusion
|
## Conclusion
|
||||||
|
|
||||||
Heroku offers a free alternative to many hosting platforms and is a perfect platform for aspiring bot developers to begin.
|
Heroku offers a free alternative to many hosting platforms and is a perfect platform for aspiring bot developers to begin.
|
||||||
|
|
||||||
More resources on hosting JVM-based applications on Heroku:
|
More resources on hosting JVM-based applications on Heroku:
|
||||||
|
|
||||||
- [Getting Started on Heroku with Java](https://devcenter.heroku.com/articles/getting-started-with-java#introduction)
|
- [Getting Started on Heroku with Java](https://devcenter.heroku.com/articles/getting-started-with-java#introduction)
|
||||||
- [Java Sample (on GitHub)](https://github.com/heroku/java-sample)
|
- [Java Sample (on GitHub)](https://github.com/heroku/java-sample)
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ And we can keep going on and on, creating more arrays inside the arrays.
|
|||||||
|
|
||||||
To create multidimensional array, you do it like a regular array but with a extra `[]` at the end.
|
To create multidimensional array, you do it like a regular array but with a extra `[]` at the end.
|
||||||
|
|
||||||
##### Method 1:
|
#### Method 1:
|
||||||
|
|
||||||
Use
|
Use
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ This will make a empty 2x3 matrix that looks like:
|
|||||||
|
|
||||||
So there are 2 arrays with 3 elements each within the array `arr`.
|
So there are 2 arrays with 3 elements each within the array `arr`.
|
||||||
|
|
||||||
##### Method 2:
|
#### Method 2:
|
||||||
|
|
||||||
Use:
|
Use:
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ This will make a 2x3 matrix with preset values instead of all 0's so it'll look
|
|||||||
{{1,2,3},{4,5,6}}
|
{{1,2,3},{4,5,6}}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Method 3:
|
#### Method 3:
|
||||||
|
|
||||||
Combine both:
|
Combine both:
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ This will make a 2x3 matrix that looks like:
|
|||||||
{{1,2,3},{4,5,6}}
|
{{1,2,3},{4,5,6}}
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Note:
|
#### Note:
|
||||||
|
|
||||||
It is possible to go ever further, and so a declaration like `data_type array_name[A][B][C]` is possible and would add more layers to your array. So after going through one layer, you have to input the location of the next layer, and so on and so forth until you reach the deepest layer of the `{}` where you'll find the object.
|
It is possible to go ever further, and so a declaration like `data_type array_name[A][B][C]` is possible and would add more layers to your array. So after going through one layer, you have to input the location of the next layer, and so on and so forth until you reach the deepest layer of the `{}` where you'll find the object.
|
||||||
|
|
||||||
@@ -108,9 +108,9 @@ This will make an 2x3x4 matrix that looks like:
|
|||||||
{{{0,0,0,0},{0,0,0,0},{0,0,0,0}},{{0,0,0,0},{0,0,0,0},{0,0,0,0}}}
|
{{{0,0,0,0},{0,0,0,0},{0,0,0,0}},{{0,0,0,0},{0,0,0,0},{0,0,0,0}}}
|
||||||
```
|
```
|
||||||
|
|
||||||
### What can you do with it?
|
## What can you do with it?
|
||||||
|
|
||||||
##### Calling objects
|
#### Calling objects
|
||||||
|
|
||||||
To get the object inside, you can call for the object inside a multidimensional array by putting a integer inside the `[]`.
|
To get the object inside, you can call for the object inside a multidimensional array by putting a integer inside the `[]`.
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ This would output `2` because using array indicies, `2` is the object in positio
|
|||||||
You must put an integer in both `[]`, or else you will get an error.
|
You must put an integer in both `[]`, or else you will get an error.
|
||||||
There is no syntax to instantly view the whole array, but an easy way is to loop through the whole array and outputting it.
|
There is no syntax to instantly view the whole array, but an easy way is to loop through the whole array and outputting it.
|
||||||
|
|
||||||
##### Reassigning objects
|
#### Reassigning objects
|
||||||
|
|
||||||
Like with a regular array, you can also reassign what object is being held inside a exact position using `=`
|
Like with a regular array, you can also reassign what object is being held inside a exact position using `=`
|
||||||
So let's take for example the array made in example 2 above, which looks like:
|
So let's take for example the array made in example 2 above, which looks like:
|
||||||
@@ -142,7 +142,7 @@ This would cause the array to change to looking like:
|
|||||||
{{1,2,3},{4,1337,6}}
|
{{1,2,3},{4,1337,6}}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Restrictions
|
## Restrictions
|
||||||
|
|
||||||
1. The size is locked upon initialization. Once you make the array, its size is locked. For example, if you make a 2x3 array, and realize you need another column or row, you have to either make a new one, or change the size where you initialize it. If you want a dynamic size array, a array which you can change the size of, consider using `std::vector` instead.
|
1. The size is locked upon initialization. Once you make the array, its size is locked. For example, if you make a 2x3 array, and realize you need another column or row, you have to either make a new one, or change the size where you initialize it. If you want a dynamic size array, a array which you can change the size of, consider using `std::vector` instead.
|
||||||
2. There is no way to search except by iterating through each object in the entire array. For example, if you need to find the number `2` in:
|
2. There is no way to search except by iterating through each object in the entire array. For example, if you need to find the number `2` in:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
authors:
|
authors:
|
||||||
- "cros#0001"
|
- "cros#0001"
|
||||||
created_at: "2020/04/23"
|
created_at: "2020/04/23"
|
||||||
title: The Past, Present, and the Future
|
title: The Past, Present, and the Future
|
||||||
---
|
---
|
||||||
@@ -16,6 +16,7 @@ This document will constantly evolve to document this everlasting war. We don't
|
|||||||
## The Past
|
## The Past
|
||||||
|
|
||||||
### The War of Xerxes & Greeks
|
### The War of Xerxes & Greeks
|
||||||
|
|
||||||
Some of the earliest accounts of hiding secrets date back to Herodotus, a legendary figure known as "the father of history". Herodotus in one of his accounts, chronicles the conflict of Persia and Xerxes, the despotic king of Persians. It was art of hiding secrets that saved Persia from falling into slavery.
|
Some of the earliest accounts of hiding secrets date back to Herodotus, a legendary figure known as "the father of history". Herodotus in one of his accounts, chronicles the conflict of Persia and Xerxes, the despotic king of Persians. It was art of hiding secrets that saved Persia from falling into slavery.
|
||||||
|
|
||||||
Greek and Persia are both known for their well running feud. This feud reached a never before seen crisis after Xerxes started construction of the new capital of his great kingdom. Gifts and tributes arrived from kings, merchants, and the likes with the sole exception of Athens and Sparta. Xerxes was extremely proud and this insolence has reached his bottom line. Xerxes started mobilizing a force and declared that he will conquer the world and Persia's boundary would be the heavens itself. He actually assembled the greatest fighting force in the history of mankind, and that too in secrecy, and was ready to launch an attack.
|
Greek and Persia are both known for their well running feud. This feud reached a never before seen crisis after Xerxes started construction of the new capital of his great kingdom. Gifts and tributes arrived from kings, merchants, and the likes with the sole exception of Athens and Sparta. Xerxes was extremely proud and this insolence has reached his bottom line. Xerxes started mobilizing a force and declared that he will conquer the world and Persia's boundary would be the heavens itself. He actually assembled the greatest fighting force in the history of mankind, and that too in secrecy, and was ready to launch an attack.
|
||||||
@@ -70,7 +71,7 @@ When she returned back to Scotland, the nation was completely different. Scotlan
|
|||||||
|
|
||||||
Her next marriage was hardly more successful. James Hepburn, was publically considered the murderer of Mary's husband. Further events occurred, and the protestant nobles rebelled against Mary. Mary was imprisoned. However, next year she escaped from her prison, and fled for her life to England through a fishing boat. She had hoped that Elizabeth, her cousin, would provide her refuge. This was a big mistake, as English Catholics considered Mary, a catholic, to be the true queen of England. Thus Elizabeth imprisioned Mary, on paper the reason was her connection with hthe murder of Darnley fearing her rise.
|
Her next marriage was hardly more successful. James Hepburn, was publically considered the murderer of Mary's husband. Further events occurred, and the protestant nobles rebelled against Mary. Mary was imprisoned. However, next year she escaped from her prison, and fled for her life to England through a fishing boat. She had hoped that Elizabeth, her cousin, would provide her refuge. This was a big mistake, as English Catholics considered Mary, a catholic, to be the true queen of England. Thus Elizabeth imprisioned Mary, on paper the reason was her connection with hthe murder of Darnley fearing her rise.
|
||||||
|
|
||||||
Mary was imprisoned for 18 years. Her sole crime was her existence. This had pushed her to limit. Some of her catholics supporters who were increasingly being harassed and disposed by the protestants, hatched a plan to rescue her. Mary, then masterminded a plan to assassinate Elizabeth. As she is next in line for the throne, she would effectively be able to escape imprisonment. Mary designed a cipher to communicate with her correspondents, consisting of cipher alphabets and code words. Her plans were going well, and she would have succeeded, but her courier was actually double agent to Walsingham, principal secretary to the Queen. While Mary used a fairly complicated cipher, her correspondents only sent an extremely simple cipher when replying to her. Walsingham was a firm believer in cryptography. He has learnt his lessons and understood the power of cryptography after he was able to deter an invasion of England thanks to it. So he was very ready to fight off this threat. He laid down waiting for the right moment to strike.
|
Mary was imprisoned for 18 years. Her sole crime was her existence. This had pushed her to limit. Some of her catholics supporters who were increasingly being harassed and disposed by the protestants, hatched a plan to rescue her. Mary, then masterminded a plan to assassinate Elizabeth. As she is next in line for the throne, she would effectively be able to escape imprisonment. Mary designed a cipher to communicate with her correspondents, consisting of cipher alphabets and code words. Her plans were going well, and she would have succeeded, but her courier was actually double agent to Walsingham, principal secretary to the Queen. While Mary used a fairly complicated cipher, her correspondents only sent an extremely simple cipher when replying to her. Walsingham was a firm believer in cryptography. He has learnt his lessons and understood the power of cryptography after he was able to deter an invasion of England thanks to it. So he was very ready to fight off this threat. He laid down waiting for the right moment to strike.
|
||||||
|
|
||||||
Walsingham had to convince Queen Elizabeth of Mary's guilt. While Queen Elizabeth despised Mary, she was a Scottish Queen and there would be a giant controversy if a head of a state could execute a foreign head of state. Elizabeth wasn't willing to try this gamble as this would set an interesting precedent, which means that rebels wouldn't have much reservations about killing another Queen. Her name? Elizabeth. Then they were related by blood, and it's not exactly a great feeling to execute your own family, especially someone who's life was such a tragedy. The essence of this assasination was the exact opposite of what caused her abdication from the throne. In this case, some catholic noblemen wanted to replace Elizabeth,a protestant with a catholic queen, and Mary perfectly fit the bill. It was clear that Mary was involved in the incident, but they were not sure if Mary was a figurehead, or if she was the mastermind herself. Thus, Walsingham's challenge was to find definitive proof that could link Mary as the mastermind behind plotting the assasination of the Queen of England.
|
Walsingham had to convince Queen Elizabeth of Mary's guilt. While Queen Elizabeth despised Mary, she was a Scottish Queen and there would be a giant controversy if a head of a state could execute a foreign head of state. Elizabeth wasn't willing to try this gamble as this would set an interesting precedent, which means that rebels wouldn't have much reservations about killing another Queen. Her name? Elizabeth. Then they were related by blood, and it's not exactly a great feeling to execute your own family, especially someone who's life was such a tragedy. The essence of this assasination was the exact opposite of what caused her abdication from the throne. In this case, some catholic noblemen wanted to replace Elizabeth,a protestant with a catholic queen, and Mary perfectly fit the bill. It was clear that Mary was involved in the incident, but they were not sure if Mary was a figurehead, or if she was the mastermind herself. Thus, Walsingham's challenge was to find definitive proof that could link Mary as the mastermind behind plotting the assasination of the Queen of England.
|
||||||
|
|
||||||
@@ -80,5 +81,4 @@ Unfortunately for her, Walsingham had another designation. He was England's spym
|
|||||||
|
|
||||||
Mary was soon caught, and was sent to trial. Although, they used a cipher, there belonged to an era where ciphers were constantly being weakened by advances in cryptanalysis. It stood no chance against Phellippes. There was sufficient proof, and death penalty was recommended. Elizabeth signed her death warrant. With her family motto in mind, "In my end is my beginning", she approached the block. The executioners requested her forgiveness, and she was finally released from her misery.
|
Mary was soon caught, and was sent to trial. Although, they used a cipher, there belonged to an era where ciphers were constantly being weakened by advances in cryptanalysis. It stood no chance against Phellippes. There was sufficient proof, and death penalty was recommended. Elizabeth signed her death warrant. With her family motto in mind, "In my end is my beginning", she approached the block. The executioners requested her forgiveness, and she was finally released from her misery.
|
||||||
|
|
||||||
|
|
||||||
This document is a work in progress, and will be constantly updated.
|
This document is a work in progress, and will be constantly updated.
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ title: Introduction to Cryptography
|
|||||||
- [The Codebreakers](https://www.amazon.com/Codebreakers-Comprehensive-History-Communication-Internet/dp/0684831309)
|
- [The Codebreakers](https://www.amazon.com/Codebreakers-Comprehensive-History-Communication-Internet/dp/0684831309)
|
||||||
- [Seizing the Enigma: The Race to Break the German U-Boats Codes](https://www.amazon.com/Seizing-Enigma-German-U-Boats-1939-1943/dp/0395427398)
|
- [Seizing the Enigma: The Race to Break the German U-Boats Codes](https://www.amazon.com/Seizing-Enigma-German-U-Boats-1939-1943/dp/0395427398)
|
||||||
|
|
||||||
|
|
||||||
## Books
|
## Books
|
||||||
|
|
||||||
- [Handbook of Applied Cryptography - Free](http://cacr.uwaterloo.ca/hac/)
|
- [Handbook of Applied Cryptography - Free](http://cacr.uwaterloo.ca/hac/)
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
---
|
---
|
||||||
authors:
|
authors:
|
||||||
- "AstronautEVA#0331"
|
- "AstronautEVA#0331"
|
||||||
title: "What is a class?"
|
title: "Classes"
|
||||||
created_at: 2019/10/20
|
created_at: 2019/10/20
|
||||||
---
|
---
|
||||||
|
|
||||||
# What is a class?
|
## What is a class?
|
||||||
|
|
||||||
Oftentimes in programs, it is necessary to represent objects from the real world as computer data.
|
Oftentimes in programs, it is necessary to represent objects from the real world as computer data.
|
||||||
For example, if someone is writing a program that simulates a zoo, they will need to create some animals.
|
For example, if someone is writing a program that simulates a zoo, they will need to create some animals.
|
||||||
@@ -56,7 +56,7 @@ public class Animal {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Class vs Object
|
## Class vs Object
|
||||||
|
|
||||||
Remember this is only the description of what an animal is, not a specific animal. A specific animal such as Max is one instance of a class and is called an object. It has values assigned to its attributes like the name is Max and the age is 5. You can call methods on the object such as move or sleep. And there can be an infinite number of objects constructed from a class. To create objects from your class, you need something called a constructor.
|
Remember this is only the description of what an animal is, not a specific animal. A specific animal such as Max is one instance of a class and is called an object. It has values assigned to its attributes like the name is Max and the age is 5. You can call methods on the object such as move or sleep. And there can be an infinite number of objects constructed from a class. To create objects from your class, you need something called a constructor.
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
---
|
---
|
||||||
authors:
|
authors:
|
||||||
- "AstronautEVA#0331"
|
- "AstronautEVA#0331"
|
||||||
title: "What is Inheritance?"
|
title: "Inheritance"
|
||||||
created_at: 2019/10/24
|
created_at: 2019/10/24
|
||||||
recommended_reading:
|
recommended_reading:
|
||||||
- java/class
|
- java/class
|
||||||
---
|
---
|
||||||
|
|
||||||
# What is Inheritance?
|
## What is Inheritance?
|
||||||
|
|
||||||
Inheritance is the idea that when two classes have similar implementations, rather than duplicating code for each class one can obtain it from the other. Much like a human child will inherit traits from their parents, a child class will inherit data from
|
Inheritance is the idea that when two classes have similar implementations, rather than duplicating code for each class one can obtain it from the other. Much like a human child will inherit traits from their parents, a child class will inherit data from
|
||||||
its parent class.
|
its parent class.
|
||||||
@@ -123,7 +123,7 @@ public class Animal {
|
|||||||
|
|
||||||
It can be used in our subclass.
|
It can be used in our subclass.
|
||||||
|
|
||||||
```
|
```java
|
||||||
Animal myAnimal = new Animal();
|
Animal myAnimal = new Animal();
|
||||||
myAnimal.sleep(); // returns "The animal sleeps."
|
myAnimal.sleep(); // returns "The animal sleeps."
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ public class Bird extends Animal {
|
|||||||
|
|
||||||
It cannot be used in our superclass.
|
It cannot be used in our superclass.
|
||||||
|
|
||||||
```
|
```java
|
||||||
Animal myAnimal = new Animal();
|
Animal myAnimal = new Animal();
|
||||||
myAnimal.chirp(); // error, the method does not exist
|
myAnimal.chirp(); // error, the method does not exist
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ external_resources:
|
|||||||
href: https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
|
href: https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
|
||||||
---
|
---
|
||||||
|
|
||||||
## An introduction to Streams
|
|
||||||
|
|
||||||
Basically everyone, who spent a few hours coding some Java came across a situation like this:
|
Basically everyone, who spent a few hours coding some Java came across a situation like this:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@@ -30,7 +28,7 @@ Arrays.stream(intArray).forEach(n -> System.out.println(n));
|
|||||||
|
|
||||||
You just made one line out of three. But how does this work? `int[] intArray = {1,2,3,4,5};` does just declare and initialize a new array of integers. This stays the same as before, because you can use the `Arrays.stream()` method for every Array, [as it works with generic Arrays](https://www.mkyong.com/java8/java-how-to-convert-array-to-stream/). This method creates a [Stream Object](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html), that has several methods to perform operations on an array.
|
You just made one line out of three. But how does this work? `int[] intArray = {1,2,3,4,5};` does just declare and initialize a new array of integers. This stays the same as before, because you can use the `Arrays.stream()` method for every Array, [as it works with generic Arrays](https://www.mkyong.com/java8/java-how-to-convert-array-to-stream/). This method creates a [Stream Object](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html), that has several methods to perform operations on an array.
|
||||||
|
|
||||||
### Sorting
|
## Sorting
|
||||||
|
|
||||||
Now maybe you have a list instead of an array and you also want the list to get sorted before you print it. Without Streams you would first have to sort the list and then iterate through it. Way too many lines! Using Streams you can simply do:
|
Now maybe you have a list instead of an array and you also want the list to get sorted before you print it. Without Streams you would first have to sort the list and then iterate through it. Way too many lines! Using Streams you can simply do:
|
||||||
|
|
||||||
@@ -46,7 +44,7 @@ intList.stream().sorted().forEach(n -> System.out.print(n + ", "));
|
|||||||
|
|
||||||
At the end, you have again only one line, for sorting and printing every element in a list. Due to the fact that Streams mostly use Method Chaining, you could perform a bunch of operations.
|
At the end, you have again only one line, for sorting and printing every element in a list. Due to the fact that Streams mostly use Method Chaining, you could perform a bunch of operations.
|
||||||
|
|
||||||
### Filtering
|
## Filtering
|
||||||
|
|
||||||
Now you are able to print out every element of an array, a list, a stack, etc. sorted as well as unsorted. The last essential operation is, how to filter your array/list. One often has to only get specific elements of an array. Using Streams you could do it like this:
|
Now you are able to print out every element of an array, a list, a stack, etc. sorted as well as unsorted. The last essential operation is, how to filter your array/list. One often has to only get specific elements of an array. Using Streams you could do it like this:
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ completed at a later time. Much like in real life, when you create a promise, yo
|
|||||||
to fulfill that promise. However, sometimes things go wrong where you can no longer fulfill
|
to fulfill that promise. However, sometimes things go wrong where you can no longer fulfill
|
||||||
a promise you made. This is essentially the main idea behind how promises work in javascript.
|
a promise you made. This is essentially the main idea behind how promises work in javascript.
|
||||||
|
|
||||||
### Basics
|
## Basics
|
||||||
|
|
||||||
When you create a Promise or call a function that returns a Promise in Javascript, you're left
|
When you create a Promise or call a function that returns a Promise in Javascript, you're left
|
||||||
with an object that can either resolve into the actual value that you were promised, or it
|
with an object that can either resolve into the actual value that you were promised, or it
|
||||||
@@ -24,7 +24,7 @@ can reject and leave you with an error for why that promise failed.
|
|||||||
|
|
||||||
We can access these values using the `.then` and `.catch` methods on the `Promise` object respectively.
|
We can access these values using the `.then` and `.catch` methods on the `Promise` object respectively.
|
||||||
|
|
||||||
### A Simple Example
|
## A Simple Example
|
||||||
|
|
||||||
First, let's explore a bit of a made-up example. Imagine we have a promise-returning function
|
First, let's explore a bit of a made-up example. Imagine we have a promise-returning function
|
||||||
called `getMembers` that retrieves all the members in a discord server. When we execute this
|
called `getMembers` that retrieves all the members in a discord server. When we execute this
|
||||||
@@ -50,7 +50,7 @@ getMembers("The Programmers Hangout").then(members => {
|
|||||||
|
|
||||||
This way we are able to make sure that we only try to `console.log` when the `getMembers` function has resolved and ready to be used.
|
This way we are able to make sure that we only try to `console.log` when the `getMembers` function has resolved and ready to be used.
|
||||||
|
|
||||||
### Beginner Mistakes
|
## Beginner Mistakes
|
||||||
|
|
||||||
Promises are possibly the #1 most common source of confusion for beginners. In order
|
Promises are possibly the #1 most common source of confusion for beginners. In order
|
||||||
to avoid falling in pitfalls yourself, you have to remember 2 things about Javascript when
|
to avoid falling in pitfalls yourself, you have to remember 2 things about Javascript when
|
||||||
@@ -85,7 +85,7 @@ getWeather("Los Angeles").then(weather => {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Real World Example
|
## Real World Example
|
||||||
|
|
||||||
Here is a real function that gets character information from the Rick and Morty API.
|
Here is a real function that gets character information from the Rick and Morty API.
|
||||||
You can try it in your browser if you want to test it out.
|
You can try it in your browser if you want to test it out.
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ external_resources:
|
|||||||
href: "https://www.freecodecamp.org/news/spread-operator-and-rest-parameter-in-javascript-es6-4416a9f47e5e/"
|
href: "https://www.freecodecamp.org/news/spread-operator-and-rest-parameter-in-javascript-es6-4416a9f47e5e/"
|
||||||
---
|
---
|
||||||
|
|
||||||
# An overview of the spread operator
|
|
||||||
|
|
||||||
Spread operator (or spread syntax) is a powerful feature in Javascript which allows you to do such things as merging or copying objects, expanding an array into function arguments and a lot more. In this post, we are going to cover most of its use-cases.
|
Spread operator (or spread syntax) is a powerful feature in Javascript which allows you to do such things as merging or copying objects, expanding an array into function arguments and a lot more. In this post, we are going to cover most of its use-cases.
|
||||||
|
|
||||||
## Copying an object
|
## Copying an object
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ created_at: "2019/10/06"
|
|||||||
title: Variables
|
title: Variables
|
||||||
---
|
---
|
||||||
|
|
||||||
## Variables
|
|
||||||
|
|
||||||
A `variable` in Javascript is a "named container" that can hold data. Variables are declared by giving them a name and a value - the `name` allows you to reference the variable throughout your program, and the `value` is the current value the variable represents.
|
A `variable` in Javascript is a "named container" that can hold data. Variables are declared by giving them a name and a value - the `name` allows you to reference the variable throughout your program, and the `value` is the current value the variable represents.
|
||||||
|
|
||||||
Variables can be defined in the following ways:
|
Variables can be defined in the following ways:
|
||||||
@@ -117,15 +115,15 @@ console.log(myVar); // 'test'
|
|||||||
This is fixed using `let`
|
This is fixed using `let`
|
||||||
|
|
||||||
```js
|
```js
|
||||||
for(let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
console.log(i);
|
console.log(i);
|
||||||
}
|
}
|
||||||
console.log(i); // Error: i is undefined
|
console.log(i); // Error: i is undefined
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
let myVar = 'test';
|
let myVar = "test";
|
||||||
}
|
}
|
||||||
console.log(myVar) // Error: myVar is undefined
|
console.log(myVar); // Error: myVar is undefined
|
||||||
```
|
```
|
||||||
|
|
||||||
However, if the above code was in a function, this would not be the case. In the case of a function, the variable will be scoped to the given function, and not accessible outside it. Eg:
|
However, if the above code was in a function, this would not be the case. In the case of a function, the variable will be scoped to the given function, and not accessible outside it. Eg:
|
||||||
|
|||||||
Reference in New Issue
Block a user