Check-in [6bcb16dedf]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:add netwrok
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:6bcb16dedf8da4c77dc7ba7609bd20707345a4e0
User & Date: liyujiel 2016-12-05 23:31:24
Context
2016-12-05
23:31
add netwrok Leaf check-in: 6bcb16dedf user: liyujiel tags: trunk
06:22
spell check-in: dba7d3e776 user: hhammadu tags: trunk
Changes

Added net.jl.



































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module net

if Pkg.installed("HttpServer") == nothing
    Pkg.add("HttpServer")
end
if Pkg.installed("WebSockets") == nothing
    Pkg.add("WebSockets")
end
push!(LOAD_PATH,pwd())
#using game
using HttpServer
using WebSockets
using SQLite
using databases
db = SQLite.DB("database.db")

#user flag is for check who is the fisrt user start this game
user_flag = 0
game_times = 0

#<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>


#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;


#wincode from server 0:palyer1 1:player2 2:server quit 3:there is a draw 8:not your turn 9:its your turn 10:bad payrole e:errorm

type Client
  role::String
  anti_role::String
  connection::TCPSocket
  connection_open::Bool
  rcv_msg::String
  sed_msg::String
  wincode::String
  errormessage::String
  message::String
  gametype::String
  legality::String
  timelimit::String
  limitadd::String
  authString::String
  movenum::String
  movetype::String
  sourcex::String
  sourcey::String
  targetx::String
  targety::String
  option::String
  cheating::String
  targetx2::String
  targety2::String
end

function parse_msg(msg)
  rst_msg = split(msg, ":")
  return rst_msg
end

function analyze_rcv_msg(client,msg)
  msg = parse_msg(msg)
#<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>
#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;
  ret_msg = []
  wincode = msg[1]
  if wincode == "0" && length(msg) == 5
    gametype = msg[2]
    legality = msg[3]
    timelimit = msg[4]
    limitadd = msg[5]
    if server_Decide_playload(wincode,gametype,legality,timelimit,limitadd)
      client.wincode,client.gametype,client.legality,client.timelimit,client.limitadd = wincode,gametype,legality,timelimit,limitadd
      push!(ret_msg,wincode,gametype,legality,timelimit,limitadd)
      return client,ret_msg
    end
  elseif wincode == "M"
    authString = msg[2]
    message = msg[3]
    client.wincode,client.authString,client.message = wincode,authString,message
    push!(ret_msg,wincode,authString,message)
    return client,ret_msg

  elseif wincode == "e"
    errormessage = msg[2]
    client.wincode,client.errormessage = wincode,errormessage
    push!(ret_msg,wincode,errormessage)
    return client,ret_msg
  elseif length(msg) == 12
    authString = msg[2]
    movenum = msg[3]
    movetype = msg[4]
    sourcex = msg[5]
    sourcey = msg[6]
    targetx = msg[7]
    targety = msg[8]
    option = msg[9]
    cheating = msg[10]
    targetx2 = msg[11]
    targety2 = msg[12]
    # if targety2 == "0" && targetx2 == "0" #value is 0 when targetx2 and targety2 NULL
    #   client.wincode,client.authString,client.movenum,client.sourcex,client.sourcey,client.targetx,client.targety,client.option,client.cheating =wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating
    #   push!(ret_msg,wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating)
    #   return client,ret_msg
    # end
    client.wincode,client.authString,client.movenum,client.sourcex,client.sourcey,client.targetx,client.targety,client.option,client.cheating =wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating
    client.targetx2,client.targety2 = targetx2,targety2
    push!(ret_msg,wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating,targetx2,targety2)
    return client,ret_msg
  end
  return client,false
end







function Decide_userip()
  println("Hi, your IP address is : ")
  user_IP = getipaddr()
  print(user_IP)
  return user_IP
end

function Decide_ip(args)
  if args == "re_input"
    println("Please re-input another user's IP address\n")
    ip = readline(STDIN)
  else
    ip = args
  end
  i = 1
  if length(ip) <= 16
    while (isdigit(ip[i]) || ip[i] == '.')&& i< length(ip)
      i = i + 1
    end
  else
      println("Error IP input")
      ip = Decide_ip("re_input")
  end
  if i != length(ip)
    println("Error IP input")
    ip = Decide_ip("re_input")
  end
  return ip
end


