Skip to content

Commit

Permalink
visitor implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfriend77 committed Sep 11, 2019
1 parent bb909c3 commit d81149a
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 21 deletions.
12 changes: 10 additions & 2 deletions SabberStoneContract/Client/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ private void ProcessChannelMessage(GameServerStream current)

_gameController.GameId = gameData.GameId;
_gameController.PlayerId = gameData.PlayerId;

// visitors are ignoring ....invitation
if (GameClientState == GameClientState.Placed)
{
break;
}

GameClientState = GameClientState.Invited;

// action call here
Expand Down Expand Up @@ -299,7 +306,7 @@ private void ProcessChannelMessage(GameServerStream current)
case GameDataType.Result:
GameClientState = GameClientState.Registered;
_gameController.SetResult();
OnMatchFinished();
CallMatchFinished();
break;
}
break;
Expand Down Expand Up @@ -367,8 +374,9 @@ public async virtual void CallInvitation()
await Task.Run(() => { _gameController.SendInvitationReply(true); });
}

public virtual void OnMatchFinished()
public virtual void CallMatchFinished()
{

}
}
}
12 changes: 10 additions & 2 deletions SabberStoneContract/Client/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ protected virtual async void CallPowerChoices()
{
await Task.Run(() =>
{
SendPowerChoicesChoice(GameAI.PowerChoices(_poGame, PowerChoices));
var powerChoice = GameAI.PowerChoices(_poGame, PowerChoices);
if (powerChoice != null)
{
SendPowerChoicesChoice(GameAI.PowerChoices(_poGame, PowerChoices));
}
});
}

Expand All @@ -142,7 +146,11 @@ protected virtual async void CallPowerOptions()
{
await Task.Run(() =>
{
SendPowerOptionChoice(GameAI.PowerOptions(_poGame, PowerOptions.PowerOptionList));
var powerOption = GameAI.PowerOptions(_poGame, PowerOptions.PowerOptionList);
if (powerOption != null)
{
SendPowerOptionChoice(GameAI.PowerOptions(_poGame, PowerOptions.PowerOptionList));
}
});
}

Expand Down
19 changes: 19 additions & 0 deletions SabberStoneContract/Helper/TypeOnlyContractResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace SabberStoneContract.Helper
{
public class TypeOnlyContractResolver<T> : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.ShouldSerialize = instance => property.DeclaringType == typeof(T);
return property;
}
}
}
38 changes: 38 additions & 0 deletions SabberStoneContract/Interface/VisitorAI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SabberStoneContract.Model;
using SabberStoneCore.Kettle;
using SabberStoneCore.Model;
using SabberStoneCore.Tasks.PlayerTasks;

namespace SabberStoneContract.Interface
{
public class VisitorAI : IGameAI
{
public PowerChoices PowerChoices(Game game, PowerChoices powerChoices)
{
return null;
}

public void InitialiseAgent()
{

}

public void InitialiseGame()
{

}

public PlayerTask GetMove(Game game)
{
return null;
}

public PowerOptionChoice PowerOptions(Game game, List<PowerOption> powerOptionList)
{
return null;
}
}
}
17 changes: 17 additions & 0 deletions SabberStoneServer/Services/GameServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private bool TokenAuthentification(Metadata metaData, out string clientTokenValu
public class UserClient : UserInfo
{
public string Token { get; set; }

public string Peer { get; set; }

public Task ResponseStreamWriterTask { get; set; }
Expand All @@ -385,10 +386,26 @@ public class UserClient : UserInfo
public UserClient()
{
CancellationTokenSource = new CancellationTokenSource();

responseQueue = new ConcurrentQueue<GameServerStream>();

Visitors = new List<UserClient>();
}

public UserInfo GetUserInfoClone()
{
return new UserInfo()
{
AccountName = base.AccountName,
DeckData = base.DeckData,
GameConfigInfo = base.GameConfigInfo,
GameId = base.GameId,
PlayerId = base.PlayerId,
PlayerState = base.PlayerState,
SessionId = base.SessionId,
UserState = base.UserState
};
}
}


Expand Down
13 changes: 9 additions & 4 deletions SabberStoneServer/Services/MatchGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void Initialize()
SendGameData(Player2, MsgType.Invitation, true, GameDataType.None);
}

