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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
import enet, strutils,
sfml, sfml_colors, sg_gui, input_helpers,
math_helpers, sg_packets, estreams, tables,
json, sg_assets, client_helpers
if enetInit() != 0:
quit "Could not initialize ENet"
type
TClientSettings = object
resolution*: TVideoMode
offlineFile: string
dirserver: tuple[host: string, port: int16]
website*: string
var
clientSettings: TClientSettings
event: enet.TEvent
bConnected = false
runServer = true
gui = newGuiContainer()
zonelist = newGuiContainer()
kc = newKeyClient(setActive = true)
clock = newClock()
chatBox: PMessageArea
chatInput: PTextEntry
loginBtn, playBtn: PButton
fpsText = newText("", guiFont, 18)
connectionButtons: seq[PButton]
connectButton: PButton
u_alias, u_passwd: PTextEntry
dirServer: PServer
zone: PServer
showZoneList = false
myCreds = newScLogin(0, "", "") ##my session token
proc handleChat(server: PServer; buf: PBuffer) =
let msg = readScChat(buf)
chatBox.add msg
proc handlePlayerLogin(server: PServer; buf: PBuffer) =
let login = readScLogin(buf)
myCreds = login
echo("I am ", $myCreds)
kc.registerHandler MouseLeft, down, proc() =
gui.click(input_helpers.getMousePos())
block:
var pos = vec2f(15, 550)
chatBox = gui.newMessageArea(pos)
pos.y += 20
chatInput = gui.newTextEntry("...", pos, proc() =
sendPubChat dirServer, chatInput.getText()
chatInput.clearText())
gui.setActive(chatInput)
proc dispMessage(args: varargs[string, `$`]) =
var s = ""
for it in items(args):
s.add it
chatbox.add(s)
proc dispMessage(text: string) {.inline.} =
chatbox.add(text)
proc dispError(text: string) {.inline.} =
chatBox.add(newScChat(kind = CError, text = text))
proc updateButtons() =
let conn = dirServer.connected
for b in connectionButtons: setEnabled(b, conn)
if conn:
connectButton.setString "Disconnect"
else:
connectButton.setString "Connect"
proc poll(serv: PServer; timeout: cuint = 30) =
if serv.isNil or serv.host.isNil: return
if serv.connected:
while serv.host.hostService(event, timeout) > 0:
case event.kind
of EvtReceive:
var buf = newBuffer(event.packet)
serv.handlePackets(buf)
event.packet.destroy()
of EvtDisconnect:
dispMessage "Disconnected"
serv.connected = false
event.peer.data = nil
updateButtons()
of EvtNone: discard
else:
echo repr(event)
else:
if serv.host.hostService(event, timeout) > 0 and event.kind == EvtConnect:
dispMessage "Connected"
serv.connected = true
if serv.peer != event.peer:
serv.peer = event.peer
event.peer.data = serv
updateButtons()
proc tryLogin*(b: PButton) =
var login = newCsLogin(
alias = u_alias.getText(),
passwd = u_passwd.getText())
dirServer.send HLogin, login
proc tryTransition*(b: PButton) =
discard
#zone.writePkt HZoneJoinReq, myCreds
proc tryConnect*(b: PButton) =
if not dirServer.connected:
var error: string
if not dirServer.connect(
clientSettings.dirServer.host,
clientSettings.dirServer.port,
error):
dispError(error)
else:
dirServer.peer.disconnect(1)
proc playOffline*(b: PButton) =
var errors: seq[string] = @[]
if loadSettingsFromFile(clientSettings.offlineFile, errors):
transition()
else:
dispMessage "Errors reading the file (", clientSettings.offlineFile, "):"
for e in errors: dispError(e)
proc getClientSettings*(): TClientSettings =
result = clientSettings
proc lobbyInit*() =
var s = json.parseFile("./client_settings.json")
clientSettings.offlineFile = "data/"
clientSettings.offlineFile.add s["default-file"].str
let dirserv = s["directory-server"]
clientSettings.dirserver.host = dirserv["host"].str
clientSettings.dirserver.port = dirserv["port"].num.int16
clientSettings.resolution.width = s["resolution"][0].num.cint
clientSettings.resolution.height= s["resolution"][1].num.cint
clientSettings.resolution.bitsPerPixel = s["resolution"][2].num.cint
clientSettings.website = s["website"].str
zonelist.setPosition(vec2f(200.0, 100.0))
connectionButtons = @[]
var pos = vec2f(10, 10)
u_alias = gui.newTextEntry(
if s.hasKey("alias"): s["alias"].str else: "alias",
pos)
pos.y += 20
u_passwd = gui.newTextEntry("buzz", pos)
pos.y += 20
connectionButtons.add(gui.newButton(
text = "Login",
position = pos,
onClick = tryLogin,
startEnabled = false))
pos.y += 20
fpsText.setPosition pos
pos.y += 20
connectButton = gui.newButton(
text = "Connect",
position = pos,
onClick = tryConnect)
pos.y += 20
gui.newButton("Test Files", position = pos, onClick = proc(b: PButton) =
var req = newCsZoneJoinReq(myCreds)
dirServer.send HZoneJoinReq, req)
pos.y += 20
connectionButtons.add(gui.newButton(
text = "Test Chat",
position = pos,
onClick = (proc(b: PButton) =
var pkt = newCsChat(text = "ohai")
dirServer.send HChat, pkt),
startEnabled = false))
pos.y += 20
downloadProgress.setPosition(pos)
downloadProgress.bg.setFillColor(color(34, 139, 34))
downloadProgress.bg.setSize(vec2f(0, 0))
gui.add(downloadProgress)
playBtn = gui.newButton(
text = "Play",
position = vec2f(680.0, 8.0),
onClick = tryTransition,
startEnabled = false)
gui.newButton(
text = "Play Offline",
position = vec2f(680.0, 28.0),
onClick = playOffline)
discard """gui.newButton(text = "Scrollback + 1", position = vec2f(185, 10), onClick = proc(b: PButton) =
messageArea.scrollBack += 1
update(messageArea))
gui.newButton(text = "Scrollback - 1", position = vec2f(185+160, 10), onClick = proc(b: PButton) =
messageArea.scrollBack -= 1
update(messageArea))
gui.newButton(text = "Flood msg area", position = vec2f(185, 30), onClick = proc(b: PButton) =
for i in 0.. <30:
dispMessage($i))"""
dirServer = newServer()
dirServer.addHandler HChat, handleChat
dirServer.addHandler HLogin, handlePlayerLogin
dirServer.addHandler HFileTransfer, client_helpers.handleFilePartRecv
dirServer.addHandler HChallengeResult, client_helpers.handleFileChallengeResult
dirServer.addHandler HFileChallenge, client_helpers.handleFileChallenge
proc lobbyReady*() =
kc.setActive()
gui.setActive(u_alias)
var i = 0
proc lobbyUpdate*(dt: float) =
dirServer.poll()
#let res = disp.poll()
gui.update(dt)
i = (i + 1) mod 60
if i == 0:
fpsText.setString("FPS: " & ff(1.0/dt))
proc lobbyDraw*(window: PRenderWindow) =
window.clear(Black)
window.draw chatBox
window.draw gui
window.draw fpsText
if showZonelist: window.draw zonelist
window.display()
|