Skip to content

Commit

Permalink
Allow player color to be set through Python
Browse files Browse the repository at this point in the history
  • Loading branch information
iGemini committed Dec 23, 2015
1 parent 325a2ef commit 435af26
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 9 deletions.
7 changes: 7 additions & 0 deletions octgnFX/Octgn.Server/BinaryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,13 @@ public void Parse(byte[] data)
handler.SetBoard(arg0);
break;
}
case 102:
{
byte arg0 = reader.ReadByte();
string arg1 = reader.ReadString();
handler.SetPlayerColor(arg0, arg1);
break;
}
default:
Debug.WriteLine(L.D.ServerMessage__UnknownBinaryMessage + method);
break;
Expand Down
16 changes: 16 additions & 0 deletions octgnFX/Octgn.Server/BinaryStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,22 @@ public void SetBoard(string name)
writer.Close();
Send(stream.ToArray());
}

public void SetPlayerColor(byte player, string color)
{
MemoryStream stream = new MemoryStream(512);
stream.Seek(4, SeekOrigin.Begin);
BinaryWriter writer = new BinaryWriter(stream);

writer.Write(handler.muted);
writer.Write((byte)102);
writer.Write(player);
writer.Write(color);
writer.Flush(); writer.Seek(0, SeekOrigin.Begin);
writer.Write((int)stream.Length);
writer.Close();
Send(stream.ToArray());
}
}

class BinarySenderStub : BaseBinaryStub
Expand Down
6 changes: 6 additions & 0 deletions octgnFX/Octgn.Server/Broadcaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,11 @@ public void SetBoard(string name)
bin.SetBoard(name);
Send();
}

public void SetPlayerColor(byte player, string color)
{
bin.SetPlayerColor(player, color);
Send();
}
}
}
5 changes: 5 additions & 0 deletions octgnFX/Octgn.Server/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,5 +861,10 @@ public void SetBoard(string name)
{
_broadcaster.SetBoard(name);
}

public void SetPlayerColor(byte player, string colorHex)
{
_broadcaster.SetPlayerColor(player, colorHex);
}
}
}
1 change: 1 addition & 0 deletions octgnFX/Octgn.Server/IClientCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ interface IClientCalls
void ResetCardProperties(int id, byte player);
void Filter(int card, string color);
void SetBoard(string name);
void SetPlayerColor(byte player, string color);
}
}
4 changes: 4 additions & 0 deletions octgnFX/Octgn.Server/Protocol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,8 @@
<msg name="SetBoard" client="true" server="true">
<param name="name" type="string" />
</msg>
<msg name="SetPlayerColor" client="true" server="true">
<param name="player" type="Player"/>
<param name="color" type="string"/>
</msg>
</protocol>
9 changes: 9 additions & 0 deletions octgnFX/Octgn/Networking/BinaryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,15 @@ public void Parse(byte[] data)
handler.SetBoard(arg0);
break;
}
case 102:
{
Player arg0 = Player.Find(reader.ReadByte());
if (arg0 == null)
{ Debug.WriteLine("[SetPlayerColor] Player not found."); return; }
string arg1 = reader.ReadString();
handler.SetPlayerColor(arg0, arg1);
break;
}
default:
Debug.WriteLine("[Client Parser] Unknown message (id =" + method + ")");
break;
Expand Down
21 changes: 21 additions & 0 deletions octgnFX/Octgn/Networking/BinaryStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,27 @@ public void SetBoard(string name)
writer.Close();
Send(stream.ToArray());
}

public void SetPlayerColor(Player player, string color)
{
//Log.Info("[ProtOut] SetPlayerColor");
if(Program.Client == null)return;
MemoryStream stream = new MemoryStream(512);
stream.Seek(4, SeekOrigin.Begin);
BinaryWriter writer = new BinaryWriter(stream);

if (Program.Client.Muted != 0)
writer.Write(Program.Client.Muted);
else
writer.Write(0);
writer.Write((byte)102);
writer.Write(player.Id);
writer.Write(color);
writer.Flush(); writer.Seek(0, SeekOrigin.Begin);
writer.Write((int)stream.Length);
writer.Close();
Send(stream.ToArray());
}
}