function Decide_port(args)
  if args == "re_input"
    println("\nPlease enter your Port #: ")
    port = readline(STDIN)
  else
    port = args
  end
  i= 1
  if length(port) < 6
    while i < length(port)
      if isdigit(port[i])
        i += 1
      end
    end
  else
    println("Error port Please try again")
    port = Decide_port("re_input")
    i = length(port)
  end
  if i != length(port)
    println("Error Port input")
    port = Decide_port("re_input")
  end
  return port
end

function Build_msg(client)
  msg = ""
  authString = client.authString
  if client.wincode == "0"
    client.gametype = Access_By_key(db, 'type')
  elseif client.wincode == "2"
    #<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>
    movenum = Access_By_key(db, 'move_number')
    movetype = Access_By_key(db, 'move_type')
    sourcex = Access_By_key(db, 'sourcex')
    sourcey = Access_By_key(db, 'sourcey')
    targetx = Access_By_key(db, 'targetx')
    targety = Access_By_key(db,'targety')
    option = Access_By_key(db,'option')
    cheating = Access_By_key(db,'i_am_cheating')
    targetx2 = Access_By_key(db,'targetx2')
    targety2 = Access_By_key(db,'targety2')
    msg = client.wincode * ":" * client.authString * ":" * movenum * ":" * movetype * ":" *sourcex * ":" * sourcey * ":" *targetx * ":" *targety * ":" * option * ":" * cheating * ":" * targetx2 * ":" * targety2
  end
end

function Net_start(args)
  # IP = Decide_ip()
  user_IP = Decide_userip()
  println("\n")
  println(args[1], " " , args[2])
  if args[1] == "localhost"
    args[1] = "127.0.0.1"
  end
  println(args[1])
  ip = IPv4(Decide_ip(args[1]))
  port = Decide_port(args[2])
  connection = connect(ip, parse(port))
  client = Client("","",connection,isopen(connection),"","","","","","","","","","","","","","","","","","","","")

  println("Listen from server...")
  #connection = (Server)
  println("with server connceted...")
  @async begin
    while true
        rcv_msg = chomp(readline(client.connection))
        client.rcv_msg = rcv_msg
        if game_times == 1
          auth_ana_msg = parse_msg(rcv_msg)
          client.authString = auth_ana_msg[2]
          global game_times += 1
        end
        println("The message you recive is from server :", client.rcv_msg, "\n")
        client,rtn_rcv_msg= analyze_rcv_msg(client,client.rcv_msg)
        if client.wincode == "M"
          println(client.message)
          continue
        elseif client.wincode == "e"
          println("error")
        else
          updateMoves(db,client.movenum, client.movetype ,client.sourcex, client.sourcey, client.targetx, client.targety, client.option, client.cheating, client.targetx2, client.targety2)

    end
  end
  @async begin
    while true
      #println("911")
        println("\nPlease enter the message you wanna pass to another client: \n")
        user_msg = Build_msg(client)
        println("\nUser msg is : ", user_msg)
        println("Passing Message...")
        println(client.connection,user_msg)
        println("Message Passed")
    end
  end

  while true
    sleep(5)
  end
end

Net_start(ARGS)

end  # module net

#0:S:0:100:10

Added net_user.jl.



































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module net

if Pkg.installed("HttpServer") == nothing
    Pkg.add("HttpServer")
end
if Pkg.installed("WebSockets") == nothing
    Pkg.add("WebSockets")
end
push!(LOAD_PATH,pwd())
#using game
using HttpServer
using WebSockets
using SQLite
using databases
db = SQLite.DB("database.db")

#user flag is for check who is the fisrt user start this game
user_flag = 0
game_times = 0

#<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>


#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;


#wincode from server 0:palyer1 1:player2 2:server quit 3:there is a draw 8:not your turn 9:its your turn 10:bad payrole e:errorm

type Client
  role::String
  anti_role::String
  connection::TCPSocket
  connection_open::Bool
  rcv_msg::String
  sed_msg::String
  wincode::String
  errormessage::String
  message::String
  gametype::String
  legality::String
  timelimit::String
  limitadd::String
  authString::String
  movenum::String
  movetype::String
  sourcex::String
  sourcey::String
  targetx::String
  targety::String
  option::String
  cheating::String
  targetx2::String
  targety2::String
end

function parse_msg(msg)
  rst_msg = split(msg, ":")
  return rst_msg
end