public void Start(GameConfigInfo gameConfigInfo)
private void Start(GameConfigInfo gameConfigInfo)
{
Log.Info($"[_gameId:{GameId}] Game creation is happening in a few seconds!!!");

Expand Down Expand Up @@ -115,12 +115,14 @@ internal void InvitationReply(bool state, GameData gameData)
Player1.GameConfigInfo = gameConfigInfo;
Player2.GameConfigInfo = gameConfigInfo;

//var serializerSetting = new JsonSerializerSettings { ContractResolver = new TypeOnlyContractResolver<List<UserInfo>>() };

// we send over opponend user info which is currently not reduced and has all available informations
SendGameData(Player1, MsgType.InGame, true, GameDataType.Initialisation,
JsonConvert.SerializeObject(new List<UserInfo> { Player1, Player2 }));
JsonConvert.SerializeObject(new List<UserInfo> { Player1.GetUserInfoClone(), Player2.GetUserInfoClone() }));

SendGameData(Player2, MsgType.InGame, true, GameDataType.Initialisation,
JsonConvert.SerializeObject(new List<UserInfo> { Player1, Player2 }));
JsonConvert.SerializeObject(new List<UserInfo> { Player1.GetUserInfoClone(), Player2.GetUserInfoClone() }));

SendHistoryToPlayers();
SendOptionsOrChoicesToPlayers();
Expand Down Expand Up @@ -263,7 +265,10 @@ public void SendGameData(UserClient player, int playerId, MsgType messageType, b
})
};
player.responseQueue.Enqueue(gameServerStream);
player.Visitors.ForEach(p => p.responseQueue.Enqueue(gameServerStream));

player.Visitors.ForEach(p => {
p.responseQueue.Enqueue(gameServerStream);
});
}

public void Stop()
Expand Down
33 changes: 24 additions & 9 deletions SabberStoneXConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SabberStoneContract.Client;
using SabberStoneContract.Core;
using SabberStoneContract.Interface;
using SabberStoneContract.Model;
using SabberStoneServer.Core;
using System;
using System.Diagnostics;
Expand All @@ -15,10 +16,10 @@ class Program
static void Main(string[] args)
{

//RunServerWith(1);
RunServerWith(1);
//FullTest();
//RunServer();
VisitorTest();
//VisitorTest();


Console.ReadKey();
Expand All @@ -28,25 +29,38 @@ private static void VisitorTest()
{

RunServer();

GameClient testClient = new VisitorClient("TestClient1", "127.0.0.1", 50051, new GameController(new RandomAI()));
testClient.Connect();
GameController testController1 = new GameController(new RandomAI());
GameClient testClient1 = new GameClient("127.0.0.1", 50051, testController1);
testClient1.Connect();
testClient1.Register("TestClient1", "1234");

Thread.Sleep(1000);

GameClient client = new VisitorClient("VisitorClient1", "127.0.0.1", 50051, new GameController(new RandomAI()));
client.Connect();
GameController testController2 = new GameController(new RandomAI());
GameClient testClient2 = new GameClient("127.0.0.1", 50051, testController2);
testClient2.Connect();
testClient2.Register("TestClient2", "1234");

GameClient visitorClient1 = new GameClient("127.0.0.1", 50051, new GameController(new VisitorAI()));
visitorClient1.Connect();
visitorClient1.Register("VisitorClient1", "1234");

Thread.Sleep(1000);

client.VisitAccount(true, "TestClient1");
visitorClient1.VisitAccount(true, "TestClient1");

Thread.Sleep(1000);

client.VisitAccount(false, "");
testClient1.Queue(GameType.Normal, "AAEBAQcCrwSRvAIOHLACkQP/A44FqAXUBaQG7gbnB+8HgrACiLACub8CAA==");

Thread.Sleep(1000);

testClient2.Queue(GameType.Normal, "AAEBAQcCrwSRvAIOHLACkQP/A44FqAXUBaQG7gbnB+8HgrACiLACub8CAA==");

Thread.Sleep(10000);

visitorClient1.VisitAccount(false, "");

}

public static void RunServer()
Expand Down Expand Up @@ -146,4 +160,5 @@ private static GameClient CreateGameClientTask(string targetIp, int port, string
}

}

}
10 changes: 6 additions & 4 deletions SabberStoneXConsole/VisitorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace SabberStoneXConsole
public class VisitorClient : GameClient
{
private string _accountName;

public VisitorClient(string accountName, string targetIp, int port, GameController gameController) : base(targetIp, port, gameController)
{
_accountName = accountName;
Expand All @@ -29,16 +30,17 @@ public override void CallGameClientState(GameClientState oldState, GameClientSta

case GameClientState.Queued:
break;

case GameClientState.Placed:
break;

case GameClientState.Invited:
break;

case GameClientState.InGame:
break;
}
}

private void Register()
{
throw new NotImplementedException();
}
}
}

0 comments on commit d81149a

Please sign in to comment.