Ocaml Lwt-多人游戏的一些实现 [英] Ocaml Lwt - some implementations of multiplayer game
问题描述
我将继续编写tic-tac-toe服务器的简单实现(通过telnet).任务-玩家连接到服务器,并且在他们发送START之后,服务器也会寻找也输入START的伙伴,然后游戏开始.一段代码:
I'm going on to writing a simple implementation of tic-tac-toe server (via telnet). The task - players connect to server and after they send START the server looks for a partner who typed START too, and game begins. A piece of code:
let handle_income () =
let con = Lwt_unix.accept sock in
con >>= fun (cli, addr) ->
let player = Lwt.return {state = Sleeping; descriptor = Lwt.return cli} in
send_to_client player "Welcome to the server. To start game type in START and press Enter";
player;;
let rec make_ready player =
player >>= fun {state; descriptor} ->
send_to_client player "Waiting for start command";
let answer = read_from_client player in
answer >>= fun str ->
match str with
|"Start" ->
let ready_client = Lwt.return { state = Ready; descriptor = descriptor} in
ready_client
| _ ->
send_to_client player "Unknown command. try again";
make_ready player;;
我是Ocaml的新手(尤其是Lwt).那么,您是否愿意给我一些建议,如何使玩家开始寻找其他玩家?如果我将列表与始终处于迭代检查状态的播放器一起使用,则等待第二个播放器键入START(我不确定这是不可能的)的高级功能,Lwt唤醒者,Lwt广播,并创建另一个Lwt,直到Sleep为止有2个Lwt.t玩家或什么?我不知道如何实现最聪明的方法.非常感谢.
I'm completely new to Ocaml (Lwt especially). So, will you be so kind as to give to me a piece of advice how to make players' START to look for another player? Should I use list with all-time iteration checking players state, high-level functions which waits for the second player typed START(I'm not sure it's possible), Lwt wakers, Lwt broadcast, creating another a' Lwt wich is Sleep until has 2 Lwt.t players or something? I don't know how to implement that the cleverest way. Thank you much.
推荐答案
一种可能性:
具有connect函数将每个新连接放入Lwt_mvar
.
Have the connect function put each new connection in a Lwt_mvar
.
具有一个循环的Lwt.async
线程.在每次迭代中,从mvar中获得两个连接,并在它们之间生成一个游戏.
Have a Lwt.async
thread that loops. On each iteration take two connections from the mvar and spawn a game between them.
这篇关于Ocaml Lwt-多人游戏的一些实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!