function analyze_rcv_msg(client,msg)
  msg = parse_msg(msg)
#<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>
#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;
  ret_msg = []
  wincode = msg[1]
  if wincode == "0" && length(msg) == 5
    gametype = msg[2]
    legality = msg[3]
    timelimit = msg[4]
    limitadd = msg[5]
    if server_Decide_playload(wincode,gametype,legality,timelimit,limitadd)
      client.wincode,client.gametype,client.legality,client.timelimit,client.limitadd = wincode,gametype,legality,timelimit,limitadd
      push!(ret_msg,wincode,gametype,legality,timelimit,limitadd)
      return client,ret_msg
    end
  elseif wincode == "M"
    authString = msg[2]
    message = msg[3]
    client.wincode,client.authString,client.message = wincode,authString,message
    push!(ret_msg,wincode,authString,message)
    return client,ret_msg

  elseif wincode == "e"
    errormessage = msg[2]
    client.wincode,client.errormessage = wincode,errormessage
    push!(ret_msg,wincode,errormessage)
    return client,ret_msg
  elseif length(msg) == 12
    authString = msg[2]
    movenum = msg[3]
    movetype = msg[4]
    sourcex = msg[5]
    sourcey = msg[6]
    targetx = msg[7]
    targety = msg[8]
    option = msg[9]
    cheating = msg[10]
    targetx2 = msg[11]
    targety2 = msg[12]
    # if targety2 == "0" && targetx2 == "0" #value is 0 when targetx2 and targety2 NULL
    #   client.wincode,client.authString,client.movenum,client.sourcex,client.sourcey,client.targetx,client.targety,client.option,client.cheating =wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating
    #   push!(ret_msg,wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating)
    #   return client,ret_msg
    # end
    client.wincode,client.authString,client.movenum,client.sourcex,client.sourcey,client.targetx,client.targety,client.option,client.cheating =wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating
    client.targetx2,client.targety2 = targetx2,targety2
    push!(ret_msg,wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating,targetx2,targety2)
    return client,ret_msg
  end
  return client,false
end







function Decide_userip()
  println("Hi, your IP address is : ")
  user_IP = getipaddr()
  print(user_IP)
  return user_IP
end

function Decide_ip(args)
  if args == "re_input"
    println("Please re-input another user's IP address\n")
    ip = readline(STDIN)
  else
    ip = args
  end
  i = 1
  if length(ip) <= 16
    while (isdigit(ip[i]) || ip[i] == '.')&& i< length(ip)
      i = i + 1
    end
  else
      println("Error IP input")
      ip = Decide_ip("re_input")
  end
  if i != length(ip)
    println("Error IP input")
    ip = Decide_ip("re_input")
  end
  return ip
end


function Decide_port(args)
  if args == "re_input"
    println("\nPlease enter your Port #: ")
    port = readline(STDIN)
  else
    port = args
  end
  i= 1
  if length(port) < 6
    while i < length(port)
      if isdigit(port[i])
        i += 1
      end
    end
  else
    println("Error port Please try again")
    port = Decide_port("re_input")
    i = length(port)
  end
  if i != length(port)
    println("Error Port input")
    port = Decide_port("re_input")
  end
  return port
end

function Build_msg(client)
  msg = ""
  authString = client.authString
  if client.wincode == "0"
    client.gametype = Access_By_key(db, 'type')
  elseif client.wincode == "2"
    #<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>
    movenum = Access_By_key(db, 'move_number')
    movetype = Access_By_key(db, 'move_type')
    sourcex = Access_By_key(db, 'sourcex')
    sourcey = Access_By_key(db, 'sourcey')
    targetx = Access_By_key(db, 'targetx')
    targety = Access_By_key(db,'targety')
    option = Access_By_key(db,'option')
    cheating = Access_By_key(db,'i_am_cheating')
    targetx2 = Access_By_key(db,'targetx2')
    targety2 = Access_By_key(db,'targety2')
    msg = client.wincode * ":" * client.authString * ":" * movenum * ":" * movetype * ":" *sourcex * ":" * sourcey * ":" *targetx * ":" *targety * ":" * option * ":" * cheating * ":" * targetx2 * ":" * targety2
  end
end

