-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.cs
139 lines (115 loc) · 4.07 KB
/
Dashboard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Windows.Forms;
using Fleck;
using SmartBot.Plugins.API;
namespace SmartBot.Plugins {
[Serializable]
class BPluginDataContainer : PluginDataContainer {
public BPluginDataContainer() {
Name = "SmartBotDashboard";
}
}
public class DashBoard : Plugin {
// Variables
readonly Server _server = new Server();
readonly BoardCdm _board = new BoardCdm();
MemoryStream _stream = new MemoryStream();
readonly DataContractJsonSerializer _json = new DataContractJsonSerializer(typeof(BoardCdm));
public override void OnPluginCreated() {
Init();
}
public override void Dispose() {
_server.Stop();
Debug.OnLogReceived -= OnLogging;
}
public override void OnVictory() {
_board.Wins++;
}
public override void OnDefeat() {
_board.Losses++;
}
public override void OnStarted() {
_board.BotStarted = true;
}
public override void OnStopped() {
_board.BotStarted = false;
}
private void OnLogging(string message) {
_board.Log.Add(message);
}
public override void OnTick() {
if (Bot.CurrentBoard != null) {
_board.Hero = Bot.CurrentBoard.HeroFriend;
_board.HeroId = Bot.CurrentBoard.HeroFriend.Template.Id.ToString();
_board.Enemy = Bot.CurrentBoard.HeroEnemy;
_board.EnemyId = Bot.CurrentBoard.HeroEnemy.Template.Id.ToString();
}
_stream = new MemoryStream();
_json.WriteObject(_stream, _board);
_stream.Position = 0;
var sr = new StreamReader(_stream);
var boardStateString = sr.ReadToEnd();
_server.Send(boardStateString);
// After send dump the logs since we already sent them.
_board.Log = new List<string>();
}
private void Init() {
Bot.Log("[PLUGIN] -> Dashboard: Plugin Created");
// Events
Debug.OnLogReceived += OnLogging;
_server.Start();
// Init the _board objects if needed.
_board.Log = new List<string>();
_board.BotStarted = false;
}
}
public class BoardCdm {
public Boolean BotStarted { get; set; }
public int Wins { get; set; }
public int Losses { get; set; }
public Card Hero { get; set; }
public String HeroId { get; set; }
public Card Enemy { get; set; }
public String EnemyId { get; set; }
public List<String> Log { get; set; }
}
public class Server {
readonly List<IWebSocketConnection> _allSockets = new List<IWebSocketConnection>();
readonly IWebSocketServer _server = new WebSocketServer("ws://127.0.0.1:8081");
public void Start() {
_server.Start(socket => {
socket.OnOpen = () => {
Bot.Log("[PLUGIN] -> Dashboard: Client connected...");
_allSockets.Add(socket);
};
socket.OnClose = () => {
_allSockets.Remove(socket);
Bot.Log("[PLUGIN] -> Dashboard: Client disconnected...");
};
socket.OnMessage = HandleMessage;
});
Bot.Log("[PLUGIN] -> Dashboard: Server started...");
}
public void Stop() {
_server.Dispose();
}
public void Send(String message) {
foreach(var socket in _allSockets) {
socket.Send(message);
}
}
private void HandleMessage(String message) {
switch (message) {
case "start":
Bot.StartBot();
break;
case "stop":
Bot.StopBot();
break;
}
}
}
}