Merge pull request #236 from ryanesch/AI

Add bot registration
This commit is contained in:
ryanesch 2019-10-07 16:13:16 -05:00 committed by GitHub
commit d7d6444fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 25 deletions

View File

@ -17,6 +17,9 @@
<div class="form-item">
<button type="button" (click)="showGuestLogin()" [disabled]="isPerformingSsoRedirect">Play as Guest</button>
</div>
<div class="form-item">
<button type="button" (click)="showBotLogin()" [disabled]="isPerformingSsoRedirect">Register Bot</button>
</div>
<div class="form-item">
<button type="button" (click)="loginGoogle()" [disabled]="isPerformingSsoRedirect || !isGoogleConfigured">Sign in with Google</button>
</div>
@ -114,6 +117,23 @@
</div>
</div>
</div>
<div aiPane>
<div class="form-group">
<div class="form-item">
<div class="form-item">
<label>bot name</label>
<input type="text" id="ainame" name="ainame" [(ngModel)]="ainame" />
</div>
<div class="form-item">
<button type="submit" id="registerBot" (click)="registerBot(ainame)">Register Bot</button>
</div>
<div class="form-item">
<button type="submit" (click)="cancelLogin()">Back</button>
</div>
</div>
</div>
</div>
</app-slider>
</div>
</div>

View File

@ -390,4 +390,42 @@ export class LoginComponent implements OnInit, OnDestroy {
sessionStorage.removeItem('userId');
this.pane = 'left';
}
makeid() {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var charactersLength = characters.length;
for ( var i = 1; i <= 16; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
if (i % 4 == 0 && i != 16) {
result += "-";
}
}
return result;
}
showBotLogin() {
this.pane='ai';
}
async registerBot(name: string) {
console.log(`Bot name input: "${name}"`);
// ensure username is valid before creating
let usernameError = this.validateUsername(name);
if (usernameError !== null) {
alert(usernameError);
return false;
}
name = name.trim();
let userid = this.makeid();
console.log("id: " + userid);
// register a new user
let key: any = await this.http.post(`${environment.API_URL_PLAYERS}?name=${name}&id=${userid}`, '', {
responseType: 'text'
}).toPromise();
alert(`Key for ${name}: ${key}`);
}
}

View File

@ -3,4 +3,5 @@
<div class="center-pane" [@fade]="isActivePane('center')"><ng-content select="[centerPane]"></ng-content></div>
<div class="right-pane" [@fade]="isActivePane('right')"><ng-content select="[rightPane]"></ng-content></div>
<div class="queue-pane" [@fade]="isActivePane('queue')"><ng-content select="[queuePane]"></ng-content></div>
<div class="ai-pane" [@fade]="isActivePane('ai')"><ng-content select="[aiPane]"></ng-content></div>
</div>

View File

@ -4,7 +4,7 @@
.parent {
height: 100%;
width: 400%;
width: 500%;
display: flex;

View File

@ -9,9 +9,10 @@ import { trigger, state, style, transition, animate, query, group, animateChild
animations: [
trigger('slide', [
state('left', style({ transform: 'translateX(0)' })),
state('center', style({ transform: 'translateX(-25%)' })),
state('right', style({ transform: 'translateX(-50%)' })),
state('queue', style({ transform: 'translateX(-75%)'})),
state('center', style({ transform: 'translateX(-20%)' })),
state('right', style({ transform: 'translateX(-40%)' })),
state('queue', style({ transform: 'translateX(-60%)'})),
state('ai', style({ transform: 'translateX(-80%)'})),
transition('void => *', animate(0)),
transition('* => *', [
group([
@ -35,4 +36,4 @@ export class SliderComponent {
}
}
export type PaneType = 'left' | 'center' | 'right' | 'queue';
export type PaneType = 'left' | 'center' | 'right' | 'queue' | 'ai';

View File

@ -4,6 +4,8 @@
package org.libertybikes.game.round.service;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.enterprise.context.Dependent;
@ -93,26 +95,13 @@ public class GameRoundWebsocket {
} else if (msg.direction != null) {
round.updatePlayerDirection(session, msg);
} else if (msg.playerJoinedId != null) {
/*
* If an external service joins through just opening a websocket and sending a message
* We will get an exception trying to find the player in the playerservice DB
* TODO: we need to handle this better for our externalAI, either here or have the
* external service call playerservice first and create the player
* msg.playerJoinedId.startsWith("BOT:") is currently a hack and a player joining
* vs externalAI should not be different.
*/
if (!msg.playerJoinedId.startsWith("BOT:")) {
// Call playerserver for player in DB
org.libertybikes.restclient.Player playerResponse = getPlayer(msg.playerJoinedId);
if (!round.addPlayer(session, msg.playerJoinedId, playerResponse.name, msg.hasGameBoard))
sendToClient(session, new OutboundMessage.ErrorEvent("Unable to add player " + playerResponse.name
+ " to game. This is probably because someone else with the same name is already in the game."));
} else {
String botName = msg.playerJoinedId.substring(4);
if (!round.addPlayer(session, msg.playerJoinedId, botName, true))
sendToClient(session, new OutboundMessage.ErrorEvent("Unable to add player " + msg.playerJoinedId
+ " to game. This is probably because someone else with the same name is already in the game."));
}
// Call playerserver for player in DB
org.libertybikes.restclient.Player playerResponse = getPlayer(msg.playerJoinedId);
if (!round.addPlayer(session, msg.playerJoinedId, playerResponse.name, msg.hasGameBoard == null ? true : msg.hasGameBoard))
sendToClient(session, new OutboundMessage.ErrorEvent("Unable to add player " + playerResponse.name
+ " to game. This is probably because someone else with the same name is already in the game."));
} else if (Boolean.TRUE == msg.isSpectator) {
round.addSpectator(session);