
Due to numerous reader requests, a large series of articles begins on the application of serverless computing technology for developing a real application. This series will cover application development, testing, and deployment to end-users using modern tools: microservices architecture (in a serverless variant, based on ), a cluster for deploying the application, a database , oriented towards cloud clustering and application, as well as a cloud bus . The application implements the game "Bylinas", one variation of the popular parlor game "Mafia".
What is "Bylinas"?
It is a variation of the game "Mafia", also known as "Werewolf". At its core, it’s a team game where participants must gradually figure out who is who and try to win. Unfortunately, when played online, a crucial aspect of the game—personal interaction—disappears, and because the rules of classic "Mafia" are quite simple, additional characters are usually added for a more nonlinear and engaging gameplay process, while still retaining the main features of the original "Mafia", such as the transition between day and night, actions occurring only at night, as well as alliances between participants. Another significant difference in online gameplay is that the host (also known as the Game Master, Storyteller) is usually a computer program.
Game Description
The rules of the game I want to implement are taken from an old IRC bot that I saved in my personal archive about 10 years ago. "Bylinas" has a backstory that starts every game:
In the ninth kingdom, in the thirtieth state, beyond seven seas, there lived several villages, and in them Brave young men and Beautiful maidens. They sowed grain and wandered into the nearby forest to gather mushrooms and berries… And thus it continued from generation to generation, until a terrible catastrophe shook the Earth and evil began to spread across the world! Nights grew long and cold, and in the darkness, terrible creatures prowled the forests and wandered into the villages. From somewhere came Zmey Gorynych and began to steal the Beautiful maidens and take everything valuable from the peasants. The harmful and greedy Baba Yaga, arriving on a broom from distant forests, unsettled the minds of the inhabitants, and even some abandoned their crafts and went into the woods to become bandits, forming a gang there. The rogues encountered Leshy, who could transform into trees and bushes, began to watch the peaceful villagers and serve the bandits, sniffing around to see if the Brave Young Men were planning anything to rid their settlements of the evil. The Brave Young Men and the Beautiful Maidens, tired of the bandit raids and the terrible deaths at the hands of the dreadful One-Eyed Woe, gathered gold and invited a renowned warrior from a neighboring city — Ivan Tsarevich, who promised to rid the village of the bandits. In a clearing in the forest, Ivan saved from imminent death Gray Wolf, who had fallen into a bandit trap. In return, the Wolf promised to inform the Tsarevich about various forest evils. A famous healer passed by Vasilisa the Wise, and when she saw the trouble, she stayed to care for the villagers who suffered from the attacks of the wicked. Behind the forest appeared a black palace, where, according to rumors, settled Koshchei the Deathless, who visited the villages every night and enchanted the Brave Young Men and the Beautiful Maidens, so they would not dare disobey his commands, doing exactly as he said. And in the Lifeless Forest dwelled Cat-Bayun, and anyone who encountered him fell into a deathly sleep after his tales or perished from his iron claws.

The Three-Nines Kingdom
As already evident, the players are divided into several groups:
- peaceful inhabitants (Brave Young Men, Beautiful Maidens, Ivan Tsarevich, Gray Wolf, and Vasilisa the Wise)
- bandits (the bandits themselves, as well as Baba Yaga and Leshy)
- independents (Zmey Gorynych, One-Eyed Woe, Princess Frog, Koshchei the Deathless, Cat-Bayun)
The goal of the game, as mentioned above, is to survive and win. Opponents must leave the game one way or another, and independents must also stay alive until the end of the game. There is gold in the game, a kind of game currency that players earn only within the game. The winners receive gold. The more gold, the higher the player's rating.
I will dwell a little more on the description of the characters.

Brave Young Man

Beautiful Maiden
Brave Young Man and Beautiful Maiden is the most common and primary role in the game. These are peaceful villagers who sleep at night and work during the day. At night, they are attacked by one of the robbers, Zmey Gorynych, and other roles, while Vasilisa the Wise heals them. With a certain small probability, the Good Young Man or Red Maiden may survive an attack without harm (possibly losing gold in the process); however, everyone learns the player's pseudonym the very next day after the attack. At night, these players do not make any moves but analyze the game situation based on messages in the game chat. During the day, these players decide by voting who among them is not a Good Young Man or Red Maiden. The player who receives the majority of votes from other players is eliminated from the game, while the remaining players either gain or lose gold. If the players do not choose anyone by majority vote, no players will be executed.