function Net_user_start(args)
  # IP = Decide_ip()
  user_IP = Decide_userip()
  println("\n")
  println(args[1], " " , args[2])
  if args[1] == "localhost"
    args[1] = "127.0.0.1"
  end
  println(args[1])
  ip = IPv4(Decide_ip(args[1]))
  port = Decide_port(args[2])
  connection = connect(ip, parse(port))
  client = Client("","",connection,isopen(connection),"","","","","","","","","","","","","","","","","","","","")

  println("Listen from server...")
  #connection = (Server)
  println("with server connceted...")
  @async begin
    while true
        rcv_msg = chomp(readline(client.connection))
        client.rcv_msg = rcv_msg
        if game_times == 1
          auth_ana_msg = parse_msg(rcv_msg)
          client.authString = auth_ana_msg[2]
          global game_times += 1
        end
        println("The message you recive is from server :", client.rcv_msg, "\n")
        client,rtn_rcv_msg= analyze_rcv_msg(client,client.rcv_msg)
        if client.wincode == "M"
          println(client.message)
          continue
        elseif client.wincode == "e"
          println("error")
        else
          updateMoves(db,client.movenum, client.movetype ,client.sourcex, client.sourcey, client.targetx, client.targety, client.option, client.cheating, client.targetx2, client.targety2)

    end
  end
  @async begin
    while true
      #println("911")
        println("\nPlease enter the message you wanna pass to another client: \n")
        user_msg = Build_msg(client)
        println("\nUser msg is : ", user_msg)
        println("Passing Message...")
        println(client.connection,user_msg)
        println("Message Passed")
    end
  end

  while true
    sleep(5)
  end
end

Net_user_start(ARGS)

end  # module net

#0:S:0:100:10

Added server.jl.

































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
module server

if Pkg.installed("HttpServer") == nothing
    Pkg.add("HttpServer")
end
if Pkg.installed("WebSockets") == nothing
    Pkg.add("WebSockets")
end
push!(LOAD_PATH,pwd())
#using game
using HttpServer
using WebSockets
using SQLite
using databases
db = SQLite.DB("database.db")

#user flag is for check who is the fisrt user start this game
user_flag = 0
game1_times = 0
game2_times = 0
Game_type = ""
waiting_msg = "Now your waiting for: "
Game_player = 0

#<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>
type client
  role::String
  anti_role::String
  connection::TCPSocket
  connection_open::Bool
  rcv_msg::String
  sed_msg::String
  wincode::String
  errormessage::String
  message::String
  gametype::String
  legality::String
  timelimit::String
  limitadd::String
  authString::String
  movenum::String
  movetype::String
  sourcex::String
  sourcey::String
  targetx::String
  targety::String
  option::String
  cheating::String
  targetx2::String
  targety2::String
end

# client1 = client("",false,"","","","","","","","","","","","","","","","","","")
# client2 = client("",false,"","","","","","","","","","","","","","","","","","")


#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;

#authString is server give to user
function Decide_send_authString()
  if user_flag == 0
    global user_flag = 1
    return "user_player1"
  end
  return "user_player2"
end


#wincode from server 0:palyer1 1:player2 2:server quit 3:there is a draw 8:not your turn 9:its your turn 10:bad payrole e:errorm

#in julia "*" join the string
# function build_msg()
#  msg[1] =

#check the playload user input is true
function server_Decide_playload(wincode,gametype,legality,timelimit,limitadd)

  if (Decide_wincode_from_clinet(wincode) && Decide_gametype(gametype) && Decide_legality(legality) && isdigit(timelimit))
    println("Play load Test Finished!")
    return true
  end
  println("Play load Test Failed!")
  return false
  # global wincode = "10"
end

#wincode from client 0:request game 1:play game 2:play move 3:accuse cheating 10: bad palyload
function Decide_wincode_from_clinet(wincode)
  println("Decide wincode...")
  if wincode == "0" ||wincode == "1"|| wincode == "2" ||wincode == "3" || wincode == "10"
    println("Wincode Test Finished!")
    return true
  end
  return false
end

function Decide_rec_authString()
  #first palyer wincode = 2 second palyer wincode = 9
  if authString == "user_player1" && wincode == "2"
    return true
  elseif authString == "user_player2" && wincode == "9"
    return true
  end
  return false
end

#legality 0:cheating 1: no cheating
function Decide_legality(legality)
  println("Decide legality...")
  if legality == "0"
    println("Legality Test Finished!")
    return true
  elseif legality == "1"
    println("Legality Test Finished!")
    return true
  end
  return false
