Skip to content

Commit

Permalink
Merge pull request #28 from ElectroAAC/develop
Browse files Browse the repository at this point in the history
Release v0.0.2
  • Loading branch information
WalistonBelles authored Feb 5, 2022
2 parents d1d3291 + 89968fe commit 2e1a95f
Show file tree
Hide file tree
Showing 123 changed files with 3,964 additions and 713 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
<br>

## 👨🏾‍💻 Project documentation.
<a href="https://walistonbelles1.gitbook.io/electroaac/" target="_blank"> Documentation </a>
Learn all about the project through our documentation: <a href="https://walistonbelles1.gitbook.io/electroaac/" target="_blank"> Documentation </a>

<br>

## 👨🏾‍💻 Demo.
Want to see the project better? Access our demo: <a href="http://www.electro-aac.org/" target="_blank"> Demo </a>

<br>

## 👨🏾‍💻 Techs

Expand Down Expand Up @@ -108,7 +115,7 @@ This repository follows the semantic commit pattern, with the addition of the ta
<ul>
<li> [x] Widget Highscores </li>
<li> [x] Show Highscores </li>
<li> [ ] Create/Apply Filters </li>
<li> [x] Create/Apply Filters </li>
</ul>

### Guilds
Expand Down Expand Up @@ -163,6 +170,17 @@ This repository follows the semantic commit pattern, with the addition of the ta
<li> [x] Edit post </li>
<li> [x] Delete post </li>
</ul>
<li> [x] Accounts </li>
<ul>
<li> [x] Find Account </li>
<li> [x] Change account information </li>
<li> [x] View all account characters </li>
</ul>
<li> [x] Players </li>
<ul>
<li> [x] Find Player </li>
<li> [x] Edit Info and Skills </li>
</ul>
</ul>

## Show-Off
Expand Down
62 changes: 42 additions & 20 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ $ npm run start
| GET | /news/find/:id | Find post |
| POST | /news/edit | Edit post |

<br>

## 🕵🏾‍♂️ Routes Account

| METHOD | ENDPOINT | DESCRIPTION |
Expand All @@ -44,9 +46,21 @@ $ npm run start
| PUT | /accounts/forgot-password | Update Password |
| GET | /accounts/characters/:id | Get characters to account |
| POST | /accounts/create-character | Create new character |
| GET | /accounts/change-character-name | Status system |
| POST | /accounts/change-character-name | Change character name |
| POST | /accounts/delete-character | Delete character |

<br>

## 🕵🏾‍♂️ Routes Players

| METHOD | ENDPOINT | DESCRIPTION |
| :----- | :------------------------- | :------------------------------------ |
| GET | /player/:name | Get Player Infos |
| GET | /player/:id/skills | Get Player Skills |
| GET | /player/:id/storage/:key | Get Player Storage |
| POST | /player/:id/deaths | Get Player Last 10 Deaths |
| GET | /player/:id/items | Get Player Items equipment |
| GET | /player/change-name | Status system |
| POST | /player/change-name | Change character name |
| POST | /player/delete | Delete character |

<br>

Expand All @@ -62,10 +76,10 @@ $ npm run start

## 🕵🏾‍♂️ Routes Highscores

| METHOD | ENDPOINT | DESCRIPTION |
| :----- | :------------------------- | :------------------------------------ |
| GET | /highscores | Get Top 5 Players |
| POST | /highscores/:page/:limit | Ranking Players |
| METHOD | ENDPOINT | DESCRIPTION |
| :----- | :------------------------------- | :------------------------------------ |
| GET | /highscores | Get Top 5 Players |
| POST | /highscores/:page/:limit/:type | Ranking Players |

<br>

Expand All @@ -85,18 +99,6 @@ $ npm run start

<br>

## 🕵🏾‍♂️ Routes Players

| METHOD | ENDPOINT | DESCRIPTION |
| :----- | :------------------------- | :------------------------------------ |
| GET | /player/:name | Get Player Infos |
| GET | /player/:id/skills | Get Player Skills |
| GET | /player/:id/storage/:key | Get Player Storage |
| POST | /player/:id/deaths | Get Player Last 10 Deaths |
| GET | /player/:id/items | Get Player Items equipment |

<br>

## 🕵🏾‍♂️ Routes Guilds

| METHOD | ENDPOINT | DESCRIPTION |
Expand All @@ -116,4 +118,24 @@ $ npm run start
| GET | /dashboard/players | Get Total PLayers |
| GET | /dashboard/guilds | Get Total Guilds |
| GET | /dashboard/premium-points | Get accounts with the most premium points |
| GET | /dashboard/vocation | Get all vocations by id |
| GET | /dashboard/vocation | Get all vocations by id |

<br>

## 🕵🏾‍♂️ Routes Dashboard - Accounts

| METHOD | ENDPOINT | DESCRIPTION |
| :----- | :------------------------- | :-----------------------------------------|
| GET | /dashboard/account/:name | Find account |
| POST | /dashboard/account | Update account |

