Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 86 lines (70 sloc) 2.711 kb
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
1 module Main
2
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
3 open DarkMadness2.Client.Console
4 open DarkMadness2.NetworkCommunication
5 open DarkMadness2.NetworkCommunication.Serializer
d30cb06 @yurii-litvinov attempt to implement unified event source abstraction for asynchronous e...
authored
6 open DarkMadness2.Core
7e88fbf @yurii-litvinov asynchronous event processing schema.
authored
7 open DarkMadness2.Core.EventSource.EventSourceUtils
ea1293f @yurii-litvinov completely removed spin waiting code, small refactoring
authored
8 open DarkMadness2.Core.EventProcessing
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
9
10 let random = System.Random ()
11 let mutable charPosition = (random.Next 10, random.Next 10)
12 let mutable otherCharPosition = (0, 0)
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
13 let mutable clientId = -1
42f5035 @yurii-litvinov moved NetworkCommunicator to NetworkCommunication lib and renamed it to ...
authored
14 let communicator = Client ("127.0.0.1", 8181)
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
15
16 let (+) (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
17
e6b8528 @yurii-litvinov actual event generation and handling
authored
18 let processKeyPress (key : System.ConsoleKey) =
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
19 if otherCharPosition <> charPosition then
20 putChar ' ' charPosition
e6b8528 @yurii-litvinov actual event generation and handling
authored
21 let delta = match key with
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
22 | System.ConsoleKey.UpArrow -> (0, -1)
23 | System.ConsoleKey.DownArrow -> (0, 1)
24 | System.ConsoleKey.LeftArrow -> (-1, 0)
25 | System.ConsoleKey.RightArrow -> (1, 0)
26 | _ -> (0, 0)
f922ee3 @yurii-litvinov - fixed crash when trying to step out of the window border
authored
27 let correctCoords (x, y) =
49921e1 @yurii-litvinov fixed (mainly) "shadow of your own self" bug
authored
28 x >= 0 && x < maxX () && y >= 0 && y <= maxY ()
f922ee3 @yurii-litvinov - fixed crash when trying to step out of the window border
authored
29 let newCoords = charPosition + delta
30 if correctCoords newCoords then
31 charPosition <- newCoords
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
32 putChar '@' charPosition
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
33 CharacterMoveRequest charPosition |> serialize |> communicator.Send
e6b8528 @yurii-litvinov actual event generation and handling
authored
34
35 let processNewCoords coords =
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
36 if otherCharPosition <> charPosition then
e6b8528 @yurii-litvinov actual event generation and handling
authored
37 putChar ' ' otherCharPosition
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
38 otherCharPosition <- coords
39 putChar '@' otherCharPosition
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
40
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
41 // maximizeWindow ()
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
42 hideCursor ()
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
43
44 // Connecting to server...
45 communicator.Send <| serialize ConnectionRequest
7e88fbf @yurii-litvinov asynchronous event processing schema.
authored
46 let response = receive communicator |> deserialize
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
47 match response with
48 | ConnectionResponse id -> clientId <- id
49 | _ -> failwith "Connection failed"
50
51 System.Console.Title <- sprintf "Dark Madness 2 {Connected, client id = %d}" clientId
52
53 // Done.
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
54 putChar '@' charPosition
55
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
56 CharacterMoveRequest charPosition |> serialize |> communicator.Send
c8cafc3 @yurii-litvinov Dark Madness 2 Network Edition
authored
57
e6b8528 @yurii-litvinov actual event generation and handling
authored
58 type Event =
59 | ClientEvent of System.ConsoleKey
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
60 | ServerEvent of Message
e6b8528 @yurii-litvinov actual event generation and handling
authored
61
62 let processEvent event =
63 match event with
45be974 @yurii-litvinov spin waiting eliminated
authored
64 | ClientEvent System.ConsoleKey.Escape -> false
65 | ClientEvent key ->
66 processKeyPress key
67 true
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
68 | ServerEvent msg ->
69 match msg with
70 | CharacterPositionUpdate (id, x, y) ->
71 if id <> clientId then
72 processNewCoords (x, y)
45be974 @yurii-litvinov spin waiting eliminated
authored
73 true
74 | _ -> true
aae5529 @yurii-litvinov rewritten all communications to new protocol, so there will be no more "...
authored
75
7e88fbf @yurii-litvinov asynchronous event processing schema.
authored
76 let consoleEventSource = DarkMadness2.Core.EventSource.SyncEventSourceWrapper (fun () -> System.Console.ReadKey true)
e6b8528 @yurii-litvinov actual event generation and handling
authored
77
d30cb06 @yurii-litvinov attempt to implement unified event source abstraction for asynchronous e...
authored
78 let eventSource =
7e88fbf @yurii-litvinov asynchronous event processing schema.
authored
79 combine (event communicator) (event consoleEventSource) (
073749f @yurii-litvinov seems much better, type safety ok
authored
80 function
81 | Choice1Of2 messageFromServer -> ServerEvent (messageFromServer |> deserialize)
82 | Choice2Of2 keyInfo -> ClientEvent keyInfo.Key
83 )
e6b8528 @yurii-litvinov actual event generation and handling
authored
84
45be974 @yurii-litvinov spin waiting eliminated
authored
85 eventSource |> eventLoop processEvent
Something went wrong with that request. Please try again.