Skip to content

Commit

Permalink
添加 GameModeCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
akemimadoka committed Sep 13, 2017
1 parent a38b896 commit e2d5d1b
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/MineCase.Server.Commands/MineCase.Server.Commands.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MineCase.Server.Grains\MineCase.Server.Grains.csproj" />
<ProjectReference Include="..\MineCase.Server.Interfaces\MineCase.Server.Interfaces.csproj" />
</ItemGroup>

</Project>
105 changes: 105 additions & 0 deletions src/MineCase.Server.Commands/Vanilla/GameModeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MineCase.Server.Game;
using MineCase.Server.Game.Entities;
using MineCase.Server.Game.Commands;

namespace MineCase.Server.Commands.Vanilla
{
public class GameModeCommand
: SimpleCommand
{
public GameModeCommand()
: base("gamemode", "Changes the player to a specific game mode")
{
}

public override async Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args)
{
var player = (IPlayer)commandSender;

if (args.Count < 1)
{
throw new CommandWrongUsageException(this, "Invalid arguments");
}

GameMode.Class gameModeClass;

switch (((UnresolvedArgument)args[0]).RawContent)
{
case "survival":
case "s":
case "0":
gameModeClass = GameMode.Class.Survival;
break;
case "creative":
case "c":
case "1":
gameModeClass = GameMode.Class.Creative;
break;
case "adventure":
case "ad":
case "2":
gameModeClass = GameMode.Class.Adventure;
break;
case "spectator":
case "sp":
case "3":
gameModeClass = GameMode.Class.Spectator;
break;
default:
throw new CommandWrongUsageException(this);
}

var target = player;

if (args.Count == 2)
{
var rawArg = (UnresolvedArgument)args[1];

if (rawArg is TargetSelectorArgument targetSelector)
{
switch (targetSelector.Type)
{
case TargetSelectorType.NearestPlayer:
break;
case TargetSelectorType.RandomPlayer:
break;
case TargetSelectorType.AllPlayers:
break;
case TargetSelectorType.AllEntites:
break;
case TargetSelectorType.Executor:
break;
default:
throw new CommandException(this);
}

throw new CommandException(this, "Sorry, this feature has not been implemented.");
}
else
{
var user = await player.GetUser();
var session = await user.GetGameSession();

target = await (await session.FindUserByName(rawArg.RawContent)).GetPlayer();

if (target == null)
{
throw new CommandException(this, $"Player \"{rawArg.RawContent}\" not found, may be offline or not existing.");
}
}
}

var targetDesc = await target.GetDescription();
var mode = targetDesc.GameMode;
mode.ModeClass = gameModeClass;
targetDesc.GameMode = mode;

return true;
}
}
}
6 changes: 4 additions & 2 deletions src/MineCase.Server.Grains/Game/Entities/PlayerGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,14 @@ private class WindowContext

public Task<bool> HasPermission(Permission permission)
{
throw new NotImplementedException();
// TODO: 临时提供所有权限,需要实现权限管理
return Task.FromResult(true);
}

public Task SendMessage(string msg)
{
throw new NotImplementedException();
// TODO: 向玩家发送信息
return Task.CompletedTask;
}
}
}
43 changes: 32 additions & 11 deletions src/MineCase.Server.Grains/Game/GameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public override async Task OnActivateAsync()
_gameTick = RegisterTimer(OnGameTick, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(50));
}

public Task<IEnumerable<IUser>> GetUsers()
{
return Task.FromResult((IEnumerable<IUser>)_users.Keys);
}

public async Task<IUser> FindUserByName(string name)
{
foreach (var user in _users)
{
if (await user.Key.GetName() == name)
{
return user.Key;
}
}

return null;
}

