Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make private ids private #247

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added frontend/.gitignore
Empty file.
3 changes: 1 addition & 2 deletions frontend/prebuild/src/app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ export class LoginComponent implements OnInit, OnDestroy {
}
name = name.trim();

let userId = this.makeId();
console.log(`id: ${userId}`);
let userId = `BOT:${this.makeId()}`;
// register a new user
let key: any = await this.http.post(`${environment.API_URL_PLAYERS}?name=${name}&id=${userId}`, '', {
responseType: 'text'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ else if (!round.isPlayer(session))
if (playerResponse == null)
closeWithError(session, roundId, "Unable to add player " + msg.playerJoinedId +
" to game. This is probably because the player has not been registered yet");
else if (!round.addPlayer(session, msg.playerJoinedId, playerResponse.name, msg.hasGameBoard == null ? true : msg.hasGameBoard))
else if (!round.addPlayer(session, playerResponse.id, playerResponse.name, msg.hasGameBoard == null ? true : msg.hasGameBoard))
closeWithError(session, roundId, "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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public void update(Player p) {

@Override
public Player get(String id) {
return allPlayers.get(id);
Player p = allPlayers.get(id);
if (p != null) {
if (p.key == null)
return p;
return null;
}
return getBot(id);
}

@Override
Expand All @@ -50,4 +56,13 @@ public boolean exists(String id) {
return allPlayers.containsKey(id);
}

private Player getBot(String secret) {
for (Map.Entry<String, Player> entry : allPlayers.entrySet()) {
if (secret.equals(entry.getValue().key)) {
return entry.getValue();
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PersistentPlayerDB implements PlayerDB {

private static final String TABLE_NAME = "PLAYERS";
private static final String COL_ID = "id";
private static final String COL_KEY = "key";
private static final String COL_NAME = "name";
private static final String COL_NUM_GAMES = "totalGames";
private static final String COL_NUM_WINS = "totalWins";
Expand All @@ -40,6 +41,8 @@ public boolean isAvailable() {
.append(" (")
.append(COL_ID)
.append(" varchar(30) not null, ")
.append(COL_KEY)
.append(" varchar(30) not null, ")
.append(COL_NAME)
.append(" varchar(20) not null, ")
.append(COL_NUM_GAMES)
Expand Down Expand Up @@ -109,15 +112,22 @@ public void update(Player p) {

@Override
public Player get(String id) {
Player p = null;
try (Connection con = ds.getConnection()) {
PreparedStatement ps = con.prepareStatement("SELECT * FROM " + TABLE_NAME + " WHERE " + COL_ID + " = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
return rs.next() ? inflate(rs) : null;
p = rs.next() ? inflate(rs) : null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
if (p != null) {
if (p.key == null)
return p;
return null;
}
return getBot(id);
}

@Override
Expand Down Expand Up @@ -172,8 +182,20 @@ private static Player inflate(ResultSet rs) throws SQLException {
stats.totalGames = rs.getInt(COL_NUM_GAMES);
stats.numWins = rs.getInt(COL_NUM_WINS);
stats.rating = rs.getInt(COL_RATING);
Player p = new Player(rs.getString(COL_NAME), rs.getString(COL_ID), stats);
Player p = new Player(rs.getString(COL_NAME), rs.getString(COL_ID), null, stats);
return p;
}

public Player getBot(String key) {
try (Connection con = ds.getConnection()) {
PreparedStatement ps = con.prepareStatement("SELECT * FROM " + TABLE_NAME + " WHERE " + COL_KEY + " = ?");
ps.setString(1, key);
ResultSet rs = ps.executeQuery();
return rs.next() ? inflate(rs) : null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface PlayerDB {

public boolean exists(String id);

// public Player getBot(String key);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public String toString() {

public final String name;

public final String key;

public final PlayerStats stats;

public Player(String name) {
Expand All @@ -35,13 +37,18 @@ public Player(String name) {
@JsonbCreator
public Player(@JsonbProperty("name") String name,
@JsonbProperty("id") String id) {
this(name, id, new PlayerStats());
this(name, id, null, new PlayerStats());
}

public Player(String name, String id, String key) {
this(name, id, key, new PlayerStats());
}

public Player(String name, String id, PlayerStats stats) {
public Player(String name, String id, String key, PlayerStats stats) {
this.id = (id == null || id.equals("null")) ? createDefaultId(name) : id;
this.name = name;
this.stats = stats;
this.key = key;
}

public static int compareByWins(Player a, Player b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public String createPlayer(@QueryParam("name") String name, @QueryParam("id") St
if (name.length() > 20)
name = name.substring(0, 20);

String key = null;
if (id != null && id.startsWith("BOT:")) {
key = id.substring(4);
id = "BOT:" + key.substring(0, 4);
Player p = new Player(name, id, key);
if (db.create(p))
System.out.println("Created a new bot with id=" + p.id);
else
System.out.println("A player already existed with id=" + p.id);
return p.key;
}
Player p = new Player(name, id);
if (db.create(p))
System.out.println("Created a new player with id=" + p.id);
Expand Down