public class BinarySenderStub : BaseBinaryStub
Expand Down
5 changes: 5 additions & 0 deletions octgnFX/Octgn/Networking/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,5 +1122,10 @@ public void ResetCardProperties(Card card, Player player)
if (player == Player.LocalPlayer) return;
card.ResetProperties(false);
}

public void SetPlayerColor(Player player, string colorHex)
{
player.SetPlayerColor(colorHex);
}
}
}
1 change: 1 addition & 0 deletions octgnFX/Octgn/Networking/IServerCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ interface IServerCalls
void ResetCardProperties(Card id, Player player);
void Filter(Card card, Color? color);
void SetBoard(string name);
void SetPlayerColor(Player player, string color);
}
}
16 changes: 8 additions & 8 deletions octgnFX/Octgn/Play/Gui/ChatControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static Block GameMessageToBlock(IGameMessage m)
if (m == null)
return null;

if (m is PlayerEventMessage)
if (m is PlayerEventMessage)
{
if (m.IsMuted) return null;
var b = new GameMessageBlock(m);
Expand Down Expand Up @@ -200,9 +200,9 @@ public static Block GameMessageToBlock(IGameMessage m)
var inline = new Span();

inline.Foreground = m.From.Color.CacheToBrush();
var chatRun = new Run("<" + m.From + "> ");
var chatRun = new Run("<" + m.From + "> ");
chatRun.Foreground = m.From.Color.CacheToBrush();
chatRun.FontWeight = FontWeights.Bold;
chatRun.FontWeight = FontWeights.Bold;
inline.Inlines.Add(chatRun);

inline.Inlines.Add(MergeArgsv2(m.Message, m.Arguments));
Expand All @@ -222,7 +222,7 @@ public static Block GameMessageToBlock(IGameMessage m)
b.Padding = new Thickness(5);
b.BorderBrush = Brushes.LightGray;
b.Foreground = m.From.Color.CacheToBrush();
var par = new Paragraph(MergeArgsv2(m.Message, m.Arguments));
var par = new Paragraph(MergeArgsv2(m.Message, m.Arguments));
par.Margin = new Thickness(0);
b.Blocks.Add(par);
//var block = new BlockUIContainer();
Expand Down Expand Up @@ -259,7 +259,7 @@ public static Block GameMessageToBlock(IGameMessage m)
var b = new GameMessageBlock(m);
var chatRun = MergeArgsv2(m.Message, m.Arguments);
chatRun.Foreground = m.From.Color.CacheToBrush();
p.Inlines.Add(chatRun);
p.Inlines.Add(chatRun);
b.Blocks.Add(p);
return b;
}
Expand All @@ -271,7 +271,7 @@ public static Block GameMessageToBlock(IGameMessage m)
var b = new GameMessageBlock(m);
var chatRun = MergeArgsv2(m.Message, m.Arguments);
chatRun.Foreground = m.From.Color.CacheToBrush();
b.Blocks.Add(p);
b.Blocks.Add(p);
p.Inlines.Add(chatRun);
return b;
}
Expand All @@ -281,7 +281,7 @@ public static Block GameMessageToBlock(IGameMessage m)

var brush = m.From.Color.CacheToBrush();

var p = new Paragraph();
var p = new Paragraph();
var b = new GameMessageBlock(m);
b.TextAlignment = TextAlignment.Center;
b.Margin = new Thickness(2);
Expand Down Expand Up @@ -334,7 +334,7 @@ public static Block GameMessageToBlock(IGameMessage m)
var b = new GameMessageBlock(m);
var chatRun = MergeArgsv2(m.Message, m.Arguments);
chatRun.Foreground = m.From.Color.CacheToBrush();
p.Inlines.Add(chatRun);
p.Inlines.Add(chatRun);
b.Blocks.Add(p);
return b;
}
Expand Down
24 changes: 24 additions & 0 deletions octgnFX/Octgn/Play/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public bool InvertedTable
//Color for the chat.
// Associated color
public Color Color { get; set; }
public Color ActualColor { get; set; }