<br>

## 🕵🏾‍♂️ Routes Dashboard - Characters

| METHOD | ENDPOINT | DESCRIPTION |
| :----- | :-------------------------------| :-----------------------------------------|
| GET | /dashboard/character/:name | Find character |
| GET | /dashboard/character-skills/:id | Find character |
| POST | /dashboard/character | Update character |
| POST | /dashboard/character-skills | Update characterSkills |
173 changes: 33 additions & 140 deletions backend/app/Controllers/Account/Characters.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import { DeleteValidator, StoreValidator, UpdateValidator } from 'App/Validators/Account/Character'
import { StoreValidator } from 'App/Validators/Account/Character'
import { Vocations } from 'Contracts/enums/Vocations'
import { Account } from 'App/Models'
import Database from '@ioc:Adonis/Lucid/Database'
import { Item, Player } from 'App/Models'
import Env from '@ioc:Adonis/Core/Env'
import encrypt from 'js-sha1'

import {
Character,
CharacterView,
CharacterRepository
} from 'App/Services'

export default class CharactersAccount {

private async getCharacters(account_id: number) {
return await Database
.from('players')
.select('players.id', 'players.level', 'players.name', 'players.vocation')
.where('players.account_id', account_id)
.orderBy('players.experience', 'desc');
}
public character: Character = new Character();
public characterView: CharacterView = new CharacterView();
public characterRepository: CharacterRepository = new CharacterRepository();

public async store({ request, response, auth }: HttpContextContract) {
public async store(ctx: HttpContextContract) {
try {
const data = await request.validate(StoreValidator);
const data = await ctx.request.validate(StoreValidator);

const account = auth.user;
const account = ctx.auth.user;

if (!account || !account.id) {
return response.unauthorized();
return ctx.response.unauthorized();
}

const characters = await this.getCharacters(account.id);
const characters = await this.characterView.getByAccount(account.id);

if (characters.length >= Env.get('CHARACTERS_PER_ACCOUNT')) {
return response.status(404).send({ message: 'You have too many characters on your account ' + characters.length + '/' + Env.get('CHARACTERS_PER_ACCOUNT')});
return ctx.response.status(404).send({ message: 'You have too many characters on your account ' + characters.length + '/' + Env.get('CHARACTERS_PER_ACCOUNT')});
}

const vocation = Vocations.find((vocation) => vocation.vocation_id === data.vocation);

if (!vocation || !vocation.vocation_id) {
return response.status(404).send({ message: 'Vocation invalid'});
return ctx.response.status(404).send({ message: 'Vocation invalid'});
}

const characterCopy = await Database.from('players').select('*').where('name', '=', vocation.name);
const characterCopy: Player[] = await this.characterView.findByName(vocation.name) as Player[];

if (!characterCopy.length) {
console.log("Error find Sample Character: Edit file Vocations in contracts/enums/Vocations and set valid characters to copy names. Name Character to copy: " + vocation.name + " doesn\'t exist.");
return response.status(404).send({ message: "Wrong characters configuration. Try again or contact with admin."});
return ctx.response.status(404).send({ message: "Wrong characters configuration. Try again or contact with admin."});
}

const newPlayer = Object.assign(characterCopy[0], {
Expand All @@ -59,149 +58,43 @@ export default class CharactersAccount {
created: 0
});

const result = await Database.table('players').returning('id').insert(newPlayer);
const result = await this.characterRepository.create(newPlayer);

const player = await Database.from('players').select('*').where('id', '=', result[0]);
const player: Player[] = await this.characterView.findById(result[0]) as Player[];

if (!player.length) {
return response.status(404).send({ message: "Error. Can't create character. Probably problem with database. Please try again later or contact with admin."});
return ctx.response.status(404).send({ message: "Error. Can't create character. Probably problem with database. Please try again later or contact with admin."});
}

for(let i = 0; i < 7; i++) {
const skillExists = await Database.from('player_skills').select('skillid').where('player_id', '=', player[0].id).andWhere('skillid', '=', i);
const skillExists = await this.characterView.checkSkillId(player[0].id, i);

if (!skillExists.length) {
await Database.table('player_skills').insert({
player_id: player[0].id,
skillid: i,
value: 10,
count: 0
})
await this.character.insertSkill(player[0].id, i);
}
}

const items_to_copy = await Database.from('player_items').select('*').where('player_id', '=', characterCopy[0].id);
const items_to_copy: Item[] = await this.characterView.getItems(characterCopy[0].id) as Item[];

for(let item of items_to_copy) {
await Database.table('player_skills').insert({
player_id: player[0].id,
pid: item.pid,
sid: item.sid,
itemtype: item.itemtype,
count: item.count,
attributes: item.attributes
})
await this.character.insertItems(player[0].id, item.pid, item.sid, item.itemtype, item.count, item.attributes);
};

return response.status(200).send({ status: 200, message: 'Character created successfully!'});
return ctx.response.status(200).send({ status: 200, message: 'Character created successfully!'});
} catch (err) {
console.log('Error getPlayersAccount Query: ', err);
return response.badRequest(err.messages);
return ctx.response.badRequest(err.messages);
}
}

public async show({ request, response }: HttpContextContract) {
public async show(ctx: HttpContextContract) {
try {
const characters = await this.getCharacters(request.param('id'));
const characters = await this.characterView.getByAccount(ctx.request.param('id'));

return response.status(200).send({ result: characters});
return ctx.response.status(200).send({ result: characters});
} catch(err) {
console.log('Error getPlayersAccount Query: ', err);
return response.status(400).send({ message: 'An error occurred, check the api console.'})
}
}

public async update({ request, response, auth }: HttpContextContract) {
try {
if (!Env.get('CHANGE_NAME')) {
return response.status(404).send({ message: 'Change character name is disabled.' });
}

const data = await request.validate(UpdateValidator);

const account = auth.user;

if (!account || !account.id) {
return response.unauthorized();
}

if (account.premium_points < Env.get('POINTS_TO_CHANGE_NAME')) {
return response.status(404).send({ message: "You don't have the points needed to change the name. It is necessary: " + Env.get('POINTS_TO_CHANGE_NAME') + " points." });
}

const character = await Database.from('players').select('name', 'online').where('id', '=', data.character_id).andWhere('account_id', '=', account.id);

if (!character.length)
{
return response.status(404).send({ message: 'The character does not belong to your account.' });
}

if (character[0].online === 1)
{
return response.status(404).send({ message: 'The character cannot be online.' });
}

const updateCharacter = await Database.from('players').where('id', '=', data.character_id).update({ name: data.new_name });

await Database.from('accounts').where('id', '=', account.id).update({ premium_points: account.premium_points - Env.get('POINTS_TO_CHANGE_NAME')});

if (!updateCharacter) {
return response.status(404).send({ message: "Error. Can't change character name. Probably problem with database. Please try again later or contact with admin." });
}

return response.status(200).send({ status: 200, message: "The character " + character[0].name + " name has been changed to " + data.new_name + "." });
} catch(err) {
console.log('Error Change Character Name Query: ', err);
return response.badRequest(err.messages);
}
}

public async destroy({ request, response, auth }: HttpContextContract) {
try {
const data = await request.validate(DeleteValidator);

const account = auth.user;

if (!account || !account.id)
return response.unauthorized();

const verifyPassword = await Account
.query()
.where('name', account.name)
.where('password', encrypt(data.password))
.firstOrFail();

if (!verifyPassword)
return response.status(404).send({ message: 'Wrong password.' });

const character = await Database
.from('players')
.select('name', 'deleted', 'online')
.where('id', '=', data.character_id)
.andWhere('account_id', '=', account.id);

if (!character.length)
return response.status(404).send({ message: 'The character does not belong to your account.' });

if (character[0].online === 1)
return response.status(404).send({ message: 'You cannot delete a character that is online.' });

if (character[0].deleted === 1)
return response.status(404).send({ message: 'This character is already deleted.' });

const house = await Database.from('houses').select('id').where('owner', '=', data.character_id);

if (house.length)
return response.status(404).send({ message: 'You cannot delete a character that has a house.' });

const updateCharacter = await Database.from('players').where('id', '=', data.character_id).update({ deleted: 1 });

if (!updateCharacter)
return response.status(404).send({ message: "Error. Unable to delete character. Probably problem with database. Please try again later or contact with admin." });

return response.status(200).send({ status: 200, message: "The character " + character[0].name + " has been deleted." });
} catch (err) {

return ctx.response.status(400).send({ message: 'An error occurred, check the api console.'})
}
}
}
12 changes: 6 additions & 6 deletions backend/app/Controllers/Account/ForgotPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Mail from '@ioc:Adonis/Addons/Mail'
import faker from 'faker'

export default class UserForgotPasswordController {
public async store({ request }: HttpContextContract) {
const { email, redirectUrl } = await request.validate(StoreValidator)
public async store(ctx: HttpContextContract) {
const { email, redirectUrl } = await ctx.request.validate(StoreValidator)

const account = await Account.findByOrFail('email', email)

Expand All @@ -24,12 +24,12 @@ export default class UserForgotPasswordController {
})
}

public async show({ params }: HttpContextContract) {
await AccountKey.findByOrFail('key', params.key)
public async show(ctx: HttpContextContract) {
await AccountKey.findByOrFail('key', ctx.params.key)
}

public async update({ request }: HttpContextContract) {
const { key, password } = await request.validate(UpdateValidator)
public async update(ctx: HttpContextContract) {
const { key, password } = await ctx.request.validate(UpdateValidator)

const accountKey = await AccountKey.findByOrFail('key', key)

Expand Down
Loading

0 comments on commit 2e1a95f

Please sign in to comment.