gamnit: tweak network doc
[nit.git] / contrib / tinks / src / client / base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Shader client code to manage the game context
16 module base
17
18 import app
19
20 import context
21
22 redef class App
23
24 # Context of the game, either local or remote
25 var context: GameContext is lazy do
26
27 # Server info
28 var address = null
29 var port = default_listening_port
30
31 if args.not_empty then
32 # Use first argument as the server address
33 address = args[0]
34 if args.length > 1 then port = args[1].to_i
35 else
36 print "Looking for a server..."
37
38 var servers = discover_local_servers
39 if servers.not_empty then
40 address = servers.first.address
41 port = servers.first.port
42 end
43 end
44
45 if address == null then
46 print "Launching a local server"
47
48 # No command line
49 return new LocalServerContext
50 else
51 print "Connecting to {address}:{port}"
52
53 # Args are: tinks server_address {port}
54 if args.length > 1 then port = args[1].to_i
55
56 # Setup connection config
57 var server_config = new RemoteServerConfig(address, port)
58 var server = new RemoteServer(server_config)
59
60 # Connect then complete handshake
61 assert server.connect else print_error "Connection to server failed with {server.socket.last_error or else "none"}"
62 assert server.handshake else print_error "Handshake with server failed"
63
64 # Download and setup remote game
65 var context = new RemoteGameContext(server)
66 context.setup
67
68 return context
69 end
70 end
71
72 # `Tank` of the local player, if any
73 fun local_tank: nullable Tank
74 do
75 # FIXME use a ? to one line this
76 var local_player = context.local_player
77 if local_player == null then return null
78 return local_player.tank
79 end
80 end