end
#gametype: C:chu shogi M S
function Decide_gametype(gametype)
  println("Decide game type ...")
  if gametype == "C" || gametype == "M" || gametype == "S"
    println("Gametype Test Finished!")
    return true
  end
  return false
end
#movetype: 1:move 2:drop 3:resign
function Decide_movetype(movetype)
  if movetype == "1"
    return true
  elseif movetype == "2"
    return true
  elseif movetype == "3"
    return true
  end
  return false
end
#option: 0 for null ,! for promotion take in that place
function Decide_option(option)
  if option == "0"
    return "not_use"
  elseif option == "!"||movetype == "drop"
    promotion_take()
  end
  return false
end
#cheating: 0 not cheating 1 cheating
function Decide_cheat(cheating)
  if cheating == "0"
    true
  else cheating == "1"
    true
  end
  return false
end
#Message: Message wanna send to another user





function parse_msg(msg)
  rst_msg = split(msg, ":")
  return rst_msg
end

function analyze_rcv_msg(client,msg)
  msg = parse_msg(msg)
#<wincode>:<authString>:<movenum>:<movetype>:<sourcex>:<sourcey>:<targetx>:<targety>:<option>:<cheating>:<targetx2>:<targety2>
#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;
  ret_msg = []
  wincode = msg[1]
  if wincode == "1"
    println(client.connection,"Connection Breaked!")
    close(client.connection)
    quit()
  elseif wincode == "0" && length(msg) == 5
    gametype = msg[2]
    legality = msg[3]
    timelimit = msg[4]
    limitadd = msg[5]
    if server_Decide_playload(wincode,gametype,legality,timelimit,limitadd)
      client.wincode,client.gametype,client.legality,client.timelimit,client.limitadd = wincode,gametype,legality,timelimit,limitadd
      push!(ret_msg,wincode,gametype,legality,timelimit,limitadd)
      return client,ret_msg
    end
  elseif wincode == "M"
    authString = msg[2]
    message = msg[3]
    client.wincode,client.authString,client.message = wincode,authString,message
    push!(ret_msg,wincode,authString,message)
    return client,ret_msg

  elseif wincode == "e"
    errormessage = msg[2]
    client.wincode,client.errormessage = wincode,errormessage
    push!(ret_msg,wincode,errormessage)
    return client,ret_msg
  elseif length(msg) == 12
    authString = msg[2]
    movenum = msg[3]
    movetype = msg[4]
    sourcex = msg[5]
    sourcey = msg[6]
    targetx = msg[7]
    targety = msg[8]
    option = msg[9]
    cheating = msg[10]
    targetx2 = msg[11]
    targety2 = msg[12]
    if targety2 == "0" && targetx2 == "0" #value is 0 when targetx2 and targety2 NULL
      client.wincode,client.authString,client.movenum,client.sourcex,client.sourcey,client.targetx,client.targety,client.option,client.cheating =wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating
      push!(ret_msg,wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating)
      return client,ret_msg
    end
    client.wincode,client.authString,client.movenum,client.sourcex,client.sourcey,client.targetx,client.targety,client.option,client.cheating =wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating
    client.targetx2,client.targety2 = targetx2,targety2
    push!(ret_msg,wincode,authString,movenum,sourcex,sourcey,targetx,targety,option,cheating,targetx2,targety2)
    return client,ret_msg
  end
  return client,false
end






function decodeMessage( msg )
    bytestring(msg)
end

function Decide_userip()
  println("Hi, your IP address is : ")
  user_IP = getipaddr()
  print(user_IP)
  return user_IP
end

function Decide_ip()
  println("Please input another user's IP address\n")
  ip = readline(STDIN)
  i = 1
  if length(ip) <= 16
    while isdigit(ip[i]) || ip[i] == '.'
      i = i + 1
    end
  else
      println("Error IP input")
      ip = Decide_ip()
  end
  if i != length(ip)
    println("Error IP input")
    ip = Decide_ip()
  end
  return IPv4(ip)
end


function Decide_port(args)
  if args == "re_input"
    println("\nPlease enter your Port #: ")
    port = readline(STDIN)
  else
    port = args[1]
  end
  i= 1
  if length(port) < 6
    while i < length(port)
      if isdigit(port[i])
        i += 1
      end
    end
  else
    println("Error port Please try again")
    port = Decide_port("re_input")
    i = length(port)
  end
  if i != length(port)
    println("Error Port input")
    port = Decide_port("re_input")
  end
  return port