public async Task JoinGame(IUser user)
{
var sink = await user.GetClientPacketSink();
Expand Down Expand Up @@ -97,8 +115,8 @@ public async Task SendChatMessage(IUser sender, IUser receiver, string message)
var senderName = await sender.GetName();
var receiverName = await receiver.GetName();

Chat jsonData = await CreateStandardChatMessage(senderName, message);
byte position = 0; // It represents user message in chat box
var jsonData = await CreateStandardChatMessage(senderName, message);
const byte position = 0; // It represents user message in chat box
foreach (var item in _users.Keys)
{
if (await item.GetName() == receiverName ||
Expand All @@ -123,20 +141,23 @@ await Task.WhenAll(from u in _tickables

private Task<Chat> CreateStandardChatMessage(string name, string message)
{
StringComponent nameComponent = new StringComponent(name);
nameComponent.ClickEvent = new ChatClickEvent(ClickEventType.SuggestCommand, "/msg " + name);
nameComponent.HoverEvent = new ChatHoverEvent(HoverEventType.ShowEntity, name);
nameComponent.Insertion = name;
var nameComponent = new StringComponent(name)
{
ClickEvent = new ChatClickEvent(ClickEventType.SuggestCommand, "/msg " + name),
HoverEvent = new ChatHoverEvent(HoverEventType.ShowEntity, name),
Insertion = name
};

// construct message
StringComponent messageComponent = new StringComponent(message);
var messageComponent = new StringComponent(message);

// list
List<ChatComponent> list = new List<ChatComponent>();
list.Add(nameComponent);
list.Add(messageComponent);
var list = new List<ChatComponent>
{
nameComponent, messageComponent
};

Chat jsonData = new Chat(new TranslationComponent("chat.type.text", list));
var jsonData = new Chat(new TranslationComponent("chat.type.text", list));
return Task.FromResult(jsonData);
}

Expand Down
5 changes: 5 additions & 0 deletions src/MineCase.Server.Grains/World/WorldGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public Task<IEntity> FindEntity(uint eid)
return Task.FromException<IEntity>(new EntityNotFoundException());
}

public Task<IEnumerable<IEntity>> GetEntities()
{
return Task.FromResult((IEnumerable<IEntity>)_entities.Values);
}

public Task<(long age, long timeOfDay)> GetTime()
{
return Task.FromResult((_worldAge, _worldAge % 24000));
Expand Down
2 changes: 1 addition & 1 deletion src/MineCase.Server.Interfaces/Game/Commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public interface ICommand
/// <summary>
/// 命令执行过程中可能发生的异常的基类
/// </summary>
/// <remarks>派生自此类的异常在 <see cref="M:MineCase.Server.Game.Commands.CommandMap.Dispatch(MineCase.Server.Game.Commands.ICommandSender,System.String)" /> 中将会被吃掉,不会传播到外部</remarks>
/// <remarks>派生自此类的异常在 <see cref="CommandMap.Dispatch(ICommandSender, string)" /> 中将会被吃掉,不会传播到外部</remarks>
public class CommandException : Exception
{
public ICommand Command { get; }
Expand Down
1 change: 1 addition & 0 deletions src/MineCase.Server.Interfaces/Game/Entities/IPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;

using MineCase.Protocol.Play;
using MineCase.Server.Game.Commands;
using MineCase.Server.Game.Windows;
using MineCase.Server.Network;
using MineCase.Server.User;
Expand Down
4 changes: 4 additions & 0 deletions src/MineCase.Server.Interfaces/Game/IGameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace MineCase.Server.Game
{
public interface IGameSession : IGrainWithStringKey
{
Task<IEnumerable<IUser>> GetUsers();

Task<IUser> FindUserByName(string name);

Task JoinGame(IUser player);

Task LeaveGame(IUser player);
Expand Down
2 changes: 2 additions & 0 deletions src/MineCase.Server.Interfaces/World/IWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface IWorld : IGrainWithStringKey

Task<IEntity> FindEntity(uint eid);

Task<IEnumerable<IEntity>> GetEntities();

Task<(long age, long timeOfDay)> GetTime();

Task<long> GetAge();
Expand Down
8 changes: 7 additions & 1 deletion src/MineCase.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
VisualStudioVersion = 15.0.26730.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MineCase.Server", "MineCase.Server\MineCase.Server.csproj", "{8E71CBEC-5804-4125-B651-C78426E57C8C}"
EndProject
Expand Down Expand Up @@ -38,6 +38,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "data", "data", "{7DC8CDBD-0
..\data\furnace.txt = ..\data\furnace.txt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MineCase.Commands", "MineCase.Commands\MineCase.Commands.csproj", "{A56AEC13-918A-4A54-94DA-7CE8F01C378B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -80,6 +82,10 @@ Global
{B7B1A959-72F3-42C6-B138-93C8D654F139}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7B1A959-72F3-42C6-B138-93C8D654F139}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7B1A959-72F3-42C6-B138-93C8D654F139}.Release|Any CPU.Build.0 = Release|Any CPU
{A56AEC13-918A-4A54-94DA-7CE8F01C378B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A56AEC13-918A-4A54-94DA-7CE8F01C378B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A56AEC13-918A-4A54-94DA-7CE8F01C378B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A56AEC13-918A-4A54-94DA-7CE8F01C378B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit e2d5d1b

Please sign in to comment.