Skip to content

Commit

Permalink
add game round info, not shown yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Li Wenbo committed Jul 20, 2012
1 parent eca51e2 commit 40c5c85
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 57 deletions.
38 changes: 25 additions & 13 deletions src/game.opa
Expand Up @@ -39,6 +39,8 @@ type Game.t = {
int ready_flags, //用于表示玩家是否准备好(1 + 2 + 4 + 8) int ready_flags, //用于表示玩家是否准备好(1 + 2 + 4 + 8)
int ok_flags, //用户表示玩家是否关闭了结算界面 int ok_flags, //用户表示玩家是否关闭了结算界面
int last_act, //最后一次出手时间,用于判断超时 int last_act, //最后一次出手时间,用于判断超时
int round, //第几局
int dealer, //庄家
bool change_flag, //标志这个游戏自上次广播之后状态(游戏人数,准备状态等)是否变化 bool change_flag, //标志这个游戏自上次广播之后状态(游戏人数,准备状态等)是否变化
list(int) winners, //游戏胜利玩家 list(int) winners, //游戏胜利玩家
option(Card.t) curr_card, //当前牌面上打出的牌 option(Card.t) curr_card, //当前牌面上打出的牌
Expand Down Expand Up @@ -112,7 +114,9 @@ public function get_flag_cnt(flags){
} }


