X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fshared%2Fcli.cpp;h=c144d8677830281318f09585705168f0d9d3ca90;hb=cdd865c1934eccbb1f1d0ffaf041e53f0fdd524b;hp=bb4aa6de706e3259f25f374a9fdfe15d57808870;hpb=b00e4b77859d13bdf29bd50e91315a46a15bd01d;p=blank.git diff --git a/src/shared/cli.cpp b/src/shared/cli.cpp index bb4aa6d..c144d86 100644 --- a/src/shared/cli.cpp +++ b/src/shared/cli.cpp @@ -5,6 +5,7 @@ #include "../io/TokenStreamReader.hpp" #include "../world/Entity.hpp" #include "../world/Player.hpp" +#include "../world/World.hpp" #include #include @@ -18,6 +19,7 @@ namespace blank { CLI::CLI(World &world) : world(world) , commands() { + AddCommand("as", new ImpersonateCommand); AddCommand("tp", new TeleportCommand); } @@ -73,6 +75,43 @@ CLIContext::CLIContext(Player *p, Entity *e) } } +std::string CLIContext::Name() const { + if (HasPlayer()) return GetPlayer().Name(); + if (HasEntity()) return GetEntity().Name(); + return "anonymous"; +} + + +void ImpersonateCommand::Execute(CLI &cli, CLIContext &ctx, TokenStreamReader &args) { + if (!args.HasMore()) { + // no argument => reset + ctx.Reset(); + ctx.Broadcast(ctx.Name() + " returned to their own self"); + return; + } + // TODO: broadcast who (real player name) impersonates who + string old_name = ctx.Name(); + string name(args.GetString()); + + Player *p = cli.GetWorld().FindPlayer(name); + if (p) { + ctx.SetPlayer(*p); + ctx.SetEntity(p->GetEntity()); + ctx.Broadcast(old_name + " now impersonating " + p->Name()); + return; + } + + // not a player, try an entity + Entity *e = cli.GetWorld().FindEntity(name); + if (e) { + ctx.SetEntity(*e); + ctx.Broadcast(old_name + " now impersonating " + e->Name()); + return; + } + + ctx.Error("no player or entity with name " + name); +} + void TeleportCommand::Execute(CLI &, CLIContext &ctx, TokenStreamReader &args) { if (!ctx.HasEntity()) {