Completed ability to choose a node as player.

This commit is contained in:
Sergiotarxz 2024-01-14 04:18:14 +01:00
parent 35a65cfa1f
commit 30188a1a76
10 changed files with 101 additions and 10 deletions

View File

@ -38,15 +38,46 @@ export default class SelectTeamUI extends AbstractTopBarInterface {
const teamDiv = this.getNodeFromTemplateId('conquer-team-to-select-template')
const nameParagraph = teamDiv.querySelector('p.conquer-name');
const descriptionParagraph = teamDiv.querySelector('p.conquer-description');
if (!(nameParagraph instanceof HTMLParagraphElement) || !(descriptionParagraph instanceof HTMLParagraphElement)) {
const submit = teamDiv.querySelector('button.conquer-submit');
if (!(nameParagraph instanceof HTMLParagraphElement)
|| !(descriptionParagraph instanceof HTMLParagraphElement)
|| !(submit instanceof HTMLButtonElement)) {
Conquer.fail('Select team name inclusive or description container are not correctly defined in template.');
}
nameParagraph.innerText = team.getName();
descriptionParagraph.innerText = team.getDescription();
nameParagraph.style.color = team.getColor();
submit.addEventListener('click', async () => {
this.onSelectTeam(team);
});
this.getForm().append(teamDiv);
}
private async onSelectTeam(team: ConquerTeam) {
const urlTeam = new URL('/conquer/user/team',
window.location.protocol + '//'
+ window.location.hostname + ':'
+ window.location.port);
const response = await fetch(urlTeam, {
method: 'POST',
body: JSON.stringify({
team: team.getUUID(),
node: this.node.getUUID(),
}),
});
let responseBody;
try {
responseBody = await response.json();
if (response.status !== 200) {
console.error(responseBody.error);
return;
}
this.runCallbacks('close')
} catch (error) {
console.error('Error parsing json', error);
}
}
private getForm(): HTMLElement {
if (this.form === null) {
const form = this.getNodeFromTemplateId('conquer-select-team-list-template')

View File

@ -104,17 +104,16 @@ export default class SelfPlayerUI extends AbstractTopBarInterface {
+ ' ve al nodo más cercano para unirte a un equipo.';
return this.userTeamData;
}
const spanText = document.createElement('span');
spanText.innerText = 'Equipo: ';
element.append(spanText);
const spanCircle = document.createElement('span');
spanCircle.classList.add('conquer-team-circle');
spanCircle.style.backgroundColor = team.getColor();
element.append(spanCircle);
const spanText = document.createElement('span');
spanText.innerText = 'Perteneces al equipo: ';
element.append(spanText);
const spanTeamName = document.createElement('span');
spanTeamName.style.color = team.getColor();
spanTeamName.style.backgroundColor = 'white';
spanTeamName.innerText = team.getName();
spanTeamName.innerText = ' ' + team.getName();
element.append(spanTeamName);
return this.userTeamData;
}

View File

@ -111,6 +111,10 @@ export default class MapNode {
return 'node-' + this.uuid;
}
public getUUID(): string {
return this.uuid;
}
public getFeature(): Feature {
if (this.feature === null) {
this.feature = new Feature({

View File

@ -17,6 +17,10 @@ export default class ConquerTeam {
@JsonProperty()
private color: string;
public getUUID(): string {
return this.uuid;
}
public getName(): string {
return this.name;
}

View File

@ -93,6 +93,7 @@ sub startup ($self) {
$r->get('/conquer')->to('Conquer#index');
$r->put('/conquer/user')->to('UserConquer#create');
$r->get('/conquer/user/team')->to('UserConquer#getSelfTeam');
$r->post('/conquer/user/team')->to('UserConquer#setTeamForUser');
$r->post('/conquer/user/coordinates')->to('UserConquer#setCoordinates');
$r->get('/conquer/team/<uuid>')->to('ConquerTeam#get');
$r->put('/conquer/team')->to('ConquerTeam#put');

View File

@ -21,6 +21,46 @@ my $username_maximum_chars = 15;
my $password_minimum_chars = 8;
my $password_maximum_chars = 4096;
sub setTeamForUser($self) {
my $user = $self->current_user;
if (!defined $user) {
return $self->_renderError(401, 'No estás loggeado.');
}
my $input = $self->_expectJson;
if (!defined $input) {
return;
}
my $node_uuid = $input->{node};
my $team_uuid = $input->{team};
my $resultset_team = BurguillosInfo::Schema->Schema->resultset('ConquerTeam');
my $resultset_node = BurguillosInfo::Schema->Schema->resultset('ConquerNode');
my @teams = $resultset_team->search({uuid => $team_uuid});
my @nodes = $resultset_node->search({uuid => $node_uuid});
if (scalar @teams < 1) {
return $self->render(status => 404, json => {
error => 'No se encontró ese equipo.',
});
}
if (scalar @nodes < 1) {
return $self->render(status => 404, json => {
error => 'No se encontró este nodo.',
});
}
my $team = $teams[0];
my $node = $nodes[0];
if (!$node->is_near($user)) {
return $self->render(status => 400, json => {
error => 'Estás demasiado lejos del nodo.',
});
}
$user = $user->get_from_storage;
$user->team_object($team);
$user->update;
return $self->render(json => {
ok => $JSON::true,
});
}
sub get_self ($self) {
my $user = $self->current_user;
if ( !defined $user ) {

View File

@ -83,6 +83,7 @@ sub serialize_to_owner ($self) {
};
}
__PACKAGE__->set_primary_key('uuid');
__PACKAGE__->belongs_to('team_object', 'BurguillosInfo::Schema::Result::ConquerTeam', 'team');
__PACKAGE__->add_unique_constraint( "unique_constraint_username",
['username'] );
1;

View File

@ -9,6 +9,11 @@ body {
min-height: 100%;
width: 100%;
height: 100%; }
body span.conquer-team-circle {
display: inline-block;
aspect-ratio: 1 / 1;
height: 1rem;
border-radius: 50%; }
body div.conquer-team-to-select {
padding: 5px;
border-radius: 5px;

View File

@ -17,6 +17,12 @@ html {
}
body {
span.conquer-team-circle {
display: inline-block;
aspect-ratio: 1 / 1;
height: 1rem;
border-radius: 50%;
}
div.conquer-team-to-select {
padding: 5px;
border-radius: 5px;

File diff suppressed because one or more lines are too long