module Game { module Game {

AUTO_READY = {false};
AUTO_RESTART = {false};

init_board = { init_board = {
//初始化创建10个房间 //初始化创建10个房间
ignore(for(0,function(i){ ignore(for(0,function(i){
Expand All @@ -125,6 +129,8 @@ module Game {
last_act: 0, last_act: 0,
ready_flags: 0, ready_flags: 0,
ok_flags: 0, ok_flags: 0,
round: 0,
dealer: -1,
change_flag: {false}, change_flag: {false},
curr_card: {none}, curr_card: {none},
players: LowLevelArray.create(4,{none}), players: LowLevelArray.create(4,{none}),
Expand Down Expand Up @@ -313,6 +319,8 @@ module Game {
{id: game.id, {id: game.id,
st: encode_status(game.status), st: encode_status(game.status),
ct: game.curr_turn, ct: game.curr_turn,
rd: game.round,
dl: game.dealer,
cc: game.curr_card, cc: game.curr_card,
rf: game.ready_flags, rf: game.ready_flags,
pls: game.players, pls: game.players,
Expand All @@ -326,6 +334,8 @@ module Game {
{ id: game.id, { id: game.id,
status: game.status, status: game.status,
curr_turn: game.curr_turn, curr_turn: game.curr_turn,
round: game.round,
dealer: game.dealer,
curr_card: game.curr_card, curr_card: game.curr_card,
ready_flags: game.ready_flags, ready_flags: game.ready_flags,
players: game.players, players: game.players,
Expand Down Expand Up @@ -388,7 +398,7 @@ module Game {
case {none}: void case {none}: void
default: { default: {
Render.refresh(); Render.refresh();
Render.play_sound("button.wav"); play_sound("button.wav");
Mahjong.request_action(game.id,game.idx,action); Mahjong.request_action(game.id,game.idx,action);
} }
} }
Expand All @@ -407,7 +417,7 @@ module Game {
Render.refresh(); Render.refresh();
} }
case {GAME_START: game_msg}:{ case {GAME_START: game_msg}:{
Render.play_sound("start.wav"); play_sound("start.wav");
Render.update(game_msg); Render.update(game_msg);
Render.update_deck(); Render.update_deck();
Render.start_timer(); Render.start_timer();
Expand All @@ -427,7 +437,7 @@ module Game {
refresh_players(game_msg.pls); refresh_players(game_msg.pls);
} }
case {DISCARD_CARD: msg}:{ //玩家弃牌消息 case {DISCARD_CARD: msg}:{ //玩家弃牌消息
Render.play_sound("da.wav"); play_sound("da.wav");
Render.stop_timer() Render.stop_timer()
Render.recv_discard_msg(msg); Render.recv_discard_msg(msg);
Render.refresh(); Render.refresh();
Expand Down Expand Up @@ -463,7 +473,7 @@ module Game {
Render.refresh(); Render.refresh();
player_idx = get_game().idx; player_idx = get_game().idx;
List.iter(function(win_idx){ List.iter(function(win_idx){
if(player_idx == win_idx) Render.play_sound("win.wav") if(player_idx == win_idx) play_sound("win.wav")
Render.draw_win(Board.get_rel_pos(player_idx,win_idx)) Render.draw_win(Board.get_rel_pos(player_idx,win_idx))
},winners); },winners);
} }
Expand All @@ -480,7 +490,7 @@ module Game {
set_game({game with ~players}); set_game({game with ~players});
refresh_players(players); refresh_players(players);


Render.play_sound("countfan.wav"); play_sound("countfan.wav");
Render.refresh(); Render.refresh();
Render.show_result(game,some,195,75); Render.show_result(game,some,195,75);
} }
Expand Down Expand Up @@ -524,9 +534,9 @@ module Game {
//加载资源 //加载资源
imgs = ["table_bg.png","board.png","result.png","arrow.png","win.png","menu_bar.png","ting.png","dialog.png", imgs = ["table_bg.png","board.png","result.png","arrow.png","win.png","menu_bar.png","ting.png","dialog.png",
"tiles.png","tiles_small.png","numbers.png","start.png","offline.png","player_frame_h.png","player_frame_v.png", "tiles.png","tiles_small.png","numbers.png","start.png","offline.png","player_frame_h.png","player_frame_v.png",
"portrait.jpg","eswn.png"]; "portrait.jpg","eswn.png","btn_tutor.png"];
auds = ["start.wav","da.wav","button.wav","tray.wav","countfan.wav","win.wav"]; auds = ["start.wav","da.wav","button.wav","tray.wav","countfan.wav","win.wav"];
Render.preload(imgs,auds,function(){ preload(imgs,auds,function(){
Dom.set_value(#loading_info,"prepare game..."); Dom.set_value(#loading_info,"prepare game...");
game_obs = Network.observe(game_msg_received,game.game_channel); game_obs = Network.observe(game_msg_received,game.game_channel);
chat_obs = Network.observe(user_update,game.chat_channel); chat_obs = Network.observe(user_update,game.chat_channel);
Expand All @@ -539,6 +549,8 @@ module Game {
game = update_player(game,player); game = update_player(game,player);
set_game(game_obj(game,player)); set_game(game_obj(game,player));
refresh_players(game.players); refresh_players(game.players);

if(AUTO_READY || player.is_bot) Mahjong.set_ready(game,player.idx);


//离开页面的提示(对Opera无效) //离开页面的提示(对Opera无效)
Dom.bind_beforeunload_confirmation(function(_){ Dom.bind_beforeunload_confirmation(function(_){
Expand Down Expand Up @@ -616,7 +628,7 @@ module Game {
function game_view(game,idx){ function game_view(game,idx){
//更新第idx个玩家的client_ctx //更新第idx个玩家的client_ctx
player = Option.get(LowLevelArray.get(game.players,idx)); player = Option.get(LowLevelArray.get(game.players,idx));
/** _ = Client.setTimeout(function(){ _ = ClientEvent.set_on_disconnect_client(function(ctx){
//如果ctx和game的第idx个client一致,说明这个玩家处于死链接状态,去除之。 //如果ctx和game的第idx个client一致,说明这个玩家处于死链接状态,去除之。
with_game(game.id,function(game){ with_game(game.id,function(game){
match(LowLevelArray.get(game.clients,idx)){ match(LowLevelArray.get(game.clients,idx)){
Expand All @@ -628,14 +640,12 @@ module Game {
} }
} }
}); });
jlog("timeout"); });
},20);*/



Resource.styled_page("Mahjong",["/resources/style.css"], Resource.styled_page("Mahjong",["/resources/style.css"],
<> <>
<div class="game" onready={function(_){game_ready(game,player) }}> <div class="game" onready={function(_){game_ready(game,player) }}>
<div class="canvas"> <div class="canvas">
<div id=#gmloader > <div id=#gmloader >
<p id=#loading_info>loading</p> <p id=#loading_info>loading</p>
</div> </div>
Expand Down Expand Up @@ -729,6 +739,8 @@ module Game {
change_flag: {true}, change_flag: {true},
ready_flags: 0, ready_flags: 0,
ok_flags: 0, ok_flags: 0,
round: 0,
dealer: 0
} }


Mahjong.reset_actions(game); Mahjong.reset_actions(game);
Expand Down
7 changes: 5 additions & 2 deletions src/mahjong.opa
Expand Up @@ -116,7 +116,10 @@ module Mahjong {
//准备 //准备
if(not(test_flag(game.ready_flags,game.idx)) && Button.is_pressed(pos,Render.btn_ready)){ if(not(test_flag(game.ready_flags,game.idx)) && Button.is_pressed(pos,Render.btn_ready)){
{set_ready} {set_ready}
} else {none} } else{
if(Button.is_pressed(pos,Render.btn_tutor)) Tutor.show_tutor();
{none}
}
} }
case {select_action}: { case {select_action}: {
//选择弃掉的牌 //选择弃掉的牌
Expand Down Expand Up @@ -379,7 +382,7 @@ module Mahjong {
/** /**
* 设置用户准备好 * 设置用户准备好
*/ */
private function set_ready(game,idx){ exposed function set_ready(game,idx){
game = {game with ready_flags: set_flag(game.ready_flags,idx), change_flag: {true}} game = {game with ready_flags: set_flag(game.ready_flags,idx), change_flag: {true}}
if(game.ready_flags == ALL_IS_READY){ if(game.ready_flags == ALL_IS_READY){
game = Game.start(game) |> update(_); game = Game.start(game) |> update(_);
Expand Down
22 changes: 11 additions & 11 deletions src/main.opa
Expand Up @@ -27,23 +27,22 @@ function login_required( -> resource page){
} }
} }


function is_logged_in(){
match(Login.get_user()){
case {unlogged}: {false}
case {user:_}: {true}
}
}


function with_user((Login.user -> 'a) f, 'a otherwise){ function with_user((Login.user -> 'a) f, 'a otherwise){
match(Login.get_user()){ match(get_user()){
case ~{user}: f(user); case ~{user}: f(user);
case {unlogged}: otherwise; case {unlogged}: otherwise;
} }
} }


function get_username(){
match(get_user()){
case ~{user}: user.name
case {unlogged}: "anonymous"
}
}

function game_view(game_id,need_bot){ function game_view(game_id,need_bot){
match(Login.get_user()){ match(get_user()){
case {unlogged}: Login.login_view(); case {unlogged}: Login.login_view();
case {user:player}: { case {user:player}: {
match(Game.get(game_id)){ match(Game.get(game_id)){
Expand Down Expand Up @@ -79,6 +78,7 @@ function start(url){
case {path:["gamex",id|_] ...} : game_view(id,{true}); case {path:["gamex",id|_] ...} : game_view(id,{true});
case {path:["how_to_play.html"] ...}: @static_resource("resources/how_to_play.html"); case {path:["how_to_play.html"] ...}: @static_resource("resources/how_to_play.html");
case {path:["hall"] ...} : login_required(function(){Page.game_list_view()}) case {path:["hall"] ...} : login_required(function(){Page.game_list_view()})
case {path:["tutor"] ...} : Tutor.page_view();
case {path: _ ...} : Main.fourOffour() case {path: _ ...} : Main.fourOffour()
} }


Expand All @@ -98,4 +98,4 @@ module Main {
<><h1>404</h1></> <><h1>404</h1></>
); );
} }
} }
2 changes: 2 additions & 0 deletions src/network.opa
Expand Up @@ -30,6 +30,8 @@ type Game.msg = {
string id, //游戏id string id, //游戏id
int st, //游戏状态 status int st, //游戏状态 status
int ct, //当前玩家 curr_turn int ct, //当前玩家 curr_turn
int rd, //第几局 round
int dl, //庄家 dealer
int rf, //玩家准备好标志 ready_flags int rf, //玩家准备好标志 ready_flags
option(Card.t) cc, //当前牌面上的牌 curr_card option(Card.t) cc, //当前牌面上的牌 curr_card
llarray(option(Player.t)) pls, //玩家列表 players llarray(option(Player.t)) pls, //玩家列表 players
Expand Down
75 changes: 44 additions & 31 deletions src/render.opa
Expand Up @@ -34,6 +34,8 @@ type Game.obj = {
Status.t status, //游戏状态 Status.t status, //游戏状态
int curr_turn, //当前玩家 int curr_turn, //当前玩家
int ready_flags, //玩家的准备好标志 int ready_flags, //玩家的准备好标志
int round,
int dealer,
option(Card.t) curr_card, //当前牌面上的牌 option(Card.t) curr_card, //当前牌面上的牌
llarray(option(Player.t)) players, //玩家列表 llarray(option(Player.t)) players, //玩家列表
llarray(Deck.t) decks, //所有玩家牌面 llarray(Deck.t) decks, //所有玩家牌面
Expand All @@ -51,20 +53,46 @@ type Game.obj = {
set_game = %%engine2d.set_game%%; set_game = %%engine2d.set_game%%;
get_game = %%engine2d.get_game%%; get_game = %%engine2d.get_game%%;


PREFIX = "/resources/"

function get_ctx(id){
canvas = Canvas.get(id);
Option.get(Canvas.get_context_2d(Option.get(canvas)));
}

/** 获得图片资源 */
function get(key){
{image: %%engine2d.get%%(PREFIX ^ key)}
}

/** 预加载图片和声音资源 */
function preload(imgs,auds,f){
imgs = List.map(function(img){
PREFIX ^ img
},imgs);
auds = List.map(function(aud){
PREFIX ^ aud
},auds);
%%engine2d.preload%%(imgs,auds,f);
}

function play_sound(key){
%%engine2d.play_sound%%(PREFIX ^ key);
}

/** /**
* 游戏的绘制引擎 * 游戏的绘制引擎
*/ */
client module Render { client module Render {
bool DEBUG = {true} bool DEBUG = {false}
bool SHOW_TILE = {true} bool SHOW_TILE = {false}


//常用颜色的定义 //常用颜色的定义
BLACK = {color:Color.rgb(0,0,0)}; BLACK = {color:Color.rgb(0,0,0)};
RED = {color: Color.rgb(255,0,0)}; RED = {color: Color.rgb(255,0,0)};
BLUE = {color: Color.rgb(0,0,255)}; BLUE = {color: Color.rgb(0,0,255)};


PREFIX = "/resources/"

//等待时间 //等待时间
WAIT_TIME = 10 WAIT_TIME = 10


Expand All @@ -74,6 +102,7 @@ client module Render {
Button.t btn_cancel = Button.simple(670,475,60,60,"放弃"); //放弃 Button.t btn_cancel = Button.simple(670,475,60,60,"放弃"); //放弃
Button.t btn_ready = Button.simple(330,555,150,57,"准备"); //准备 Button.t btn_ready = Button.simple(330,555,150,57,"准备"); //准备
Button.t btn_hide_result = Button.simple(333,423,75,45,"确定"); //关闭结算 Button.t btn_hide_result = Button.simple(333,423,75,45,"确定"); //关闭结算
Button.t btn_tutor = Button.simple(600,555,100,100,"指导"); //关闭结算


/** 是否在麻将牌上显示数字 */ /** 是否在麻将牌上显示数字 */
g_show_number = Mutable.make(bool {false}); g_show_number = Mutable.make(bool {false});
Expand All @@ -89,31 +118,7 @@ client module Render {
refresh(); refresh();
} }


function get_ctx(id){
canvas = Canvas.get(id);
Option.get(Canvas.get_context_2d(Option.get(canvas)));
}


/** 获得图片资源 */
function get(key){
{image: %%engine2d.get%%(PREFIX ^ key)}
}

/** 预加载图片和声音资源 */
function preload(imgs,auds,f){
imgs = List.map(function(img){
PREFIX ^ img
},imgs);
auds = List.map(function(aud){
PREFIX ^ aud
},auds);
%%engine2d.preload%%(imgs,auds,f);
}

function play_sound(key){
%%engine2d.play_sound%%(PREFIX ^ key);
}

start_timer = %%engine2d.start_timer%% start_timer = %%engine2d.start_timer%%
stop_timer = %%engine2d.stop_timer%% stop_timer = %%engine2d.stop_timer%%
show_menu = %%engine2d.show_menu%% show_menu = %%engine2d.show_menu%%
Expand All @@ -140,7 +145,10 @@ client module Render {


if(game.status == {prepare} || ( game.status == {show_result} && game.is_ok)){ if(game.status == {prepare} || ( game.status == {show_result} && game.is_ok)){
draw_prepare(ctx,game,game.idx); //绘制牌堆 draw_prepare(ctx,game,game.idx); //绘制牌堆
if(not(test_flag(game.ready_flags,game.idx))) Canvas.draw_image(ctx,get("start.png"),330,555); //绘制“准备”按钮 if(not(test_flag(game.ready_flags,game.idx))){
Canvas.draw_image(ctx,get("start.png"),330,555); //绘制“准备”按钮
Canvas.draw_image(ctx,get("btn_tutor.png"),620,510); //绘制“指导”按钮
}
}else{ }else{
draw_discards(ctx,game.discards,game.idx); //绘制玩家弃牌 draw_discards(ctx,game.discards,game.idx); //绘制玩家弃牌
draw_handcards(ctx,game); //绘制玩家手牌(不包括自己) draw_handcards(ctx,game); //绘制玩家手牌(不包括自己)
Expand Down Expand Up @@ -260,12 +268,12 @@ client module Render {
},game.players); },game.players);


//绘制等待信息 //绘制等待信息
Canvas.set_fill_style(ctx,BLACK); /** Canvas.set_fill_style(ctx,BLACK);
Canvas.set_font(ctx,"normal bold 11pt serif"); Canvas.set_font(ctx,"normal bold 11pt serif");
Canvas.set_text_align(ctx,{align_center}); Canvas.set_text_align(ctx,{align_center});
Canvas.fill_text(ctx,"Waiting for players to set ready",400,360); Canvas.fill_text(ctx,"Waiting for players to set ready",400,360);
Canvas.fill_text(ctx,"Game will start when four players are",400,385); Canvas.fill_text(ctx,"Game will start when four players are",400,385);
Canvas.fill_text(ctx,"all ready",400,410); Canvas.fill_text(ctx,"all ready",400,410);*/
Canvas.restore(ctx); Canvas.restore(ctx);
} }


Expand Down Expand Up @@ -396,6 +404,9 @@ client module Render {
} }
} }
} }

//绘制当前局数和庄家
//Canvas.fill_text(ctx,"ROUND {game.round+1} EAST",15,15);
} }


/** /**
Expand Down Expand Up @@ -917,6 +928,8 @@ client module Render {
status: decode_status(game_msg.st), status: decode_status(game_msg.st),
curr_turn: game_msg.ct, curr_turn: game_msg.ct,
curr_card: game_msg.cc, curr_card: game_msg.cc,
round: game_msg.rd,
dealer: game_msg.dl,
ready_flags: game_msg.rf, ready_flags: game_msg.rf,
players: game_msg.pls, players: game_msg.pls,
decks: game_msg.dks, decks: game_msg.dks,
Expand Down

0 comments on commit 40c5c85

Please sign in to comment.