Ivan Tsarevich
Ivan Tsarevich is originally an anonymous protector of peaceful villagers. At night, he checks the roles of other players, as he only knows one ally — the Gray Wolf. With the direct participation of the Gray Wolf (who can also check the roles of other players), Ivan Tsarevich can, instead of checking, kill another character at night. If during his check Ivan Tsarevich sees the role of the Good Young Man or Red Maiden in a player, he can invite them to meet the Gray Wolf and other Good Young Men and Red Maidens. Ivan may be hindered by the Princess Frog, who can seduce him at night without revealing his role to other players during the day. If Ivan discovers the Princess Frog himself, he can offer her to join the peaceful villagers; however, if the Princess refuses, she dies at Ivan's hand. Zmey Gorynych can also obstruct Ivan Tsarevich's checks, but unlike the Princess Frog, he will tell the other players during the day who among them is Ivan Tsarevich. During the day, Ivan Tsarevich has no distinction from other Good Young Men.

Gray Wolf
Gray Wolf — the assistant of Ivan Tsarevich, whose sharp instincts help Ivan find other Good Fellows and Red Maidens. The Gray Wolf tells these players who Ivan Tsarevich is and also informs about other players with the roles of Good Fellows and Red Maidens. If the Wolf discovers a bandit or another opponent, he immediately informs Ivan Tsarevich so that he can take action the next night. If the Princess-Frog attacks the Wolf, he turns into an ordinary Good Fellow and cannot check anyone, and the Princess will not know that it was really the Gray Wolf, as the Wolf does not sleep at night. However, during the day, the Wolf learns who the players are—the Princess-Frog—and can try to persuade the other Good Fellows and Red Maidens he brought to Ivan Tsarevich to vote for the execution of the Princess-Frog. He may also try to anonymously sway the Princess-Frog to side with the peaceful villagers the next night so that she does not harm any of them. At night, the Wolf can sacrifice himself to save Ivan Tsarevich or Vasilisa the Wise if he suspects that they might suddenly come under attack from bandits or have been zombified by Koschei (the Wolf has an innate immunity to Koschei's charms); however, the Wolf is eliminated from the game after self-sacrifice.

Vasilisa the Wise
Vasilisa the Wise — plays for the peaceful villagers, but they do not know about her because Vasilisa is very modest. Also, Vasilisa the Wise does not ask questions when healing and treats everyone like a good doctor. But if Koschei, Likhо, or Leshiy drink her medicine, they will not survive more than one day, as Vasilisa only heals humans. Zmey Gorynych or Kot-Bayun will also not benefit from Vasilisa's medicine, but it will not harm them either. Additionally, the night Kot-Bayun does not disturb Vasilisa because she does not go for healing herbs in the Lifeless Forest. Moreover, the feminine charms of the Princess-Frog do not affect Vasilisa. If her patient is attempted to be killed twice, her medicine will prove ineffective. Vasilisa cannot save from magical attacks, for example, from Likhо's curse. During the day, Vasilisa behaves like a Red Maiden, and only a fleeting, slightly sad glance might hint that she is the best healer in the Three-Nine Kingdom.

Bandit
Bandits, unlike all previous roles, know each other as they live in the same Lair, and are also familiar with Leshy and Baba Yaga, so they can act in concert right from the first move. However, only the Gang Leader takes action at night and does not vote during the day, while the other Bandits diligently pretend to be Good Fellows and Red Maidens. If the Leader is out of the game for any reason, one of the remaining Bandits immediately takes his place. First and foremost, the Bandits aim to incapacitate Ivan Tsarevich before he gathers enough strength from the Good Fellows and Red Maidens for active daytime resistance against the Bandits.

Leshy
Leshy spies for the Bandits at night, informing them of found roles in their Lair, but does not vote during the day as he does not live in the village. However, other players can vote for Leshy and thus execute him. Since Leshy comes from the swamps, he cannot be tempted by the Princess Frog, and if she tries, Leshy will mark her home, and the villagers will learn who she really is. Leshy need not fear the charms of Koschei, but Vasilisa could heal him to death. If Kot-Bayun tries to attack Leshy, he risks losing his iron claws and will have to put his victims to sleep with nothing but purring.

Baba Yaga
Baba Yaga also works with the Bandits, casting spells at night: she can either inflict illness on other players or protect one of her allies from attack. Her magic is even stronger than the curse of the Death. During the day, Baba Yaga is also active: anyone under her protection cannot be executed even by a majority vote. However, the supply of magical roots for daytime protection is limited, so Baba Yaga cannot protect anyone, including herself, more than three times in a game. During the day, Baba Yaga pretends to be an ordinary Red Maiden and votes along with everyone else.

Zmey Gorynych
Zmey Gorynych At night, it flies over villages, forests, and swamps, engaging in robbery while revealing the role of the robbed during the day. During the day, the Serpent sleeps and therefore does not vote, but it can be executed by majority vote. The Serpent is very dangerous to everyone, especially to the Robbers and Ivan Tsarevich. The Serpent does not care whom to rob, but if the Wolf or Leshy exposes it, it can become a valuable ally. If the Serpent is killed at night, there is a chance to obtain a very valuable item — the Serpent's Skin, which will protect its owner from a physical attack once.

One-eyed Trouble
One-eyed Trouble kills anyone who crosses its path at night, and those it cannot kill (Leshy, Kot-Bayun, or Zmey Gorynych) are cursed, so anyone who tries to communicate with the cursed that same night will die within the day. The cursed one also dies, except Kot-Bayun, who simply goes to sleep to regain strength, skipping a turn the next night. Only Baba Yaga can save someone from the curse. The curse does not affect anyone who has defeated Kot-Bayun: like Kot, they simply go to sleep, skipping a turn.

Princess Frog
Princess Frog cannot win the game, but can earn a lot of money by seducing other players at night. The seduced player skips a turn. The Frog cannot seduce Vasilisa the Wise and should also avoid Leshy, who will expose her to everyone the next day. If Ivan Tsarevich or the Robber Chief finds the Frog, they can invite her to join the peaceful villagers or the Robbers, with Ivan unwilling to accept the Frog's refusal, while the Chief is less picky. However, the Princess is clever enough to become a double agent; even though she cannot win on her own, it’s a great opportunity to accumulate gold, as the chances to last until the end of the game significantly increase! During the day, the Princess Frog pretends to be the Red Maiden and votes alongside everyone.

Koshchei the Deathless
Koshchei the Deathless lives in his castle. At night, he roams the nearby villages and zombifies the Good Youths and Red Maidens, who then serve him and comply with all his orders. Refusing to follow a command, such as voting during the day differently than how Koschei instructed, or sending messages in the chat during the day if Koschei prohibited it, leads to the Servant of Koschei's demise. Thus, Koschei can influence the results of daytime voting, even though he does not vote personally. If Koschei is killed, all his victims also perish. Vasilisa can heal the Servant of Koschei, who then returns to his original role. Zmey-Gorynych and the Wolf have an inherent immunity to zombification, so Koschei, despite his desires, cannot recruit them to his service. Additionally, the Wolf can rescue Ivan or Vasilisa, sacrificing himself in the process. Those saved by the Wolf gain Wolf's immunity to zombification.

Cat-Bayun
Cat-Bayun lives in the forest, hunting at night. During the day, he sleeps in his hollow, so he does not participate in voting. However, he can be executed during the day by a majority vote. The Cat can attack in two ways: by purring — causing his victim to fall asleep and rendering them unable to walk at night or vote the next day — or by killing immediately with iron claws. The claw attack does not work on Zmey-Gorynych, and by attacking Leshy, the Cat might even end up without claws! Likh never can curse the Cat, who will simply sleep for one night after the curse. If anyone is able to defeat the Cat-Bayun, they will be healed from any disease or ailment, including Likh's curse. This ability of the Cat remains with the player until the end of the game. Koschei's Servants cannot vote for the Cat during the day but can indirectly discover who the Cat is without informing Koschei. Cat-Bayun does not form alliances with Ivan or the Robbers, making them a primary target for the Cat.
Technologies used
To create the game, I chose a serverless computing technology based on OpenFaaS, as it is simple enough to organize the game and sophisticated enough to write complex game rules without unnecessary complications. I will also use a Kubernetes cluster since this deployment method allows for quite easy and reliable rapid deployment and straightforward scalability. While it's possible to handle game logic solely with OpenFaaS, I will also try to create a Storyteller as a separate container to compare implementation complexity. For the main programming language for microservices and functions, I chose , as I have been learning it for some time in my spare time to replace Perl, and for user interaction with microservices and functions, I will use JS based on a certain framework. I will discuss the final decision in the corresponding article of the series. For communication between functions, I chose NATS.io, as I have encountered it before and it integrates quite easily into Kubernetes.
Announcement
- Introduction
- Setting up the development environment, breaking the task into functions
- Working on the backend
- Working on the frontend
- Setting up CI/CD, organizing testing
- Launching the trial game session
- Summary
Source: habr.com
