private void Timer_Elapsed(object sender, ElapsedEventArgs e) { KeepAlive keepAlive = new KeepAlive(); sendData("KeepAlive", keepAlive); ServerLog.writeLog("Ping..{0}" + ID); }
protected override void OnMessage(MessageEventArgs e) { try { ServerLog.writeLog(string.Format("Player:{0}, Msg:", ID, e.Data)); string data = e.Data; object jsonObject = new JavaScriptSerializer().DeserializeObject(data); IDictionary <string, object> payload = (IDictionary <string, object>)jsonObject; string pid = (string)payload["pid"]; switch (pid) { case "LoginInfo": LoginInfo loginInfo = new JavaScriptSerializer().ConvertToType <LoginInfo>(jsonObject); ServerLog.writeLog(string.Format("Loginin GamePlayer:{0} DeviceId:{1}", ID, loginInfo.deviceId)); LoginInfoRes loginRes = new LoginInfoRes(); //Check LoginInfo if (loginInfo.deviceId.Length > 10) { myDeviceID = loginInfo.deviceId; myNickName = loginInfo.nickName; loginRes.loginResult = 1; } sendData("LoginInfoRes", loginRes); if (loginRes.loginResult > 0) { GameInfo gameinfo = new GameInfo(); sendData("GameInfo", gameinfo); } break; case "QuickSeatReq": myGame = ServerApp.getGameTableActor().quickJoin(this); break; case "MoveInfoReq": MoveInfoReq moveInfo = new JavaScriptSerializer().ConvertToType <MoveInfoReq>(jsonObject); myGame.moveInfoReq(moveInfo, isBlack); break; case "GameResultInfo": GameResultInfo gameResult = new JavaScriptSerializer().ConvertToType <GameResultInfo>(jsonObject); if (gameResult.wiinnerIsme == true) { Task resultTask = new Task(() => { Task.Delay(5000).Wait(); myGame.prePareGame(); }); resultTask.Start(); } break; } } catch (Exception ex) { ServerLog.writeLog(ex.Message); } }
public void prePareGame() { ServerLog.writeLog(string.Format("startGame:{0}", tableInfo.gameNo)); var whiteDolInfo = whitePlayer.dolsInfo; var blackDolInfo = blackPlayer.dolsInfo; isNowPlayerBlack = false; //send white whiteDolInfo.isMe = true; whitePlayer.sendData("DolsInfo", whiteDolInfo); whiteDolInfo.isMe = false; blackPlayer.sendData("DolsInfo", whiteDolInfo); //send black blackDolInfo.isMe = false; whitePlayer.sendData("DolsInfo", blackDolInfo); blackDolInfo.isMe = true; blackPlayer.sendData("DolsInfo", blackDolInfo); gameState = GameTableState.PLAYING; sendTurnInfo(); }
public void leaveGame(GamePlayer gamePlayer) { lock (this) { List <GamePlayer> removeList = new List <GamePlayer>(); foreach (GamePlayer idxPlayer in gamePlayers) { if (gamePlayer.myDeviceID == idxPlayer.myDeviceID) { removeList.Add(gamePlayer); ServerLog.writeLog(string.Format("leaveGame {0} in GameNo:{1} plyCount:{2} isblack:{3}", gamePlayer.ID, tableInfo.gameNo, tableInfo.plyCount, gamePlayer.isBlack)); } else { CrashGameInfo crashinfo = new CrashGameInfo(); crashinfo.reason = 0; idxPlayer.sendData("CrashGameInfo", crashinfo); } } foreach (GamePlayer removeply in removeList) { gamePlayers.Remove(removeply); tableInfo.plyCount--; } } }
public GameActor GetMyGameActor() { if (myGame == null) { ServerLog.writeLog(string.Format("myGame is null")); } return(myGame); }
protected override void OnClose(CloseEventArgs e) { timer.Stop(); if (myGame != null) { ServerApp.getGameTableActor().leaveGame(this); myGame = null; } ServerLog.writeLog(string.Format("OnClose GamePlayer:{0} , {1}", ID, e.Reason)); }
public static void runServer() { var wssv = new WebSocketServer(9100); ServerLog.writeLog(string.Format("ServerStart:{0}", 9100)); wssv.Start(); wssv.AddWebSocketService <GamePlayer>("/GoGame"); Console.ReadKey(true); wssv.Stop(); }
protected override void OnError(ErrorEventArgs e) { timer.Stop(); if (myGame != null) { ServerApp.getGameTableActor().leaveGame(this); myGame = null; } ServerLog.writeLog(string.Format("Onerror GamePlayer:{0} , {1}", ID, e.Message)); }
public bool joinGame(GamePlayer gamePlayer) { lock (this) { if (tableInfo.GetAvableUserCount() < 1) { ServerLog.writeLog(string.Format("joinGame Failed {0} in GameNo:{1} plyCount:{2}", gamePlayer.ID, tableInfo.gameNo, tableInfo.plyCount)); return(false); } foreach (GamePlayer player in gamePlayers) { if (gamePlayer.myDeviceID == player.myDeviceID) { gamePlayers.Remove(player); break; } } gamePlayers.Add(gamePlayer); tableInfo.plyCount++; if (tableInfo.plyCount == 1) { } if (tableInfo.plyCount == 2) { int idx = 0; foreach (GamePlayer player in gamePlayers) { if (idx == 0) { player.createDolInfo(false, player.myNickName); whitePlayer = player; } else { gamePlayer.createDolInfo(true, player.myNickName); blackPlayer = gamePlayer; } idx++; } prePareGame(); } ServerLog.writeLog(string.Format("joinGame {0} in GameNo:{1} plyCount:{2}", gamePlayer.ID, tableInfo.gameNo, tableInfo.plyCount)); } return(true); }
protected override void OnOpen() { ServerLog.writeLog(string.Format("OnOpen GamePlayer:{0}", ID)); }
public GamePlayer() { timer.Elapsed += Timer_Elapsed; ServerLog.writeLog(string.Format("Creator GamePlayer:{0}", GetHashCode())); }