Merge branch 'feat/92-add-serverside-add-player-command' into 'feat/97-add-joinlobby-command-on-server-side'
Feat: Add addPlayer command on sever side See merge request cs108-fs26/Gruppe-13!100
This commit was merged in pull request #256.
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.add_player;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.User;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.domain.user.UserRegistry;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.execution.CommandHandler;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.ErrorResponse;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.OkResponse;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/** Handler for the ADD_PLAYER command. */
|
||||||
|
public class AddPlayerHandler extends CommandHandler<AddPlayerRequest> {
|
||||||
|
private final UserRegistry userRegistry;
|
||||||
|
|
||||||
|
public AddPlayerHandler(UserRegistry userRegistry, ResponseDispatcher responseDispatcher) {
|
||||||
|
super(responseDispatcher);
|
||||||
|
this.userRegistry = userRegistry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(AddPlayerRequest request) {
|
||||||
|
Optional<User> created =
|
||||||
|
userRegistry.registerIfAvailable(
|
||||||
|
request.getName(), request.getContext().sessionId());
|
||||||
|
if (created.isEmpty()) {
|
||||||
|
responseDispatcher.dispatch(
|
||||||
|
new ErrorResponse(request.getContext(), "NAME_TAKEN", "Name already taken"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
responseDispatcher.dispatch(new OkResponse(request.getContext()));
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.add_player;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.command.parsing.CommandParser;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.PrimitiveRequest;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.accessor.RequestParameterAccessor;
|
||||||
|
|
||||||
|
/** Parser for the ADD_PLAYER command. */
|
||||||
|
public class AddPlayerParser implements CommandParser<AddPlayerRequest> {
|
||||||
|
@Override
|
||||||
|
public AddPlayerRequest parse(PrimitiveRequest primitiveRequest) {
|
||||||
|
RequestParameterAccessor accessor =
|
||||||
|
new RequestParameterAccessor(primitiveRequest.parameters());
|
||||||
|
|
||||||
|
String name = accessor.require("NAME");
|
||||||
|
|
||||||
|
int chips = accessor.require("CHIPS", Integer::parseInt);
|
||||||
|
|
||||||
|
return new AddPlayerRequest(primitiveRequest.context(), name, chips);
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.casono.server.app.commands.add_player;
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.Request;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.request.RequestContext;
|
||||||
|
|
||||||
|
/** Request data for the ADD_PLAYER command. */
|
||||||
|
public class AddPlayerRequest extends Request {
|
||||||
|
private final String name;
|
||||||
|
private final int chips;
|
||||||
|
|
||||||
|
public AddPlayerRequest(RequestContext context, String name, int chips) {
|
||||||
|
super(context);
|
||||||
|
this.name = name;
|
||||||
|
this.chips = chips;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getChips() {
|
||||||
|
return chips;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user