end

function Server_setup(args)
  #Set up two client
  user_IP = Decide_userip()
  println("\n")
  port = Decide_port(args)
  println(typeof(port))
  Server = listen(parse(port))
  println("Listen server...")
  connection1 = accept(Server)
  client1 = client("","",connection1,isopen(connection1),"","","","","","","","","","","","","","","","","","","","")
  client1.authString = Decide_send_authString()
  client1.role = "player1"
  println(client1.connection,"Hi, you are authString is:",client1.authString)
  connection2 = accept(Server)
  client2 = client("","",connection2,isopen(connection2),"","","","","","","","","","","","","","","","","","","","")
  client2.authString = Decide_send_authString()
  client2.role = "player2"
  println(client2.connection,"Hi, you are authString is:" , client2.authString)

  client1.anti_role = client2.role
  client2.anti_role = client1.role
  println("server connceted...")
  #Set up end
  return client1,client2
end


function Gametype_check(client)
  if client.gametype != Game_type
    println(client.connection, "Dismatch the game type with ", client.anti_role)
    println(client.connection, "Your game type should be: ", Game_type)
    println(client.connection,"Please input again")
    println(client.connection,"10")
    return false
  end
  return true
end

function Decide_playload(client)
  #check playload when time = 0
  #recive msg from connection and analyze input works or not.
  client.sed_msg = chomp(readline(client.connection))
  println("\nThe message you recive is from : ",client.authString)
  info("1")
  print(chomp(client.sed_msg))
  client,rst_rcv_msg = analyze_rcv_msg(client,client.sed_msg)
  if rst_rcv_msg == false
    println(client.connection,"10:'Wrong intput. Please input again'")
    Decide_playload(client)
  end   #end of check user input
  if client.role == "player1"
    global Game_type = client.gametype
  end
  if client.role == "player2"
    if !Gametype_check(client)
      Decide_playload(client)
    end
  end
  return client
end


#play load: <wincode>:<gametype>:<legality>:<timelimit>:<limitadd> wincode = 0;
function Server_build_msg_playload(client)
  msg = ""
  if client.role == "player1"
    wincode = "0"
  end
  if client.role == "player2"
    wincode = "1"
  end
  authString = client.authString
  gametype = Game_type
  legality = client.legality
  timelimit = client.timelimit
  limitadd = client.limitadd
  msg = wincode * ":" * authString * ":" * gametype * ":" * legality  * ":" * timelimit  * ":" * limitadd
  println(client.connection, msg)
end

function Pass_msg(client1,client2)
  println("\nGet  the message ", client1.role, " wanna pass to ",client1.anti_role)
  client1.sed_msg = chomp(readline(client1.connection))
  client,ana1_msg = analyze_rcv_msg(client1,client1.sed_msg)
  println(client)
  while client1.authString != client.authString
    println("\nGet  the message ", client1.role, " wanna pass to ",client1.anti_role)
    client1.sed_msg = chomp(readline(client1.connection))
    client,ana1_msg = analyze_rcv_msg(client1,client.sed_msg)
  end
  client2.rcv_msg = client1.sed_msg
  println("Passing Message...")
  println(client2.rcv_msg)
  println(client2.connection,client2.rcv_msg)
  println("Message Passed")
end


function Server_start(args)
  #IP = Decide_ip()
  #Set up two client
  client1,client2 = Server_setup(args)
  println("Set up client Finished!")
  Game_player = client1.role
  #Client 1
  @async begin
    while true
      while isopen(client1.connection)
        if game1_times == 0
          client1 = Decide_playload(client1)
          println(client1.connection, waiting_msg, client1.anti_role)
          Server_build_msg_playload(client1)
          global game1_times += 1
        else
          Pass_msg(client1,client2)
          global game1_times += 1

        end
     end
    end
  end
  #Client 2
  @async begin
    while true
      while isopen(client2.connection)
        if game2_times == 0
          client2 = Decide_playload(client2)
          println(client2.connection, waiting_msg, client2.anti_role)
          Server_build_msg_playload(client2)
          global game2_times += 1
        else
          Pass_msg(client2,client1)
          global game2_times += 1
        end
      end
    end
  end
  while true
    sleep(5)
  end

end

Server_start(ARGS)

end  # module server