summaryrefslogtreecommitdiff
path: root/engine/Action.java
blob: 3f2f3656eb85c1e6210c763058741fa3916c43f5 (plain)
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
package mahjong.engine;

import java.util.Random;

/**
 * Created by joe on 12/3/14.
 * Action handles information passing between server and clients
 */
public class Action {
    // TODO Generic action
    // Requirement: printable,parseable,coverage
    // Coverage: System,player
    private static Random idGen = new Random();
    private static int cnter = 0;
    public enum Type {System,Player}
    // System info merely reflect system state
    // Player info pertaining to players;
    public enum Place {Info,East,South,West,North}
    public enum Act {Info,Draw,Put,Meld}

    Type type;
    Act act;
    Place place;
    String message;
    int guid;

    public void newGUID()
    {
        this.guid=(idGen.nextInt()&0xFFF000)+cnter;
        cnter = (cnter+1)&0xFFF;
    }

    public Action(Type type,Place place,Act act,String message)
    {
        newGUID();
        this.type = type;
        this.place = place;
        this.act = act;
        this.message = message;
    }
    public Action(String str)
    {
        String[] res=str.trim().split("\t");
        guid = Integer.parseInt(res[0]);
        switch (res[1].trim().toUpperCase().charAt(0))
        {
            case 'S':
                type=Type.System;
                break;
            case 'P':
                type=Type.Player;
        }
        switch (res[2].trim().toUpperCase().charAt(0))
        {
            case 'I':
                place=Place.Info;
                break;
            case 'E':
                place=Place.East;
                break;
            case 'S':
                place=Place.South;
                break;
            case 'W':
                place=Place.West;
                break;
            case 'N':
                place=Place.North;
        }
        switch (res[3].trim().toUpperCase().charAt(0))
        {
            case 'I':
                act=Act.Info;
                break;
            case 'D':
                act=Act.Draw;
                break;
            case 'P':
                act=Act.Put;
                break;
            case 'M':
                act=Act.Meld;
        }
        message=res[4];
    }

    @Override
    public String toString() {
        return String.format("%d\t%c\t%c\t%c\t%s",guid,type.toString().charAt(0),place.toString().charAt(0),act.toString().charAt(0),message);
    }

    public Act getAct() {
        return act;
    }

    public Place getPlace() {
        return place;
    }

    public String getMessage() {
        return message;
    }

    public int getGuid() {
        return guid;
    }

    public Type getType() {
        return type;
    }
}