// Work around a WPF binding bug ? Borders don't seem to bind correctly to Color!
public Brush Brush
Expand Down Expand Up @@ -371,6 +372,29 @@ public void SetPlayerColor(int idx)
OnPropertyChanged("TransparentBrush");
}

public void SetPlayerColor(string colorHex)
{
var convertFromString = ColorConverter.ConvertFromString(colorHex);
if (convertFromString != null)
{
ActualColor = (Color)convertFromString;

if (this == LocalPlayer)
{
return;
}

Color = (Color) convertFromString;

_solidBrush = new SolidColorBrush(Color);
_transparentBrush = new SolidColorBrush(Color) {Opacity = 0.4};

OnPropertyChanged("Color");
OnPropertyChanged("Brush");
OnPropertyChanged("TransparentBrush");
}
}

#endregion

#region Public interface
Expand Down
7 changes: 7 additions & 0 deletions octgnFX/Octgn/Play/State/GameSaveState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */


namespace Octgn.Play.State
{
using System;
Expand Down Expand Up @@ -71,6 +73,9 @@ public GameSaveState Load(GameEngine engine, Play.Player fromPlayer)
foreach (var p in state.Players)
{
var player = Play.Player.Find(p.Id);
player.Color = p.Color;
player.Brush = new SolidColorBrush(player.Color);
player.TransparentBrush = new SolidColorBrush(player.Color) { Opacity = 0.4};
foreach (var gv in p.GlobalVariables)
{
player.GlobalVariables[gv.Key] = gv.Value;
Expand Down Expand Up @@ -261,6 +266,7 @@ public class PlayerSaveState : SaveState<Play.Player, PlayerSaveState>
public GroupSaveState[] Groups { get; set; }
public Dictionary<string, string> GlobalVariables { get; set; }
public CounterSaveState[] Counters { get; set; }
public Color Color { get; set; }

public PlayerSaveState()
{
Expand All @@ -273,6 +279,7 @@ public override PlayerSaveState Create(Play.Player play, Play.Player fromPlayer)
this.GlobalVariables = play.GlobalVariables;
this.Counters = play.Counters.Select(x => new CounterSaveState().Create(x, fromPlayer)).ToArray();
this.Groups = play.Groups.Select(x => new GroupSaveState().Create(x, fromPlayer)).ToArray();
this.Color = play.Color;
return this;
}
}
Expand Down
2 changes: 2 additions & 0 deletions octgnFX/Octgn/Scripting/Versions/3.1.0.2.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ def hand(self): return self._hand
def piles(self): return self._piles
@property
def color(self): return _api.PlayerColor(self._id)
@color.setter
def color(self, colorHex): _api.SetPlayerColor(self._id, colorHex)
@property
def isInverted(self): return _api.PlayerHasInvertedTable(self._id)
def getGlobalVariable(self,gname):
Expand Down
12 changes: 11 additions & 1 deletion octgnFX/Octgn/Scripting/Versions/Script_3_1_0_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public string PlayerName(int id)

public string PlayerColor(int id)
{
return Player.Find((byte)id).Color.ToString().Remove(1, 2);
return Player.Find((byte)id).ActualColor.ToString().Remove(1, 2);
}

public bool IsActivePlayer(int id)
Expand Down Expand Up @@ -103,6 +103,16 @@ public bool PlayerHasInvertedTable(int id)
return Player.Find((byte)id).InvertedTable;
}

public void SetPlayerColor(int playerId, string colorHex)
{
if (playerId == Player.LocalPlayer.Id)
Program.Client.Rpc.SetPlayerColor(Player.Find((byte) playerId), colorHex);
else
{
Whisper("You can only change your own color.");
}
}

#endregion Player API

#region Counter API
Expand Down
1 change: 1 addition & 0 deletions recentchanges.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

Allow player color to be set through python - Gemini

0 comments on commit 435af26

Please sign in to comment.