/
client.cs
196 lines (174 loc) · 6.48 KB
/
client.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using SharpKit.Html;
using SharpKit.JavaScript;
using SharpKit.jQuery;
using SharpKitWebApp.core;
using SharpKitWebApp.data;
namespace SharpKitWebApp
{
public delegate void EventListenerEx(objectEx e);
[JsType(JsMode.Prototype, Filename = "gen/default.js")]
public class client
{
private string _turnClientId;
private HtmlTableElement _table;
private EState _figure;
private HtmlDivElement _turn;
public string ClientId
{
get
{
JsString url = HtmlContext.window.document.location.href;
int idx = url.lastIndexOf('?') + 1;
JsString name = url.substring(idx);
JsArray arr = name.split('=');
name = arr[1].ToString();
return name;
}
}
public client()
{
init();
}
[JsMethod]
private void init()
{
commet.subscribe(delegate(objectEx e)
{
response r = e.As<response>();
if (r is startGameResponse)
{
startGameResponse response = r as startGameResponse;
OnStartGame(response);
}
if (r is messageResponse)
{
messageResponse response = r as messageResponse;
//_div.innerHTML += "Message From:" + response.From + " Message:" + response.Message + "<br/>";
}
if (r is turnResponse)
{
turnResponse response = r as turnResponse;
if (response.message == "")
{
HtmlTableRowElement row = _table.rows[response.y].As<HtmlTableRowElement>();
HtmlTableCellElement cell = row.cells[response.x].As<HtmlTableCellElement>();
cell.innerHTML = (response.figure == EState.x) ? "x" : "o";
if((response.figure == EState.x))
{
js.addClass(cell,"tableCellX");
}
else
{
js.addClass(cell, "tableCell0");
}
_turnClientId = response.nextClientId;
RefreshTurn();
}
else if (response.message == "turn again")
{
}
else if (response.message == "winn")
{
HtmlContext.window.alert("You winn");
}
else if (response.message == "loss")
{
HtmlContext.window.alert("You loss");
}
else if (response.message == "equal")
{
HtmlContext.window.alert("equal");
}
}
if (r is connectResponse)
{
OnConnectResponse(r as connectResponse);
}
if (r is disconnectResponse)
{
disconnectResponse response = r as disconnectResponse;
//_div.innerHTML += "Disconnect ID = " + response.ClientID + "<br/>";
}
});
HtmlDivElement myButton = new HtmlDivElement();
js.addClass(myButton, "button");
myButton.innerHTML = "[Connect]";
myButton.addEventListener("click", OnConnectClick);
HtmlContext.document.body.appendChild(myButton);
HtmlContext.window.addEventListener("unload", OnBeforeUnload);
}
private void OnStartGame(startGameResponse response)
{
//_div.innerHTML += "Start Game ID = " + response.GameId + "<br/>";
_figure = (response.FirstClientId == this.ClientId) ? EState.x : EState.o;
//_div.innerHTML += "You are " + _figure + "<br/>";
_turnClientId = response.FirstClientId;
RefreshTurn();
}
private void RefreshTurn()
{
if (_turnClientId == this.ClientId)
{
js.removeClass(_turn, "notMyTurn");
js.addClass(_turn, "myTurn");
}
else
{
js.removeClass(_turn, "myTurn");
js.addClass(_turn, "notMyTurn");
}
}
private void OnConnectClick(DOMEvent evt)
{
commet.connect(this.ClientId);
}
private void OnConnectResponse(connectResponse response)
{
_turn = new HtmlDivElement();
js.addClass(_turn,"turn");
HtmlContext.document.body.appendChild(_turn);
_table = new HtmlTableElement();
js.attach(_table, "click", onTableClick);
js.addClass(_table, "table");
for (int y = 0; y < 3; ++y)
{
HtmlTableRowElement row = new HtmlTableRowElement();
_table.appendChild(row);
for (int x = 0; x < 3; ++x)
{
HtmlTableCellElement cell = new HtmlTableCellElement();
row.appendChild(cell);
js.addClass(cell,"tableCell");
}
}
HtmlContext.document.body.appendChild(_table);
}
private void onTableClick(DOMEvent evt)
{
if (_turnClientId == this.ClientId)
{
HtmlElement el = evt.target.As<HtmlElement>();
if (el is HtmlTableCellElement)
{
HtmlTableCellElement cell = el.As<HtmlTableCellElement>();
HtmlTableRowElement row = cell.parentNode.As<HtmlTableRowElement>();
//el.innerHTML = _figure;
turnRequest request = new turnRequest();
request.x = cell.cellIndex;
request.y = row.rowIndex;
request.clientId = this.ClientId;
request.figure = _figure;
server.send(request);
}
}
}
private void OnBeforeUnload(DOMEvent evt)
{
commet.disconnect();
}
}
}